nodes: added distort nodes
Adds nodes for cropping and transforming
This commit is contained in:
@@ -20,12 +20,9 @@
|
||||
|
||||
#include "matrix.h"
|
||||
|
||||
#include <QGuiApplication>
|
||||
#include <QMatrix4x4>
|
||||
#include <QVector2D>
|
||||
|
||||
#include "common/range.h"
|
||||
|
||||
namespace olive {
|
||||
|
||||
MatrixGenerator::MatrixGenerator()
|
||||
@@ -94,144 +91,54 @@ void MatrixGenerator::Retranslate()
|
||||
NodeValueTable MatrixGenerator::Value(NodeValueDatabase &value) const
|
||||
{
|
||||
// Push matrix output
|
||||
QMatrix4x4 mat = GenerateMatrix(value);
|
||||
QMatrix4x4 mat = GenerateMatrix(value, true, false, false);
|
||||
NodeValueTable output = value.Merge();
|
||||
output.Push(NodeParam::kMatrix, mat, this);
|
||||
return output;
|
||||
}
|
||||
|
||||
bool MatrixGenerator::GizmoPress(NodeValueDatabase &db, const QPointF &p, const QVector2D &scale, const QSize &viewport)
|
||||
{
|
||||
GizmoSharedData gizmo_data(viewport, scale);
|
||||
|
||||
QPointF anchor_pt = GetGizmoAnchorPoint(db, gizmo_data);
|
||||
int anchor_radius = GetGizmoAnchorPointRadius();
|
||||
|
||||
if (InRange(p.x(), anchor_pt.x(), anchor_radius * 2.0)
|
||||
&& InRange(p.y(), anchor_pt.y(), anchor_radius * 2.0)) {
|
||||
gizmo_drag_ = anchor_input_;
|
||||
gizmo_start2_ = db[position_input_].Get(NodeParam::kVec2).value<QVector2D>();
|
||||
} else {
|
||||
gizmo_drag_ = position_input_;
|
||||
}
|
||||
|
||||
gizmo_start_ = db[gizmo_drag_].Get(NodeParam::kVec2).value<QVector2D>();
|
||||
gizmo_mouse_start_ = p;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MatrixGenerator::GizmoMove(const QPointF &p, const QVector2D &scale, const rational &time)
|
||||
{
|
||||
QVector2D movement = 2.0 * QVector2D(p - gizmo_mouse_start_) / scale;
|
||||
QVector2D new_pos = gizmo_start_ + movement;
|
||||
|
||||
if (!gizmo_x_dragger_.IsStarted()) {
|
||||
gizmo_x_dragger_.Start(gizmo_drag_, time, 0);
|
||||
}
|
||||
|
||||
if (!gizmo_y_dragger_.IsStarted()) {
|
||||
gizmo_y_dragger_.Start(gizmo_drag_, time, 1);
|
||||
}
|
||||
|
||||
gizmo_x_dragger_.Drag(new_pos.x());
|
||||
gizmo_y_dragger_.Drag(new_pos.y());
|
||||
|
||||
if (gizmo_drag_ == anchor_input_) {
|
||||
// If we're dragging the anchor, counter the position at the same time
|
||||
QVector2D new_pos2 = gizmo_start2_ + movement;
|
||||
|
||||
if (!gizmo_x2_dragger_.IsStarted()) {
|
||||
gizmo_x2_dragger_.Start(position_input_, time, 0);
|
||||
}
|
||||
|
||||
if (!gizmo_y2_dragger_.IsStarted()) {
|
||||
gizmo_y2_dragger_.Start(position_input_, time, 1);
|
||||
}
|
||||
|
||||
gizmo_x2_dragger_.Drag(new_pos2.x());
|
||||
gizmo_y2_dragger_.Drag(new_pos2.y());
|
||||
}
|
||||
}
|
||||
|
||||
void MatrixGenerator::GizmoRelease()
|
||||
{
|
||||
gizmo_x_dragger_.End();
|
||||
gizmo_y_dragger_.End();
|
||||
gizmo_x2_dragger_.End();
|
||||
gizmo_y2_dragger_.End();
|
||||
}
|
||||
|
||||
bool MatrixGenerator::HasGizmos() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void MatrixGenerator::DrawGizmos(NodeValueDatabase &db, QPainter *p, const QVector2D &scale, const QSize& viewport) const
|
||||
{
|
||||
p->setPen(Qt::white);
|
||||
|
||||
GizmoSharedData gizmo_data(viewport, scale);
|
||||
|
||||
{
|
||||
// Fold values into a matrix
|
||||
QMatrix4x4 matrix;
|
||||
matrix.scale(gizmo_data.half_scale);
|
||||
matrix *= GenerateMatrix(db, false);
|
||||
matrix.scale(gizmo_data.inverted_half_scale);
|
||||
|
||||
// Create rect and transform it
|
||||
QVector<QPointF> points = {QPointF(-100, -100),
|
||||
QPointF(100, -100),
|
||||
QPointF(100, 100),
|
||||
QPointF(-100, 100),
|
||||
QPointF(-100, -100)};
|
||||
QPolygonF poly(points);
|
||||
poly = matrix.toTransform().map(poly);
|
||||
poly.translate(gizmo_data.half_viewport);
|
||||
|
||||
// Draw square
|
||||
p->drawPolyline(poly);
|
||||
}
|
||||
|
||||
{
|
||||
// Draw anchor point marker (with no anchor point translation)
|
||||
QPointF pt = GetGizmoAnchorPoint(db, gizmo_data);
|
||||
|
||||
// Draw anchor point
|
||||
int anchor_pt_radius = GetGizmoAnchorPointRadius();
|
||||
|
||||
p->drawEllipse(pt, anchor_pt_radius, anchor_pt_radius);
|
||||
|
||||
p->drawLines({QLineF(pt.x() - anchor_pt_radius, pt.y(),
|
||||
pt.x() + anchor_pt_radius, pt.y()),
|
||||
QLineF(pt.x(), pt.y() - anchor_pt_radius,
|
||||
pt.x(), pt.y() + anchor_pt_radius)});
|
||||
}
|
||||
}
|
||||
|
||||
QMatrix4x4 MatrixGenerator::GenerateMatrix(NodeValueDatabase &value) const
|
||||
{
|
||||
return GenerateMatrix(value[position_input_].Take(NodeParam::kVec2).value<QVector2D>(),
|
||||
value[rotation_input_].Take(NodeParam::kFloat).toFloat(),
|
||||
value[scale_input_].Take(NodeParam::kVec2).value<QVector2D>(),
|
||||
value[uniform_scale_input_].Take(NodeParam::kBoolean).toBool(),
|
||||
value[anchor_input_].Take(NodeParam::kVec2).value<QVector2D>());
|
||||
}
|
||||
|
||||
QMatrix4x4 MatrixGenerator::GenerateMatrix(NodeValueDatabase &value, bool ignore_anchor) const
|
||||
QMatrix4x4 MatrixGenerator::GenerateMatrix(NodeValueDatabase &value, bool take, bool ignore_anchor, bool ignore_position) const
|
||||
{
|
||||
QVector2D anchor;
|
||||
QVector2D position;
|
||||
|
||||
if (!ignore_anchor) {
|
||||
anchor = value[anchor_input_].Get(NodeParam::kVec2).value<QVector2D>();
|
||||
if (take) {
|
||||
// Take and store
|
||||
anchor = value[anchor_input_].Take(NodeParam::kVec2).value<QVector2D>();
|
||||
} else {
|
||||
// Get and store
|
||||
anchor = value[anchor_input_].Get(NodeParam::kVec2).value<QVector2D>();
|
||||
}
|
||||
} else if (take) {
|
||||
// Just take
|
||||
value[anchor_input_].Take(NodeParam::kVec2).value<QVector2D>();
|
||||
}
|
||||
|
||||
return GenerateMatrix(value[position_input_].Get(NodeParam::kVec2).value<QVector2D>(),
|
||||
value[rotation_input_].Get(NodeParam::kFloat).toFloat(),
|
||||
value[scale_input_].Get(NodeParam::kVec2).value<QVector2D>(),
|
||||
value[uniform_scale_input_].Get(NodeParam::kBoolean).toBool(),
|
||||
anchor);
|
||||
if (!ignore_position) {
|
||||
if (take) {
|
||||
position = value[position_input_].Take(NodeParam::kVec2).value<QVector2D>();
|
||||
} else {
|
||||
position = value[position_input_].Get(NodeParam::kVec2).value<QVector2D>();
|
||||
}
|
||||
} else if (take) {
|
||||
value[position_input_].Take(NodeParam::kVec2).value<QVector2D>();
|
||||
}
|
||||
|
||||
if (take) {
|
||||
return GenerateMatrix(position,
|
||||
value[rotation_input_].Take(NodeParam::kFloat).toFloat(),
|
||||
value[scale_input_].Take(NodeParam::kVec2).value<QVector2D>(),
|
||||
value[uniform_scale_input_].Take(NodeParam::kBoolean).toBool(),
|
||||
anchor);
|
||||
} else {
|
||||
return GenerateMatrix(position,
|
||||
value[rotation_input_].Get(NodeParam::kFloat).toFloat(),
|
||||
value[scale_input_].Get(NodeParam::kVec2).value<QVector2D>(),
|
||||
value[uniform_scale_input_].Get(NodeParam::kBoolean).toBool(),
|
||||
anchor);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
QMatrix4x4 MatrixGenerator::GenerateMatrix(const QVector2D& pos,
|
||||
@@ -263,35 +170,9 @@ QMatrix4x4 MatrixGenerator::GenerateMatrix(const QVector2D& pos,
|
||||
return mat;
|
||||
}
|
||||
|
||||
QPointF MatrixGenerator::GetGizmoAnchorPoint(NodeValueDatabase &db,
|
||||
const GizmoSharedData& gizmo_data) const
|
||||
{
|
||||
QMatrix4x4 matrix;
|
||||
matrix.scale(gizmo_data.half_scale);
|
||||
matrix *= GenerateMatrix(db, true);
|
||||
matrix.scale(gizmo_data.inverted_half_scale);
|
||||
|
||||
QPointF pt = matrix.toTransform().map(QPointF());
|
||||
pt += gizmo_data.half_viewport;
|
||||
|
||||
return pt;
|
||||
}
|
||||
|
||||
int MatrixGenerator::GetGizmoAnchorPointRadius()
|
||||
{
|
||||
return QFontMetrics(qApp->font()).height() / 2;
|
||||
}
|
||||
|
||||
void MatrixGenerator::UniformScaleChanged()
|
||||
{
|
||||
scale_input_->set_property("disabley", uniform_scale_input_->get_standard_value().toBool());
|
||||
}
|
||||
|
||||
MatrixGenerator::GizmoSharedData::GizmoSharedData(const QSize &viewport, const QVector2D &scale)
|
||||
{
|
||||
half_viewport = QPointF(viewport.width() / 2, viewport.height() / 2);
|
||||
half_scale = scale * 0.5;
|
||||
inverted_half_scale = QVector2D(1.0f / half_scale.x(), 1.0f / half_scale.y());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user