1 // circle.cpp: Circle object
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 03/28/2011 Created this file
12 // JLH 09/26/2011 Added hover effects
13 // JLH 09/26/2011 Major cleanup of this class
22 Circle::Circle(Vector p1, double r, Object * p/*= NULL*/): Object(p1, p), radius(r),
23 draggingEdge(false), draggingCenter(false), hitCenter(false), hitCircle(false)
34 /*virtual*/ void Circle::Draw(Painter * painter)
36 if (state == OSSelected || hitCircle || hitCenter)
37 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
39 painter->SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
42 // QBrush brush(Qt::DiagCrossPattern);
43 // brush.setColor(QColor(255, 255, 0));
44 // painter->SetBrush(brush);
45 painter->SetBrush(QBrush(Qt::NoBrush));
48 painter->DrawEllipse(position, radius, radius);
50 // & draw handles (if needed)
51 if (state == OSSelected || hitCenter)
52 painter->DrawHandle(position);
54 if (state == OSSelected && draggingEdge && objectWasDragged)
55 painter->DrawHandle(dragPoint);
59 /*virtual*/ Vector Circle::Center(void)
65 /*virtual*/ bool Circle::Collided(Vector point)
67 // We can assume this, since this is a mouse down event here.
68 objectWasDragged = false;
71 draggingCenter = hitCenter;
72 draggingEdge = hitCircle;
74 if (hitCenter || hitCircle)
82 // We didn't hit anything, so deselect this object and report failure to hit
88 /*virtual*/ void Circle::PointerMoved(Vector point)
90 if (selectionInProgress)
92 // Check for whether or not the rect contains this circle
93 if (selection.normalized().contains(Extents()))
101 // Hit test tells us what we hit (if anything) through boolean variables. It
102 // also tells us whether or not the state changed.
103 needUpdate = HitTest(point);
104 objectWasDragged = (draggingEdge | draggingCenter);
107 radius = Vector::Magnitude(point, position);
108 else if (draggingCenter)
111 // Save this point so the rendering code knows where to draw the handle...
116 /*virtual*/ void Circle::PointerReleased(void)
118 // Mouse went up, so our dragging is done (if any *was* done, that is)
119 draggingEdge = draggingCenter = false;
120 hitCenter = hitCircle = false;
122 // If the object was dragged, then revert to the old state.
123 // Otherwise, we were probably just clicked, and want to stay in the selected state.
124 if (objectWasDragged)
129 /*virtual*/ bool Circle::HitTest(Point point)
132 hitCenter = hitCircle = false;
133 double length = Vector::Magnitude(position, point);
134 //printf("Circle::length = %lf, radius = %lf\n", length, radius);
135 //How to translate this into pixels from Document space???
136 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
137 //the caller knows about the zoom factor and all that good kinda crap
139 Document passes in the correct Cartesian coordinates being pointed to by the mouse.
140 So all we have to be concerned with is properly scaling our hot zones/handle sizes,
141 since we generally *don't* want those to scale with the zoom level. ;-)
143 What is going on here?
144 If we're zoomed out to, say, 50%, & our radius is 10.0 (absolute), then on screen
145 the radius will be 5.0. By multiplying the length by the zoom factor, we align our
146 pointed at length with our on screen length.
148 if ((length * Painter::zoom) < 8.0)
150 //wrong: else if ((length < (radius + 2.0)) && (length > (radius - 2.0)))
151 /*NB: The following should be identical to what we have down below, but it doesn't work out that way... :-P */
152 //close, but no else if (((length * Painter::zoom) < ((radius * Painter::zoom) + 2.0)) && ((length * Painter::zoom) > ((radius * Painter::zoom) - 2.0)))
153 //really wrong! else if (((length * Painter::zoom) < (radius + 2.0)) && ((length * Painter::zoom) > (radius - 2.0)))
154 // close again, but sill no else if (((length * Painter::zoom) < ((radius + 2.0) * Painter::zoom)) && ((length * Painter::zoom) > ((radius - 2.0) * Painter::zoom)))
155 else if ((fabs(length - radius) * Painter::zoom) < 2.0)
158 return StateChanged();
162 /*virtual*/ QRectF Circle::Extents(void)
164 return QRectF(QPointF(position.x - radius, position.y - radius), QPointF(position.x + radius, position.y + radius));
168 void Circle::SaveState(void)
170 oldHitCenter = hitCenter;
171 oldHitCircle = hitCircle;
175 bool Circle::StateChanged(void)
177 if ((hitCenter != oldHitCenter) || (hitCircle != oldHitCircle))
184 /*virtual*/ void Circle::Enumerate(FILE * file)
186 fprintf(file, "CIRCLE (%lf,%lf) %lf\n", position.x, position.y, radius);