Code

enable snapping of pattern's origin
[inkscape.git] / src / knot-holder-entity.cpp
1 #define __KNOT_HOLDER_ENTITY_C__
3 /** \file 
4  * KnotHolderEntity definition. 
5  * 
6  * Authors:
7  *   Mitsuru Oka <oka326@parkcity.ne.jp>
8  *   Maximilian Albert <maximilian.albert@gmail.com>
9  *
10  * Copyright (C) 1999-2001 Lauris Kaplinski
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  * Copyright (C) 2001 Mitsuru Oka
13  * Copyright (C) 2004 Monash University
14  * Copyright (C) 2008 Maximilian Albert
15  *
16  * Released under GNU GPL
17  */
19 #include "knotholder.h"
20 #include "sp-item.h"
21 #include "style.h"
22 #include "prefs-utils.h"
23 #include "macros.h"
24 #include <libnr/nr-matrix-ops.h>
25 #include "sp-pattern.h"
26 #include "inkscape.h"
27 #include "snap.h"
28 #include "desktop-affine.h"
29 #include "desktop.h"
30 #include "desktop-handles.h"
31 #include "sp-namedview.h"
33 int KnotHolderEntity::counter = 0;
35 void
36 KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, const gchar *tip,
37                          SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
38 {
39     knot = sp_knot_new(desktop, tip);
41     this->parent_holder = parent;
42     this->item = item; // TODO: remove the item either from here or from knotholder.cpp
44     my_counter = KnotHolderEntity::counter++;
46     g_object_set(G_OBJECT (knot->item), "shape", shape, NULL);
47     g_object_set(G_OBJECT (knot->item), "mode", mode, NULL);
49     knot->fill [SP_KNOT_STATE_NORMAL] = color;
50     g_object_set (G_OBJECT (knot->item), "fill_color", color, NULL);
52     update_knot();
53     sp_knot_show(knot);
55     _moved_connection = knot->_moved_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_moved_handler));
56     _click_connection = knot->_click_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_clicked_handler));
57     _ungrabbed_connection = knot->_ungrabbed_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_ungrabbed_handler));
58 }
61 KnotHolderEntity::~KnotHolderEntity()
62 {
63     _moved_connection.disconnect();
64     _click_connection.disconnect();
65     _ungrabbed_connection.disconnect();
67     /* unref should call destroy */
68     if (knot) {
69         g_object_unref(knot);
70     } else {
71         // FIXME: This shouldn't occur. Perhaps it is caused by LPE PointParams being knotholder entities, too
72         //        If so, it will likely be fixed with upcoming refactoring efforts.
73         g_return_if_fail(knot);
74     }
75 }
77 void
78 KnotHolderEntity::update_knot()
79 {
80     NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
82     NR::Point dp(knot_get() * i2d);
84     _moved_connection.block();
85     sp_knot_set_position(knot, &dp, SP_KNOT_STATE_NORMAL); 
86     _moved_connection.unblock();
87 }
89 /* Pattern manipulation */
91 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
92 {
93     gdouble theta = asin(pat->patternTransform[1] / scale);
94     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
95     return theta;
96 }
98 static gdouble sp_pattern_extract_scale(SPPattern *pat)
99 {
100     gdouble s = pat->patternTransform[1];
101     gdouble c = pat->patternTransform[0];
102     gdouble xscale = sqrt(c * c + s * s);
103     return xscale;
106 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
108     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
111 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
113     SPDesktop const *desktop = inkscape_active_desktop();
114     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
115     NR::Point s = p * i2d;
116     SnapManager &m = desktop->namedview->snap_manager;
117     m.setup(desktop, item);
118     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, s);
119     return s * i2d.inverse();
122 void
123 PatternKnotHolderEntityXY::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
125     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
127     NR::Point p_snapped = snap_knot_position(item, p); //p;
129     if ( state & GDK_CONTROL_MASK ) {
130         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
131             p_snapped[NR::Y] = origin[NR::Y];
132         } else {
133             p_snapped[NR::X] = origin[NR::X];
134         }
135     }
137     if (state)  {
138         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
139         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
140     }
142     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
145 NR::Point
146 PatternKnotHolderEntityXY::knot_get()
148     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
149     return sp_pattern_extract_trans(pat);
152 NR::Point
153 PatternKnotHolderEntityAngle::knot_get()
155     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
157     gdouble x = (pattern_width(pat)*0.5);
158     gdouble y = 0;
159     NR::Point delta = NR::Point(x,y);
160     gdouble scale = sp_pattern_extract_scale(pat);
161     gdouble theta = sp_pattern_extract_theta(pat, scale);
162     delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
163     delta = delta + sp_pattern_extract_trans(pat);
164     return delta;
167 void
168 PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
170     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
172     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
174     // get the angle from pattern 0,0 to the cursor pos
175     NR::Point delta = p - sp_pattern_extract_trans(pat);
176     gdouble theta = atan2(delta);
178     if ( state & GDK_CONTROL_MASK ) {
179         theta = sp_round(theta, M_PI/snaps);
180     }
182     // get the scale from the current transform so we can keep it.
183     gdouble scl = sp_pattern_extract_scale(pat);
184     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
185     NR::Point const t = sp_pattern_extract_trans(pat);
186     rot[4] = t[NR::X];
187     rot[5] = t[NR::Y];
188     sp_item_adjust_pattern(item, rot, true);
189     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
192 void
193 PatternKnotHolderEntityScale::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
195     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
197     // Get the scale from the position of the knotholder,
198     NR::Point d = p - sp_pattern_extract_trans(pat);
199     gdouble s = NR::L2(d);
200     gdouble pat_x = pattern_width(pat) * 0.5;
201     gdouble pat_y = pattern_height(pat) * 0.5;
202     gdouble pat_h = hypot(pat_x, pat_y);
203     gdouble scl = s / pat_h;
205     // get angle from current transform, (need get current scale first to calculate angle)
206     gdouble oldscale = sp_pattern_extract_scale(pat);
207     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
209     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
210     NR::Point const t = sp_pattern_extract_trans(pat);
211     rot[4] = t[NR::X];
212     rot[5] = t[NR::Y];
213     sp_item_adjust_pattern(item, rot, true);
214     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
218 NR::Point
219 PatternKnotHolderEntityScale::knot_get()
221     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
223     gdouble x = pattern_width(pat)*0.5;
224     gdouble y = pattern_height(pat)*0.5;
225     NR::Point delta = NR::Point(x,y);
226     NR::Matrix a = pat->patternTransform;
227     a[4] = 0;
228     a[5] = 0;
229     delta = delta * a;
230     delta = delta + sp_pattern_extract_trans(pat);
231     return delta;
234 /*
235   Local Variables:
236   mode:c++
237   c-file-style:"stroustrup"
238   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
239   indent-tabs-mode:nil
240   fill-column:99
241   End:
242 */
243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :