X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fcircle.cpp;h=e73bcdc9607fb6dd209a088e87bfb5d696062a35;hb=11cff81f10ccca1b31288fce04d696e715b922b0;hp=ea56d906098e69a25e87ddd1c0101f84ed848a4b;hpb=3b9b56d429a8a8733c8195e41f9b5e525b9b730d;p=architektonas diff --git a/src/circle.cpp b/src/circle.cpp index ea56d90..e73bcdc 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,14 +20,17 @@ 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) { + type = OTCircle; } + Circle::~Circle() { } + /*virtual*/ void Circle::Draw(Painter * painter) { if (state == OSSelected || hitCircle || hitCenter) @@ -33,215 +38,202 @@ Circle::~Circle() else painter->SetPen(QPen(Qt::black, 1.0, Qt::SolidLine)); + // Hatch/Fill... +// QBrush brush(Qt::DiagCrossPattern); +// brush.setColor(QColor(255, 255, 0)); +// painter->SetBrush(brush); + painter->SetBrush(QBrush(Qt::NoBrush)); + + // Draw the object... + painter->DrawEllipse(position, radius, radius); + + // & draw handles (if needed) if (state == OSSelected || hitCenter) - painter->DrawEllipse(position, 4.0, 4.0); + painter->DrawHandle(position); - if (state == OSSelected && dragging) - painter->DrawEllipse(oldPoint, 4.0, 4.0); + if (state == OSSelected && draggingEdge && objectWasDragged) + painter->DrawHandle(dragPoint); - painter->DrawEllipse(position, radius, radius); + // If resizing the circle, draw an information panel showing the new radius. + if (draggingEdge) + { + QString text = QObject::tr("Radius: %1\nScale: %2%"); + text = text.arg(radius, 0, 'd', 4).arg(radius / oldRadius * 100.0, 0, 'd', 0); +#if 0 + QPen pen = QPen(QColor(0x00, 0xFF, 0x00), 1.0, Qt::SolidLine); + painter->SetPen(pen); + painter->SetBrush(QBrush(QColor(0x40, 0xFF, 0x40, 0x9F))); + QRectF textRect(10.0, 10.0, 270.0, 70.0); // x, y, w, h + painter->DrawRoundedRect(textRect, 7.0, 7.0); + + textRect.setLeft(textRect.left() + 14); + painter->SetFont(*Object::font); + pen = QPen(QColor(0x00, 0x5F, 0xDF)); + painter->SetPen(pen); + painter->DrawText(textRect, Qt::AlignVCenter, text); +#else + painter->DrawInformativeText(text); +#endif + } } + /*virtual*/ Vector Circle::Center(void) { return position; } + /*virtual*/ bool Circle::Collided(Vector point) { // We can assume this, since this is a mouse down event here. objectWasDragged = false; HitTest(point); -// objectWasDragged = false; - Vector v1 = position - point; + // Now that we've done our hit testing on the non-snapped point, snap it if + // necessary... + if (snapToGrid) + point = SnapPointToGrid(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; + oldRadius = radius; + 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) { - // 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 = true; - - if (dragging) + if (selectionInProgress) { - // Here we need to check whether or not we're dragging a handle or the object itself... -// Vector delta = point - oldPoint; - -// position += delta; -// endpoint += delta; - radius = Vector(point - position).Magnitude(); + // Check for whether or not the rect contains this circle +// if (selection.normalized().contains(Extents())) + if (selection.contains(Extents())) + state = OSSelected; + else + state = OSInactive; - oldPoint = point; - needUpdate = true; + return; } - else if (draggingHandle1) - { - Vector delta = point - oldPoint; - position += delta; - oldPoint = point; - needUpdate = true; - } -/* else if (draggingHandle2) - { - Vector delta = point - oldPoint; - endpoint += delta; + // Hit test tells us what we hit (if anything) through boolean variables. It + // also tells us whether or not the state changed. + SaveHitState(); + HitTest(point); + needUpdate = HitStateChanged(); + objectWasDragged = (draggingEdge | draggingCenter); - oldPoint = point; + if (objectWasDragged) needUpdate = true; - }*/ -// else -// needUpdate = false; + + 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; } + /*virtual*/ void Circle::PointerReleased(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... -*/ + // Mouse went up, so our dragging is done (if any *was* done, that is) + draggingEdge = draggingCenter = false; + hitCenter = hitCircle = false; + + // 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; } -#if 0 -/*virtual*/ bool Circle::NeedsUpdate(void) -{ - return needUpdate; -} -#endif -bool Circle::HitTest(Point point) +/*virtual*/ bool Circle::HitTest(Point point) { - SaveState(); - +// SaveHitState(); hitCenter = hitCircle = false; - -#if 0 - Vector lineSegment = endpoint - position; - Vector v1 = point - position; - Vector v2 = point - endpoint; - double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance; - - // Geometric interpretation: - // The parameterized point on the vector lineSegment is where the perpendicular - // intersects lineSegment. If pp < 0, then the perpendicular lies beyond the 1st - // endpoint. If pp > length of ls, then the perpendicular lies beyond the 2nd endpoint. - - if (parameterizedPoint < 0.0) - distance = v1.Magnitude(); - else if (parameterizedPoint > lineSegment.Magnitude()) - distance = v2.Magnitude(); - else - // distance = ?Det?(ls, v1) / |ls| - distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude()); - - // Geometric interpretation of the above: - // If the segment endpoints are s and e, and the point is p, then the test - // for the perpendicular intercepting the segment is equivalent to insisting - // that the two dot products {s-e}.{s-p} and {e-s}.{e-p} are both non-negative. - // Perpendicular distance from the point to the segment is computed by first - // computing the area of the triangle the three points form, then dividing by - // the length of the segment. Distances are done just by the Pythagorean - // theorem. Twice the area of the triangle formed by three points is the - // determinant of the following matrix: - // - // sx sy 1 0 0 1 0 0 0 - // ex ey 1 ==> ex ey 1 ==> ex ey 0 - // px py 1 px py 1 px py 0 - // - // By translating the start point to the origin, and subtracting row 1 from - // all other rows, we end up with the matrix on the right which greatly - // simplifies the calculation of the determinant. - -//How do we determine distance here? Especially if zoomed in or out??? -#warning "!!! Distances tested for may not be valid if zoomed in or out !!!" - if (v1.Magnitude() < 8.0) - hitPoint1 = true; - else if (v2.Magnitude() < 8.0) - hitPoint2 = true; - else if (distance < 5.0) - hitLine = true; -#else - Vector v1 = position - point; + 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 - if (v1.Magnitude() < 10.0) +/* +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. ;-) + +What is going on here? +If we're zoomed out to, say, 50%, & our radius is 10.0 (absolute), then on screen +the radius will be 5.0. By multiplying the length by the zoom factor, we align our +pointed at length with our on screen length. +*/ + if ((length * Painter::zoom) < 8.0) hitCenter = true; - else if ((v1.Magnitude() < radius + 2.0) && (v1.Magnitude() > radius - 2.0)) +//wrong: else if ((length < (radius + 2.0)) && (length > (radius - 2.0))) +/*NB: The following should be identical to what we have down below, but it doesn't work out that way... :-P */ +//close, but no else if (((length * Painter::zoom) < ((radius * Painter::zoom) + 2.0)) && ((length * Painter::zoom) > ((radius * Painter::zoom) - 2.0))) +//really wrong! else if (((length * Painter::zoom) < (radius + 2.0)) && ((length * Painter::zoom) > (radius - 2.0))) +// close again, but sill no else if (((length * Painter::zoom) < ((radius + 2.0) * Painter::zoom)) && ((length * Painter::zoom) > ((radius - 2.0) * Painter::zoom))) + else if ((fabs(length - radius) * Painter::zoom) < 2.0) hitCircle = true; -#endif - return StateChanged(); +// return HitStateChanged(); + return (hitCenter || hitCircle ? true : false); +} + + +/*virtual*/ QRectF Circle::Extents(void) +{ + return QRectF(QPointF(position.x - radius, position.y - radius), QPointF(position.x + radius, position.y + radius)); } -void Circle::SaveState(void) + +void Circle::SaveHitState(void) { oldHitCenter = hitCenter; oldHitCircle = hitCircle; } -bool Circle::StateChanged(void) + +bool Circle::HitStateChanged(void) { if ((hitCenter != oldHitCenter) || (hitCircle != oldHitCircle)) return true; return false; } + + +/*virtual*/ void Circle::Enumerate(FILE * file) +{ + fprintf(file, "CIRCLE %i (%lf,%lf) %lf\n", layer, position.x, position.y, radius); +} + + +/*virtual*/ Object * Circle::Copy(void) +{ +#warning "!!! This doesn't take care of attached Dimensions !!!" +/* +This is a real problem. While having a pointer in the Dimension to this line's points +is fast & easy, it creates a huge problem when trying to replicate an object like this. + +Maybe a way to fix that then, is to have reference numbers instead of pointers. That +way, if you copy them, ... you might still have problems. Because you can't be sure if +a copy will be persistant or not, you then *definitely* do not want them to have the +same reference number. +*/ + return new Circle(position, radius, parent); +} +