forked from jahnf/Projecteur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotshapes.cc
252 lines (207 loc) · 7.1 KB
/
spotshapes.cc
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// This file is part of Projecteur - https://github.com/jahnf/projecteur - See LICENSE.md and README.md
#include "spotshapes.h"
#include <QSGGeometryNode>
#include <QSGFlatColorMaterial>
#include <cmath>
namespace {
const bool registered = [](){
SpotShapeStar::qmlRegister();
SpotShapeNGon::qmlRegister();
return true;
}();
}
SpotShapeStar::SpotShapeStar(QQuickItem* parent) : QQuickItem (parent)
{
setEnabled(false);
setFlags(QQuickItem::ItemHasContents);
}
int SpotShapeStar::qmlRegister()
{
return qmlRegisterType<SpotShapeStar>("Projecteur.Shapes", 1, 0, "Star");
}
QSGNode* SpotShapeStar::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* updatePaintNodeData)
{
if (width() <= 0 || height() <= 0 || m_color.alpha() == 0 ) {
delete oldNode;
return nullptr;
}
// Directly access the QSG transformnode for the Items node: updatePaintNodeData->transformNode->...;
Q_UNUSED(updatePaintNodeData)
const auto vertexCount = m_points*2+2;
// Create geometry node for colored shape
QSGGeometryNode* geometryNode = static_cast<QSGGeometryNode *>(oldNode);
if (geometryNode == nullptr)
{
geometryNode = new QSGGeometryNode();
// Set geometry
QSGGeometry* const geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount);
#if QT_VERSION >= 0x050800
geometry->setDrawingMode(QSGGeometry::DrawTriangleFan);
#else
geometry->setDrawingMode(GL_TRIANGLE_FAN);
#endif
geometryNode->setGeometry(geometry);
geometryNode->setFlag(QSGNode::OwnsGeometry, true);
QSGFlatColorMaterial* const material = new QSGFlatColorMaterial();
material->setColor(m_color);
geometryNode->setMaterial(material);
geometryNode->setFlag(QSGNode::OwnsMaterial);
}
else {
const auto geometry = geometryNode->geometry();
if( geometry->vertexCount() != vertexCount ) {
geometry->allocate(vertexCount);
}
}
QSGGeometry::Point2D* const vertices = geometryNode->geometry()->vertexDataAsPoint2D();
const int numSegments = m_points * 2;
const float cx = static_cast<float>(width()/2); // center X
const float cy = static_cast<float>(height()/2); // center Y
const float deltaRad = static_cast<float>((360.0 / m_points) * (M_PI/180.0));
float theta = -static_cast<float>(90.0 * M_PI/180.0);
vertices[0].set(cx, cy);
// Vertices for (outer) star points
for(int seg=1; seg < numSegments; seg+=2, theta+=deltaRad)
{
const float x = cx * cosf(theta);
const float y = cy * sinf(theta);
vertices[seg].set(x + cx, y + cy);
}
const float dist0_1 = std::sqrt(std::pow(vertices[0].x-vertices[1].x, 2.0f)
+ std::pow(vertices[0].y-vertices[1].y, 2.0f));
const float dist1_3_2 = std::sqrt(std::pow(vertices[1].x-vertices[3].x, 2.0f)
+ std::pow(vertices[1].y-vertices[3].y, 2.0f)) / 2.0f;
const float maxInnerDist = std::sqrt(std::pow(dist0_1,2.0f) - std::pow(dist1_3_2, 2.0f));
const float innerDistance = maxInnerDist * float(m_innerRadius)/100.0f;
// Vertices for inner radius
theta = -static_cast<float>(90.0 * M_PI/180.0) + deltaRad/2 ;
for(int seg=2; seg < numSegments+1; seg+=2, theta+=deltaRad)
{
const float x = innerDistance * std::cos(theta);
const float y = innerDistance * std::sin(theta);
vertices[seg].set(x + cx, y + cy);
}
vertices[vertexCount-1] = vertices[1]; // last star point = first star point
geometryNode->markDirty(QSGGeometryNode::DirtyGeometry);
return geometryNode;
}
QColor SpotShapeStar::color() const
{
return m_color;
}
void SpotShapeStar::setColor(const QColor &color)
{
if (m_color == color)
return;
m_color = color;
emit colorChanged(color);
update(); // redraw, schedules updatePaintNode()...
}
int SpotShapeStar::points() const
{
return m_points;
}
void SpotShapeStar::setPoints(int points)
{
if (m_points == points)
return;
m_points = qMin(qMax(3, points), 100);
emit pointsChanged(m_points);
update(); // redraw, schedules updatePaintNode()...
}
int SpotShapeStar::innerRadius() const
{
return m_innerRadius;
}
void SpotShapeStar::setInnerRadius(int radiusPercentage)
{
if (radiusPercentage > m_innerRadius || radiusPercentage < m_innerRadius)
{
m_innerRadius = qMin(qMax(5, radiusPercentage), 100);
emit innerRadiusChanged(m_innerRadius);
update(); // redraw, schedules updatePaintNode()...
}
}
SpotShapeNGon::SpotShapeNGon(QQuickItem* parent) : QQuickItem (parent)
{
setEnabled(false);
setFlags(QQuickItem::ItemHasContents);
}
int SpotShapeNGon::qmlRegister()
{
return qmlRegisterType<SpotShapeNGon>("Projecteur.Shapes", 1, 0, "NGon");
}
QColor SpotShapeNGon::color() const
{
return m_color;
}
void SpotShapeNGon::setColor(const QColor &color)
{
if (m_color == color)
return;
m_color = color;
emit colorChanged(color);
update(); // redraw, schedules updatePaintNode()...
}
int SpotShapeNGon::sides() const
{
return m_sides;
}
void SpotShapeNGon::setSides(int sides)
{
if (m_sides == sides)
return;
m_sides = qMin(qMax(3, sides), 100);
emit sidesChanged(m_sides);
update(); // redraw, schedules updatePaintNode()...
}
QSGNode* SpotShapeNGon::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* updatePaintNodeData)
{
if (width() <= 0 || height() <= 0 || m_color.alpha() == 0 ) {
delete oldNode;
return nullptr;
}
// Directly access the QSG transformnode for the Items node: updatePaintNodeData->transformNode->...;
Q_UNUSED(updatePaintNodeData)
const auto vertexCount = m_sides + 2;
// Create geometry node for colored shape
QSGGeometryNode* geometryNode = static_cast<QSGGeometryNode *>(oldNode);
if (geometryNode == nullptr)
{
geometryNode = new QSGGeometryNode();
// Set geometry
QSGGeometry* const geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), vertexCount);
#if QT_VERSION >= 0x050800
geometry->setDrawingMode(QSGGeometry::DrawTriangleFan);
#else
geometry->setDrawingMode(GL_TRIANGLE_FAN);
#endif
geometryNode->setGeometry(geometry);
geometryNode->setFlag(QSGNode::OwnsGeometry, true);
QSGFlatColorMaterial * const material = new QSGFlatColorMaterial();
material->setColor(m_color);
geometryNode->setMaterial(material);
geometryNode->setFlag(QSGNode::OwnsMaterial);
}
else {
const auto geometry = geometryNode->geometry();
if( geometry->vertexCount() != vertexCount ) {
geometry->allocate(vertexCount);
}
}
QSGGeometry::Point2D* const vertices = geometryNode->geometry()->vertexDataAsPoint2D();
const float cx = static_cast<float>(width()/2); // center X
const float cy = static_cast<float>(height()/2); // center Y
const float deltaRad = static_cast<float>((360.0 / m_sides) * (M_PI/180.0));
float theta = -static_cast<float>(90.0 * M_PI/180.0);
vertices[0].set(cx, cy);
for(int seg=1; seg < vertexCount; ++seg, theta+=deltaRad)
{
const float x = cx * cosf(theta);
const float y = cy * sinf(theta);
vertices[seg].set(x + cx, y + cy);
}
vertices[vertexCount-1] = vertices[1]; // last shape point = first shape point
geometryNode->markDirty(QSGGeometryNode::DirtyGeometry);
return geometryNode;
}