1 /*
2 * tweaking paths without node editing
3 *
4 * Authors:
5 * bulia byak
6 * Jon A. Cruz <jon@joncruz.org>
7 * Abhishek Sharma
8 *
9 * Copyright (C) 2007 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "config.h"
16 #include <gtk/gtk.h>
17 #include <gdk/gdkkeysyms.h>
18 #include <glibmm/i18n.h>
20 #include <numeric>
22 #include "svg/svg.h"
24 #include <glib/gmem.h>
25 #include "macros.h"
26 #include "document.h"
27 #include "selection.h"
28 #include "desktop.h"
29 #include "desktop-events.h"
30 #include "desktop-handles.h"
31 #include "desktop-style.h"
32 #include "message-context.h"
33 #include "pixmaps/cursor-tweak-move.xpm"
34 #include "pixmaps/cursor-thin.xpm"
35 #include "pixmaps/cursor-thicken.xpm"
36 #include "pixmaps/cursor-attract.xpm"
37 #include "pixmaps/cursor-repel.xpm"
38 #include "pixmaps/cursor-push.xpm"
39 #include "pixmaps/cursor-roughen.xpm"
40 #include "pixmaps/cursor-color.xpm"
41 #include <boost/optional.hpp>
42 #include "libnr/nr-matrix-ops.h"
43 #include "libnr/nr-scale-translate-ops.h"
44 #include "xml/repr.h"
45 #include "context-fns.h"
46 #include "sp-item.h"
47 #include "inkscape.h"
48 #include "color.h"
49 #include "svg/svg-color.h"
50 #include "splivarot.h"
51 #include "sp-item-group.h"
52 #include "sp-shape.h"
53 #include "sp-path.h"
54 #include "path-chemistry.h"
55 #include "sp-gradient.h"
56 #include "sp-stop.h"
57 #include "sp-gradient-reference.h"
58 #include "sp-linear-gradient.h"
59 #include "sp-radial-gradient.h"
60 #include "gradient-chemistry.h"
61 #include "sp-text.h"
62 #include "sp-flowtext.h"
63 #include "display/sp-canvas.h"
64 #include "display/canvas-bpath.h"
65 #include "display/canvas-arena.h"
66 #include "display/curve.h"
67 #include "livarot/Shape.h"
68 #include <2geom/isnan.h>
69 #include <2geom/transforms.h>
70 #include "preferences.h"
71 #include "style.h"
72 #include "box3d.h"
73 #include "sp-item-transform.h"
74 #include "filter-chemistry.h"
75 #include "sp-gaussian-blur-fns.h"
76 #include "sp-gaussian-blur.h"
78 #include "tweak-context.h"
80 using Inkscape::DocumentUndo;
82 #define DDC_RED_RGBA 0xff0000ff
84 #define DYNA_MIN_WIDTH 1.0e-6
86 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
87 static void sp_tweak_context_init(SPTweakContext *ddc);
88 static void sp_tweak_context_dispose(GObject *object);
90 static void sp_tweak_context_setup(SPEventContext *ec);
91 static void sp_tweak_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
92 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
94 static SPEventContextClass *parent_class;
96 GtkType
97 sp_tweak_context_get_type(void)
98 {
99 static GType type = 0;
100 if (!type) {
101 GTypeInfo info = {
102 sizeof(SPTweakContextClass),
103 NULL, NULL,
104 (GClassInitFunc) sp_tweak_context_class_init,
105 NULL, NULL,
106 sizeof(SPTweakContext),
107 4,
108 (GInstanceInitFunc) sp_tweak_context_init,
109 NULL, /* value_table */
110 };
111 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
112 }
113 return type;
114 }
116 static void
117 sp_tweak_context_class_init(SPTweakContextClass *klass)
118 {
119 GObjectClass *object_class = (GObjectClass *) klass;
120 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
122 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
124 object_class->dispose = sp_tweak_context_dispose;
126 event_context_class->setup = sp_tweak_context_setup;
127 event_context_class->set = sp_tweak_context_set;
128 event_context_class->root_handler = sp_tweak_context_root_handler;
129 }
131 static void
132 sp_tweak_context_init(SPTweakContext *tc)
133 {
134 SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
136 event_context->cursor_shape = cursor_push_xpm;
137 event_context->hot_x = 4;
138 event_context->hot_y = 4;
140 /* attributes */
141 tc->dragging = FALSE;
143 tc->width = 0.2;
144 tc->force = 0.2;
145 tc->pressure = TC_DEFAULT_PRESSURE;
147 tc->is_dilating = false;
148 tc->has_dilated = false;
150 tc->do_h = true;
151 tc->do_s = true;
152 tc->do_l = true;
153 tc->do_o = false;
155 new (&tc->style_set_connection) sigc::connection();
156 }
158 static void
159 sp_tweak_context_dispose(GObject *object)
160 {
161 SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
163 tc->style_set_connection.disconnect();
164 tc->style_set_connection.~connection();
166 if (tc->dilate_area) {
167 gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
168 tc->dilate_area = NULL;
169 }
171 if (tc->_message_context) {
172 delete tc->_message_context;
173 }
175 G_OBJECT_CLASS(parent_class)->dispose(object);
176 }
178 bool is_transform_mode (gint mode)
179 {
180 return (mode == TWEAK_MODE_MOVE ||
181 mode == TWEAK_MODE_MOVE_IN_OUT ||
182 mode == TWEAK_MODE_MOVE_JITTER ||
183 mode == TWEAK_MODE_SCALE ||
184 mode == TWEAK_MODE_ROTATE ||
185 mode == TWEAK_MODE_MORELESS);
186 }
188 bool is_color_mode (gint mode)
189 {
190 return (mode == TWEAK_MODE_COLORPAINT || mode == TWEAK_MODE_COLORJITTER || mode == TWEAK_MODE_BLUR);
191 }
193 void
194 sp_tweak_update_cursor (SPTweakContext *tc, bool with_shift)
195 {
196 SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
197 SPDesktop *desktop = event_context->desktop;
199 guint num = 0;
200 gchar *sel_message = NULL;
201 if (!desktop->selection->isEmpty()) {
202 num = g_slist_length((GSList *) desktop->selection->itemList());
203 sel_message = g_strdup_printf(ngettext("<b>%i</b> object selected","<b>%i</b> objects selected",num), num);
204 } else {
205 sel_message = g_strdup_printf(_("<b>Nothing</b> selected"));
206 }
209 switch (tc->mode) {
210 case TWEAK_MODE_MOVE:
211 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>move</b>."), sel_message);
212 event_context->cursor_shape = cursor_tweak_move_xpm;
213 break;
214 case TWEAK_MODE_MOVE_IN_OUT:
215 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>move in</b>; with Shift to <b>move out</b>."), sel_message);
216 event_context->cursor_shape = cursor_tweak_move_xpm;
217 break;
218 case TWEAK_MODE_MOVE_JITTER:
219 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>move randomly</b>."), sel_message);
220 event_context->cursor_shape = cursor_tweak_move_xpm;
221 break;
222 case TWEAK_MODE_SCALE:
223 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>scale down</b>; with Shift to <b>scale up</b>."), sel_message);
224 event_context->cursor_shape = cursor_tweak_move_xpm;
225 break;
226 case TWEAK_MODE_ROTATE:
227 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>rotate clockwise</b>; with Shift, <b>counterclockwise</b>."), sel_message);
228 event_context->cursor_shape = cursor_tweak_move_xpm;
229 break;
230 case TWEAK_MODE_MORELESS:
231 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>duplicate</b>; with Shift, <b>delete</b>."), sel_message);
232 event_context->cursor_shape = cursor_tweak_move_xpm;
233 break;
234 case TWEAK_MODE_PUSH:
235 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag to <b>push paths</b>."), sel_message);
236 event_context->cursor_shape = cursor_push_xpm;
237 break;
238 case TWEAK_MODE_SHRINK_GROW:
239 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>inset paths</b>; with Shift to <b>outset</b>."), sel_message);
240 if (with_shift) {
241 event_context->cursor_shape = cursor_thicken_xpm;
242 } else {
243 event_context->cursor_shape = cursor_thin_xpm;
244 }
245 break;
246 case TWEAK_MODE_ATTRACT_REPEL:
247 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>attract paths</b>; with Shift to <b>repel</b>."), sel_message);
248 if (with_shift) {
249 event_context->cursor_shape = cursor_repel_xpm;
250 } else {
251 event_context->cursor_shape = cursor_attract_xpm;
252 }
253 break;
254 case TWEAK_MODE_ROUGHEN:
255 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>roughen paths</b>."), sel_message);
256 event_context->cursor_shape = cursor_roughen_xpm;
257 break;
258 case TWEAK_MODE_COLORPAINT:
259 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>paint objects</b> with color."), sel_message);
260 event_context->cursor_shape = cursor_color_xpm;
261 break;
262 case TWEAK_MODE_COLORJITTER:
263 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>randomize colors</b>."), sel_message);
264 event_context->cursor_shape = cursor_color_xpm;
265 break;
266 case TWEAK_MODE_BLUR:
267 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag or click to <b>increase blur</b>; with Shift to <b>decrease</b>."), sel_message);
268 event_context->cursor_shape = cursor_color_xpm;
269 break;
270 }
271 sp_event_context_update_cursor(event_context);
272 g_free(sel_message);
273 }
275 static bool
276 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
277 {
278 if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
279 // we cannot store properties with uris
280 css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
281 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
282 prefs->setStyle("/tools/tweak/style", const_cast<SPCSSAttr*>(css));
283 return true;
284 }
285 return false;
286 }
289 static void
290 sp_tweak_context_setup(SPEventContext *ec)
291 {
292 SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
294 if (((SPEventContextClass *) parent_class)->setup)
295 ((SPEventContextClass *) parent_class)->setup(ec);
297 {
298 /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
299 SPCurve *c = new SPCurve();
300 const double C1 = 0.552;
301 c->moveto(-1,0);
302 c->curveto(-1, C1, -C1, 1, 0, 1 );
303 c->curveto(C1, 1, 1, C1, 1, 0 );
304 c->curveto(1, -C1, C1, -1, 0, -1 );
305 c->curveto(-C1, -1, -1, -C1, -1, 0 );
306 c->closepath();
307 tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
308 c->unref();
309 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
310 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
311 sp_canvas_item_hide(tc->dilate_area);
312 }
314 tc->is_drawing = false;
316 tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
318 sp_event_context_read(ec, "width");
319 sp_event_context_read(ec, "mode");
320 sp_event_context_read(ec, "fidelity");
321 sp_event_context_read(ec, "force");
322 sp_event_context_read(ec, "usepressure");
323 sp_event_context_read(ec, "doh");
324 sp_event_context_read(ec, "dol");
325 sp_event_context_read(ec, "dos");
326 sp_event_context_read(ec, "doo");
328 tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
329 sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
330 );
332 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
333 if (prefs->getBool("/tools/tweak/selcue")) {
334 ec->enableSelectionCue();
335 }
337 if (prefs->getBool("/tools/tweak/gradientdrag")) {
338 ec->enableGrDrag();
339 }
340 }
342 static void
343 sp_tweak_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
344 {
345 SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
346 Glib::ustring path = val->getEntryName();
348 if (path == "width") {
349 tc->width = CLAMP(val->getDouble(0.1), -1000.0, 1000.0);
350 } else if (path == "mode") {
351 tc->mode = val->getInt();
352 sp_tweak_update_cursor(tc, false);
353 } else if (path == "fidelity") {
354 tc->fidelity = CLAMP(val->getDouble(), 0.0, 1.0);
355 } else if (path == "force") {
356 tc->force = CLAMP(val->getDouble(1.0), 0, 1.0);
357 } else if (path == "usepressure") {
358 tc->usepressure = val->getBool();
359 } else if (path == "doh") {
360 tc->do_h = val->getBool();
361 } else if (path == "dos") {
362 tc->do_s = val->getBool();
363 } else if (path == "dol") {
364 tc->do_l = val->getBool();
365 } else if (path == "doo") {
366 tc->do_o = val->getBool();
367 }
368 }
370 static void
371 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
372 {
373 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
374 tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
375 else
376 tc->pressure = TC_DEFAULT_PRESSURE;
377 }
379 double
380 get_dilate_radius (SPTweakContext *tc)
381 {
382 // 10 times the pen width:
383 return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
384 }
386 double
387 get_path_force (SPTweakContext *tc)
388 {
389 double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
390 /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
391 if (force > 3) {
392 force += 4 * (force - 3);
393 }
394 return force * tc->force;
395 }
397 double
398 get_move_force (SPTweakContext *tc)
399 {
400 double force = (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE);
401 return force * tc->force;
402 }
404 bool
405 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, Geom::Point p, Geom::Point vector, gint mode, double radius, double force, double fidelity, bool reverse)
406 {
407 bool did = false;
409 if (SP_IS_BOX3D(item) && !is_transform_mode(mode) && !is_color_mode(mode)) {
410 // convert 3D boxes to ordinary groups before tweaking their shapes
411 item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
412 selection->add(item);
413 }
415 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
416 GSList *items = g_slist_prepend (NULL, item);
417 GSList *selected = NULL;
418 GSList *to_select = NULL;
419 SPDocument *doc = SP_OBJECT_DOCUMENT(item);
420 sp_item_list_to_curves (items, &selected, &to_select);
421 g_slist_free (items);
422 SPObject* newObj = doc->getObjectByRepr((Inkscape::XML::Node *) to_select->data);
423 g_slist_free (to_select);
424 item = (SPItem *) newObj;
425 selection->add(item);
426 }
428 if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item)) {
429 GSList *children = NULL;
430 for (SPObject *child = item->firstChild() ; child; child = child->getNext() ) {
431 if (SP_IS_ITEM(child)) {
432 children = g_slist_prepend(children, child);
433 }
434 }
436 for (GSList *i = children; i; i = i->next) {
437 SPItem *child = SP_ITEM(i->data);
438 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity, reverse))
439 did = true;
440 }
442 g_slist_free(children);
444 } else {
445 if (mode == TWEAK_MODE_MOVE) {
447 Geom::OptRect a = item->getBounds(item->i2doc_affine());
448 if (a) {
449 double x = Geom::L2(a->midpoint() - p)/radius;
450 if (a->contains(p)) x = 0;
451 if (x < 1) {
452 Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * vector;
453 sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
454 did = true;
455 }
456 }
458 } else if (mode == TWEAK_MODE_MOVE_IN_OUT) {
460 Geom::OptRect a = item->getBounds(item->i2doc_affine());
461 if (a) {
462 double x = Geom::L2(a->midpoint() - p)/radius;
463 if (a->contains(p)) x = 0;
464 if (x < 1) {
465 Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) *
466 (reverse? (a->midpoint() - p) : (p - a->midpoint()));
467 sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
468 did = true;
469 }
470 }
472 } else if (mode == TWEAK_MODE_MOVE_JITTER) {
474 Geom::OptRect a = item->getBounds(item->i2doc_affine());
475 if (a) {
476 double dp = g_random_double_range(0, M_PI*2);
477 double dr = g_random_double_range(0, radius);
478 double x = Geom::L2(a->midpoint() - p)/radius;
479 if (a->contains(p)) x = 0;
480 if (x < 1) {
481 Geom::Point move = force * 0.5 * (cos(M_PI * x) + 1) * Geom::Point(cos(dp)*dr, sin(dp)*dr);
482 sp_item_move_rel(item, Geom::Translate(move[Geom::X], -move[Geom::Y]));
483 did = true;
484 }
485 }
487 } else if (mode == TWEAK_MODE_SCALE) {
489 Geom::OptRect a = item->getBounds(item->i2doc_affine());
490 if (a) {
491 double x = Geom::L2(a->midpoint() - p)/radius;
492 if (a->contains(p)) x = 0;
493 if (x < 1) {
494 double scale = 1 + (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1);
495 sp_item_scale_rel(item, Geom::Scale(scale, scale));
496 did = true;
497 }
498 }
500 } else if (mode == TWEAK_MODE_ROTATE) {
502 Geom::OptRect a = item->getBounds(item->i2doc_affine());
503 if (a) {
504 double x = Geom::L2(a->midpoint() - p)/radius;
505 if (a->contains(p)) x = 0;
506 if (x < 1) {
507 double angle = (reverse? force : -force) * 0.05 * (cos(M_PI * x) + 1) * M_PI;
508 sp_item_rotate_rel(item, Geom::Rotate(angle));
509 did = true;
510 }
511 }
513 } else if (mode == TWEAK_MODE_MORELESS) {
515 Geom::OptRect a = item->getBounds(item->i2doc_affine());
516 if (a) {
517 double x = Geom::L2(a->midpoint() - p)/radius;
518 if (a->contains(p)) x = 0;
519 if (x < 1) {
520 double prob = force * 0.5 * (cos(M_PI * x) + 1);
521 double chance = g_random_double_range(0, 1);
522 if (chance <= prob) {
523 if (reverse) { // delete
524 sp_object_ref(SP_OBJECT(item), NULL);
525 SP_OBJECT(item)->deleteObject(true, true);
526 sp_object_unref(SP_OBJECT(item), NULL);
527 } else { // duplicate
528 SPDocument *doc = SP_OBJECT_DOCUMENT(item);
529 Inkscape::XML::Document* xml_doc = doc->getReprDoc();
530 Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
531 SPObject *old_obj = doc->getObjectByRepr(old_repr);
532 Inkscape::XML::Node *parent = old_repr->parent();
533 Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
534 parent->appendChild(copy);
535 SPObject *new_obj = doc->getObjectByRepr(copy);
536 if (selection->includes(old_obj)) {
537 selection->add(new_obj);
538 }
539 Inkscape::GC::release(copy);
540 }
541 did = true;
542 }
543 }
544 }
546 } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item)) {
548 Inkscape::XML::Node *newrepr = NULL;
549 gint pos = 0;
550 Inkscape::XML::Node *parent = NULL;
551 char const *id = NULL;
552 if (!SP_IS_PATH(item)) {
553 newrepr = sp_selected_item_to_curved_repr(item, 0);
554 if (!newrepr)
555 return false;
557 // remember the position of the item
558 pos = SP_OBJECT_REPR(item)->position();
559 // remember parent
560 parent = SP_OBJECT_REPR(item)->parent();
561 // remember id
562 id = SP_OBJECT_REPR(item)->attribute("id");
563 }
566 // skip those paths whose bboxes are entirely out of reach with our radius
567 Geom::OptRect bbox = item->getBounds(item->i2doc_affine());
568 if (bbox) {
569 bbox->expandBy(radius);
570 if (!bbox->contains(p)) {
571 return false;
572 }
573 }
575 Path *orig = Path_for_item(item, false);
576 if (orig == NULL) {
577 return false;
578 }
580 Path *res = new Path;
581 res->SetBackData(false);
583 Shape *theShape = new Shape;
584 Shape *theRes = new Shape;
585 Geom::Matrix i2doc(item->i2doc_affine());
587 orig->ConvertWithBackData((0.08 - (0.07 * fidelity)) / i2doc.descrim()); // default 0.059
588 orig->Fill(theShape, 0);
590 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
591 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
592 if (val && strcmp(val, "nonzero") == 0)
593 {
594 theRes->ConvertToShape(theShape, fill_nonZero);
595 }
596 else if (val && strcmp(val, "evenodd") == 0)
597 {
598 theRes->ConvertToShape(theShape, fill_oddEven);
599 }
600 else
601 {
602 theRes->ConvertToShape(theShape, fill_nonZero);
603 }
605 if (Geom::L2(vector) != 0)
606 vector = 1/Geom::L2(vector) * vector;
608 bool did_this = false;
609 if (mode == TWEAK_MODE_SHRINK_GROW) {
610 if (theShape->MakeTweak(tweak_mode_grow, theRes,
611 reverse? force : -force,
612 join_straight, 4.0,
613 true, p, Geom::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
614 did_this = true;
615 } else if (mode == TWEAK_MODE_ATTRACT_REPEL) {
616 if (theShape->MakeTweak(tweak_mode_repel, theRes,
617 reverse? force : -force,
618 join_straight, 4.0,
619 true, p, Geom::Point(0,0), radius, &i2doc) == 0)
620 did_this = true;
621 } else if (mode == TWEAK_MODE_PUSH) {
622 if (theShape->MakeTweak(tweak_mode_push, theRes,
623 1.0,
624 join_straight, 4.0,
625 true, p, force*2*vector, radius, &i2doc) == 0)
626 did_this = true;
627 } else if (mode == TWEAK_MODE_ROUGHEN) {
628 if (theShape->MakeTweak(tweak_mode_roughen, theRes,
629 force,
630 join_straight, 4.0,
631 true, p, Geom::Point(0,0), radius, &i2doc) == 0)
632 did_this = true;
633 }
635 // the rest only makes sense if we actually changed the path
636 if (did_this) {
637 theRes->ConvertToShape(theShape, fill_positive);
639 res->Reset();
640 theRes->ConvertToForme(res);
642 double th_max = (0.6 - 0.59*sqrt(fidelity)) / i2doc.descrim();
643 double threshold = MAX(th_max, th_max*force);
644 res->ConvertEvenLines(threshold);
645 res->Simplify(threshold / (selection->desktop()->current_zoom()));
647 if (newrepr) { // converting to path, need to replace the repr
648 bool is_selected = selection->includes(item);
649 if (is_selected)
650 selection->remove(item);
652 // It's going to resurrect, so we delete without notifying listeners.
653 SP_OBJECT(item)->deleteObject(false);
655 // restore id
656 newrepr->setAttribute("id", id);
657 // add the new repr to the parent
658 parent->appendChild(newrepr);
659 // move to the saved position
660 newrepr->setPosition(pos > 0 ? pos : 0);
662 if (is_selected)
663 selection->add(newrepr);
664 }
666 if (res->descr_cmd.size() > 1) {
667 gchar *str = res->svg_dump_path();
668 if (newrepr) {
669 newrepr->setAttribute("d", str);
670 } else {
671 if (SP_IS_LPE_ITEM(item) && sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(item))) {
672 SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
673 } else {
674 SP_OBJECT_REPR(item)->setAttribute("d", str);
675 }
676 }
677 g_free(str);
678 } else {
679 // TODO: if there's 0 or 1 node left, delete this path altogether
680 }
682 if (newrepr) {
683 Inkscape::GC::release(newrepr);
684 newrepr = NULL;
685 }
686 }
688 delete theShape;
689 delete theRes;
690 delete orig;
691 delete res;
693 if (did_this)
694 did = true;
695 }
697 }
699 return did;
700 }
702 void
703 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
704 {
705 float rgb_g[3];
707 if (!do_h || !do_s || !do_l) {
708 float hsl_g[3];
709 sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
710 float hsl_c[3];
711 sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
712 if (!do_h)
713 hsl_g[0] = hsl_c[0];
714 if (!do_s)
715 hsl_g[1] = hsl_c[1];
716 if (!do_l)
717 hsl_g[2] = hsl_c[2];
718 sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
719 } else {
720 rgb_g[0] = SP_RGBA32_R_F(goal);
721 rgb_g[1] = SP_RGBA32_G_F(goal);
722 rgb_g[2] = SP_RGBA32_B_F(goal);
723 }
725 for (int i = 0; i < 3; i++) {
726 double d = rgb_g[i] - color[i];
727 color[i] += d * force;
728 }
729 }
731 void
732 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
733 {
734 float hsl_c[3];
735 sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
737 if (do_h) {
738 hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
739 if (hsl_c[0] > 1)
740 hsl_c[0] -= 1;
741 if (hsl_c[0] < 0)
742 hsl_c[0] += 1;
743 }
744 if (do_s) {
745 hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
746 }
747 if (do_l) {
748 hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
749 }
751 sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
752 }
754 void
755 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
756 {
757 if (mode == TWEAK_MODE_COLORPAINT) {
758 tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
759 } else if (mode == TWEAK_MODE_COLORJITTER) {
760 tweak_colorjitter (color, force, do_h, do_s, do_l);
761 }
762 }
764 void
765 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
766 {
767 double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
769 if (mode == TWEAK_MODE_COLORPAINT) {
770 double d = opacity_goal - opacity;
771 opacity += d * force;
772 } else if (mode == TWEAK_MODE_COLORJITTER) {
773 opacity += g_random_double_range(-opacity, 1 - opacity) * force;
774 }
776 style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
777 }
780 double
781 tweak_profile (double dist, double radius)
782 {
783 if (radius == 0)
784 return 0;
785 double x = dist / radius;
786 double alpha = 1;
787 if (x >= 1) {
788 return 0;
789 } else if (x <= 0) {
790 return 1;
791 } else {
792 return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
793 }
794 }
796 void
797 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
798 guint32 const rgb_goal, Geom::Point p_w, double radius, double force, guint mode,
799 bool do_h, bool do_s, bool do_l, bool /*do_o*/)
800 {
801 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
803 if (!gradient || !SP_IS_GRADIENT(gradient))
804 return;
806 Geom::Matrix i2d (item->i2doc_affine ());
807 Geom::Point p = p_w * i2d.inverse();
808 p *= (gradient->gradientTransform).inverse();
809 // now p is in gradient's original coordinates
811 double pos = 0;
812 double r = 0;
813 if (SP_IS_LINEARGRADIENT(gradient)) {
814 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
816 Geom::Point p1(lg->x1.computed, lg->y1.computed);
817 Geom::Point p2(lg->x2.computed, lg->y2.computed);
818 Geom::Point pdiff(p2 - p1);
819 double vl = Geom::L2(pdiff);
821 // This is the matrix which moves and rotates the gradient line
822 // so it's oriented along the X axis:
823 Geom::Matrix norm = Geom::Matrix(Geom::Translate(-p1)) * Geom::Matrix(Geom::Rotate(-atan2(pdiff[Geom::Y], pdiff[Geom::X])));
825 // Transform the mouse point by it to find out its projection onto the gradient line:
826 Geom::Point pnorm = p * norm;
828 // Scale its X coordinate to match the length of the gradient line:
829 pos = pnorm[Geom::X] / vl;
830 // Calculate radius in lenfth-of-gradient-line units
831 r = radius / vl;
833 } else if (SP_IS_RADIALGRADIENT(gradient)) {
834 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
835 Geom::Point c (rg->cx.computed, rg->cy.computed);
836 pos = Geom::L2(p - c) / rg->r.computed;
837 r = radius / rg->r.computed;
838 }
840 // Normalize pos to 0..1, taking into accound gradient spread:
841 double pos_e = pos;
842 if (gradient->getSpread() == SP_GRADIENT_SPREAD_PAD) {
843 if (pos > 1) {
844 pos_e = 1;
845 }
846 if (pos < 0) {
847 pos_e = 0;
848 }
849 } else if (gradient->getSpread() == SP_GRADIENT_SPREAD_REPEAT) {
850 if (pos > 1 || pos < 0) {
851 pos_e = pos - floor(pos);
852 }
853 } else if (gradient->getSpread() == SP_GRADIENT_SPREAD_REFLECT) {
854 if (pos > 1 || pos < 0) {
855 bool odd = ((int)(floor(pos)) % 2 == 1);
856 pos_e = pos - floor(pos);
857 if (odd) {
858 pos_e = 1 - pos_e;
859 }
860 }
861 }
863 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
865 double offset_l = 0;
866 double offset_h = 0;
867 SPObject *child_prev = NULL;
868 for (SPObject *child = vector->firstChild(); child; child = child->getNext()) {
869 if (!SP_IS_STOP(child)) {
870 continue;
871 }
872 SPStop *stop = SP_STOP (child);
874 offset_h = stop->offset;
876 if (child_prev) {
878 if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
879 // the summit falls in this interstop, and the radius is small,
880 // so it only affects the ends of this interstop;
881 // distribute the force between the two endstops so that they
882 // get all the painting even if they are not touched by the brush
883 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
884 force * (pos_e - offset_l) / (offset_h - offset_l),
885 do_h, do_s, do_l);
886 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
887 force * (offset_h - pos_e) / (offset_h - offset_l),
888 do_h, do_s, do_l);
889 SP_OBJECT(stop)->updateRepr();
890 child_prev->updateRepr();
891 break;
892 } else {
893 // wide brush, may affect more than 2 stops,
894 // paint each stop by the force from the profile curve
895 if (offset_l <= pos_e && offset_l > pos_e - r) {
896 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
897 force * tweak_profile (fabs (pos_e - offset_l), r),
898 do_h, do_s, do_l);
899 child_prev->updateRepr();
900 }
902 if (offset_h >= pos_e && offset_h < pos_e + r) {
903 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
904 force * tweak_profile (fabs (pos_e - offset_h), r),
905 do_h, do_s, do_l);
906 SP_OBJECT(stop)->updateRepr();
907 }
908 }
909 }
911 offset_l = offset_h;
912 child_prev = child;
913 }
914 }
916 bool
917 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
918 guint32 fill_goal, bool do_fill,
919 guint32 stroke_goal, bool do_stroke,
920 float opacity_goal, bool do_opacity,
921 bool do_blur, bool reverse,
922 Geom::Point p, double radius, double force,
923 bool do_h, bool do_s, bool do_l, bool do_o)
924 {
925 bool did = false;
927 if (SP_IS_GROUP(item)) {
928 for (SPObject *child = item->firstChild() ; child; child = child->getNext() ) {
929 if (SP_IS_ITEM(child)) {
930 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
931 fill_goal, do_fill,
932 stroke_goal, do_stroke,
933 opacity_goal, do_opacity,
934 do_blur, reverse,
935 p, radius, force, do_h, do_s, do_l, do_o))
936 did = true;
937 }
938 }
940 } else {
941 SPStyle *style = SP_OBJECT_STYLE(item);
942 if (!style) {
943 return false;
944 }
945 Geom::OptRect bbox = item->getBounds(item->i2doc_affine(),
946 SPItem::GEOMETRIC_BBOX);
947 if (!bbox) {
948 return false;
949 }
951 Geom::Rect brush(p - Geom::Point(radius, radius), p + Geom::Point(radius, radius));
953 Geom::Point center = bbox->midpoint();
954 double this_force;
956 // if item == item_at_point, use max force
957 if (item == item_at_point) {
958 this_force = force;
959 // else if no overlap of bbox and brush box, skip:
960 } else if (!bbox->intersects(brush)) {
961 return false;
962 //TODO:
963 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
964 //} else if (bbox->maxExtent() > 3 * radius) {
965 //}
966 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
967 // else if still smaller, then check only the object center:
968 } else {
969 this_force = force * tweak_profile (Geom::L2 (p - center), radius);
970 }
972 if (this_force > 0.002) {
974 if (do_blur) {
975 Geom::OptRect bbox = item->getBounds(item->i2doc_affine(),
976 SPItem::GEOMETRIC_BBOX);
977 if (!bbox) {
978 return did;
979 }
981 double blur_now = 0;
982 Geom::Matrix i2d = item->i2d_affine ();
983 if (style->filter.set && style->getFilter()) {
984 //cycle through filter primitives
985 SPObject *primitive_obj = style->getFilter()->children;
986 while (primitive_obj) {
987 if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
988 SPFilterPrimitive *primitive = SP_FILTER_PRIMITIVE(primitive_obj);
989 //if primitive is gaussianblur
990 if(SP_IS_GAUSSIANBLUR(primitive)) {
991 SPGaussianBlur * spblur = SP_GAUSSIANBLUR(primitive);
992 float num = spblur->stdDeviation.getNumber();
993 blur_now += num * i2d.descrim(); // sum all blurs in the filter
994 }
995 }
996 primitive_obj = primitive_obj->next;
997 }
998 }
999 double perimeter = bbox->dimensions()[Geom::X] + bbox->dimensions()[Geom::Y];
1000 blur_now = blur_now / perimeter;
1002 double blur_new;
1003 if (reverse)
1004 blur_new = blur_now - 0.06 * force;
1005 else
1006 blur_new = blur_now + 0.06 * force;
1007 if (blur_new < 0.0005 && blur_new < blur_now) {
1008 blur_new = 0;
1009 }
1011 if (blur_new == 0) {
1012 remove_filter(item, false);
1013 } else {
1014 double radius = blur_new * perimeter;
1015 SPFilter *filter = modify_filter_gaussian_blur_from_item(SP_OBJECT_DOCUMENT(item), item, radius);
1017 sp_style_set_property_url(item, "filter", filter, false);
1018 }
1019 return true; // do not do colors, blur is a separate mode
1020 }
1022 if (do_fill) {
1023 if (style->fill.isPaintserver()) {
1024 tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1025 did = true;
1026 } else if (style->fill.isColor()) {
1027 tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
1028 item->updateRepr();
1029 did = true;
1030 }
1031 }
1032 if (do_stroke) {
1033 if (style->stroke.isPaintserver()) {
1034 tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
1035 did = true;
1036 } else if (style->stroke.isColor()) {
1037 tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
1038 item->updateRepr();
1039 did = true;
1040 }
1041 }
1042 if (do_opacity && do_o) {
1043 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
1044 }
1045 }
1046 }
1048 return did;
1049 }
1052 bool
1053 sp_tweak_dilate (SPTweakContext *tc, Geom::Point event_p, Geom::Point p, Geom::Point vector, bool reverse)
1054 {
1055 Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
1056 SPDesktop *desktop = SP_EVENT_CONTEXT(tc)->desktop;
1058 if (selection->isEmpty()) {
1059 return false;
1060 }
1062 bool did = false;
1063 double radius = get_dilate_radius(tc);
1065 SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->getItemAtPoint(event_p, TRUE);
1067 bool do_fill = false, do_stroke = false, do_opacity = false;
1068 guint32 fill_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", true, &do_fill);
1069 guint32 stroke_goal = sp_desktop_get_color_tool(desktop, "/tools/tweak", false, &do_stroke);
1070 double opacity_goal = sp_desktop_get_master_opacity_tool(desktop, "/tools/tweak", &do_opacity);
1071 if (reverse) {
1072 #if 0
1073 // HSL inversion
1074 float hsv[3];
1075 float rgb[3];
1076 sp_color_rgb_to_hsv_floatv (hsv,
1077 SP_RGBA32_R_F(fill_goal),
1078 SP_RGBA32_G_F(fill_goal),
1079 SP_RGBA32_B_F(fill_goal));
1080 sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1081 fill_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1082 sp_color_rgb_to_hsv_floatv (hsv,
1083 SP_RGBA32_R_F(stroke_goal),
1084 SP_RGBA32_G_F(stroke_goal),
1085 SP_RGBA32_B_F(stroke_goal));
1086 sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
1087 stroke_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
1088 #else
1089 // RGB inversion
1090 fill_goal = SP_RGBA32_U_COMPOSE(
1091 (255 - SP_RGBA32_R_U(fill_goal)),
1092 (255 - SP_RGBA32_G_U(fill_goal)),
1093 (255 - SP_RGBA32_B_U(fill_goal)),
1094 (255 - SP_RGBA32_A_U(fill_goal)));
1095 stroke_goal = SP_RGBA32_U_COMPOSE(
1096 (255 - SP_RGBA32_R_U(stroke_goal)),
1097 (255 - SP_RGBA32_G_U(stroke_goal)),
1098 (255 - SP_RGBA32_B_U(stroke_goal)),
1099 (255 - SP_RGBA32_A_U(stroke_goal)));
1100 #endif
1101 opacity_goal = 1 - opacity_goal;
1102 }
1104 double path_force = get_path_force(tc);
1105 if (radius == 0 || path_force == 0) {
1106 return false;
1107 }
1108 double move_force = get_move_force(tc);
1109 double color_force = MIN(sqrt(path_force)/20.0, 1);
1111 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
1112 items != NULL;
1113 items = items->next) {
1115 SPItem *item = (SPItem *) items->data;
1117 if (is_color_mode (tc->mode)) {
1118 if (do_fill || do_stroke || do_opacity) {
1119 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
1120 fill_goal, do_fill,
1121 stroke_goal, do_stroke,
1122 opacity_goal, do_opacity,
1123 tc->mode == TWEAK_MODE_BLUR, reverse,
1124 p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
1125 did = true;
1126 }
1127 } else if (is_transform_mode(tc->mode)) {
1128 if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, move_force, tc->fidelity, reverse))
1129 did = true;
1130 } else {
1131 if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, path_force, tc->fidelity, reverse))
1132 did = true;
1133 }
1134 }
1136 return did;
1137 }
1139 void
1140 sp_tweak_update_area (SPTweakContext *tc)
1141 {
1142 double radius = get_dilate_radius(tc);
1143 Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
1144 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1145 sp_canvas_item_show(tc->dilate_area);
1146 }
1148 void
1149 sp_tweak_switch_mode (SPTweakContext *tc, gint mode, bool with_shift)
1150 {
1151 SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1152 // need to set explicitly, because the prefs may not have changed by the previous
1153 tc->mode = mode;
1154 sp_tweak_update_cursor (tc, with_shift);
1155 }
1157 void
1158 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode, bool with_shift)
1159 {
1160 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1161 // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
1162 gint now_mode = prefs->getInt("/tools/tweak/mode", 0);
1163 SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
1164 // button has changed prefs, restore
1165 prefs->setInt("/tools/tweak/mode", now_mode);
1166 // changing prefs changed tc->mode, restore back :)
1167 tc->mode = mode;
1168 sp_tweak_update_cursor (tc, with_shift);
1169 }
1171 gint
1172 sp_tweak_context_root_handler(SPEventContext *event_context,
1173 GdkEvent *event)
1174 {
1175 SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
1176 SPDesktop *desktop = event_context->desktop;
1178 gint ret = FALSE;
1180 switch (event->type) {
1181 case GDK_ENTER_NOTIFY:
1182 sp_canvas_item_show(tc->dilate_area);
1183 break;
1184 case GDK_LEAVE_NOTIFY:
1185 sp_canvas_item_hide(tc->dilate_area);
1186 break;
1187 case GDK_BUTTON_PRESS:
1188 if (event->button.button == 1 && !event_context->space_panning) {
1190 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
1191 return TRUE;
1192 }
1194 Geom::Point const button_w(event->button.x,
1195 event->button.y);
1196 Geom::Point const button_dt(desktop->w2d(button_w));
1197 tc->last_push = desktop->dt2doc(button_dt);
1199 sp_tweak_extinput(tc, event);
1201 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
1202 tc->is_drawing = true;
1203 tc->is_dilating = true;
1204 tc->has_dilated = false;
1206 ret = TRUE;
1207 }
1208 break;
1209 case GDK_MOTION_NOTIFY:
1210 {
1211 Geom::Point const motion_w(event->motion.x,
1212 event->motion.y);
1213 Geom::Point motion_dt(desktop->w2d(motion_w));
1214 Geom::Point motion_doc(desktop->dt2doc(motion_dt));
1215 sp_tweak_extinput(tc, event);
1217 // draw the dilating cursor
1218 double radius = get_dilate_radius(tc);
1219 Geom::Matrix const sm (Geom::Scale(radius, radius) * Geom::Translate(desktop->w2d(motion_w)));
1220 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
1221 sp_canvas_item_show(tc->dilate_area);
1223 guint num = 0;
1224 if (!desktop->selection->isEmpty()) {
1225 num = g_slist_length((GSList *) desktop->selection->itemList());
1226 }
1227 if (num == 0) {
1228 tc->_message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
1229 }
1231 // dilating:
1232 if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1233 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
1234 //tc->last_push = motion_doc;
1235 tc->has_dilated = true;
1236 // it's slow, so prevent clogging up with events
1237 gobble_motion_events(GDK_BUTTON1_MASK);
1238 return TRUE;
1239 }
1241 }
1242 break;
1245 case GDK_BUTTON_RELEASE:
1246 {
1247 Geom::Point const motion_w(event->button.x, event->button.y);
1248 Geom::Point const motion_dt(desktop->w2d(motion_w));
1250 sp_canvas_end_forced_full_redraws(desktop->canvas);
1251 tc->is_drawing = false;
1253 if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1254 if (!tc->has_dilated) {
1255 // if we did not rub, do a light tap
1256 tc->pressure = 0.03;
1257 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), Geom::Point(0,0), MOD__SHIFT);
1258 }
1259 tc->is_dilating = false;
1260 tc->has_dilated = false;
1261 switch (tc->mode) {
1262 case TWEAK_MODE_MOVE:
1263 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1264 SP_VERB_CONTEXT_TWEAK, _("Move tweak"));
1265 break;
1266 case TWEAK_MODE_MOVE_IN_OUT:
1267 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1268 SP_VERB_CONTEXT_TWEAK, _("Move in/out tweak"));
1269 break;
1270 case TWEAK_MODE_MOVE_JITTER:
1271 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1272 SP_VERB_CONTEXT_TWEAK, _("Move jitter tweak"));
1273 break;
1274 case TWEAK_MODE_SCALE:
1275 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1276 SP_VERB_CONTEXT_TWEAK, _("Scale tweak"));
1277 break;
1278 case TWEAK_MODE_ROTATE:
1279 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1280 SP_VERB_CONTEXT_TWEAK, _("Rotate tweak"));
1281 break;
1282 case TWEAK_MODE_MORELESS:
1283 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1284 SP_VERB_CONTEXT_TWEAK, _("Duplicate/delete tweak"));
1285 break;
1286 case TWEAK_MODE_PUSH:
1287 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1288 SP_VERB_CONTEXT_TWEAK, _("Push path tweak"));
1289 break;
1290 case TWEAK_MODE_SHRINK_GROW:
1291 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1292 SP_VERB_CONTEXT_TWEAK, _("Shrink/grow path tweak"));
1293 break;
1294 case TWEAK_MODE_ATTRACT_REPEL:
1295 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1296 SP_VERB_CONTEXT_TWEAK, _("Attract/repel path tweak"));
1297 break;
1298 case TWEAK_MODE_ROUGHEN:
1299 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1300 SP_VERB_CONTEXT_TWEAK, _("Roughen path tweak"));
1301 break;
1302 case TWEAK_MODE_COLORPAINT:
1303 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1304 SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1305 break;
1306 case TWEAK_MODE_COLORJITTER:
1307 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1308 SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1309 break;
1310 case TWEAK_MODE_BLUR:
1311 DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1312 SP_VERB_CONTEXT_TWEAK, _("Blur tweak"));
1313 break;
1314 }
1315 }
1316 break;
1317 }
1319 case GDK_KEY_PRESS:
1320 switch (get_group0_keyval (&event->key)) {
1321 case GDK_m:
1322 case GDK_M:
1323 case GDK_0:
1324 if (MOD__SHIFT_ONLY) {
1325 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE, MOD__SHIFT);
1326 ret = TRUE;
1327 }
1328 break;
1329 case GDK_i:
1330 case GDK_I:
1331 case GDK_1:
1332 if (MOD__SHIFT_ONLY) {
1333 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_IN_OUT, MOD__SHIFT);
1334 ret = TRUE;
1335 }
1336 break;
1337 case GDK_z:
1338 case GDK_Z:
1339 case GDK_2:
1340 if (MOD__SHIFT_ONLY) {
1341 sp_tweak_switch_mode(tc, TWEAK_MODE_MOVE_JITTER, MOD__SHIFT);
1342 ret = TRUE;
1343 }
1344 break;
1345 case GDK_less:
1346 case GDK_comma:
1347 case GDK_greater:
1348 case GDK_period:
1349 case GDK_3:
1350 if (MOD__SHIFT_ONLY) {
1351 sp_tweak_switch_mode(tc, TWEAK_MODE_SCALE, MOD__SHIFT);
1352 ret = TRUE;
1353 }
1354 break;
1355 case GDK_bracketright:
1356 case GDK_bracketleft:
1357 case GDK_4:
1358 if (MOD__SHIFT_ONLY) {
1359 sp_tweak_switch_mode(tc, TWEAK_MODE_ROTATE, MOD__SHIFT);
1360 ret = TRUE;
1361 }
1362 break;
1363 case GDK_d:
1364 case GDK_D:
1365 case GDK_5:
1366 if (MOD__SHIFT_ONLY) {
1367 sp_tweak_switch_mode(tc, TWEAK_MODE_MORELESS, MOD__SHIFT);
1368 ret = TRUE;
1369 }
1370 break;
1371 case GDK_p:
1372 case GDK_P:
1373 case GDK_6:
1374 if (MOD__SHIFT_ONLY) {
1375 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH, MOD__SHIFT);
1376 ret = TRUE;
1377 }
1378 break;
1379 case GDK_s:
1380 case GDK_S:
1381 case GDK_7:
1382 if (MOD__SHIFT_ONLY) {
1383 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1384 ret = TRUE;
1385 }
1386 break;
1387 case GDK_a:
1388 case GDK_A:
1389 case GDK_8:
1390 if (MOD__SHIFT_ONLY) {
1391 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT_REPEL, MOD__SHIFT);
1392 ret = TRUE;
1393 }
1394 break;
1395 case GDK_r:
1396 case GDK_R:
1397 case GDK_9:
1398 if (MOD__SHIFT_ONLY) {
1399 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN, MOD__SHIFT);
1400 ret = TRUE;
1401 }
1402 break;
1403 case GDK_c:
1404 case GDK_C:
1405 if (MOD__SHIFT_ONLY) {
1406 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT, MOD__SHIFT);
1407 ret = TRUE;
1408 }
1409 break;
1410 case GDK_j:
1411 case GDK_J:
1412 if (MOD__SHIFT_ONLY) {
1413 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER, MOD__SHIFT);
1414 ret = TRUE;
1415 }
1416 break;
1417 case GDK_b:
1418 case GDK_B:
1419 if (MOD__SHIFT_ONLY) {
1420 sp_tweak_switch_mode(tc, TWEAK_MODE_BLUR, MOD__SHIFT);
1421 ret = TRUE;
1422 }
1423 break;
1425 case GDK_Up:
1426 case GDK_KP_Up:
1427 if (!MOD__CTRL_ONLY) {
1428 tc->force += 0.05;
1429 if (tc->force > 1.0)
1430 tc->force = 1.0;
1431 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1432 ret = TRUE;
1433 }
1434 break;
1435 case GDK_Down:
1436 case GDK_KP_Down:
1437 if (!MOD__CTRL_ONLY) {
1438 tc->force -= 0.05;
1439 if (tc->force < 0.0)
1440 tc->force = 0.0;
1441 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1442 ret = TRUE;
1443 }
1444 break;
1445 case GDK_Right:
1446 case GDK_KP_Right:
1447 if (!MOD__CTRL_ONLY) {
1448 tc->width += 0.01;
1449 if (tc->width > 1.0)
1450 tc->width = 1.0;
1451 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1452 sp_tweak_update_area(tc);
1453 ret = TRUE;
1454 }
1455 break;
1456 case GDK_Left:
1457 case GDK_KP_Left:
1458 if (!MOD__CTRL_ONLY) {
1459 tc->width -= 0.01;
1460 if (tc->width < 0.01)
1461 tc->width = 0.01;
1462 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1463 sp_tweak_update_area(tc);
1464 ret = TRUE;
1465 }
1466 break;
1467 case GDK_Home:
1468 case GDK_KP_Home:
1469 tc->width = 0.01;
1470 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1471 sp_tweak_update_area(tc);
1472 ret = TRUE;
1473 break;
1474 case GDK_End:
1475 case GDK_KP_End:
1476 tc->width = 1.0;
1477 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1478 sp_tweak_update_area(tc);
1479 ret = TRUE;
1480 break;
1481 case GDK_x:
1482 case GDK_X:
1483 if (MOD__ALT_ONLY) {
1484 desktop->setToolboxFocusTo ("altx-tweak");
1485 ret = TRUE;
1486 }
1487 break;
1489 case GDK_Shift_L:
1490 case GDK_Shift_R:
1491 sp_tweak_update_cursor(tc, true);
1492 break;
1494 case GDK_Control_L:
1495 case GDK_Control_R:
1496 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK_GROW, MOD__SHIFT);
1497 break;
1498 default:
1499 break;
1500 }
1501 break;
1503 case GDK_KEY_RELEASE: {
1504 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1505 switch (get_group0_keyval(&event->key)) {
1506 case GDK_Shift_L:
1507 case GDK_Shift_R:
1508 sp_tweak_update_cursor(tc, false);
1509 break;
1510 case GDK_Control_L:
1511 case GDK_Control_R:
1512 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1513 tc->_message_context->clear();
1514 break;
1515 default:
1516 sp_tweak_switch_mode (tc, prefs->getInt("/tools/tweak/mode"), MOD__SHIFT);
1517 break;
1518 }
1519 }
1521 default:
1522 break;
1523 }
1525 if (!ret) {
1526 if (((SPEventContextClass *) parent_class)->root_handler) {
1527 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1528 }
1529 }
1531 return ret;
1532 }
1535 /*
1536 Local Variables:
1537 mode:c++
1538 c-file-style:"stroustrup"
1539 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1540 indent-tabs-mode:nil
1541 fill-column:99
1542 End:
1543 */
1544 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :