X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fcircle.cpp;h=5750b1f0ecabb66ac2929f68cb4b6543ea035bbf;hb=9d5d4488ba75c407709556a7ed56b8d16e21834e;hp=a8902cc4da11d38449e10f2a8f595991df01c881;hpb=bd9b40058a376c946318a444dd6c77737ec6ac98;p=architektonas diff --git a/src/circle.cpp b/src/circle.cpp index a8902cc..5750b1f 100644 --- a/src/circle.cpp +++ b/src/circle.cpp @@ -4,11 +4,13 @@ // (C) 2011 Underground Software // See the README and GPLv3 files for licensing and warranty information // -// JLH = James L. Hammons +// JLH = James Hammons // // WHO WHEN WHAT // --- ---------- ------------------------------------------------------------ // JLH 03/28/2011 Created this file +// JLH 09/26/2011 Added hover effects +// JLH 09/26/2011 Major cleanup of this class // #include "circle.h" @@ -18,7 +20,7 @@ Circle::Circle(Vector p1, double r, Object * p/*= NULL*/): Object(p1, p), radius(r), - dragging(false), draggingHandle1(false), draggingHandle2(false)//, needUpdate(false) + draggingEdge(false), draggingCenter(false), hitCenter(false), hitCircle(false) { } @@ -28,22 +30,20 @@ Circle::~Circle() /*virtual*/ void Circle::Draw(Painter * painter) { - if (state == OSSelected) + if (state == OSSelected || hitCircle || hitCenter) painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine)); else painter->SetPen(QPen(Qt::black, 1.0, Qt::SolidLine)); -// if (draggingHandle1) - if (state == OSSelected) - painter->DrawEllipse(position, 4.0, 4.0); + // Draw the object... + painter->DrawEllipse(position, radius, radius); -// if (draggingHandle2) -// if (state == OSSelected) -// painter->drawEllipse(QPointF(endpoint.x, endpoint.y), 4.0, 4.0); - if (state == OSSelected && dragging) - painter->DrawEllipse(oldPoint, 4.0, 4.0); + // & draw handles (if needed) + if (state == OSSelected || hitCenter) + painter->DrawHandle(position); - painter->DrawEllipse(position, radius, radius); + if (state == OSSelected && draggingEdge && objectWasDragged) + painter->DrawHandle(dragPoint); } /*virtual*/ Vector Circle::Center(void) @@ -53,112 +53,86 @@ Circle::~Circle() /*virtual*/ bool Circle::Collided(Vector point) { + // We can assume this, since this is a mouse down event here. objectWasDragged = false; - Vector v1 = position - point; + HitTest(point); - if (state == OSInactive) - { -//printf("Circle: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance); -//printf(" v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude()); -//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); -//printf(" \n", ); -//How to translate this into pixels from Document space??? -//Maybe we need to pass a scaling factor in here from the caller? That would make sense, as -//the caller knows about the zoom factor and all that good kinda crap - if (v1.Magnitude() < 10.0) - { - oldState = state; - state = OSSelected; - oldPoint = position; //maybe "position"? - draggingHandle1 = true; - return true; - } - else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0)) - { - oldState = state; - state = OSSelected; - oldPoint = point; - dragging = true; - return true; - } - } - else if (state == OSSelected) + draggingCenter = hitCenter; + draggingEdge = hitCircle; + + if (hitCenter || hitCircle) { - // Here we test for collision with handles as well! (SOON!) -/* -Like so: - if (v1.Magnitude() < 2.0) // Handle #1 - else if (v2.Magnitude() < 2.0) // Handle #2 -*/ - if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0)) - { - oldState = state; -// state = OSInactive; - oldPoint = point; - dragging = true; - return true; - } + dragPoint = point; + oldState = state; + state = OSSelected; + return true; } + // We didn't hit anything, so deselect this object and report failure to hit state = OSInactive; return false; } /*virtual*/ void Circle::PointerMoved(Vector point) { - objectWasDragged = true; - - if (dragging) - { - // Here we need to check whether or not we're dragging a handle or the object itself... -// Vector delta = point - oldPoint; + // Hit test tells us what we hit (if anything) through boolean variables. It + // also tells us whether or not the state changed. + needUpdate = HitTest(point); + objectWasDragged = (draggingEdge | draggingCenter); + + if (draggingEdge) + radius = Vector::Magnitude(point, position); + else if (draggingCenter) + position = point; + + // Save this point so the rendering code knows where to draw the handle... + dragPoint = point; +} -// position += delta; -// endpoint += delta; - radius = Vector(point - position).Magnitude(); +/*virtual*/ void Circle::PointerReleased(void) +{ + // Mouse went up, so our dragging is done (if any *was* done, that is) + draggingEdge = draggingCenter = false; + hitCenter = hitCircle = false; - oldPoint = point; - needUpdate = true; - } - else if (draggingHandle1) - { - Vector delta = point - oldPoint; - position += delta; - oldPoint = point; - needUpdate = true; - } -/* else if (draggingHandle2) - { - Vector delta = point - oldPoint; + // If the object was dragged, then revert to the old state. + // Otherwise, we were probably just clicked, and want to stay in the selected state. + if (objectWasDragged) + state = oldState; +} - endpoint += delta; +bool Circle::HitTest(Point point) +{ + SaveState(); + hitCenter = hitCircle = false; + double length = Vector::Magnitude(position, point); +//printf("Circle::length = %lf, radius = %lf\n", length, radius); +//How to translate this into pixels from Document space??? +//Maybe we need to pass a scaling factor in here from the caller? That would make sense, as +//the caller knows about the zoom factor and all that good kinda crap +/* +Document passes in the correct Cartesian coordinates being pointed to by the mouse. +So all we have to be concerned with is properly scaling our hot zones/handle sizes, +since we generally *don't* want those to scale with the zoom level. ;-) +*/ + if (length < 8.0) + hitCenter = true; + else if ((length < (radius + 2.0)) && (length > (radius - 2.0))) + hitCircle = true; - oldPoint = point; - needUpdate = true; - }*/ - else - needUpdate = false; + return StateChanged(); } -/*virtual*/ void Circle::PointerReleased(void) +void Circle::SaveState(void) { - dragging = false; - draggingHandle1 = false; - draggingHandle2 = false; - - // Here we check for just a click: If object was clicked and dragged, then - // revert to the old state (OSInactive). Otherwise, keep the new state that - // we set. -/*Maybe it would be better to just check for "object was dragged" state and not have to worry -about keeping track of old states... -*/ - if (objectWasDragged) - state = oldState; + oldHitCenter = hitCenter; + oldHitCircle = hitCircle; } -#if 0 -/*virtual*/ bool Circle::NeedsUpdate(void) +bool Circle::StateChanged(void) { - return needUpdate; + if ((hitCenter != oldHitCenter) || (hitCircle != oldHitCircle)) + return true; + + return false; } -#endif