Code

axonomgrid: Bugfix
[inkscape.git] / src / axonomgrid-snapper.cpp
1 #ifdef AXONOM\r
2 \r
3 /**\r
4  *  \file axonomgrid-snapper.cpp\r
5  *  \brief Snapping things to axonometric grids.\r
6  *\r
7  * Author:\r
8  *   Johan Engelen <johan@shouraizou.nl>\r
9  *\r
10  * Copyright (C) 2006 Authors\r
11  *\r
12  * Released under GNU GPL, read the file 'COPYING' for more information\r
13  */\r
14 \r
15 #include "sp-namedview.h"\r
16 #include "inkscape.h"\r
17 #include "desktop.h"\r
18 #include "display/canvas-axonomgrid.h"\r
19 \r
20 /**\r
21  * \return x rounded to the nearest multiple of c1 plus c0.\r
22  *\r
23  * \note\r
24  * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero\r
25  * mean "ignore the grid in this dimention".  We're currently discussing "good" semantics\r
26  * for guide/grid snapping.\r
27  */\r
28 \r
29 /* FIXME: move this somewhere else, perhaps */\r
30 static \r
31 \r
32 Inkscape::AxonomGridSnapper::AxonomGridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)\r
33 {\r
34 \r
35 }\r
36 \r
37 Inkscape::LineSnapper::LineList \r
38 Inkscape::AxonomGridSnapper::_getSnapLines(NR::Point const &p) const\r
39 {\r
40     double round_to_nearest_multiple_plus(double x, double const c1, double const c0)\r
41     {\r
42         return floor((x - c0) / c1 + .5) * c1 + c0;\r
43     }    \r
44     \r
45     LineList s;\r
46 \r
47     if ( NULL == _named_view ) {\r
48         return s;\r
49     }\r
50 \r
51     SPCAxonomGrid *griditem = NULL;\r
52     for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {\r
53         // FIXME : this is a hack since there is only one view for now\r
54         //                 but when we'll handle multiple views, snapping should\r
55         //                 must be rethought and maybe only the current view\r
56         //                 should give back it's SHOWN lines to snap to\r
57         //                 For now, the last SPCGrid in _named_view->gridviews will be used.\r
58         griditem = SP_CAXONOMGRID(l->data);\r
59     }\r
60 \r
61     g_assert(griditem != NULL);\r
62 \r
63     for (unsigned int i = 0; i < 2; ++i) {\r
64 \r
65         /* This is to make sure we snap to only visible grid lines */\r
66         double scaled_spacing = griditem->sw[i]; // this is spacing of visible lines if screen pixels\r
67 \r
68         // convert screen pixels to px\r
69         // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary\r
70         if (SP_ACTIVE_DESKTOP) {\r
71             scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();\r
72         }\r
73 \r
74         NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],\r
75                                                                  scaled_spacing,\r
76                                                                  _named_view->gridorigin[i]);\r
77 \r
78         s.push_back(std::make_pair(NR::Dim2(i), rounded));\r
79     }\r
80 \r
81     return s;\r
82 }\r
83 \r
84 \r
85 \r
86 #endif           \r