Code

change NR::Matrix to Geom:: for many sp_item_xxx_affine functions
[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"
28 int KnotHolderEntity::counter = 0;
30 void
31 KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, const gchar *tip,
32                          SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
33 {
34     knot = sp_knot_new(desktop, tip);
36     this->parent_holder = parent;
37     this->item = item; // TODO: remove the item either from here or from knotholder.cpp
39     my_counter = KnotHolderEntity::counter++;
41     g_object_set(G_OBJECT (knot->item), "shape", shape, NULL);
42     g_object_set(G_OBJECT (knot->item), "mode", mode, NULL);
44     knot->fill [SP_KNOT_STATE_NORMAL] = color;
45     g_object_set (G_OBJECT (knot->item), "fill_color", color, NULL);
47     update_knot();
48     sp_knot_show(knot);
50     _moved_connection = knot->_moved_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_moved_handler));
51     _click_connection = knot->_click_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_clicked_handler));
52     _ungrabbed_connection = knot->_ungrabbed_signal.connect(sigc::mem_fun(*parent_holder, &KnotHolder::knot_ungrabbed_handler));
53 }
56 KnotHolderEntity::~KnotHolderEntity()
57 {
58     /* unref should call destroy */
59     g_object_unref(knot);
60 }
62 void
63 KnotHolderEntity::update_knot()
64 {
65     NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
67     NR::Point dp(knot_get() * i2d);
69     _moved_connection.block();
70     sp_knot_set_position(knot, &dp, SP_KNOT_STATE_NORMAL); 
71     _moved_connection.unblock();
72 }
74 /* Pattern manipulation */
76 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
77 {
78     gdouble theta = asin(pat->patternTransform[1] / scale);
79     if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
80     return theta;
81 }
83 static gdouble sp_pattern_extract_scale(SPPattern *pat)
84 {
85     gdouble s = pat->patternTransform[1];
86     gdouble c = pat->patternTransform[0];
87     gdouble xscale = sqrt(c * c + s * s);
88     return xscale;
89 }
91 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
92 {
93     return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
94 }
96 void
97 PatternKnotHolderEntityXY::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
98 {
99     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
101     NR::Point p_snapped = p;
103     if ( state & GDK_CONTROL_MASK ) {
104         if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
105             p_snapped[NR::Y] = origin[NR::Y];
106         } else {
107             p_snapped[NR::X] = origin[NR::X];
108         }
109     }
111     if (state)  {
112         NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
113         sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
114     }
116     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
119 NR::Point
120 PatternKnotHolderEntityXY::knot_get()
122     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
123     return sp_pattern_extract_trans(pat);
126 NR::Point
127 PatternKnotHolderEntityAngle::knot_get()
129     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
131     gdouble x = (pattern_width(pat)*0.5);
132     gdouble y = 0;
133     NR::Point delta = NR::Point(x,y);
134     gdouble scale = sp_pattern_extract_scale(pat);
135     gdouble theta = sp_pattern_extract_theta(pat, scale);
136     delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
137     delta = delta + sp_pattern_extract_trans(pat);
138     return delta;
141 void
142 PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
144     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
146     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
148     // get the angle from pattern 0,0 to the cursor pos
149     NR::Point delta = p - sp_pattern_extract_trans(pat);
150     gdouble theta = atan2(delta);
152     if ( state & GDK_CONTROL_MASK ) {
153         theta = sp_round(theta, M_PI/snaps);
154     }
156     // get the scale from the current transform so we can keep it.
157     gdouble scl = sp_pattern_extract_scale(pat);
158     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
159     NR::Point const t = sp_pattern_extract_trans(pat);
160     rot[4] = t[NR::X];
161     rot[5] = t[NR::Y];
162     sp_item_adjust_pattern(item, rot, true);
163     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
166 void
167 PatternKnotHolderEntityScale::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint /*state*/)
169     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
171     // Get the scale from the position of the knotholder,
172     NR::Point d = p - sp_pattern_extract_trans(pat);
173     gdouble s = NR::L2(d);
174     gdouble pat_x = pattern_width(pat) * 0.5;
175     gdouble pat_y = pattern_height(pat) * 0.5;
176     gdouble pat_h = hypot(pat_x, pat_y);
177     gdouble scl = s / pat_h;
179     // get angle from current transform, (need get current scale first to calculate angle)
180     gdouble oldscale = sp_pattern_extract_scale(pat);
181     gdouble theta = sp_pattern_extract_theta(pat,oldscale);
183     NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
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);
192 NR::Point
193 PatternKnotHolderEntityScale::knot_get()
195     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
197     gdouble x = pattern_width(pat)*0.5;
198     gdouble y = pattern_height(pat)*0.5;
199     NR::Point delta = NR::Point(x,y);
200     NR::Matrix a = pat->patternTransform;
201     a[4] = 0;
202     a[5] = 0;
203     delta = delta * a;
204     delta = delta + sp_pattern_extract_trans(pat);
205     return delta;
208 /*
209   Local Variables:
210   mode:c++
211   c-file-style:"stroustrup"
212   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
213   indent-tabs-mode:nil
214   fill-column:99
215   End:
216 */
217 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :