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