1 #define __SP_OBJECT_EDIT_C__
3 /*
4 * Node editing extension to objects
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Mitsuru Oka
9 *
10 * Licensed under GNU GPL
11 */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
19 #include "sp-item.h"
20 #include "sp-rect.h"
21 #include "box3d.h"
22 #include "sp-ellipse.h"
23 #include "sp-star.h"
24 #include "sp-spiral.h"
25 #include "sp-offset.h"
26 #include "sp-flowtext.h"
27 #include "prefs-utils.h"
28 #include "inkscape.h"
29 #include "snap.h"
30 #include "desktop-affine.h"
31 #include <style.h>
32 #include "desktop.h"
33 #include "desktop-handles.h"
34 #include "sp-namedview.h"
36 #include "sp-pattern.h"
37 #include "sp-path.h"
39 #include <glibmm/i18n.h>
41 #include "object-edit.h"
43 #include <libnr/nr-scale-ops.h>
46 #include "xml/repr.h"
48 #include "isnan.h"
50 #define sp_round(v,m) (((v) < 0.0) ? ((ceil((v) / (m) - 0.5)) * (m)) : ((floor((v) / (m) + 0.5)) * (m)))
52 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop);
53 //static
54 SPKnotHolder *sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop);
55 static SPKnotHolder *sp_arc_knot_holder(SPItem *item, SPDesktop *desktop);
56 static SPKnotHolder *sp_star_knot_holder(SPItem *item, SPDesktop *desktop);
57 static SPKnotHolder *sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop);
58 static SPKnotHolder *sp_offset_knot_holder(SPItem *item, SPDesktop *desktop);
59 static SPKnotHolder *sp_misc_knot_holder(SPItem *item, SPDesktop *desktop);
60 static SPKnotHolder *sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop);
61 static void sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder);
63 SPKnotHolder *
64 sp_item_knot_holder(SPItem *item, SPDesktop *desktop)
65 {
66 if (SP_IS_RECT(item)) {
67 return sp_rect_knot_holder(item, desktop);
68 } else if (SP_IS_3DBOX(item)) {
69 return sp_3dbox_knot_holder(item, desktop);
70 } else if (SP_IS_ARC(item)) {
71 return sp_arc_knot_holder(item, desktop);
72 } else if (SP_IS_STAR(item)) {
73 return sp_star_knot_holder(item, desktop);
74 } else if (SP_IS_SPIRAL(item)) {
75 return sp_spiral_knot_holder(item, desktop);
76 } else if (SP_IS_OFFSET(item)) {
77 return sp_offset_knot_holder(item, desktop);
78 } else if (SP_IS_FLOWTEXT(item) && SP_FLOWTEXT(item)->has_internal_frame()) {
79 return sp_flowtext_knot_holder(item, desktop);
80 } else {
81 return sp_misc_knot_holder(item, desktop);
82 }
84 return NULL;
85 }
88 /* Pattern manipulation */
90 static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble scale)
91 {
92 gdouble theta = asin(pat->patternTransform[1] / scale);
93 if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
94 return theta;
95 }
97 static gdouble sp_pattern_extract_scale(SPPattern *pat)
98 {
99 gdouble s = pat->patternTransform[1];
100 gdouble c = pat->patternTransform[0];
101 gdouble xscale = sqrt(c * c + s * s);
102 return xscale;
103 }
105 static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
106 {
107 return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
108 }
110 static void
111 sp_pattern_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
112 {
113 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
115 NR::Point p_snapped = p;
117 if ( state & GDK_CONTROL_MASK ) {
118 if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
119 p_snapped[NR::Y] = origin[NR::Y];
120 } else {
121 p_snapped[NR::X] = origin[NR::X];
122 }
123 }
125 if (state) {
126 NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
127 sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
128 }
130 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
131 }
134 static NR::Point sp_pattern_xy_get(SPItem *item)
135 {
136 SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
137 return sp_pattern_extract_trans(pat);
138 }
140 static NR::Point sp_pattern_angle_get(SPItem *item)
141 {
142 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
144 gdouble x = (pattern_width(pat)*0.5);
145 gdouble y = 0;
146 NR::Point delta = NR::Point(x,y);
147 gdouble scale = sp_pattern_extract_scale(pat);
148 gdouble theta = sp_pattern_extract_theta(pat, scale);
149 delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
150 delta = delta + sp_pattern_extract_trans(pat);
151 return delta;
152 }
154 static void
155 sp_pattern_angle_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
156 {
157 int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
159 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
161 // get the angle from pattern 0,0 to the cursor pos
162 NR::Point delta = p - sp_pattern_extract_trans(pat);
163 gdouble theta = atan2(delta);
165 if ( state & GDK_CONTROL_MASK ) {
166 theta = sp_round(theta, M_PI/snaps);
167 }
169 // get the scale from the current transform so we can keep it.
170 gdouble scl = sp_pattern_extract_scale(pat);
171 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
172 NR::Point const t = sp_pattern_extract_trans(pat);
173 rot[4] = t[NR::X];
174 rot[5] = t[NR::Y];
175 sp_item_adjust_pattern(item, rot, true);
176 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
177 }
179 static void
180 sp_pattern_scale_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
181 {
182 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
184 // Get the scale from the position of the knotholder,
185 NR::Point d = p - sp_pattern_extract_trans(pat);
186 gdouble s = NR::L2(d);
187 gdouble pat_x = pattern_width(pat) * 0.5;
188 gdouble pat_y = pattern_height(pat) * 0.5;
189 gdouble pat_h = hypot(pat_x, pat_y);
190 gdouble scl = s / pat_h;
192 // get angle from current transform, (need get current scale first to calculate angle)
193 gdouble oldscale = sp_pattern_extract_scale(pat);
194 gdouble theta = sp_pattern_extract_theta(pat,oldscale);
196 NR::Matrix rot = NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
197 NR::Point const t = sp_pattern_extract_trans(pat);
198 rot[4] = t[NR::X];
199 rot[5] = t[NR::Y];
200 sp_item_adjust_pattern(item, rot, true);
201 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
202 }
205 static NR::Point sp_pattern_scale_get(SPItem *item)
206 {
207 SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
209 gdouble x = pattern_width(pat)*0.5;
210 gdouble y = pattern_height(pat)*0.5;
211 NR::Point delta = NR::Point(x,y);
212 NR::Matrix a = pat->patternTransform;
213 a[4] = 0;
214 a[5] = 0;
215 delta = delta * a;
216 delta = delta + sp_pattern_extract_trans(pat);
217 return delta;
218 }
220 /* SPRect */
222 static NR::Point snap_knot_position(SPItem *item, NR::Point const &p)
223 {
224 SPDesktop const *desktop = inkscape_active_desktop();
225 NR::Matrix const i2d (sp_item_i2d_affine (item));
226 NR::Point s = p * i2d;
227 SnapManager const &m = desktop->namedview->snap_manager;
228 s = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, s, item).getPoint();
229 return s * i2d.inverse();
230 }
232 static NR::Point sp_rect_rx_get(SPItem *item)
233 {
234 SPRect *rect = SP_RECT(item);
236 return NR::Point(rect->x.computed + rect->width.computed - rect->rx.computed, rect->y.computed);
237 }
239 static void sp_rect_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
240 {
241 SPRect *rect = SP_RECT(item);
243 //In general we cannot just snap this radius to an arbitrary point, as we have only a single
244 //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
245 //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
247 if (state & GDK_CONTROL_MASK) {
248 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
249 rect->rx.computed = rect->ry.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, temp);
250 rect->rx._set = rect->ry._set = true;
252 } else {
253 rect->rx.computed = CLAMP(rect->x.computed + rect->width.computed - p[NR::X], 0.0, rect->width.computed / 2.0);
254 rect->rx._set = true;
255 }
257 ((SPObject*)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
258 }
261 static NR::Point sp_rect_ry_get(SPItem *item)
262 {
263 SPRect *rect = SP_RECT(item);
265 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->ry.computed);
266 }
268 static void sp_rect_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
269 {
270 SPRect *rect = SP_RECT(item);
272 //In general we cannot just snap this radius to an arbitrary point, as we have only a single
273 //degree of freedom. For snapping to an arbitrary point we need two DOF. If we're going to snap
274 //the radius then we should have a constrained snap. snap_knot_position() is unconstrained
276 if (state & GDK_CONTROL_MASK) {
277 gdouble temp = MIN(rect->height.computed, rect->width.computed) / 2.0;
278 rect->rx.computed = rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed, 0.0, temp);
279 rect->ry._set = rect->rx._set = true;
280 } else {
281 if (!rect->rx._set || rect->rx.computed == 0) {
282 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
283 0.0,
284 MIN(rect->height.computed / 2.0, rect->width.computed / 2.0));
285 } else {
286 rect->ry.computed = CLAMP(p[NR::Y] - rect->y.computed,
287 0.0,
288 rect->height.computed / 2.0);
289 }
291 rect->ry._set = true;
292 }
294 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
295 }
297 /**
298 * Remove rounding from a rectangle.
299 */
300 static void rect_remove_rounding(SPRect *rect)
301 {
302 SP_OBJECT_REPR(rect)->setAttribute("rx", NULL);
303 SP_OBJECT_REPR(rect)->setAttribute("ry", NULL);
304 }
306 /**
307 * Called when the horizontal rounding radius knot is clicked.
308 */
309 static void sp_rect_rx_knot_click(SPItem *item, guint state)
310 {
311 SPRect *rect = SP_RECT(item);
313 if (state & GDK_SHIFT_MASK) {
314 rect_remove_rounding(rect);
315 } else if (state & GDK_CONTROL_MASK) {
316 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
317 SP_OBJECT_REPR(rect)->setAttribute("ry", SP_OBJECT_REPR(rect)->attribute("rx"));
318 }
319 }
321 /**
322 * Called when the vertical rounding radius knot is clicked.
323 */
324 static void sp_rect_ry_knot_click(SPItem *item, guint state)
325 {
326 SPRect *rect = SP_RECT(item);
328 if (state & GDK_SHIFT_MASK) {
329 rect_remove_rounding(rect);
330 } else if (state & GDK_CONTROL_MASK) {
331 /* Ctrl-click sets the vertical rounding to be the same as the horizontal */
332 SP_OBJECT_REPR(rect)->setAttribute("rx", SP_OBJECT_REPR(rect)->attribute("ry"));
333 }
334 }
336 #define SGN(x) ((x)>0?1:((x)<0?-1:0))
338 static void sp_rect_clamp_radii(SPRect *rect)
339 {
340 // clamp rounding radii so that they do not exceed width/height
341 if (2 * rect->rx.computed > rect->width.computed) {
342 rect->rx.computed = 0.5 * rect->width.computed;
343 rect->rx._set = true;
344 }
345 if (2 * rect->ry.computed > rect->height.computed) {
346 rect->ry.computed = 0.5 * rect->height.computed;
347 rect->ry._set = true;
348 }
349 }
351 static NR::Point sp_rect_wh_get(SPItem *item)
352 {
353 SPRect *rect = SP_RECT(item);
355 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
356 }
358 static void sp_rect_wh_set_internal(SPRect *rect, NR::Point const &p, NR::Point const &origin, guint state)
359 {
360 NR::Point const s = snap_knot_position(rect, p);
362 if (state & GDK_CONTROL_MASK) {
363 // original width/height when drag started
364 gdouble const w_orig = (origin[NR::X] - rect->x.computed);
365 gdouble const h_orig = (origin[NR::Y] - rect->y.computed);
367 //original ratio
368 gdouble const ratio = (w_orig / h_orig);
370 // mouse displacement since drag started
371 gdouble const minx = s[NR::X] - origin[NR::X];
372 gdouble const miny = s[NR::Y] - origin[NR::Y];
374 if (fabs(minx) > fabs(miny)) {
376 // snap to horizontal or diagonal
377 rect->width.computed = MAX(w_orig + minx, 0);
378 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
379 // closer to the diagonal and in same-sign quarters, change both using ratio
380 rect->height.computed = MAX(h_orig + minx / ratio, 0);
381 } else {
382 // closer to the horizontal, change only width, height is h_orig
383 rect->height.computed = MAX(h_orig, 0);
384 }
386 } else {
387 // snap to vertical or diagonal
388 rect->height.computed = MAX(h_orig + miny, 0);
389 if (miny != 0 && fabs(minx/miny) > 0.5 * ratio && (SGN(minx) == SGN(miny))) {
390 // closer to the diagonal and in same-sign quarters, change both using ratio
391 rect->width.computed = MAX(w_orig + miny * ratio, 0);
392 } else {
393 // closer to the vertical, change only height, width is w_orig
394 rect->width.computed = MAX(w_orig, 0);
395 }
396 }
398 rect->width._set = rect->height._set = true;
400 } else {
401 // move freely
402 rect->width.computed = MAX(s[NR::X] - rect->x.computed, 0);
403 rect->height.computed = MAX(s[NR::Y] - rect->y.computed, 0);
404 rect->width._set = rect->height._set = true;
405 }
407 sp_rect_clamp_radii(rect);
409 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
410 }
412 static void sp_rect_wh_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
413 {
414 SPRect *rect = SP_RECT(item);
416 sp_rect_wh_set_internal(rect, p, origin, state);
417 }
419 static NR::Point sp_rect_xy_get(SPItem *item)
420 {
421 SPRect *rect = SP_RECT(item);
423 return NR::Point(rect->x.computed, rect->y.computed);
424 }
426 static void sp_rect_xy_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
427 {
428 SPRect *rect = SP_RECT(item);
430 // opposite corner (unmoved)
431 gdouble opposite_x = (rect->x.computed + rect->width.computed);
432 gdouble opposite_y = (rect->y.computed + rect->height.computed);
434 // original width/height when drag started
435 gdouble w_orig = opposite_x - origin[NR::X];
436 gdouble h_orig = opposite_y - origin[NR::Y];
438 NR::Point const s = snap_knot_position(rect, p);
440 // mouse displacement since drag started
441 gdouble minx = s[NR::X] - origin[NR::X];
442 gdouble miny = s[NR::Y] - origin[NR::Y];
444 if (state & GDK_CONTROL_MASK) {
445 //original ratio
446 gdouble ratio = (w_orig / h_orig);
448 if (fabs(minx) > fabs(miny)) {
450 // snap to horizontal or diagonal
451 rect->x.computed = MIN(s[NR::X], opposite_x);
452 rect->width.computed = MAX(w_orig - minx, 0);
453 if (minx != 0 && fabs(miny/minx) > 0.5 * 1/ratio && (SGN(minx) == SGN(miny))) {
454 // closer to the diagonal and in same-sign quarters, change both using ratio
455 rect->y.computed = MIN(origin[NR::Y] + minx / ratio, opposite_y);
456 rect->height.computed = MAX(h_orig - minx / ratio, 0);
457 } else {
458 // closer to the horizontal, change only width, height is h_orig
459 rect->y.computed = MIN(origin[NR::Y], opposite_y);
460 rect->height.computed = MAX(h_orig, 0);
461 }
463 } else {
465 // snap to vertical or diagonal
466 rect->y.computed = MIN(s[NR::Y], opposite_y);
467 rect->height.computed = MAX(h_orig - miny, 0);
468 if (miny != 0 && fabs(minx/miny) > 0.5 *ratio && (SGN(minx) == SGN(miny))) {
469 // closer to the diagonal and in same-sign quarters, change both using ratio
470 rect->x.computed = MIN(origin[NR::X] + miny * ratio, opposite_x);
471 rect->width.computed = MAX(w_orig - miny * ratio, 0);
472 } else {
473 // closer to the vertical, change only height, width is w_orig
474 rect->x.computed = MIN(origin[NR::X], opposite_x);
475 rect->width.computed = MAX(w_orig, 0);
476 }
478 }
480 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
482 } else {
483 // move freely
484 rect->x.computed = MIN(s[NR::X], opposite_x);
485 rect->width.computed = MAX(w_orig - minx, 0);
486 rect->y.computed = MIN(s[NR::Y], opposite_y);
487 rect->height.computed = MAX(h_orig - miny, 0);
488 rect->width._set = rect->height._set = rect->x._set = rect->y._set = true;
489 }
491 sp_rect_clamp_radii(rect);
493 ((SPObject *)rect)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
494 }
496 static SPKnotHolder *sp_rect_knot_holder(SPItem *item, SPDesktop *desktop)
497 {
498 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
500 sp_knot_holder_add_full(
501 knot_holder, sp_rect_rx_set, sp_rect_rx_get, sp_rect_rx_knot_click,
502 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
503 _("Adjust the <b>horizontal rounding</b> radius; with <b>Ctrl</b> to make the vertical "
504 "radius the same"));
506 sp_knot_holder_add_full(
507 knot_holder, sp_rect_ry_set, sp_rect_ry_get, sp_rect_ry_knot_click,
508 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
509 _("Adjust the <b>vertical rounding</b> radius; with <b>Ctrl</b> to make the horizontal "
510 "radius the same")
511 );
513 sp_knot_holder_add_full(
514 knot_holder, sp_rect_wh_set, sp_rect_wh_get, NULL,
515 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
516 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
517 "or stretch in one dimension only")
518 );
520 sp_knot_holder_add_full(
521 knot_holder, sp_rect_xy_set, sp_rect_xy_get, NULL,
522 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
523 _("Adjust the <b>width and height</b> of the rectangle; with <b>Ctrl</b> to lock ratio "
524 "or stretch in one dimension only")
525 );
527 sp_pat_knot_holder(item, knot_holder);
528 return knot_holder;
529 }
531 /* 3D Box */
533 static inline Box3D::Axis movement_axis_of_3dbox_corner (guint corner, guint state)
534 {
535 // this function has the purpose to simplify a change in the resizing behaviour of boxes
536 switch (corner) {
537 case 0:
538 case 1:
539 case 2:
540 case 3:
541 return ((state & GDK_SHIFT_MASK) ? Box3D::Z : Box3D::XY);
542 case 4:
543 case 5:
544 case 6:
545 case 7:
546 return ((state & GDK_SHIFT_MASK) ? Box3D::XY : Box3D::Z);
547 }
548 return Box3D::NONE;
549 }
551 /*
552 * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
553 * their intersection, we remember the last snapped line and keep snapping to this specific line as long
554 * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
555 */
557 // Should we make the threshold settable in the preferences?
558 static double remember_snap_threshold = 20;
559 static guint remember_snap_index = 0;
561 static NR::Point snap_knot_position_3dbox (SP3DBox *box, guint corner, Box3D::Axis direction, NR::Point const &origin, NR::Point const &p, guint state)
562 {
563 SPDesktop * desktop = inkscape_active_desktop();
564 Box3D::Perspective3D *persp = sp_desktop_document (desktop)->get_persp_of_box (box);
566 g_return_val_if_fail (!is_single_axis_direction (direction), p);
568 Box3D::Axis axis1 = Box3D::extract_first_axis_direction (direction);
569 Box3D::Axis axis2 = Box3D::extract_second_axis_direction (direction);
571 NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (box)));
572 NR::Point origin_dt = origin * i2d;
573 NR::Point p_dt = p * i2d;
575 Box3D::PerspectiveLine pl1 (origin_dt, axis1, persp);
576 Box3D::PerspectiveLine pl2 (origin_dt, axis2, persp);
577 Box3D::Line diag1 (origin_dt, box->corners[corner ^ Box3D::XY]);
579 int num_snap_lines = 3;
580 NR::Point snap_pts[num_snap_lines];
582 snap_pts[0] = pl1.closest_to (p_dt);
583 snap_pts[1] = pl2.closest_to (p_dt);
584 snap_pts[2] = diag1.closest_to (p_dt);
586 gdouble const zoom = desktop->current_zoom();
588 double snap_dists[num_snap_lines];
590 for (int i = 0; i < num_snap_lines; ++i) {
591 snap_dists[i] = NR::L2 (snap_pts[i] - p_dt) * zoom;
592 }
594 bool within_tolerance = true;
595 for (int i = 0; i < num_snap_lines; ++i) {
596 if (snap_dists[i] > remember_snap_threshold) {
597 within_tolerance = false;
598 break;
599 }
600 }
602 int snap_index = -1;
603 double snap_dist = NR_HUGE;
604 for (int i = 0; i < num_snap_lines; ++i) {
605 if (snap_dists[i] < snap_dist) {
606 snap_index = i;
607 snap_dist = snap_dists[i];
608 }
609 }
611 if (within_tolerance) {
612 return snap_pts[remember_snap_index] * i2d.inverse();
613 } else {
614 remember_snap_index = snap_index;
615 return snap_pts[snap_index] * i2d.inverse();
616 }
617 }
619 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
620 {
621 g_assert(item != NULL);
622 SP3DBox *box = SP_3DBOX(item);
624 NR::Matrix const i2d (sp_item_i2d_affine (item));
625 return sp_3dbox_get_corner(box, knot_id) * i2d;
626 }
628 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, NR::Point const &new_pos, NR::Point const &origin, guint state)
629 {
630 g_assert(item != NULL);
631 SP3DBox *box = SP_3DBOX(item);
633 NR::Matrix const i2d (sp_item_i2d_affine (item));
634 Box3D::Axis direction = movement_axis_of_3dbox_corner (knot_id, state);
635 if ((state & GDK_CONTROL_MASK) && !is_single_axis_direction (direction)) {
636 // snap if Ctrl is pressed and movement isn't already constrained to a single axis
637 NR::Point const s = snap_knot_position_3dbox (box, knot_id, direction, origin, new_pos, state);
638 sp_3dbox_move_corner_in_Z_direction (box, knot_id, s * i2d, false);
639 } else {
640 if (direction == Box3D::Z) {
641 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, true);
642 } else {
643 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, false);
644 }
645 }
646 sp_3dbox_update_curves (box);
647 sp_3dbox_set_ratios (box);
648 sp_3dbox_update_perspective_lines ();
649 sp_3dbox_set_z_orders_later_on (box);
650 }
652 static void sp_3dbox_knot_center_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
653 {
654 SP3DBox *box = SP_3DBOX(item);
656 NR::Matrix const i2d (sp_item_i2d_affine (item));
657 if (state & GDK_SHIFT_MASK) {
658 sp_3dbox_recompute_Z_corners_from_new_center (box, new_pos * i2d);
659 } else {
660 sp_3dbox_recompute_XY_corners_from_new_center (box, new_pos * i2d);
661 }
663 sp_3dbox_update_curves (box);
664 sp_3dbox_set_z_orders_later_on (box);
665 }
667 static NR::Point sp_3dbox_knot_center_get(SPItem *item)
668 {
669 NR::Maybe<NR::Point> center = sp_3dbox_get_center(SP_3DBOX(item));
670 if (!center) return NR::Point (0, 0);
671 NR::Matrix const i2d (sp_item_i2d_affine (item));
672 return (*center) * i2d;
673 }
675 static void sp_3dbox_knot0_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
676 {
677 sp_3dbox_knot_set(item, 0, new_pos, origin, state);
678 }
680 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
681 {
682 sp_3dbox_knot_set(item, 1, new_pos, origin, state);
683 }
685 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
686 {
687 sp_3dbox_knot_set(item, 2, new_pos, origin, state);
688 }
690 static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
691 {
692 sp_3dbox_knot_set(item, 3, new_pos, origin, state);
693 }
695 static void sp_3dbox_knot4_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
696 {
697 sp_3dbox_knot_set(item, 4, new_pos, origin, state);
698 }
700 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
701 {
702 sp_3dbox_knot_set(item, 5, new_pos, origin, state);
703 }
705 static void sp_3dbox_knot6_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
706 {
707 sp_3dbox_knot_set(item, 6, new_pos, origin, state);
708 }
710 static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
711 {
712 sp_3dbox_knot_set(item, 7, new_pos, origin, state);
713 }
715 static NR::Point sp_3dbox_knot0_get(SPItem *item)
716 {
717 return sp_3dbox_knot_get(item, 0);
718 }
720 static NR::Point sp_3dbox_knot1_get(SPItem *item)
721 {
722 return sp_3dbox_knot_get(item, 1);
723 }
725 static NR::Point sp_3dbox_knot2_get(SPItem *item)
726 {
727 return sp_3dbox_knot_get(item, 2);
728 }
730 static NR::Point sp_3dbox_knot3_get(SPItem *item)
731 {
732 return sp_3dbox_knot_get(item, 3);
733 }
735 static NR::Point sp_3dbox_knot4_get(SPItem *item)
736 {
737 return sp_3dbox_knot_get(item, 4);
738 }
740 static NR::Point sp_3dbox_knot5_get(SPItem *item)
741 {
742 return sp_3dbox_knot_get(item, 5);
743 }
745 static NR::Point sp_3dbox_knot6_get(SPItem *item)
746 {
747 return sp_3dbox_knot_get(item, 6);
748 }
750 static NR::Point sp_3dbox_knot7_get(SPItem *item)
751 {
752 return sp_3dbox_knot_get(item, 7);
753 }
756 //static
757 SPKnotHolder *
758 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop)
759 {
760 g_assert(item != NULL);
761 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
763 sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set, sp_3dbox_knot0_get, NULL,
764 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
765 sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,
766 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
767 sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,
768 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
769 sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
770 _("Resize box in X/Y direction; with <b>Shift</b> along the Z axis; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
771 sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set, sp_3dbox_knot4_get, NULL,
772 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
773 sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,
774 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
775 sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set, sp_3dbox_knot6_get, NULL,
776 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
777 sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,
778 _("Resize box along the Z axis; with <b>Shift</b> in X/Y direction; with <b>Ctrl</b> to constrain to the directions of edges or diagonals"));
780 // center dragging
781 sp_knot_holder_add_full(knot_holder, sp_3dbox_knot_center_set, sp_3dbox_knot_center_get, NULL,
782 SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,_("Move the box in perspective."));
784 sp_pat_knot_holder(item, knot_holder);
786 return knot_holder;
787 }
789 /* SPArc */
791 /*
792 * return values:
793 * 1 : inside
794 * 0 : on the curves
795 * -1 : outside
796 */
797 static gint
798 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
799 {
800 gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
801 gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
803 gdouble s = dx * dx + dy * dy;
804 if (s < 1.0) return 1;
805 if (s > 1.0) return -1;
806 return 0;
807 }
809 static void
810 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
811 {
812 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
814 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
815 SPArc *arc = SP_ARC(item);
817 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
819 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
820 NR::scale sc(ge->rx.computed, ge->ry.computed);
821 ge->start = atan2(delta * sc.inverse());
822 if ( ( state & GDK_CONTROL_MASK )
823 && snaps )
824 {
825 ge->start = sp_round(ge->start, M_PI/snaps);
826 }
827 sp_genericellipse_normalize(ge);
828 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
829 }
831 static NR::Point sp_arc_start_get(SPItem *item)
832 {
833 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
834 SPArc *arc = SP_ARC(item);
836 return sp_arc_get_xy(arc, ge->start);
837 }
839 static void
840 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
841 {
842 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
844 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
845 SPArc *arc = SP_ARC(item);
847 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
849 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
850 NR::scale sc(ge->rx.computed, ge->ry.computed);
851 ge->end = atan2(delta * sc.inverse());
852 if ( ( state & GDK_CONTROL_MASK )
853 && snaps )
854 {
855 ge->end = sp_round(ge->end, M_PI/snaps);
856 }
857 sp_genericellipse_normalize(ge);
858 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
859 }
861 static NR::Point sp_arc_end_get(SPItem *item)
862 {
863 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
864 SPArc *arc = SP_ARC(item);
866 return sp_arc_get_xy(arc, ge->end);
867 }
869 static void
870 sp_arc_startend_click(SPItem *item, guint state)
871 {
872 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
874 if (state & GDK_SHIFT_MASK) {
875 ge->end = ge->start = 0;
876 ((SPObject *)ge)->updateRepr();
877 }
878 }
881 static void
882 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
883 {
884 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
885 SPArc *arc = SP_ARC(item);
887 NR::Point const s = snap_knot_position(arc, p);
889 ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
891 if ( state & GDK_CONTROL_MASK ) {
892 ge->ry.computed = ge->rx.computed;
893 }
895 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
896 }
898 static NR::Point sp_arc_rx_get(SPItem *item)
899 {
900 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
902 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(ge->rx.computed, 0));
903 }
905 static void
906 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
907 {
908 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
909 SPArc *arc = SP_ARC(item);
911 NR::Point const s = snap_knot_position(arc, p);
913 ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
915 if ( state & GDK_CONTROL_MASK ) {
916 ge->rx.computed = ge->ry.computed;
917 }
919 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
920 }
922 static NR::Point sp_arc_ry_get(SPItem *item)
923 {
924 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
926 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(0, ge->ry.computed));
927 }
929 static void
930 sp_arc_rx_click(SPItem *item, guint state)
931 {
932 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
934 if (state & GDK_CONTROL_MASK) {
935 ge->ry.computed = ge->rx.computed;
936 ((SPObject *)ge)->updateRepr();
937 }
938 }
940 static void
941 sp_arc_ry_click(SPItem *item, guint state)
942 {
943 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
945 if (state & GDK_CONTROL_MASK) {
946 ge->rx.computed = ge->ry.computed;
947 ((SPObject *)ge)->updateRepr();
948 }
949 }
951 static SPKnotHolder *
952 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
953 {
954 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
956 sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
957 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
958 _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
959 sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
960 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
961 _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
962 sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
963 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
964 _("Position the <b>start point</b> of the arc or segment; with <b>Ctrl</b> to snap angle; drag <b>inside</b> the ellipse for arc, <b>outside</b> for segment"));
965 sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
966 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
967 _("Position the <b>end point</b> of the arc or segment; with <b>Ctrl</b> to snap angle; drag <b>inside</b> the ellipse for arc, <b>outside</b> for segment"));
969 sp_pat_knot_holder(item, knot_holder);
971 return knot_holder;
972 }
974 /* SPStar */
976 static void
977 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
978 {
979 SPStar *star = SP_STAR(item);
981 NR::Point const s = snap_knot_position(star, p);
983 NR::Point d = s - star->center;
985 double arg1 = atan2(d);
986 double darg1 = arg1 - star->arg[0];
988 if (state & GDK_MOD1_MASK) {
989 star->randomized = darg1/(star->arg[0] - star->arg[1]);
990 } else if (state & GDK_SHIFT_MASK) {
991 star->rounded = darg1/(star->arg[0] - star->arg[1]);
992 } else if (state & GDK_CONTROL_MASK) {
993 star->r[0] = L2(d);
994 } else {
995 star->r[0] = L2(d);
996 star->arg[0] = arg1;
997 star->arg[1] += darg1;
998 }
999 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1000 }
1002 static void
1003 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1004 {
1005 SPStar *star = SP_STAR(item);
1007 NR::Point const s = snap_knot_position(star, p);
1009 if (star->flatsided == false) {
1010 NR::Point d = s - star->center;
1012 double arg1 = atan2(d);
1013 double darg1 = arg1 - star->arg[1];
1015 if (state & GDK_MOD1_MASK) {
1016 star->randomized = darg1/(star->arg[0] - star->arg[1]);
1017 } else if (state & GDK_SHIFT_MASK) {
1018 star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
1019 } else if (state & GDK_CONTROL_MASK) {
1020 star->r[1] = L2(d);
1021 star->arg[1] = star->arg[0] + M_PI / star->sides;
1022 }
1023 else {
1024 star->r[1] = L2(d);
1025 star->arg[1] = atan2(d);
1026 }
1027 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1028 }
1029 }
1031 static NR::Point sp_star_knot1_get(SPItem *item)
1032 {
1033 g_assert(item != NULL);
1035 SPStar *star = SP_STAR(item);
1037 return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
1039 }
1041 static NR::Point sp_star_knot2_get(SPItem *item)
1042 {
1043 g_assert(item != NULL);
1045 SPStar *star = SP_STAR(item);
1047 return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
1048 }
1050 static void
1051 sp_star_knot_click(SPItem *item, guint state)
1052 {
1053 SPStar *star = SP_STAR(item);
1055 if (state & GDK_MOD1_MASK) {
1056 star->randomized = 0;
1057 ((SPObject *)star)->updateRepr();
1058 } else if (state & GDK_SHIFT_MASK) {
1059 star->rounded = 0;
1060 ((SPObject *)star)->updateRepr();
1061 } else if (state & GDK_CONTROL_MASK) {
1062 star->arg[1] = star->arg[0] + M_PI / star->sides;
1063 ((SPObject *)star)->updateRepr();
1064 }
1065 }
1067 static SPKnotHolder *
1068 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
1069 {
1070 /* we don't need to get parent knot_holder */
1071 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1072 g_assert(item != NULL);
1074 SPStar *star = SP_STAR(item);
1076 sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
1077 _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1078 if (star->flatsided == false)
1079 sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
1080 _("Adjust the <b>base radius</b> of the star; with <b>Ctrl</b> to keep star rays radial (no skew); with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1082 sp_pat_knot_holder(item, knot_holder);
1084 return knot_holder;
1085 }
1087 /* SPSpiral */
1089 /*
1090 * set attributes via inner (t=t0) knot point:
1091 * [default] increase/decrease inner point
1092 * [shift] increase/decrease inner and outer arg synchronizely
1093 * [control] constrain inner arg to round per PI/4
1094 */
1095 static void
1096 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1097 {
1098 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1100 SPSpiral *spiral = SP_SPIRAL(item);
1102 gdouble dx = p[NR::X] - spiral->cx;
1103 gdouble dy = p[NR::Y] - spiral->cy;
1105 if (state & GDK_MOD1_MASK) {
1106 // adjust divergence by vertical drag, relative to rad
1107 double new_exp = (spiral->rad + dy)/(spiral->rad);
1108 spiral->exp = new_exp > 0? new_exp : 0;
1109 } else {
1110 // roll/unroll from inside
1111 gdouble arg_t0;
1112 sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
1114 gdouble arg_tmp = atan2(dy, dx) - arg_t0;
1115 gdouble arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
1116 spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
1118 /* round inner arg per PI/snaps, if CTRL is pressed */
1119 if ( ( state & GDK_CONTROL_MASK )
1120 && ( fabs(spiral->revo) > SP_EPSILON_2 )
1121 && ( snaps != 0 ) ) {
1122 gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
1123 spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
1124 }
1126 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1127 }
1129 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1130 }
1132 /*
1133 * set attributes via outer (t=1) knot point:
1134 * [default] increase/decrease revolution factor
1135 * [control] constrain inner arg to round per PI/4
1136 */
1137 static void
1138 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1139 {
1140 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1142 SPSpiral *spiral = SP_SPIRAL(item);
1144 gdouble dx = p[NR::X] - spiral->cx;
1145 gdouble dy = p[NR::Y] - spiral->cy;
1147 if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
1148 spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
1149 if (!(state & GDK_MOD1_MASK)) {
1150 // if alt not pressed, change also rad; otherwise it is locked
1151 spiral->rad = MAX(hypot(dx, dy), 0.001);
1152 }
1153 if ( ( state & GDK_CONTROL_MASK )
1154 && snaps ) {
1155 spiral->arg = sp_round(spiral->arg, M_PI/snaps);
1156 }
1157 } else { // roll/unroll
1158 // arg of the spiral outer end
1159 double arg_1;
1160 sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
1162 // its fractional part after the whole turns are subtracted
1163 double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
1165 // arg of the mouse point relative to spiral center
1166 double mouse_angle = atan2(dy, dx);
1167 if (mouse_angle < 0)
1168 mouse_angle += 2*M_PI;
1170 // snap if ctrl
1171 if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
1172 mouse_angle = sp_round(mouse_angle, M_PI/snaps);
1173 }
1175 // by how much we want to rotate the outer point
1176 double diff = mouse_angle - arg_r;
1177 if (diff > M_PI)
1178 diff -= 2*M_PI;
1179 else if (diff < -M_PI)
1180 diff += 2*M_PI;
1182 // calculate the new rad;
1183 // the value of t corresponding to the angle arg_1 + diff:
1184 double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1185 // the rad at that t:
1186 double rad_new = 0;
1187 if (t_temp > spiral->t0)
1188 sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1190 // change the revo (converting diff from radians to the number of turns)
1191 spiral->revo += diff/(2*M_PI);
1192 if (spiral->revo < 1e-3)
1193 spiral->revo = 1e-3;
1195 // if alt not pressed and the values are sane, change the rad
1196 if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1197 // adjust t0 too so that the inner point stays unmoved
1198 double r0;
1199 sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1200 spiral->rad = rad_new;
1201 spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1202 }
1203 if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1204 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1205 }
1207 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1208 }
1210 static NR::Point sp_spiral_inner_get(SPItem *item)
1211 {
1212 SPSpiral *spiral = SP_SPIRAL(item);
1214 return sp_spiral_get_xy(spiral, spiral->t0);
1215 }
1217 static NR::Point sp_spiral_outer_get(SPItem *item)
1218 {
1219 SPSpiral *spiral = SP_SPIRAL(item);
1221 return sp_spiral_get_xy(spiral, 1.0);
1222 }
1224 static void
1225 sp_spiral_inner_click(SPItem *item, guint state)
1226 {
1227 SPSpiral *spiral = SP_SPIRAL(item);
1229 if (state & GDK_MOD1_MASK) {
1230 spiral->exp = 1;
1231 ((SPObject *)spiral)->updateRepr();
1232 } else if (state & GDK_SHIFT_MASK) {
1233 spiral->t0 = 0;
1234 ((SPObject *)spiral)->updateRepr();
1235 }
1236 }
1238 static SPKnotHolder *
1239 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1240 {
1241 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1243 sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1244 _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1245 sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1246 _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1248 sp_pat_knot_holder(item, knot_holder);
1250 return knot_holder;
1251 }
1253 /* SPOffset */
1255 static void
1256 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1257 {
1258 SPOffset *offset = SP_OFFSET(item);
1260 offset->rad = sp_offset_distance_to_original(offset, p);
1261 offset->knot = p;
1262 offset->knotSet = true;
1264 ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1265 }
1268 static NR::Point sp_offset_offset_get(SPItem *item)
1269 {
1270 SPOffset *offset = SP_OFFSET(item);
1272 NR::Point np;
1273 sp_offset_top_point(offset,&np);
1274 return np;
1275 }
1277 static SPKnotHolder *
1278 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1279 {
1280 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1282 sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1283 _("Adjust the <b>offset distance</b>"));
1285 sp_pat_knot_holder(item, knot_holder);
1287 return knot_holder;
1288 }
1290 static SPKnotHolder *
1291 sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1292 {
1293 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1294 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1295 {
1296 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1298 sp_pat_knot_holder(item, knot_holder);
1300 return knot_holder;
1301 }
1302 return NULL;
1303 }
1305 static void
1306 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1307 {
1308 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1309 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1310 {
1311 sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1312 // TRANSLATORS: This refers to the pattern that's inside the object
1313 _("<b>Move</b> the pattern fill inside the object"));
1314 sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1315 _("<b>Scale</b> the pattern fill uniformly"));
1316 sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1317 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1318 }
1319 }
1321 static NR::Point sp_flowtext_corner_get(SPItem *item)
1322 {
1323 SPRect *rect = SP_RECT(item);
1325 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1326 }
1328 static void
1329 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1330 {
1331 SPRect *rect = SP_RECT(item);
1333 sp_rect_wh_set_internal(rect, p, origin, state);
1334 }
1336 static SPKnotHolder *
1337 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1338 {
1339 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1341 sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1342 _("Drag to resize the <b>flowed text frame</b>"));
1344 return knot_holder;
1345 }
1348 /*
1349 Local Variables:
1350 mode:c++
1351 c-file-style:"stroustrup"
1352 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1353 indent-tabs-mode:nil
1354 fill-column:99
1355 End:
1356 */
1357 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :