]> Shamusworld >> Repos - architektonas/blob - src/circle.cpp
Major refactor of Architektonas: Jettisoning old cruft.
[architektonas] / src / circle.cpp
1 // circle.cpp: Circle 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 L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  03/28/2011  Created this file
12 //
13
14 #include "circle.h"
15
16 #include <QtGui>
17
18 Circle::Circle(Vector p1, double r, Object * p/*= NULL*/): Object(p1, p), radius(r),
19         dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false)
20 {
21 }
22
23 Circle::~Circle()
24 {
25 }
26
27 /*virtual*/ void Circle::Draw(QPainter * painter)
28 {
29         if (state == OSSelected)
30                 painter->setPen(QPen(Qt::red, 2.0, Qt::DotLine));
31         else
32                 painter->setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
33
34 //      if (draggingHandle1)
35         if (state == OSSelected)
36                 painter->drawEllipse(QPointF(position.x, position.y), 4.0, 4.0);
37
38 //      if (draggingHandle2)
39 //      if (state == OSSelected)
40 //              painter->drawEllipse(QPointF(endpoint.x, endpoint.y), 4.0, 4.0);
41         if (state == OSSelected && dragging)
42                 painter->drawEllipse(QPointF(oldPoint.x, oldPoint.y), 4.0, 4.0);
43
44         painter->drawEllipse(QPointF(position.x, position.y), radius, radius);
45 }
46
47 /*virtual*/ Vector Circle::Center(void)
48 {
49         return position;
50 }
51
52 /*virtual*/ bool Circle::Collided(Vector point)
53 {
54         objectWasDragged = false;
55         Vector v1 = position - point;
56
57         if (state == OSInactive)
58         {
59 //printf("Circle: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
60 //printf("      v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
61 //printf("      point = %lf,%lf,%lf; p1 = %lf,%lf,%lf; p2 = %lf,%lf,%lf\n", point.x, point.y, point.z, position.x, position.y, position.z, endpoint.x, endpoint.y, endpoint.z);
62 //printf("      \n", );
63 //How to translate this into pixels from Document space???
64 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
65 //the caller knows about the zoom factor and all that good kinda crap
66                 if (v1.Magnitude() < 10.0)
67                 {
68                         oldState = state;
69                         state = OSSelected;
70                         oldPoint = position; //maybe "position"?
71                         draggingHandle1 = true;
72                         return true;
73                 }
74                 else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
75                 {
76                         oldState = state;
77                         state = OSSelected;
78                         oldPoint = point;
79                         dragging = true;
80                         return true;
81                 }
82         }
83         else if (state == OSSelected)
84         {
85                 // Here we test for collision with handles as well! (SOON!)
86 /*
87 Like so:
88                 if (v1.Magnitude() < 2.0) // Handle #1
89                 else if (v2.Magnitude() < 2.0) // Handle #2
90 */
91                 if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0))
92                 {
93                         oldState = state;
94 //                      state = OSInactive;
95                         oldPoint = point;
96                         dragging = true;
97                         return true;
98                 }
99         }
100
101         state = OSInactive;
102         return false;
103 }
104
105 /*virtual*/ void Circle::PointerMoved(Vector point)
106 {
107         objectWasDragged = true;
108
109         if (dragging)
110         {
111                 // Here we need to check whether or not we're dragging a handle or the object itself...
112 //              Vector delta = point - oldPoint;
113
114 //              position += delta;
115 //              endpoint += delta;
116                 radius = Vector(point - position).Magnitude();
117
118                 oldPoint = point;
119                 needUpdate = true;
120         }
121         else if (draggingHandle1)
122         {
123                 Vector delta = point - oldPoint;
124                 position += delta;
125                 oldPoint = point;
126                 needUpdate = true;
127         }
128 /*      else if (draggingHandle2)
129         {
130                 Vector delta = point - oldPoint;
131
132                 endpoint += delta;
133
134                 oldPoint = point;
135                 needUpdate = true;
136         }*/
137         else
138                 needUpdate = false;
139 }
140
141 /*virtual*/ void Circle::PointerReleased(void)
142 {
143         dragging = false;
144         draggingHandle1 = false;
145         draggingHandle2 = false;
146
147         // Here we check for just a click: If object was clicked and dragged, then
148         // revert to the old state (OSInactive). Otherwise, keep the new state that
149         // we set.
150 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
151 about keeping track of old states...
152 */
153         if (objectWasDragged)
154                 state = oldState;
155 }
156
157 #if 0
158 /*virtual*/ bool Circle::NeedsUpdate(void)
159 {
160         return needUpdate;
161 }
162 #endif