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"
24 #include "display/bezier-utils.h"
26 #include <glib/gmem.h>
27 #include "macros.h"
28 #include "document.h"
29 #include "selection.h"
30 #include "desktop.h"
31 #include "desktop-events.h"
32 #include "desktop-handles.h"
33 #include "desktop-affine.h"
34 #include "desktop-style.h"
35 #include "message-context.h"
36 #include "pixmaps/cursor-thin.xpm"
37 #include "pixmaps/cursor-thicken.xpm"
38 #include "pixmaps/cursor-attract.xpm"
39 #include "pixmaps/cursor-repel.xpm"
40 #include "pixmaps/cursor-push.xpm"
41 #include "pixmaps/cursor-roughen.xpm"
42 #include "pixmaps/cursor-color.xpm"
43 #include "libnr/n-art-bpath.h"
44 #include "libnr/nr-path.h"
45 #include "libnr/nr-maybe.h"
46 #include "libnr/nr-matrix-ops.h"
47 #include "libnr/nr-scale-translate-ops.h"
48 #include "xml/repr.h"
49 #include "context-fns.h"
50 #include "sp-item.h"
51 #include "inkscape.h"
52 #include "color.h"
53 #include "svg/svg-color.h"
54 #include "splivarot.h"
55 #include "sp-item-group.h"
56 #include "sp-shape.h"
57 #include "sp-path.h"
58 #include "path-chemistry.h"
59 #include "sp-gradient.h"
60 #include "sp-stop.h"
61 #include "sp-stop-fns.h"
62 #include "sp-gradient-reference.h"
63 #include "sp-linear-gradient.h"
64 #include "sp-radial-gradient.h"
65 #include "gradient-chemistry.h"
66 #include "sp-text.h"
67 #include "sp-flowtext.h"
68 #include "display/canvas-bpath.h"
69 #include "display/canvas-arena.h"
70 #include "livarot/Shape.h"
71 #include "isnan.h"
72 #include "prefs-utils.h"
73 #include "style.h"
75 #include "tweak-context.h"
77 #define DDC_RED_RGBA 0xff0000ff
79 #define DYNA_MIN_WIDTH 1.0e-6
81 // FIXME: move it to some shared file to be reused by both calligraphy and dropper
82 #define C1 0.552
83 static NArtBpath const hatch_area_circle[] = {
84 { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
85 { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
86 { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
87 { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
88 { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
89 { NR_END, 0, 0, 0, 0, 0, 0 }
90 };
91 #undef C1
94 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
95 static void sp_tweak_context_init(SPTweakContext *ddc);
96 static void sp_tweak_context_dispose(GObject *object);
98 static void sp_tweak_context_setup(SPEventContext *ec);
99 static void sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
100 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
102 static SPEventContextClass *parent_class;
104 GtkType
105 sp_tweak_context_get_type(void)
106 {
107 static GType type = 0;
108 if (!type) {
109 GTypeInfo info = {
110 sizeof(SPTweakContextClass),
111 NULL, NULL,
112 (GClassInitFunc) sp_tweak_context_class_init,
113 NULL, NULL,
114 sizeof(SPTweakContext),
115 4,
116 (GInstanceInitFunc) sp_tweak_context_init,
117 NULL, /* value_table */
118 };
119 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
120 }
121 return type;
122 }
124 static void
125 sp_tweak_context_class_init(SPTweakContextClass *klass)
126 {
127 GObjectClass *object_class = (GObjectClass *) klass;
128 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
130 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
132 object_class->dispose = sp_tweak_context_dispose;
134 event_context_class->setup = sp_tweak_context_setup;
135 event_context_class->set = sp_tweak_context_set;
136 event_context_class->root_handler = sp_tweak_context_root_handler;
137 }
139 static void
140 sp_tweak_context_init(SPTweakContext *tc)
141 {
142 SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
144 event_context->cursor_shape = cursor_push_xpm;
145 event_context->hot_x = 4;
146 event_context->hot_y = 4;
148 /* attributes */
149 tc->dragging = FALSE;
151 tc->width = 0.2;
152 tc->force = 0.2;
153 tc->pressure = TC_DEFAULT_PRESSURE;
155 tc->is_dilating = false;
156 tc->has_dilated = false;
158 tc->do_h = true;
159 tc->do_s = true;
160 tc->do_l = true;
161 tc->do_o = false;
163 new (&tc->style_set_connection) sigc::connection();
164 }
166 static void
167 sp_tweak_context_dispose(GObject *object)
168 {
169 SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
171 tc->style_set_connection.disconnect();
172 tc->style_set_connection.~connection();
174 if (tc->dilate_area) {
175 gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
176 tc->dilate_area = NULL;
177 }
179 if (tc->_message_context) {
180 delete tc->_message_context;
181 }
183 G_OBJECT_CLASS(parent_class)->dispose(object);
184 }
186 void
187 sp_tweak_update_cursor (SPTweakContext *tc, gint mode)
188 {
189 SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
190 switch (mode) {
191 case TWEAK_MODE_PUSH:
192 event_context->cursor_shape = cursor_push_xpm;
193 break;
194 case TWEAK_MODE_SHRINK:
195 event_context->cursor_shape = cursor_thin_xpm;
196 break;
197 case TWEAK_MODE_GROW:
198 event_context->cursor_shape = cursor_thicken_xpm;
199 break;
200 case TWEAK_MODE_ATTRACT:
201 event_context->cursor_shape = cursor_attract_xpm;
202 break;
203 case TWEAK_MODE_REPEL:
204 event_context->cursor_shape = cursor_repel_xpm;
205 break;
206 case TWEAK_MODE_ROUGHEN:
207 event_context->cursor_shape = cursor_roughen_xpm;
208 break;
209 case TWEAK_MODE_COLORPAINT:
210 case TWEAK_MODE_COLORJITTER:
211 event_context->cursor_shape = cursor_color_xpm;
212 break;
213 }
214 sp_event_context_update_cursor(event_context);
215 }
217 static bool
218 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
219 {
220 if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
221 // we cannot store properties with uris
222 css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
224 sp_repr_css_change (inkscape_get_repr (INKSCAPE, "tools.tweak"), (SPCSSAttr *) css, "style");
226 return true;
227 }
228 return false;
229 }
232 static void
233 sp_tweak_context_setup(SPEventContext *ec)
234 {
235 SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
237 if (((SPEventContextClass *) parent_class)->setup)
238 ((SPEventContextClass *) parent_class)->setup(ec);
240 {
241 SPCurve *c = sp_curve_new_from_foreign_bpath(hatch_area_circle);
242 tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
243 sp_curve_unref(c);
244 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
245 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
246 sp_canvas_item_hide(tc->dilate_area);
247 }
249 sp_event_context_read(ec, "width");
250 sp_event_context_read(ec, "mode");
251 sp_event_context_read(ec, "fidelity");
252 sp_event_context_read(ec, "force");
253 sp_event_context_read(ec, "usepressure");
254 sp_event_context_read(ec, "doh");
255 sp_event_context_read(ec, "dol");
256 sp_event_context_read(ec, "dos");
257 sp_event_context_read(ec, "doo");
259 tc->is_drawing = false;
261 tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
263 tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
264 sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
265 );
266 }
268 static void
269 sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
270 {
271 SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
273 if (!strcmp(key, "width")) {
274 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
275 tc->width = CLAMP(dval, -1000.0, 1000.0);
276 } else if (!strcmp(key, "mode")) {
277 gint64 const dval = ( val ? g_ascii_strtoll (val, NULL, 10) : 0 );
278 tc->mode = dval;
279 sp_tweak_update_cursor(tc, tc->mode);
280 } else if (!strcmp(key, "fidelity")) {
281 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
282 tc->fidelity = CLAMP(dval, 0.0, 1.0);
283 } else if (!strcmp(key, "force")) {
284 double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
285 tc->force = CLAMP(dval, 0, 1.0);
286 } else if (!strcmp(key, "usepressure")) {
287 tc->usepressure = (val && strcmp(val, "0"));
288 } else if (!strcmp(key, "doh")) {
289 tc->do_h = (val && strcmp(val, "0"));
290 } else if (!strcmp(key, "dos")) {
291 tc->do_s = (val && strcmp(val, "0"));
292 } else if (!strcmp(key, "dol")) {
293 tc->do_l = (val && strcmp(val, "0"));
294 } else if (!strcmp(key, "doo")) {
295 tc->do_o = (val && strcmp(val, "0"));
296 }
297 }
299 static void
300 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
301 {
302 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
303 tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
304 else
305 tc->pressure = TC_DEFAULT_PRESSURE;
306 }
308 double
309 get_dilate_radius (SPTweakContext *tc)
310 {
311 // 10 times the pen width:
312 return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
313 }
315 double
316 get_dilate_force (SPTweakContext *tc)
317 {
318 double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
319 /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
320 if (force > 3) {
321 force += 4 * (force - 3);
322 }
323 return force * tc->force;
324 }
326 bool
327 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, NR::Point p, NR::Point vector, gint mode, double radius, double force, double fidelity)
328 {
329 bool did = false;
331 if (SP_IS_GROUP(item)) {
332 for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
333 if (SP_IS_ITEM(child)) {
334 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity))
335 did = true;
336 }
337 }
339 } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
341 Inkscape::XML::Node *newrepr = NULL;
342 gint pos = 0;
343 Inkscape::XML::Node *parent = NULL;
344 char const *id = NULL;
345 if (!SP_IS_PATH(item)) {
346 newrepr = sp_selected_item_to_curved_repr(item, 0);
347 if (!newrepr)
348 return false;
350 // remember the position of the item
351 pos = SP_OBJECT_REPR(item)->position();
352 // remember parent
353 parent = SP_OBJECT_REPR(item)->parent();
354 // remember id
355 id = SP_OBJECT_REPR(item)->attribute("id");
356 }
359 // skip those paths whose bboxes are entirely out of reach with our radius
360 NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item));
361 if (bbox) {
362 bbox->growBy(radius);
363 if (!bbox->contains(p)) {
364 return false;
365 }
366 }
368 Path *orig = Path_for_item(item, false);
369 if (orig == NULL) {
370 return false;
371 }
372 Path *res = new Path;
373 res->SetBackData(false);
375 Shape *theShape = new Shape;
376 Shape *theRes = new Shape;
378 orig->ConvertWithBackData(0.08 - (0.07 * fidelity)); // default 0.059
379 orig->Fill(theShape, 0);
381 SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
382 gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
383 if (val && strcmp(val, "nonzero") == 0)
384 {
385 theRes->ConvertToShape(theShape, fill_nonZero);
386 }
387 else if (val && strcmp(val, "evenodd") == 0)
388 {
389 theRes->ConvertToShape(theShape, fill_oddEven);
390 }
391 else
392 {
393 theRes->ConvertToShape(theShape, fill_nonZero);
394 }
396 if (NR::L2(vector) != 0)
397 vector = 1/NR::L2(vector) * vector;
399 bool did_this = false;
400 NR::Matrix i2doc(sp_item_i2doc_affine(item));
401 if (mode == TWEAK_MODE_SHRINK || mode == TWEAK_MODE_GROW) {
402 if (theShape->MakeOffset(theRes,
403 mode == TWEAK_MODE_GROW? force : -force,
404 join_straight, 4.0,
405 true, p[NR::X], p[NR::Y], radius, &i2doc) == 0) // 0 means the shape was actually changed
406 did_this = true;
407 } else if (mode == TWEAK_MODE_ATTRACT || mode == TWEAK_MODE_REPEL) {
408 if (theShape->MakeRepel(theRes,
409 mode == TWEAK_MODE_REPEL? force : -force,
410 join_straight, 4.0,
411 true, p[NR::X], p[NR::Y], radius, &i2doc) == 0)
412 did_this = true;
413 } else if (mode == TWEAK_MODE_PUSH) {
414 if (theShape->MakePush(theRes,
415 join_straight, 4.0,
416 true, p, force*2*vector, radius, &i2doc) == 0)
417 did_this = true;
418 } else if (mode == TWEAK_MODE_ROUGHEN) {
419 if (theShape->MakeJitter(theRes,
420 join_straight, 4.0,
421 true, p, force, radius, &i2doc) == 0)
422 did_this = true;
423 }
425 // the rest only makes sense if we actually changed the path
426 if (did_this) {
427 theRes->ConvertToShape(theShape, fill_positive);
429 res->Reset();
430 theRes->ConvertToForme(res);
432 double th_max = 0.6 - 0.59*sqrt(fidelity);
433 double threshold = MAX(th_max, th_max*force);
434 res->ConvertEvenLines(threshold);
435 res->Simplify(threshold);
437 if (newrepr) { // converting to path, need to replace the repr
438 bool is_selected = selection->includes(item);
439 if (is_selected)
440 selection->remove(item);
442 // It's going to resurrect, so we delete without notifying listeners.
443 SP_OBJECT(item)->deleteObject(false);
445 // restore id
446 newrepr->setAttribute("id", id);
447 // add the new repr to the parent
448 parent->appendChild(newrepr);
449 // move to the saved position
450 newrepr->setPosition(pos > 0 ? pos : 0);
452 if (is_selected)
453 selection->add(newrepr);
454 }
456 if (res->descr_cmd.size() > 1) {
457 gchar *str = res->svg_dump_path();
458 if (newrepr)
459 newrepr->setAttribute("d", str);
460 else
461 SP_OBJECT_REPR(item)->setAttribute("d", str);
462 g_free(str);
463 } else {
464 // TODO: if there's 0 or 1 node left, delete this path altogether
465 }
467 if (newrepr) {
468 Inkscape::GC::release(newrepr);
469 newrepr = NULL;
470 }
471 }
473 delete theShape;
474 delete theRes;
475 delete orig;
476 delete res;
478 if (did_this)
479 did = true;
480 }
482 return did;
483 }
485 void
486 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
487 {
488 float rgb_g[3];
490 if (!do_h || !do_s || !do_l) {
491 float hsl_g[3];
492 sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
493 float hsl_c[3];
494 sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
495 if (!do_h)
496 hsl_g[0] = hsl_c[0];
497 if (!do_s)
498 hsl_g[1] = hsl_c[1];
499 if (!do_l)
500 hsl_g[2] = hsl_c[2];
501 sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
502 } else {
503 rgb_g[0] = SP_RGBA32_R_F(goal);
504 rgb_g[1] = SP_RGBA32_G_F(goal);
505 rgb_g[2] = SP_RGBA32_B_F(goal);
506 }
508 for (int i = 0; i < 3; i++) {
509 double d = rgb_g[i] - color[i];
510 color[i] += d * force;
511 }
512 }
514 void
515 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
516 {
517 force = force*force; // in [0..1], this makes finer adjustments easier
519 float hsl_c[3];
520 sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
522 if (do_h) {
523 hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
524 if (hsl_c[0] > 1)
525 hsl_c[0] -= 1;
526 if (hsl_c[0] < 0)
527 hsl_c[0] += 1;
528 }
529 if (do_s) {
530 hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
531 }
532 if (do_l) {
533 hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
534 }
536 sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
537 }
539 void
540 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
541 {
542 if (mode == TWEAK_MODE_COLORPAINT) {
543 tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
544 } else if (mode == TWEAK_MODE_COLORJITTER) {
545 tweak_colorjitter (color, force, do_h, do_s, do_l);
546 }
547 }
549 void
550 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
551 {
552 double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
554 if (mode == TWEAK_MODE_COLORPAINT) {
555 double d = opacity_goal - opacity;
556 opacity += d * force;
557 } else if (mode == TWEAK_MODE_COLORJITTER) {
558 opacity += g_random_double_range(-opacity, 1 - opacity) * force;
559 }
561 style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
562 }
565 double
566 tweak_profile (double dist, double radius)
567 {
568 if (radius == 0)
569 return 0;
570 double x = dist / radius;
571 double alpha = 1;
572 if (x >= 1) {
573 return 0;
574 } else if (x <= 0) {
575 return 1;
576 } else {
577 return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
578 }
579 }
581 void
582 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
583 guint32 const rgb_goal, NR::Point p_w, double radius, double force, guint mode,
584 bool do_h, bool do_s, bool do_l, bool do_o)
585 {
586 SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
588 if (!gradient || !SP_IS_GRADIENT(gradient))
589 return;
591 NR::Matrix i2d = sp_item_i2doc_affine (item);
592 NR::Point p = p_w * i2d.inverse();
593 p *= (gradient->gradientTransform).inverse();
594 // now p is in gradient's original coordinates
596 double pos = 0;
597 double r = 0;
598 if (SP_IS_LINEARGRADIENT(gradient)) {
599 SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
601 NR::Point p1(lg->x1.computed, lg->y1.computed);
602 NR::Point p2(lg->x2.computed, lg->y2.computed);
603 NR::Point pdiff(p2 - p1);
604 double vl = NR::L2(pdiff);
606 // This is the matrix which moves and rotates the gradient line
607 // so it's oriented along the X axis:
608 NR::Matrix norm = NR::Matrix(NR::translate(-p1)) * NR::Matrix(NR::rotate(-atan2(pdiff[NR::Y], pdiff[NR::X])));
610 // Transform the mouse point by it to find out its projection onto the gradient line:
611 NR::Point pnorm = p * norm;
613 // Scale its X coordinate to match the length of the gradient line:
614 pos = pnorm[NR::X] / vl;
615 // Calculate radius in lenfth-of-gradient-line units
616 r = radius / vl;
618 } else if (SP_IS_RADIALGRADIENT(gradient)) {
619 SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
620 NR::Point c (rg->cx.computed, rg->cy.computed);
621 pos = NR::L2(p - c) / rg->r.computed;
622 r = radius / rg->r.computed;
623 }
625 // Normalize pos to 0..1, taking into accound gradient spread:
626 double pos_e = pos;
627 if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
628 if (pos > 1)
629 pos_e = 1;
630 if (pos < 0)
631 pos_e = 0;
632 } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
633 if (pos > 1 || pos < 0)
634 pos_e = pos - floor(pos);
635 } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
636 if (pos > 1 || pos < 0) {
637 bool odd = ((int)(floor(pos)) % 2 == 1);
638 pos_e = pos - floor(pos);
639 if (odd)
640 pos_e = 1 - pos_e;
641 }
642 }
644 SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
646 double offset_l = 0;
647 double offset_h = 0;
648 SPObject *child_prev = NULL;
649 for (SPObject *child = sp_object_first_child(vector);
650 child != NULL; child = SP_OBJECT_NEXT(child)) {
651 if (!SP_IS_STOP(child))
652 continue;
653 SPStop *stop = SP_STOP (child);
655 offset_h = stop->offset;
657 if (child_prev) {
659 if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
660 // the summit falls in this interstop, and the radius is small,
661 // so it only affects the ends of this interstop;
662 // distribute the force between the two endstops so that they
663 // get all the painting even if they are not touched by the brush
664 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
665 force * (pos_e - offset_l) / (offset_h - offset_l),
666 do_h, do_s, do_l);
667 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
668 force * (offset_h - pos_e) / (offset_h - offset_l),
669 do_h, do_s, do_l);
670 SP_OBJECT(stop)->updateRepr();
671 child_prev->updateRepr();
672 break;
673 } else {
674 // wide brush, may affect more than 2 stops,
675 // paint each stop by the force from the profile curve
676 if (offset_l <= pos_e && offset_l > pos_e - r) {
677 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
678 force * tweak_profile (fabs (pos_e - offset_l), r),
679 do_h, do_s, do_l);
680 child_prev->updateRepr();
681 }
683 if (offset_h >= pos_e && offset_h < pos_e + r) {
684 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
685 force * tweak_profile (fabs (pos_e - offset_h), r),
686 do_h, do_s, do_l);
687 SP_OBJECT(stop)->updateRepr();
688 }
689 }
690 }
692 offset_l = offset_h;
693 child_prev = child;
694 }
695 }
697 bool
698 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
699 guint32 fill_goal, bool do_fill,
700 guint32 stroke_goal, bool do_stroke,
701 float opacity_goal, bool do_opacity,
702 NR::Point p, double radius, double force,
703 bool do_h, bool do_s, bool do_l, bool do_o)
704 {
705 bool did = false;
707 if (SP_IS_GROUP(item)) {
708 for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
709 if (SP_IS_ITEM(child)) {
710 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
711 fill_goal, do_fill,
712 stroke_goal, do_stroke,
713 opacity_goal, do_opacity,
714 p, radius, force, do_h, do_s, do_l, do_o));
715 did = true;
716 }
717 }
719 } else {
720 SPStyle *style = SP_OBJECT_STYLE(item);
721 if (!style) {
722 return false;
723 }
724 NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item),
725 SPItem::GEOMETRIC_BBOX);
726 if (!bbox) {
727 return false;
728 }
730 NR::Rect brush(p - NR::Point(radius, radius), p + NR::Point(radius, radius));
732 NR::Point center = bbox->midpoint();
733 double this_force;
735 // if item == item_at_point, use max force
736 if (item == item_at_point) {
737 this_force = force;
738 // else if no overlap of bbox and brush box, skip:
739 } else if (!bbox->intersects(brush)) {
740 return false;
741 //TODO:
742 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
743 //} else if (bbox->maxExtent() > 3 * radius) {
744 //}
745 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
746 // else if still smaller, then check only the object center:
747 } else {
748 this_force = force * tweak_profile (NR::L2 (p - center), radius);
749 }
751 if (this_force > 0.002) {
753 if (do_fill) {
754 if (style->fill.type == SP_PAINT_TYPE_COLOR) {
755 tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
756 item->updateRepr();
757 did = true;
758 } else if (style->fill.type == SP_PAINT_TYPE_PAINTSERVER) {
759 tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
760 did = true;
761 }
762 }
763 if (do_stroke) {
764 if (style->stroke.type == SP_PAINT_TYPE_COLOR) {
765 tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
766 item->updateRepr();
767 did = true;
768 } else if (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER) {
769 tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
770 did = true;
771 }
772 }
773 if (do_opacity && do_o) {
774 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
775 }
776 }
777 }
779 return did;
780 }
783 bool
784 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector)
785 {
786 Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
788 if (selection->isEmpty()) {
789 return false;
790 }
792 bool did = false;
793 double radius = get_dilate_radius(tc);
794 double force = get_dilate_force(tc);
795 if (radius == 0 || force == 0) {
796 return false;
797 }
798 double color_force = MIN(sqrt(force)/4.0, 1);
800 SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
801 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.tweak");
802 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
803 if (tc->mode == TWEAK_MODE_COLORPAINT && !css)
804 return false;
806 bool do_fill = false, do_stroke = false, do_opacity = false;
807 guint32 fill_goal = 0, stroke_goal = 0;
808 float opacity_goal = 1;
810 const gchar *fill_prop = sp_repr_css_property(css, "fill", NULL);
811 if (fill_prop && strcmp(fill_prop, "none")) {
812 do_fill = true;
813 fill_goal = sp_svg_read_color(fill_prop, 0);
814 }
815 const gchar *stroke_prop = sp_repr_css_property(css, "stroke", NULL);
816 if (stroke_prop && strcmp(stroke_prop, "none")) {
817 do_stroke = true;
818 stroke_goal = sp_svg_read_color(stroke_prop, 0);
819 }
820 const gchar *opacity_prop = sp_repr_css_property(css, "opacity", NULL);
821 if (opacity_prop) {
822 do_opacity = true;
823 sp_svg_number_read_f(opacity_prop, &opacity_goal);
824 }
826 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
827 items != NULL;
828 items = items->next) {
830 SPItem *item = (SPItem *) items->data;
832 if (tc->mode == TWEAK_MODE_COLORPAINT || tc->mode == TWEAK_MODE_COLORJITTER) {
833 if (do_fill || do_stroke || do_opacity) {
834 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
835 fill_goal, do_fill,
836 stroke_goal, do_stroke,
837 opacity_goal, do_opacity,
838 p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
839 did = true;
840 }
841 } else {
842 if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, force, tc->fidelity))
843 did = true;
844 }
845 }
847 return did;
848 }
850 void
851 sp_tweak_update_area (SPTweakContext *tc)
852 {
853 double radius = get_dilate_radius(tc);
854 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
855 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
856 sp_canvas_item_show(tc->dilate_area);
857 }
859 void
860 sp_tweak_switch_mode (SPTweakContext *tc, gint mode)
861 {
862 SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
863 // need to set explicitly, because the prefs may not have changed by the previous
864 tc->mode = mode;
865 sp_tweak_update_cursor (tc, mode);
866 }
868 void
869 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode)
870 {
871 // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
872 gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
873 SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
874 // button has changed prefs, restore
875 prefs_set_int_attribute("tools.tweak", "mode", now_mode);
876 // changing prefs changed tc->mode, restore back :)
877 tc->mode = mode;
878 sp_tweak_update_cursor (tc, mode);
879 }
881 gint
882 sp_tweak_context_root_handler(SPEventContext *event_context,
883 GdkEvent *event)
884 {
885 SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
886 SPDesktop *desktop = event_context->desktop;
888 gint ret = FALSE;
890 switch (event->type) {
891 case GDK_ENTER_NOTIFY:
892 sp_canvas_item_show(tc->dilate_area);
893 break;
894 case GDK_LEAVE_NOTIFY:
895 sp_canvas_item_hide(tc->dilate_area);
896 break;
897 case GDK_BUTTON_PRESS:
898 if (event->button.button == 1 && !event_context->space_panning) {
900 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
901 return TRUE;
902 }
904 NR::Point const button_w(event->button.x,
905 event->button.y);
906 NR::Point const button_dt(desktop->w2d(button_w));
907 tc->last_push = desktop->dt2doc(button_dt);
909 sp_tweak_extinput(tc, event);
911 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
912 tc->is_drawing = true;
913 tc->is_dilating = true;
914 tc->has_dilated = false;
916 ret = TRUE;
917 }
918 break;
919 case GDK_MOTION_NOTIFY:
920 {
921 NR::Point const motion_w(event->motion.x,
922 event->motion.y);
923 NR::Point motion_dt(desktop->w2d(motion_w));
924 NR::Point motion_doc(desktop->dt2doc(motion_dt));
925 sp_tweak_extinput(tc, event);
927 // draw the dilating cursor
928 double radius = get_dilate_radius(tc);
929 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
930 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
931 sp_canvas_item_show(tc->dilate_area);
933 guint num = 0;
934 if (!desktop->selection->isEmpty()) {
935 num = g_slist_length((GSList *) desktop->selection->itemList());
936 }
937 if (num == 0) {
938 tc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Select objects</b> to tweak"));
939 } else {
940 switch (tc->mode) {
941 case TWEAK_MODE_PUSH:
942 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
943 _("<b>Pushing %d</b> selected object(s)"), num);
944 break;
945 case TWEAK_MODE_SHRINK:
946 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
947 _("<b>Shrinking %d</b> selected object(s)"), num);
948 break;
949 case TWEAK_MODE_GROW:
950 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
951 _("<b>Growing %d</b> selected object(s)"), num);
952 break;
953 case TWEAK_MODE_ATTRACT:
954 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
955 _("<b>Attracting %d</b> selected object(s)"), num);
956 break;
957 case TWEAK_MODE_REPEL:
958 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
959 _("<b>Repelling %d</b> selected object(s)"), num);
960 break;
961 case TWEAK_MODE_ROUGHEN:
962 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
963 _("<b>Roughening %d</b> selected object(s)"), num);
964 case TWEAK_MODE_COLORPAINT:
965 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
966 _("<b>Painting %d</b> selected object(s)"), num);
967 break;
968 case TWEAK_MODE_COLORJITTER:
969 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
970 _("<b>Jittering colors in %d</b> selected object(s)"), num);
971 break;
972 }
973 }
976 // dilating:
977 if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
978 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push);
979 //tc->last_push = motion_doc;
980 tc->has_dilated = true;
981 // it's slow, so prevent clogging up with events
982 gobble_motion_events(GDK_BUTTON1_MASK);
983 return TRUE;
984 }
986 }
987 break;
990 case GDK_BUTTON_RELEASE:
991 {
992 NR::Point const motion_w(event->button.x, event->button.y);
993 NR::Point const motion_dt(desktop->w2d(motion_w));
995 sp_canvas_end_forced_full_redraws(desktop->canvas);
996 tc->is_drawing = false;
998 if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
999 if (!tc->has_dilated) {
1000 // if we did not rub, do a light tap
1001 tc->pressure = 0.03;
1002 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0));
1003 }
1004 tc->is_dilating = false;
1005 tc->has_dilated = false;
1006 switch (tc->mode) {
1007 case TWEAK_MODE_PUSH:
1008 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1009 SP_VERB_CONTEXT_TWEAK, _("Push tweak"));
1010 break;
1011 case TWEAK_MODE_SHRINK:
1012 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1013 SP_VERB_CONTEXT_TWEAK, _("Shrink tweak"));
1014 break;
1015 case TWEAK_MODE_GROW:
1016 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1017 SP_VERB_CONTEXT_TWEAK, _("Grow tweak"));
1018 break;
1019 case TWEAK_MODE_ATTRACT:
1020 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1021 SP_VERB_CONTEXT_TWEAK, _("Attract tweak"));
1022 break;
1023 case TWEAK_MODE_REPEL:
1024 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1025 SP_VERB_CONTEXT_TWEAK, _("Repel tweak"));
1026 break;
1027 case TWEAK_MODE_ROUGHEN:
1028 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1029 SP_VERB_CONTEXT_TWEAK, _("Roughen tweak"));
1030 break;
1031 case TWEAK_MODE_COLORPAINT:
1032 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1033 SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1034 case TWEAK_MODE_COLORJITTER:
1035 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1036 SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1037 break;
1038 }
1039 }
1040 break;
1041 }
1043 case GDK_KEY_PRESS:
1044 switch (get_group0_keyval (&event->key)) {
1045 case GDK_p:
1046 case GDK_P:
1047 case GDK_1:
1048 if (MOD__SHIFT_ONLY) {
1049 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH);
1050 ret = TRUE;
1051 }
1052 break;
1053 case GDK_s:
1054 case GDK_S:
1055 case GDK_2:
1056 if (MOD__SHIFT_ONLY) {
1057 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK);
1058 ret = TRUE;
1059 }
1060 break;
1061 case GDK_g:
1062 case GDK_G:
1063 case GDK_3:
1064 if (MOD__SHIFT_ONLY) {
1065 sp_tweak_switch_mode(tc, TWEAK_MODE_GROW);
1066 ret = TRUE;
1067 }
1068 break;
1069 case GDK_a:
1070 case GDK_A:
1071 case GDK_4:
1072 if (MOD__SHIFT_ONLY) {
1073 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT);
1074 ret = TRUE;
1075 }
1076 break;
1077 case GDK_e:
1078 case GDK_E:
1079 case GDK_5:
1080 if (MOD__SHIFT_ONLY) {
1081 sp_tweak_switch_mode(tc, TWEAK_MODE_REPEL);
1082 ret = TRUE;
1083 }
1084 break;
1085 case GDK_r:
1086 case GDK_R:
1087 case GDK_6:
1088 if (MOD__SHIFT_ONLY) {
1089 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN);
1090 ret = TRUE;
1091 }
1092 break;
1093 case GDK_c:
1094 case GDK_C:
1095 case GDK_7:
1096 if (MOD__SHIFT_ONLY) {
1097 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT);
1098 ret = TRUE;
1099 }
1100 break;
1101 case GDK_j:
1102 case GDK_J:
1103 case GDK_8:
1104 if (MOD__SHIFT_ONLY) {
1105 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER);
1106 ret = TRUE;
1107 }
1108 break;
1110 case GDK_Up:
1111 case GDK_KP_Up:
1112 if (!MOD__CTRL_ONLY) {
1113 tc->force += 0.05;
1114 if (tc->force > 1.0)
1115 tc->force = 1.0;
1116 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1117 ret = TRUE;
1118 }
1119 break;
1120 case GDK_Down:
1121 case GDK_KP_Down:
1122 if (!MOD__CTRL_ONLY) {
1123 tc->force -= 0.05;
1124 if (tc->force < 0.0)
1125 tc->force = 0.0;
1126 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1127 ret = TRUE;
1128 }
1129 break;
1130 case GDK_Right:
1131 case GDK_KP_Right:
1132 if (!MOD__CTRL_ONLY) {
1133 tc->width += 0.01;
1134 if (tc->width > 1.0)
1135 tc->width = 1.0;
1136 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1137 sp_tweak_update_area(tc);
1138 ret = TRUE;
1139 }
1140 break;
1141 case GDK_Left:
1142 case GDK_KP_Left:
1143 if (!MOD__CTRL_ONLY) {
1144 tc->width -= 0.01;
1145 if (tc->width < 0.01)
1146 tc->width = 0.01;
1147 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1148 sp_tweak_update_area(tc);
1149 ret = TRUE;
1150 }
1151 break;
1152 case GDK_Home:
1153 case GDK_KP_Home:
1154 tc->width = 0.01;
1155 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1156 sp_tweak_update_area(tc);
1157 ret = TRUE;
1158 break;
1159 case GDK_End:
1160 case GDK_KP_End:
1161 tc->width = 1.0;
1162 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1163 sp_tweak_update_area(tc);
1164 ret = TRUE;
1165 break;
1166 case GDK_x:
1167 case GDK_X:
1168 if (MOD__ALT_ONLY) {
1169 desktop->setToolboxFocusTo ("altx-tweak");
1170 ret = TRUE;
1171 }
1172 break;
1174 case GDK_Control_L:
1175 case GDK_Control_R:
1176 if (MOD__SHIFT) {
1177 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1178 } else {
1179 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1180 }
1181 break;
1182 case GDK_Shift_L:
1183 case GDK_Shift_R:
1184 if (MOD__CTRL) {
1185 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1186 }
1187 break;
1188 default:
1189 break;
1190 }
1191 break;
1193 case GDK_KEY_RELEASE:
1194 switch (get_group0_keyval(&event->key)) {
1195 case GDK_Control_L:
1196 case GDK_Control_R:
1197 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1198 tc->_message_context->clear();
1199 break;
1200 case GDK_Shift_L:
1201 case GDK_Shift_R:
1202 if (MOD__CTRL) {
1203 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1204 }
1205 break;
1206 break;
1207 default:
1208 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1209 break;
1210 }
1212 default:
1213 break;
1214 }
1216 if (!ret) {
1217 if (((SPEventContextClass *) parent_class)->root_handler) {
1218 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1219 }
1220 }
1222 return ret;
1223 }
1226 /*
1227 Local Variables:
1228 mode:c++
1229 c-file-style:"stroustrup"
1230 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1231 indent-tabs-mode:nil
1232 fill-column:99
1233 End:
1234 */
1235 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :