Code

Axonometric grid: snapping to vertical gridlines
authorjohanengelen <johanengelen@users.sourceforge.net>
Mon, 23 Oct 2006 19:02:59 +0000 (19:02 +0000)
committerjohanengelen <johanengelen@users.sourceforge.net>
Mon, 23 Oct 2006 19:02:59 +0000 (19:02 +0000)
src/Makefile_insert
src/axonomgrid-snapper.cpp [new file with mode: 0644]
src/axonomgrid-snapper.h [new file with mode: 0644]

index fec782544d394d65f5d4b88cf364fea0ddd2845e..a6a827ead030e4819ece8d5733cffb8ef51595c1 100644 (file)
@@ -40,6 +40,7 @@ libinkpre_a_SOURCES = \
        approx-equal.h remove-last.h    \
        arc-context.cpp arc-context.h   \
        attributes.cpp attributes.h     \
+       axonomgrid-snapper.cpp axonomgrid-snapper.h \
        bad-uri-exception.h     \
        brokenimage.xpm \
        color-rgba.h    \
diff --git a/src/axonomgrid-snapper.cpp b/src/axonomgrid-snapper.cpp
new file mode 100644 (file)
index 0000000..674357d
--- /dev/null
@@ -0,0 +1,84 @@
+#ifdef AXONOM\r
+\r
+/**\r
+ *  \file axonomgrid-snapper.cpp\r
+ *  \brief Snapping things to axonometric grids.\r
+ *\r
+ * Author:\r
+ *   Johan Engelen <johan@shouraizou.nl>\r
+ *\r
+ * Copyright (C) 2006 Authors\r
+ *\r
+ * Released under GNU GPL, read the file 'COPYING' for more information\r
+ */\r
+\r
+#include "sp-namedview.h"\r
+#include "inkscape.h"\r
+#include "desktop.h"\r
+#include "display/canvas-grid.h"\r
+\r
+/**\r
+ * \return x rounded to the nearest multiple of c1 plus c0.\r
+ *\r
+ * \note\r
+ * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero\r
+ * mean "ignore the grid in this dimention".  We're currently discussing "good" semantics\r
+ * for guide/grid snapping.\r
+ */\r
+\r
+/* FIXME: move this somewhere else, perhaps */\r
+static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)\r
+{\r
+    return floor((x - c0) / c1 + .5) * c1 + c0;\r
+}\r
+\r
+Inkscape::AxonomGridSnapper::AxonomGridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)\r
+{\r
+\r
+}\r
+\r
+Inkscape::LineSnapper::LineList \r
+Inkscape::AxonomGridSnapper::_getSnapLines(NR::Point const &p) const\r
+{\r
+    LineList s;\r
+\r
+    if ( NULL == _named_view ) {\r
+        return s;\r
+    }\r
+\r
+    SPCGrid *griditem = NULL;\r
+    for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {\r
+        // FIXME : this is a hack since there is only one view for now\r
+        //                 but when we'll handle multiple views, snapping should\r
+        //                 must be rethought and maybe only the current view\r
+        //                 should give back it's SHOWN lines to snap to\r
+        //                 For now, the last SPCGrid in _named_view->gridviews will be used.\r
+        griditem = SP_CGRID(l->data);\r
+    }\r
+\r
+    g_assert(griditem != NULL);\r
+\r
+    for (unsigned int i = 0; i < 2; ++i) {\r
+\r
+        /* This is to make sure we snap to only visible grid lines */\r
+        double scaled_spacing = griditem->sw[i]; // this is spacing of visible lines if screen pixels\r
+\r
+        // convert screen pixels to px\r
+        // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary\r
+        if (SP_ACTIVE_DESKTOP) {\r
+            scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();\r
+        }\r
+\r
+        NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],\r
+                                                                 scaled_spacing,\r
+                                                                 _named_view->gridorigin[i]);\r
+\r
+        s.push_back(std::make_pair(NR::Dim2(i), rounded));\r
+    }\r
+\r
+    return s;\r
+}\r
+\r
+\r
+\r
+#endif           \r
diff --git a/src/axonomgrid-snapper.h b/src/axonomgrid-snapper.h
new file mode 100644 (file)
index 0000000..46090ee
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef SEEN_AXONOMGRID_SNAPPER_H\r
+#define SEEN_AXONOMGRID_SNAPPER_H\r
+\r
+/**\r
+ *  \file axonomgrid-snapper.h\r
+ *  \brief Snapping things to axonometricgrids.\r
+ *\r
+ * Author:\r
+ *   Johan Engelen <johan@shouraizou.nl>\r
+ *\r
+ * Copyright (C) 2006 Author\r
+ *\r
+ * Released under GNU GPL, read the file 'COPYING' for more information\r
+ */\r
+\r
+#include "line-snapper.h"\r
+\r
+namespace Inkscape\r
+{\r
+\r
+class AxonomGridSnapper : public LineSnapper                             \r
+{                                                                       \r
+public:                                                                \r
+    AxonomGridSnapper(SPNamedView const *nv, NR::Coord const d);            \r
+\r
+private:    \r
+    LineList _getSnapLines(NR::Point const &p) const;\r
+};\r
+\r
+}\r
+\r
+#endif    \r
+\r