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)
31 /*virtual*/ void Circle::Draw(Painter * painter)
33 if (state == OSSelected || hitCircle || hitCenter)
34 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
36 painter->SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
39 // QBrush brush(Qt::DiagCrossPattern);
40 // brush.setColor(QColor(255, 255, 0));
41 // painter->SetBrush(brush);
42 painter->SetBrush(QBrush(Qt::NoBrush));
45 painter->DrawEllipse(position, radius, radius);
47 // & draw handles (if needed)
48 if (state == OSSelected || hitCenter)
49 painter->DrawHandle(position);
51 if (state == OSSelected && draggingEdge && objectWasDragged)
52 painter->DrawHandle(dragPoint);
55 /*virtual*/ Vector Circle::Center(void)
60 /*virtual*/ bool Circle::Collided(Vector point)
62 // We can assume this, since this is a mouse down event here.
63 objectWasDragged = false;
66 draggingCenter = hitCenter;
67 draggingEdge = hitCircle;
69 if (hitCenter || hitCircle)
77 // We didn't hit anything, so deselect this object and report failure to hit
82 /*virtual*/ void Circle::PointerMoved(Vector point)
84 // Hit test tells us what we hit (if anything) through boolean variables. It
85 // also tells us whether or not the state changed.
86 needUpdate = HitTest(point);
87 objectWasDragged = (draggingEdge | draggingCenter);
90 radius = Vector::Magnitude(point, position);
91 else if (draggingCenter)
94 // Save this point so the rendering code knows where to draw the handle...
98 /*virtual*/ void Circle::PointerReleased(void)
100 // Mouse went up, so our dragging is done (if any *was* done, that is)
101 draggingEdge = draggingCenter = false;
102 hitCenter = hitCircle = false;
104 // If the object was dragged, then revert to the old state.
105 // Otherwise, we were probably just clicked, and want to stay in the selected state.
106 if (objectWasDragged)
110 bool Circle::HitTest(Point point)
113 hitCenter = hitCircle = false;
114 double length = Vector::Magnitude(position, point);
115 //printf("Circle::length = %lf, radius = %lf\n", length, radius);
116 //How to translate this into pixels from Document space???
117 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
118 //the caller knows about the zoom factor and all that good kinda crap
120 Document passes in the correct Cartesian coordinates being pointed to by the mouse.
121 So all we have to be concerned with is properly scaling our hot zones/handle sizes,
122 since we generally *don't* want those to scale with the zoom level. ;-)
126 else if ((length < (radius + 2.0)) && (length > (radius - 2.0)))
129 return StateChanged();
132 void Circle::SaveState(void)
134 oldHitCenter = hitCenter;
135 oldHitCircle = hitCircle;
138 bool Circle::StateChanged(void)
140 if ((hitCenter != oldHitCenter) || (hitCircle != oldHitCircle))