Code

patterns: make it possible to have different scaling in X and Y directions. snap...
[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"
32 #include <2geom/matrix.h>
34 int KnotHolderEntity::counter = 0;
36 void
37 KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, const gchar *tip,
38                          SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
39 {
40     knot = sp_knot_new(desktop, tip);
42     this->parent_holder = parent;
43     this->item = item; // TODO: remove the item either from here or from knotholder.cpp
45     my_counter = KnotHolderEntity::counter++;
47     g_object_set(G_OBJECT (knot->item), "shape", shape, NULL);
48     g_object_set(G_OBJECT (knot->item), "mode", mode, NULL);
50     knot->fill [SP_KNOT_STATE_NORMAL] = color;
51     g_object_set (G_OBJECT (knot->item), "fill_color", color, NULL);
53     update_knot();
54     sp_knot_show(knot);
56     _moved_connection = knot->_moved_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_moved_handler));
57     _click_connection = knot->_click_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_clicked_handler));
58     _ungrabbed_connection = knot->_ungrabbed_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_ungrabbed_handler));
59 }
62 KnotHolderEntity::~KnotHolderEntity()
63 {
64     _moved_connection.disconnect();
65     _click_connection.disconnect();
66     _ungrabbed_connection.disconnect();
68     /* unref should call destroy */
69     if (knot) {
70         g_object_unref(knot);
71     } else {
72         // FIXME: This shouldn't occur. Perhaps it is caused by LPE PointParams being knotholder entities, too
73         //        If so, it will likely be fixed with upcoming refactoring efforts.
74         g_return_if_fail(knot);
75     }
76 }
78 void
79 KnotHolderEntity::update_knot()
80 {
81     NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
83     NR::Point dp(knot_get() * i2d);
85     _moved_connection.block();
86     sp_knot_set_position(knot, &dp, SP_KNOT_STATE_NORMAL); 
87     _moved_connection.unblock();
88 }
90 /* Pattern manipulation */
92 static gdouble sp_pattern_extract_theta(SPPattern *pat, NR::Point scale)
93 {
94     gdouble theta = asin(pat->patternTransform[1] / scale[NR::X]);
95     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
96     return theta;
97 }
99 static NR::Point sp_pattern_extract_scale(SPPattern *pat)
101     Geom::Matrix transf = to_2geom(pat->patternTransform);
102     return NR::Point( transf.expansionX(), transf.expansionY() );
105 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
107     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
110 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
112     SPDesktop const *desktop = inkscape_active_desktop();
113     NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
114     NR::Point s = p * i2d;
115     SnapManager &m = desktop->namedview->snap_manager;
116     m.setup(desktop, item);
117     m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, s);
118     return s * i2d.inverse();
121 void
122 PatternKnotHolderEntityXY::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
124     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
126     NR::Point p_snapped = snap_knot_position(item, p);
128     if ( state & GDK_CONTROL_MASK ) {
129         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
130             p_snapped[NR::Y] = origin[NR::Y];
131         } else {
132             p_snapped[NR::X] = origin[NR::X];
133         }
134     }
136     if (state)  {
137         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
138         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
139     }
141     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
144 NR::Point
145 PatternKnotHolderEntityXY::knot_get()
147     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
148     return sp_pattern_extract_trans(pat);
151 NR::Point
152 PatternKnotHolderEntityAngle::knot_get()
154     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
156     gdouble x = (pattern_width(pat)*0.5);
157     gdouble y = 0;
158     NR::Point delta = NR::Point(x,y);
159     NR::Point scale = sp_pattern_extract_scale(pat);
160     gdouble theta = sp_pattern_extract_theta(pat, scale);
161     delta = delta * NR::Matrix(NR::scale(scale))*NR::Matrix(NR::rotate(theta));
162     delta = delta + sp_pattern_extract_trans(pat);
163     return delta;
166 void
167 PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
169     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
171     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
173     // get the angle from pattern 0,0 to the cursor pos
174     NR::Point delta = p - sp_pattern_extract_trans(pat);
175     gdouble theta = atan2(delta);
177     if ( state & GDK_CONTROL_MASK ) {
178         theta = sp_round(theta, M_PI/snaps);
179     }
181     // get the scale from the current transform so we can keep it.
182     NR::Point scl = sp_pattern_extract_scale(pat);
183     NR::Matrix rot = NR::Matrix(NR::scale(scl)) * NR::Matrix(NR::rotate(theta));
184     NR::Point const t = sp_pattern_extract_trans(pat);
185     rot[4] = t[NR::X];
186     rot[5] = t[NR::Y];
187     sp_item_adjust_pattern(item, rot, true);
188     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
191 void
192 PatternKnotHolderEntityScale::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
194     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
196     NR::Point p_snapped = snap_knot_position(item, p);
198     // get angle from current transform, (need get current scale first to calculate angle)
199     NR::Point oldscale = sp_pattern_extract_scale(pat);
200     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
202     // Get the new scale from the position of the knotholder
203     NR::Point d = p_snapped - sp_pattern_extract_trans(pat);
204     d *= NR::Matrix(NR::rotate(-theta));
205     gdouble pat_x = pattern_width(pat) * 0.5;
206     gdouble pat_y = pattern_height(pat) * 0.5;
207     NR::Point scl (d[NR::X] / pat_x, d[NR::Y] / pat_y);
209     NR::Matrix rot =  NR::Matrix(NR::scale(scl)) * NR::Matrix(NR::rotate(theta));
211     NR::Point const t = sp_pattern_extract_trans(pat);
212     rot[4] = t[NR::X];
213     rot[5] = t[NR::Y];
214     sp_item_adjust_pattern(item, rot, true);
215     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
219 NR::Point
220 PatternKnotHolderEntityScale::knot_get()
222     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
224     gdouble x = pattern_width(pat)*0.5;
225     gdouble y = pattern_height(pat)*0.5;
226     NR::Point delta = NR::Point(x,y);
227     NR::Matrix a = pat->patternTransform;
228     a[4] = 0;
229     a[5] = 0;
230     delta = delta * a;
231     delta = delta + sp_pattern_extract_trans(pat);
232     return delta;
235 /*
236   Local Variables:
237   mode:c++
238   c-file-style:"stroustrup"
239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240   indent-tabs-mode:nil
241   fill-column:99
242   End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :