1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "polygon.h"
#include <QGuiApplication>
#include <QVector2D>
OLIVE_NAMESPACE_ENTER
PolygonGenerator::PolygonGenerator()
{
points_input_ = new NodeInputArray("points_in", NodeParam::kVec2);
AddInput(points_input_);
color_input_ = new NodeInput("color_in", NodeParam::kColor);
AddInput(color_input_);
// Default to "a color" that isn't
color_input_->set_standard_value(1.0, 0);
color_input_->set_standard_value(1.0, 1);
color_input_->set_standard_value(1.0, 2);
color_input_->set_standard_value(1.0, 3);
// FIXME: Test code
points_input_->SetSize(5);
points_input_->At(0)->set_standard_value(960, 0);
points_input_->At(0)->set_standard_value(240, 1);
points_input_->At(1)->set_standard_value(640, 0);
points_input_->At(1)->set_standard_value(480, 1);
points_input_->At(2)->set_standard_value(760, 0);
points_input_->At(2)->set_standard_value(800, 1);
points_input_->At(3)->set_standard_value(1100, 0);
points_input_->At(3)->set_standard_value(800, 1);
points_input_->At(4)->set_standard_value(1280, 0);
points_input_->At(4)->set_standard_value(480, 1);
// End test
}
Node *PolygonGenerator::copy() const
{
return new PolygonGenerator();
}
QString PolygonGenerator::Name() const
{
return tr("Polygon");
}
QString PolygonGenerator::id() const
{
return QStringLiteral("org.olivevideoeditor.Olive.polygon");
}
QList<Node::CategoryID> PolygonGenerator::Category() const
{
return {kCategoryGenerator};
}
QString PolygonGenerator::Description() const
{
return tr("Generate a 2D polygon of any amount of points.");
}
void PolygonGenerator::Retranslate()
{
points_input_->set_name(tr("Points"));
color_input_->set_name(tr("Color"));
}
ShaderCode PolygonGenerator::GetShaderCode(const QString &shader_id) const
{
Q_UNUSED(shader_id)
return ShaderCode(Node::ReadFileAsString(":/shaders/polygon.frag"), QString());
}
NodeValueTable PolygonGenerator::Value(NodeValueDatabase &value) const
{
ShaderJob job;
job.InsertValue(points_input_, value);
job.InsertValue(color_input_, value);
job.SetAlphaChannelRequired(true);
NodeValueTable table = value.Merge();
table.Push(NodeParam::kShaderJob, QVariant::fromValue(job), this);
return table;
}
bool PolygonGenerator::HasGizmos() const
{
return true;
}
void PolygonGenerator::DrawGizmos(NodeValueDatabase &db, QPainter *p, const QVector2D &scale, const QSize &viewport) const
{
Q_UNUSED(viewport)
if (!points_input_->GetSize()) {
return;
}
p->setPen(Qt::white);
p->setBrush(Qt::white);
QVector<QPointF> points = GetGizmoCoordinates(db, scale);
QVector<QRectF> rects = GetGizmoRects(points);
points.append(points.first());
p->drawPolyline(points.constData(), points.size());
p->drawRects(rects);
}
bool PolygonGenerator::GizmoPress(NodeValueDatabase &db, const QPointF &p, const QVector2D &scale, const QSize& viewport)
{
Q_UNUSED(viewport)
QVector<QPointF> points = GetGizmoCoordinates(db, scale);
QVector<QRectF> rects = GetGizmoRects(points);
for (int i=0;i<rects.size();i++) {
const QRectF& r = rects.at(i);
if (r.contains(p)) {
gizmo_drag_ = points_input_->At(i);
gizmo_drag_start_ = points.at(i);
return true;
}
}
return false;
}
void PolygonGenerator::GizmoMove(const QPointF &p, const QVector2D &scale, const rational& time)
{
QVector2D new_pos = QVector2D(p) / scale;
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());
}
void PolygonGenerator::GizmoRelease()
{
gizmo_x_dragger_.End();
gizmo_y_dragger_.End();
}
QVector<QPointF> PolygonGenerator::GetGizmoCoordinates(NodeValueDatabase &db, const QVector2D& scale) const
{
QVector<QPointF> points(points_input_->GetSize());
for (int i=0;i<points_input_->GetSize();i++) {
QVector2D v = db[points_input_->At(i)].Get(NodeParam::kVec2).value<QVector2D>();
v *= scale;
QPointF pt = v.toPointF();
points[i] = pt;
}
return points;
}
QVector<QRectF> PolygonGenerator::GetGizmoRects(const QVector<QPointF> &points) const
{
QVector<QRectF> rects(points.size());
int rect_sz = QFontMetrics(qApp->font()).height() / 8;
for (int i=0;i<points.size();i++) {
const QPointF& p = points.at(i);
rects[i] = QRectF(p - QPointF(rect_sz, rect_sz),
p + QPointF(rect_sz, rect_sz));
}
return rects;
}
OLIVE_NAMESPACE_EXIT
|