]> Shamusworld >> Repos - architektonas/blob - src/arc.cpp
In the middle of refactoring objects for loading/saving.
[architektonas] / src / arc.cpp
1 // arc.cpp: Arc object
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  03/30/2011  Created this file
12 // JLH  04/03/2011  Added information panel (angles) rendering
13 //
14
15 #include "arc.h"
16
17 #include <QtGui>
18 #include "mathconstants.h"
19 #include "painter.h"
20
21
22 Arc::Arc(Vector p1, double r, double a1, double a2, Object * p/*= NULL*/): Object(p1, p),
23         radius(r), startAngle(a1), angleSpan(a2)
24 {
25 }
26
27
28 Arc::~Arc()
29 {
30 }
31
32
33 /*virtual*/ void Arc::Draw(Painter * painter)
34 {
35         QPen pen;
36
37         if (state == OSSelected)
38         {
39                 Point p1(cos(startAngle), sin(startAngle));
40                 Point p2(cos(startAngle + angleSpan), sin(startAngle + angleSpan));
41                 Vector handle2 = (p1 * radius) + position;
42                 Vector handle3 = (p2 * radius) + position;
43
44                 if ((hitHandle2 || hitHandle3) && objectWasDragged)
45                 {
46                         if (hitHandle2)
47                         {
48                                 // If we rotating, we draw a guideline showing the angle we're
49                                 // moving it from.
50                                 Point p3(cos(oldAngle), sin(oldAngle));
51                                 Vector oldLine = (p3 * (radius * 1.25)) + position;
52                                 pen = QPen(QColor(0x80, 0x80, 0x80), 1.0, Qt::DashLine);
53                                 painter->SetPen(pen);
54                                 painter->DrawLine((int)position.x, (int)position.y, (int)oldLine.x, (int)oldLine.y);
55                         }
56
57                         // In rotating and setting the span, we draw a line showing where
58                         // we angle/span is that we're setting.
59                         pen = QPen(QColor(0x00, 0xC0, 0x80), 1.0, Qt::DashLine);
60                         painter->SetPen(pen);
61                         painter->DrawLine((int)position.x, (int)position.y, (int)oldPoint.x, (int)oldPoint.y);
62                 }
63
64                 // Draw the center point of the arc
65                 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
66                 painter->DrawHandle(position);
67
68                 // Draw the rotation & span setting handles
69                 painter->DrawHandle(handle2);
70                 painter->DrawHandle(handle3);
71
72                 // If we're rotating or setting the span, draw an information panel
73                 // showing both absolute and relative angles being set.
74                 if ((hitHandle2 || hitHandle3 || hitHandle4) && objectWasDragged)
75                 {
76                         double absAngle = (Vector(oldPoint - position).Angle()) * RADIANS_TO_DEGREES;
77                         double relAngle = (startAngle >= oldAngle ? startAngle - oldAngle :
78                                 startAngle - oldAngle + (2.0 * PI)) * RADIANS_TO_DEGREES;
79
80                         QString text;
81
82                         if (hitHandle2)
83                         {
84                                 text = QObject::tr("Abs ") + QChar(0x2221) + ": %1" + QChar(0x00B0)
85                                         + QObject::tr("\nRel ") + QChar(0x2221) + ": %2" + QChar(0x00B0);
86                                 text = text.arg(absAngle, 0, 'd', 4).arg(relAngle, 0, 'd', 4);
87                         }
88                         else if (hitHandle3)
89                         {
90                                 text = QObject::tr("Abs ") + QChar(0x2221) + ": %1" + QChar(0x00B0)
91                                         + QObject::tr("\nSpan: %2") + QChar(0x00B0);
92                                 text = text.arg(absAngle, 0, 'd', 4).arg(angleSpan * RADIANS_TO_DEGREES, 0, 'd', 4);
93                         }
94                         else if (hitHandle4)
95                         {
96                                 text = QObject::tr("Radius: %1\nScale: %2%");
97                                 text = text.arg(radius, 0, 'd', 4).arg(radius / oldRadius * 100.0, 0, 'd', 0);
98                         }
99
100                         pen = QPen(QColor(0x00, 0xFF, 0x00), 1.0, Qt::SolidLine);
101                         painter->SetPen(pen);
102                         painter->SetBrush(QBrush(QColor(0x40, 0xFF, 0x40, 0x9F)));
103                         QRectF textRect(10.0, 10.0, 260.0, 60.0);       // x, y, w, h
104                         painter->DrawRoundedRect(textRect, 7.0, 7.0);
105
106                         textRect.setLeft(textRect.left() + 14);
107                         painter->SetFont(*Object::font);
108                         pen = QPen(QColor(0xDF, 0x5F, 0x00), 1.0, Qt::SolidLine);
109                         painter->SetPen(pen);
110                         painter->DrawText(textRect, Qt::AlignVCenter, text);
111                 }
112         }
113         else
114         {
115                 pen = QPen(Qt::black, 1.0, Qt::SolidLine);
116                 painter->SetPen(pen);
117         }
118
119         painter->DrawArc(position, radius, startAngle, angleSpan);
120 }
121
122
123 /*virtual*/ Vector Arc::Center(void)
124 {
125         return position;
126 }
127
128
129 /*
130  We need at least *four* handles for this object:
131  - one for moving
132  - one for resizing
133  - one for rotation
134  - one for setting the span of the arc
135
136 We need to think about the intuitive way (if there is any) to grab and
137 manipulate a complex object like this... Need to think, "What should happen when
138 I click here and drag there?"
139
140 Also: should put the snap logic into the Object base class (as a static method)...
141 */
142
143 /*virtual*/ bool Arc::Collided(Vector point)
144 {
145         objectWasDragged = false;
146         Vector v1 = point - position;                   // Head minus tail (vector points at "point")
147
148         // Check for collision with various things...
149         hitHandle1 = false;     // Moving
150         hitHandle2 = false;     // Rotation
151         hitHandle3 = false;     // Setting span of the arc
152         hitHandle4 = false;     // Resizing
153 /*
154 What we have:
155 the center of the arc
156 the starting angle
157 the span of the arc
158 The point on a unit circle given an angle a is x = cos(a), y = sin(a)
159 This vector is already unitized, so all we need to do to get our point is to multiply it by
160 radius (to get the length correct) and add it to the center point (to get the correct position).
161 */
162         Point p1(cos(startAngle), sin(startAngle));
163         Point p2(cos(startAngle + angleSpan), sin(startAngle + angleSpan));
164         Vector handle2 = (p1 * radius) + position;
165         Vector handle3 = (p2 * radius) + position;
166         double pointerAngle = v1.Angle();
167
168 #if 1
169         // Center handle
170         if (v1.Magnitude() < 10.0)
171                 hitHandle1 = true;
172         // Span handle
173         else if (Vector(handle3 - point).Magnitude() < 10.0)
174                 hitHandle3 = true;
175         // Rotate handle
176         else if (Vector(handle2 - point).Magnitude() < 10.0)
177                 hitHandle2 = true;
178         // Resize handle (the arc itself)
179         else if ((v1.Magnitude() < radius + 3.0) && (v1.Magnitude() > radius - 3.0)
180                 && AngleInArcSpan(pointerAngle))
181                 hitHandle4 = true;
182 #endif
183
184 /*
185 State Management:
186 We want the arc to go into OSSelected mode if we click on it but don't drag.
187 If we don't click anywhere on the arc, then we want it to go into OSInactive mode.
188 Otherwise, we hit a handle and we want to modify the object whether it's in
189 OSSelected mode or not.
190
191 If I, go to S. Can drag, but only handles 2, 3, & 4 (since the center is not highlighted
192 until we select it.
193         However, if dragging, revert to I once finished.
194
195 If S, stay in S. Can drag all.
196
197 So, we know this. If wasDragged is true, then there's a chance we need to revert to I.
198 How can we be sure?
199
200 So. We have a matrix that looks like this:
201
202         |Was I|Was S
203 --------+-----+-----
204 Inactive|     |   
205 --------+-----+-----
206 Selected|     |   
207
208         |h1|h2|h3|h4
209 --------+--+--+--+--
210 Inactive|  |  |  |
211 --------+--+--+--+--
212 Selected|  |  |  |
213
214 so let's do like this:
215 */
216         if (hitHandle1 || hitHandle2 || hitHandle3 || hitHandle4)
217         {
218                 oldState = state;
219                 state = OSSelected;
220 //              oldPoint = point;
221                 oldPoint = position;
222                 oldAngle = startAngle;
223                 oldRadius = radius;
224
225                 return true;
226         }
227
228         state = OSInactive;
229         return false;
230 }
231
232
233 /*virtual*/ void Arc::PointerMoved(Vector point)
234 {
235         // The TLC will send these messages if the object is selected but not clicked on.
236         // So we have to be careful with our assumptions here.
237         // This is actually untrue in that case, we need to come up with something better
238         // here...
239         objectWasDragged = true;
240         needUpdate = false;
241
242         if (!(hitHandle1 || hitHandle2 || hitHandle3 || hitHandle4))
243                 return;
244
245         Vector delta = point - oldPoint;
246
247         if (hitHandle1)                 // Move arc
248         {
249                 position += delta;
250         }
251         else if (hitHandle2)    // Rotate arc
252         {
253                 startAngle = Vector(point - position).Angle();
254         }
255         else if (hitHandle3)    // Set arc span
256         {
257                 double angle = Vector(point - position).Angle();
258
259                 if (angle < startAngle)
260                         angle += 2.0 * PI;
261
262                 angleSpan = angle - startAngle;
263         }
264         else if (hitHandle4)    // Resize the radius of the arc
265         {
266                 radius = Vector(point - position).Magnitude();
267         }
268
269         oldPoint = point;
270         needUpdate = true;
271 }
272
273
274 /*virtual*/ void Arc::PointerReleased(void)
275 {
276         hitHandle1 = hitHandle2 = hitHandle3 = hitHandle4 = false;
277         // Here we check for just a click: If object was clicked and dragged, then
278         // revert to the old state (OSInactive). Otherwise, keep the new state that
279         // we set.
280 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
281 about keeping track of old states...
282 Well, we can't know if it was dragged from Inactive or not, that's the problem. We could make
283 a variable called "needToRevertToInactive" instead
284
285 I mean, we could write like:
286         if (objectWasDragged && oldState == OSInactive)
287                 state = OSInactive;
288
289 but this is actually more compact and cleaner.
290 */
291         if (objectWasDragged)
292                 state = oldState;
293 }
294
295 #if 0
296 /*virtual*/ bool Arc::NeedsUpdate(void)
297 {
298         return needUpdate;
299 }
300 #endif
301
302
303 /*
304 start = 350, span = 20, end = 10, angle = 5
305 angle < start, so angle = 365
306 */
307 bool Arc::AngleInArcSpan(double angle)
308 {
309         // This should be simple except for a small complication: The start angle plus
310         // the angle span can end up being less than the start angle! So, to check
311         // for this possibility, we check to see if the angle passed in is less than
312         // the start angle and if so, add 2PI to the angle passed in. Then we take the
313         // difference between our start angle and this adjusted angle and see if it
314         // falls within our span.
315         if (angle < startAngle)
316                 angle += 2.0 * PI;
317
318         double passedInSpan = angle - startAngle;
319
320         return (passedInSpan <= angleSpan ?  true : false);
321 }
322
323
324 /*virtual*/ void Arc::Enumerate(FILE * file)
325 {
326         fprintf(file, "ARC (%lf,%lf) %lf, %lf, %lf\n", position.x, position.y, radius, startAngle, angleSpan);
327 }
328