]> Shamusworld >> Repos - architektonas/blobdiff - src/object.cpp
Fix object selection to work while in snap mode.
[architektonas] / src / object.cpp
index 523601dd755b438f20e0857bad2a127b0aa33f2e..93af822b8476008c2f60e6158955078cdaf38c0d 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "object.h"
 #include <stdlib.h>
+#include <math.h>
 
 // Initialize static variables
 bool Object::fixedAngle = false;
@@ -31,6 +32,7 @@ bool Object::ignoreClicks = false;
 bool Object::dontMove = false;
 bool Object::selectionInProgress = false;
 QRectF Object::selection;
+double Object::gridSpacing;
 
 
 Object::Object(): position(Vector(0, 0)), parent(0), type(OTObject),
@@ -40,8 +42,9 @@ Object::Object(): position(Vector(0, 0)), parent(0), type(OTObject),
 }
 
 
-Object::Object(Vector v,  Object * passedInParent/*= 0*/): position(v), parent(passedInParent),
-       state(OSInactive), oldState(OSInactive), needUpdate(false)//, attachedDimension(0)
+Object::Object(Vector v,  Object * passedInParent/*= 0*/): position(v),
+       parent(passedInParent), state(OSInactive), oldState(OSInactive),
+       needUpdate(false)//, attachedDimension(0)
 {
 }
 
@@ -147,6 +150,7 @@ printf("Object: Destroyed!\n");
 
 /*virtual*/ void Object::Disconnect(Object * obj, double parameter)
 {
+#if 0
        for(uint i=0; i<connected.size(); i++)
        {
                if (connected[i].object == obj && connected[i].t == parameter)
@@ -155,11 +159,24 @@ printf("Object: Destroyed!\n");
                        return;
                }
        }
+#else
+       std::vector<Connection>::iterator i;
+
+       for(i=connected.begin(); i!=connected.end(); i++)
+       {
+               if (((*i).object == obj) && ((*i).t == parameter))
+               {
+                       connected.erase(i);
+                       return;
+               }
+       }
+#endif
 }
 
 
 /*virtual*/ void Object::DisconnectAll(Object * obj)
 {
+#if 0
        // According the std::vector docs, only items at position i and beyond are
        // invalidated, everything before i is still valid. So we use that here.
        for(uint i=0; i<connected.size();)
@@ -171,6 +188,17 @@ printf("Object: Destroyed!\n");
                else
                        i++;
        }
+#else
+       std::vector<Connection>::iterator i;
+
+       for(i=connected.begin(); i!=connected.end(); )
+       {
+               if ((*i).object == obj)
+                       connected.erase(i);
+               else
+                       i++;
+       }
+#endif
 }
 
 
@@ -189,8 +217,9 @@ printf("Object: Destroyed!\n");
 #endif
 
 
-/*virtual*/ void Object::Translate(Vector)
+/*virtual*/ void Object::Translate(Vector amount)
 {
+       position += amount;
 }
 
 
@@ -264,3 +293,25 @@ void Object::SetSnapMode(bool state/*= true*/)
 {
        snapToGrid = state;
 }
+
+
+//
+// This looks strange, but it's really quite simple: We want a point that's
+// more than half-way to the next grid point to snap there while conversely we
+// want a point that's less than half-way to to the next grid point then snap
+// to the one before it. So we add half of the grid spacing to the point, then
+// divide by it so that we can remove the fractional part, then multiply it
+// back to get back to the correct answer.
+//
+Vector Object::SnapPointToGrid(Vector point)
+{
+       point += gridSpacing / 2.0;             // *This* adds to Z!!!
+       point /= gridSpacing;
+       point.x = floor(point.x);//need to fix this for negative numbers...
+       point.y = floor(point.y);
+       point.z = 0;                                    // Make *sure* Z doesn't go anywhere!!!
+       point *= gridSpacing;
+       return point;
+}
+
+