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