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 }
550 /*
551 * To keep the snappoint from jumping randomly between the two lines when the mouse pointer is close to
552 * their intersection, we remember the last snapped line and keep snapping to this specific line as long
553 * as the distance from the intersection to the mouse pointer is less than remember_snap_threshold.
554 */
556 // Should we make the threshold settable in the preferences?
557 static double remember_snap_threshold = 20;
558 static guint remember_snap_index = 0;
560 static NR::Point snap_knot_position_3dbox (SP3DBox *box, guint corner, Box3D::Axis direction, NR::Point const &origin, NR::Point const &p, guint state)
561 {
562 SPDesktop * desktop = inkscape_active_desktop();
563 Box3D::Perspective3D *persp = sp_desktop_document (desktop)->get_persp_of_box (box);
565 g_return_val_if_fail (!is_single_axis_direction (direction), p);
567 Box3D::Axis axis1 = Box3D::extract_first_axis_direction (direction);
568 Box3D::Axis axis2 = Box3D::extract_second_axis_direction (direction);
570 NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM (box)));
571 NR::Point origin_dt = origin * i2d;
572 NR::Point p_dt = p * i2d;
574 Box3D::PerspectiveLine pl1 (origin_dt, axis1, persp);
575 Box3D::PerspectiveLine pl2 (origin_dt, axis2, persp);
576 Box3D::Line diag1 (origin_dt, box->corners[corner ^ Box3D::XY]);
578 int num_snap_lines = 3;
579 NR::Point snap_pts[num_snap_lines];
581 snap_pts[0] = pl1.closest_to (p_dt);
582 snap_pts[1] = pl2.closest_to (p_dt);
583 snap_pts[2] = diag1.closest_to (p_dt);
585 gdouble const zoom = desktop->current_zoom();
587 double snap_dists[num_snap_lines];
589 for (int i = 0; i < num_snap_lines; ++i) {
590 snap_dists[i] = NR::L2 (snap_pts[i] - p_dt) * zoom;
591 }
593 bool within_tolerance = true;
594 for (int i = 0; i < num_snap_lines; ++i) {
595 if (snap_dists[i] > remember_snap_threshold) {
596 within_tolerance = false;
597 break;
598 }
599 }
601 int snap_index = -1;
602 double snap_dist = NR_HUGE;
603 for (int i = 0; i < num_snap_lines; ++i) {
604 if (snap_dists[i] < snap_dist) {
605 snap_index = i;
606 snap_dist = snap_dists[i];
607 }
608 }
610 if (within_tolerance) {
611 return snap_pts[remember_snap_index] * i2d.inverse();
612 } else {
613 remember_snap_index = snap_index;
614 return snap_pts[snap_index] * i2d.inverse();
615 }
616 }
618 static NR::Point sp_3dbox_knot_get(SPItem *item, guint knot_id)
619 {
620 g_assert(item != NULL);
621 SP3DBox *box = SP_3DBOX(item);
623 NR::Matrix const i2d (sp_item_i2d_affine (item));
624 return sp_3dbox_get_corner(box, knot_id) * i2d;
625 }
627 static void sp_3dbox_knot_set(SPItem *item, guint knot_id, NR::Point const &new_pos, NR::Point const &origin, guint state)
628 {
629 g_assert(item != NULL);
630 SP3DBox *box = SP_3DBOX(item);
632 NR::Matrix const i2d (sp_item_i2d_affine (item));
633 Box3D::Axis direction = movement_axis_of_3dbox_corner (knot_id, state);
634 if ((state & GDK_CONTROL_MASK) && !is_single_axis_direction (direction)) {
635 // snap if Ctrl is pressed and movement isn't already constrained to a single axis
636 NR::Point const s = snap_knot_position_3dbox (box, knot_id, direction, origin, new_pos, state);
637 sp_3dbox_move_corner_in_Z_direction (box, knot_id, s * i2d, false);
638 } else {
639 if (direction == Box3D::Z) {
640 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, true);
641 } else {
642 sp_3dbox_move_corner_in_Z_direction (box, knot_id, new_pos * i2d, false);
643 }
644 }
645 sp_3dbox_update_curves (box);
646 sp_3dbox_set_ratios (box);
647 sp_3dbox_update_perspective_lines ();
648 sp_3dbox_set_z_orders (box);
649 }
651 static void sp_3dbox_knot_center_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
652 {
653 NR::Matrix const i2d (sp_item_i2d_affine (item));
654 sp_3dbox_recompute_XY_corners_from_new_center (SP_3DBOX (item), new_pos * i2d);
656 sp_3dbox_update_curves (SP_3DBOX(item));
657 }
659 static NR::Point sp_3dbox_knot_center_get(SPItem *item)
660 {
661 NR::Maybe<NR::Point> center = sp_3dbox_get_center(SP_3DBOX(item));
662 if (!center) return NR::Point (0, 0);
663 NR::Matrix const i2d (sp_item_i2d_affine (item));
664 return (*center) * i2d;
665 }
667 static void sp_3dbox_knot0_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
668 {
669 sp_3dbox_knot_set(item, 0, new_pos, origin, state);
670 }
672 static void sp_3dbox_knot1_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
673 {
674 sp_3dbox_knot_set(item, 1, new_pos, origin, state);
675 }
677 static void sp_3dbox_knot2_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
678 {
679 sp_3dbox_knot_set(item, 2, new_pos, origin, state);
680 }
682 static void sp_3dbox_knot3_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
683 {
684 sp_3dbox_knot_set(item, 3, new_pos, origin, state);
685 }
687 static void sp_3dbox_knot4_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
688 {
689 sp_3dbox_knot_set(item, 4, new_pos, origin, state);
690 }
692 static void sp_3dbox_knot5_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
693 {
694 sp_3dbox_knot_set(item, 5, new_pos, origin, state);
695 }
697 static void sp_3dbox_knot6_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
698 {
699 sp_3dbox_knot_set(item, 6, new_pos, origin, state);
700 }
702 static void sp_3dbox_knot7_set(SPItem *item, NR::Point const &new_pos, NR::Point const &origin, guint state)
703 {
704 sp_3dbox_knot_set(item, 7, new_pos, origin, state);
705 }
707 static NR::Point sp_3dbox_knot0_get(SPItem *item)
708 {
709 return sp_3dbox_knot_get(item, 0);
710 }
712 static NR::Point sp_3dbox_knot1_get(SPItem *item)
713 {
714 return sp_3dbox_knot_get(item, 1);
715 }
717 static NR::Point sp_3dbox_knot2_get(SPItem *item)
718 {
719 return sp_3dbox_knot_get(item, 2);
720 }
722 static NR::Point sp_3dbox_knot3_get(SPItem *item)
723 {
724 return sp_3dbox_knot_get(item, 3);
725 }
727 static NR::Point sp_3dbox_knot4_get(SPItem *item)
728 {
729 return sp_3dbox_knot_get(item, 4);
730 }
732 static NR::Point sp_3dbox_knot5_get(SPItem *item)
733 {
734 return sp_3dbox_knot_get(item, 5);
735 }
737 static NR::Point sp_3dbox_knot6_get(SPItem *item)
738 {
739 return sp_3dbox_knot_get(item, 6);
740 }
742 static NR::Point sp_3dbox_knot7_get(SPItem *item)
743 {
744 return sp_3dbox_knot_get(item, 7);
745 }
748 //static
749 SPKnotHolder *
750 sp_3dbox_knot_holder(SPItem *item, SPDesktop *desktop)
751 {
752 g_assert(item != NULL);
753 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
755 sp_knot_holder_add(knot_holder, sp_3dbox_knot0_set, sp_3dbox_knot0_get, NULL,
756 _("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"));
757 sp_knot_holder_add(knot_holder, sp_3dbox_knot1_set, sp_3dbox_knot1_get, NULL,
758 _("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"));
759 sp_knot_holder_add(knot_holder, sp_3dbox_knot2_set, sp_3dbox_knot2_get, NULL,
760 _("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"));
761 sp_knot_holder_add(knot_holder, sp_3dbox_knot3_set, sp_3dbox_knot3_get, NULL,
762 _("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"));
763 sp_knot_holder_add(knot_holder, sp_3dbox_knot4_set, sp_3dbox_knot4_get, NULL,
764 _("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"));
765 sp_knot_holder_add(knot_holder, sp_3dbox_knot5_set, sp_3dbox_knot5_get, NULL,
766 _("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"));
767 sp_knot_holder_add(knot_holder, sp_3dbox_knot6_set, sp_3dbox_knot6_get, NULL,
768 _("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"));
769 sp_knot_holder_add(knot_holder, sp_3dbox_knot7_set, sp_3dbox_knot7_get, NULL,
770 _("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"));
772 // center dragging
773 sp_knot_holder_add_full(knot_holder, sp_3dbox_knot_center_set, sp_3dbox_knot_center_get, NULL,
774 SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,_("Move the box in perspective."));
776 sp_pat_knot_holder(item, knot_holder);
778 return knot_holder;
779 }
781 /* SPArc */
783 /*
784 * return values:
785 * 1 : inside
786 * 0 : on the curves
787 * -1 : outside
788 */
789 static gint
790 sp_genericellipse_side(SPGenericEllipse *ellipse, NR::Point const &p)
791 {
792 gdouble dx = (p[NR::X] - ellipse->cx.computed) / ellipse->rx.computed;
793 gdouble dy = (p[NR::Y] - ellipse->cy.computed) / ellipse->ry.computed;
795 gdouble s = dx * dx + dy * dy;
796 if (s < 1.0) return 1;
797 if (s > 1.0) return -1;
798 return 0;
799 }
801 static void
802 sp_arc_start_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
803 {
804 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
806 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
807 SPArc *arc = SP_ARC(item);
809 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
811 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
812 NR::scale sc(ge->rx.computed, ge->ry.computed);
813 ge->start = atan2(delta * sc.inverse());
814 if ( ( state & GDK_CONTROL_MASK )
815 && snaps )
816 {
817 ge->start = sp_round(ge->start, M_PI/snaps);
818 }
819 sp_genericellipse_normalize(ge);
820 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
821 }
823 static NR::Point sp_arc_start_get(SPItem *item)
824 {
825 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
826 SPArc *arc = SP_ARC(item);
828 return sp_arc_get_xy(arc, ge->start);
829 }
831 static void
832 sp_arc_end_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
833 {
834 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
836 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
837 SPArc *arc = SP_ARC(item);
839 ge->closed = (sp_genericellipse_side(ge, p) == -1) ? TRUE : FALSE;
841 NR::Point delta = p - NR::Point(ge->cx.computed, ge->cy.computed);
842 NR::scale sc(ge->rx.computed, ge->ry.computed);
843 ge->end = atan2(delta * sc.inverse());
844 if ( ( state & GDK_CONTROL_MASK )
845 && snaps )
846 {
847 ge->end = sp_round(ge->end, M_PI/snaps);
848 }
849 sp_genericellipse_normalize(ge);
850 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
851 }
853 static NR::Point sp_arc_end_get(SPItem *item)
854 {
855 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
856 SPArc *arc = SP_ARC(item);
858 return sp_arc_get_xy(arc, ge->end);
859 }
861 static void
862 sp_arc_startend_click(SPItem *item, guint state)
863 {
864 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
866 if (state & GDK_SHIFT_MASK) {
867 ge->end = ge->start = 0;
868 ((SPObject *)ge)->updateRepr();
869 }
870 }
873 static void
874 sp_arc_rx_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
875 {
876 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
877 SPArc *arc = SP_ARC(item);
879 NR::Point const s = snap_knot_position(arc, p);
881 ge->rx.computed = fabs( ge->cx.computed - s[NR::X] );
883 if ( state & GDK_CONTROL_MASK ) {
884 ge->ry.computed = ge->rx.computed;
885 }
887 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
888 }
890 static NR::Point sp_arc_rx_get(SPItem *item)
891 {
892 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
894 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(ge->rx.computed, 0));
895 }
897 static void
898 sp_arc_ry_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
899 {
900 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
901 SPArc *arc = SP_ARC(item);
903 NR::Point const s = snap_knot_position(arc, p);
905 ge->ry.computed = fabs( ge->cy.computed - s[NR::Y] );
907 if ( state & GDK_CONTROL_MASK ) {
908 ge->rx.computed = ge->ry.computed;
909 }
911 ((SPObject *)arc)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
912 }
914 static NR::Point sp_arc_ry_get(SPItem *item)
915 {
916 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
918 return (NR::Point(ge->cx.computed, ge->cy.computed) - NR::Point(0, ge->ry.computed));
919 }
921 static void
922 sp_arc_rx_click(SPItem *item, guint state)
923 {
924 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
926 if (state & GDK_CONTROL_MASK) {
927 ge->ry.computed = ge->rx.computed;
928 ((SPObject *)ge)->updateRepr();
929 }
930 }
932 static void
933 sp_arc_ry_click(SPItem *item, guint state)
934 {
935 SPGenericEllipse *ge = SP_GENERICELLIPSE(item);
937 if (state & GDK_CONTROL_MASK) {
938 ge->rx.computed = ge->ry.computed;
939 ((SPObject *)ge)->updateRepr();
940 }
941 }
943 static SPKnotHolder *
944 sp_arc_knot_holder(SPItem *item, SPDesktop *desktop)
945 {
946 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
948 sp_knot_holder_add_full(knot_holder, sp_arc_rx_set, sp_arc_rx_get, sp_arc_rx_click,
949 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
950 _("Adjust ellipse <b>width</b>, with <b>Ctrl</b> to make circle"));
951 sp_knot_holder_add_full(knot_holder, sp_arc_ry_set, sp_arc_ry_get, sp_arc_ry_click,
952 SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
953 _("Adjust ellipse <b>height</b>, with <b>Ctrl</b> to make circle"));
954 sp_knot_holder_add_full(knot_holder, sp_arc_start_set, sp_arc_start_get, sp_arc_startend_click,
955 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
956 _("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"));
957 sp_knot_holder_add_full(knot_holder, sp_arc_end_set, sp_arc_end_get, sp_arc_startend_click,
958 SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
959 _("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"));
961 sp_pat_knot_holder(item, knot_holder);
963 return knot_holder;
964 }
966 /* SPStar */
968 static void
969 sp_star_knot1_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
970 {
971 SPStar *star = SP_STAR(item);
973 NR::Point const s = snap_knot_position(star, p);
975 NR::Point d = s - star->center;
977 double arg1 = atan2(d);
978 double darg1 = arg1 - star->arg[0];
980 if (state & GDK_MOD1_MASK) {
981 star->randomized = darg1/(star->arg[0] - star->arg[1]);
982 } else if (state & GDK_SHIFT_MASK) {
983 star->rounded = darg1/(star->arg[0] - star->arg[1]);
984 } else if (state & GDK_CONTROL_MASK) {
985 star->r[0] = L2(d);
986 } else {
987 star->r[0] = L2(d);
988 star->arg[0] = arg1;
989 star->arg[1] += darg1;
990 }
991 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
992 }
994 static void
995 sp_star_knot2_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
996 {
997 SPStar *star = SP_STAR(item);
999 NR::Point const s = snap_knot_position(star, p);
1001 if (star->flatsided == false) {
1002 NR::Point d = s - star->center;
1004 double arg1 = atan2(d);
1005 double darg1 = arg1 - star->arg[1];
1007 if (state & GDK_MOD1_MASK) {
1008 star->randomized = darg1/(star->arg[0] - star->arg[1]);
1009 } else if (state & GDK_SHIFT_MASK) {
1010 star->rounded = fabs(darg1/(star->arg[0] - star->arg[1]));
1011 } else if (state & GDK_CONTROL_MASK) {
1012 star->r[1] = L2(d);
1013 star->arg[1] = star->arg[0] + M_PI / star->sides;
1014 }
1015 else {
1016 star->r[1] = L2(d);
1017 star->arg[1] = atan2(d);
1018 }
1019 ((SPObject *)star)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1020 }
1021 }
1023 static NR::Point sp_star_knot1_get(SPItem *item)
1024 {
1025 g_assert(item != NULL);
1027 SPStar *star = SP_STAR(item);
1029 return sp_star_get_xy(star, SP_STAR_POINT_KNOT1, 0);
1031 }
1033 static NR::Point sp_star_knot2_get(SPItem *item)
1034 {
1035 g_assert(item != NULL);
1037 SPStar *star = SP_STAR(item);
1039 return sp_star_get_xy(star, SP_STAR_POINT_KNOT2, 0);
1040 }
1042 static void
1043 sp_star_knot_click(SPItem *item, guint state)
1044 {
1045 SPStar *star = SP_STAR(item);
1047 if (state & GDK_MOD1_MASK) {
1048 star->randomized = 0;
1049 ((SPObject *)star)->updateRepr();
1050 } else if (state & GDK_SHIFT_MASK) {
1051 star->rounded = 0;
1052 ((SPObject *)star)->updateRepr();
1053 } else if (state & GDK_CONTROL_MASK) {
1054 star->arg[1] = star->arg[0] + M_PI / star->sides;
1055 ((SPObject *)star)->updateRepr();
1056 }
1057 }
1059 static SPKnotHolder *
1060 sp_star_knot_holder(SPItem *item, SPDesktop *desktop)
1061 {
1062 /* we don't need to get parent knot_holder */
1063 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1064 g_assert(item != NULL);
1066 SPStar *star = SP_STAR(item);
1068 sp_knot_holder_add(knot_holder, sp_star_knot1_set, sp_star_knot1_get, sp_star_knot_click,
1069 _("Adjust the <b>tip radius</b> of the star or polygon; with <b>Shift</b> to round; with <b>Alt</b> to randomize"));
1070 if (star->flatsided == false)
1071 sp_knot_holder_add(knot_holder, sp_star_knot2_set, sp_star_knot2_get, sp_star_knot_click,
1072 _("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"));
1074 sp_pat_knot_holder(item, knot_holder);
1076 return knot_holder;
1077 }
1079 /* SPSpiral */
1081 /*
1082 * set attributes via inner (t=t0) knot point:
1083 * [default] increase/decrease inner point
1084 * [shift] increase/decrease inner and outer arg synchronizely
1085 * [control] constrain inner arg to round per PI/4
1086 */
1087 static void
1088 sp_spiral_inner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1089 {
1090 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1092 SPSpiral *spiral = SP_SPIRAL(item);
1094 gdouble dx = p[NR::X] - spiral->cx;
1095 gdouble dy = p[NR::Y] - spiral->cy;
1097 if (state & GDK_MOD1_MASK) {
1098 // adjust divergence by vertical drag, relative to rad
1099 double new_exp = (spiral->rad + dy)/(spiral->rad);
1100 spiral->exp = new_exp > 0? new_exp : 0;
1101 } else {
1102 // roll/unroll from inside
1103 gdouble arg_t0;
1104 sp_spiral_get_polar(spiral, spiral->t0, NULL, &arg_t0);
1106 gdouble arg_tmp = atan2(dy, dx) - arg_t0;
1107 gdouble arg_t0_new = arg_tmp - floor((arg_tmp+M_PI)/(2.0*M_PI))*2.0*M_PI + arg_t0;
1108 spiral->t0 = (arg_t0_new - spiral->arg) / (2.0*M_PI*spiral->revo);
1110 /* round inner arg per PI/snaps, if CTRL is pressed */
1111 if ( ( state & GDK_CONTROL_MASK )
1112 && ( fabs(spiral->revo) > SP_EPSILON_2 )
1113 && ( snaps != 0 ) ) {
1114 gdouble arg = 2.0*M_PI*spiral->revo*spiral->t0 + spiral->arg;
1115 spiral->t0 = (sp_round(arg, M_PI/snaps) - spiral->arg)/(2.0*M_PI*spiral->revo);
1116 }
1118 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1119 }
1121 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1122 }
1124 /*
1125 * set attributes via outer (t=1) knot point:
1126 * [default] increase/decrease revolution factor
1127 * [control] constrain inner arg to round per PI/4
1128 */
1129 static void
1130 sp_spiral_outer_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1131 {
1132 int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
1134 SPSpiral *spiral = SP_SPIRAL(item);
1136 gdouble dx = p[NR::X] - spiral->cx;
1137 gdouble dy = p[NR::Y] - spiral->cy;
1139 if (state & GDK_SHIFT_MASK) { // rotate without roll/unroll
1140 spiral->arg = atan2(dy, dx) - 2.0*M_PI*spiral->revo;
1141 if (!(state & GDK_MOD1_MASK)) {
1142 // if alt not pressed, change also rad; otherwise it is locked
1143 spiral->rad = MAX(hypot(dx, dy), 0.001);
1144 }
1145 if ( ( state & GDK_CONTROL_MASK )
1146 && snaps ) {
1147 spiral->arg = sp_round(spiral->arg, M_PI/snaps);
1148 }
1149 } else { // roll/unroll
1150 // arg of the spiral outer end
1151 double arg_1;
1152 sp_spiral_get_polar(spiral, 1, NULL, &arg_1);
1154 // its fractional part after the whole turns are subtracted
1155 double arg_r = arg_1 - sp_round(arg_1, 2.0*M_PI);
1157 // arg of the mouse point relative to spiral center
1158 double mouse_angle = atan2(dy, dx);
1159 if (mouse_angle < 0)
1160 mouse_angle += 2*M_PI;
1162 // snap if ctrl
1163 if ( ( state & GDK_CONTROL_MASK ) && snaps ) {
1164 mouse_angle = sp_round(mouse_angle, M_PI/snaps);
1165 }
1167 // by how much we want to rotate the outer point
1168 double diff = mouse_angle - arg_r;
1169 if (diff > M_PI)
1170 diff -= 2*M_PI;
1171 else if (diff < -M_PI)
1172 diff += 2*M_PI;
1174 // calculate the new rad;
1175 // the value of t corresponding to the angle arg_1 + diff:
1176 double t_temp = ((arg_1 + diff) - spiral->arg)/(2*M_PI*spiral->revo);
1177 // the rad at that t:
1178 double rad_new = 0;
1179 if (t_temp > spiral->t0)
1180 sp_spiral_get_polar(spiral, t_temp, &rad_new, NULL);
1182 // change the revo (converting diff from radians to the number of turns)
1183 spiral->revo += diff/(2*M_PI);
1184 if (spiral->revo < 1e-3)
1185 spiral->revo = 1e-3;
1187 // if alt not pressed and the values are sane, change the rad
1188 if (!(state & GDK_MOD1_MASK) && rad_new > 1e-3 && rad_new/spiral->rad < 2) {
1189 // adjust t0 too so that the inner point stays unmoved
1190 double r0;
1191 sp_spiral_get_polar(spiral, spiral->t0, &r0, NULL);
1192 spiral->rad = rad_new;
1193 spiral->t0 = pow(r0 / spiral->rad, 1.0/spiral->exp);
1194 }
1195 if (!isFinite(spiral->t0)) spiral->t0 = 0.0;
1196 spiral->t0 = CLAMP(spiral->t0, 0.0, 0.999);
1197 }
1199 ((SPObject *)spiral)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1200 }
1202 static NR::Point sp_spiral_inner_get(SPItem *item)
1203 {
1204 SPSpiral *spiral = SP_SPIRAL(item);
1206 return sp_spiral_get_xy(spiral, spiral->t0);
1207 }
1209 static NR::Point sp_spiral_outer_get(SPItem *item)
1210 {
1211 SPSpiral *spiral = SP_SPIRAL(item);
1213 return sp_spiral_get_xy(spiral, 1.0);
1214 }
1216 static void
1217 sp_spiral_inner_click(SPItem *item, guint state)
1218 {
1219 SPSpiral *spiral = SP_SPIRAL(item);
1221 if (state & GDK_MOD1_MASK) {
1222 spiral->exp = 1;
1223 ((SPObject *)spiral)->updateRepr();
1224 } else if (state & GDK_SHIFT_MASK) {
1225 spiral->t0 = 0;
1226 ((SPObject *)spiral)->updateRepr();
1227 }
1228 }
1230 static SPKnotHolder *
1231 sp_spiral_knot_holder(SPItem *item, SPDesktop *desktop)
1232 {
1233 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1235 sp_knot_holder_add(knot_holder, sp_spiral_inner_set, sp_spiral_inner_get, sp_spiral_inner_click,
1236 _("Roll/unroll the spiral from <b>inside</b>; with <b>Ctrl</b> to snap angle; with <b>Alt</b> to converge/diverge"));
1237 sp_knot_holder_add(knot_holder, sp_spiral_outer_set, sp_spiral_outer_get, NULL,
1238 _("Roll/unroll the spiral from <b>outside</b>; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to scale/rotate"));
1240 sp_pat_knot_holder(item, knot_holder);
1242 return knot_holder;
1243 }
1245 /* SPOffset */
1247 static void
1248 sp_offset_offset_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1249 {
1250 SPOffset *offset = SP_OFFSET(item);
1252 offset->rad = sp_offset_distance_to_original(offset, p);
1253 offset->knot = p;
1254 offset->knotSet = true;
1256 ((SPObject *)offset)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1257 }
1260 static NR::Point sp_offset_offset_get(SPItem *item)
1261 {
1262 SPOffset *offset = SP_OFFSET(item);
1264 NR::Point np;
1265 sp_offset_top_point(offset,&np);
1266 return np;
1267 }
1269 static SPKnotHolder *
1270 sp_offset_knot_holder(SPItem *item, SPDesktop *desktop)
1271 {
1272 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1274 sp_knot_holder_add(knot_holder, sp_offset_offset_set, sp_offset_offset_get, NULL,
1275 _("Adjust the <b>offset distance</b>"));
1277 sp_pat_knot_holder(item, knot_holder);
1279 return knot_holder;
1280 }
1282 static SPKnotHolder *
1283 sp_misc_knot_holder(SPItem *item, SPDesktop *desktop) // FIXME: eliminate, instead make a pattern-drag similar to gradient-drag
1284 {
1285 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1286 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1287 {
1288 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, item, NULL);
1290 sp_pat_knot_holder(item, knot_holder);
1292 return knot_holder;
1293 }
1294 return NULL;
1295 }
1297 static void
1298 sp_pat_knot_holder(SPItem *item, SPKnotHolder *knot_holder)
1299 {
1300 if ((SP_OBJECT(item)->style->fill.type == SP_PAINT_TYPE_PAINTSERVER)
1301 && SP_IS_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style)))
1302 {
1303 sp_knot_holder_add_full(knot_holder, sp_pattern_xy_set, sp_pattern_xy_get, NULL, SP_KNOT_SHAPE_CROSS, SP_KNOT_MODE_XOR,
1304 // TRANSLATORS: This refers to the pattern that's inside the object
1305 _("<b>Move</b> the pattern fill inside the object"));
1306 sp_knot_holder_add_full(knot_holder, sp_pattern_scale_set, sp_pattern_scale_get, NULL, SP_KNOT_SHAPE_SQUARE, SP_KNOT_MODE_XOR,
1307 _("<b>Scale</b> the pattern fill uniformly"));
1308 sp_knot_holder_add_full(knot_holder, sp_pattern_angle_set, sp_pattern_angle_get, NULL, SP_KNOT_SHAPE_CIRCLE, SP_KNOT_MODE_XOR,
1309 _("<b>Rotate</b> the pattern fill; with <b>Ctrl</b> to snap angle"));
1310 }
1311 }
1313 static NR::Point sp_flowtext_corner_get(SPItem *item)
1314 {
1315 SPRect *rect = SP_RECT(item);
1317 return NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed);
1318 }
1320 static void
1321 sp_flowtext_corner_set(SPItem *item, NR::Point const &p, NR::Point const &origin, guint state)
1322 {
1323 SPRect *rect = SP_RECT(item);
1325 sp_rect_wh_set_internal(rect, p, origin, state);
1326 }
1328 static SPKnotHolder *
1329 sp_flowtext_knot_holder(SPItem *item, SPDesktop *desktop)
1330 {
1331 SPKnotHolder *knot_holder = sp_knot_holder_new(desktop, SP_FLOWTEXT(item)->get_frame(NULL), NULL);
1333 sp_knot_holder_add(knot_holder, sp_flowtext_corner_set, sp_flowtext_corner_get, NULL,
1334 _("Drag to resize the <b>flowed text frame</b>"));
1336 return knot_holder;
1337 }
1340 /*
1341 Local Variables:
1342 mode:c++
1343 c-file-style:"stroustrup"
1344 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1345 indent-tabs-mode:nil
1346 fill-column:99
1347 End:
1348 */
1349 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :