Code

Convert 3D boxes to ordinary groups before tweaking, ungrouping or applying 'convert...
[inkscape.git] / src / tweak-context.cpp
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"
74 #include "box3d.h"
76 #include "tweak-context.h"
78 #define DDC_RED_RGBA 0xff0000ff
80 #define DYNA_MIN_WIDTH 1.0e-6
82 // FIXME: move it to some shared file to be reused by both calligraphy and dropper
83 #define C1 0.552
84 static NArtBpath const hatch_area_circle[] = {
85     { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
86     { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
87     { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
88     { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
89     { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
90     { NR_END, 0, 0, 0, 0, 0, 0 }
91 };
92 #undef C1
95 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
96 static void sp_tweak_context_init(SPTweakContext *ddc);
97 static void sp_tweak_context_dispose(GObject *object);
99 static void sp_tweak_context_setup(SPEventContext *ec);
100 static void sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
101 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
103 static SPEventContextClass *parent_class;
105 GtkType
106 sp_tweak_context_get_type(void)
108     static GType type = 0;
109     if (!type) {
110         GTypeInfo info = {
111             sizeof(SPTweakContextClass),
112             NULL, NULL,
113             (GClassInitFunc) sp_tweak_context_class_init,
114             NULL, NULL,
115             sizeof(SPTweakContext),
116             4,
117             (GInstanceInitFunc) sp_tweak_context_init,
118             NULL,   /* value_table */
119         };
120         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
121     }
122     return type;
125 static void
126 sp_tweak_context_class_init(SPTweakContextClass *klass)
128     GObjectClass *object_class = (GObjectClass *) klass;
129     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
131     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
133     object_class->dispose = sp_tweak_context_dispose;
135     event_context_class->setup = sp_tweak_context_setup;
136     event_context_class->set = sp_tweak_context_set;
137     event_context_class->root_handler = sp_tweak_context_root_handler;
140 static void
141 sp_tweak_context_init(SPTweakContext *tc)
143     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
145     event_context->cursor_shape = cursor_push_xpm;
146     event_context->hot_x = 4;
147     event_context->hot_y = 4;
149     /* attributes */
150     tc->dragging = FALSE;
152     tc->width = 0.2;
153     tc->force = 0.2;
154     tc->pressure = TC_DEFAULT_PRESSURE;
156     tc->is_dilating = false;
157     tc->has_dilated = false;
159     tc->do_h = true;
160     tc->do_s = true;
161     tc->do_l = true;
162     tc->do_o = false;
164     new (&tc->style_set_connection) sigc::connection();
167 static void
168 sp_tweak_context_dispose(GObject *object)
170     SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
172     tc->style_set_connection.disconnect();
173     tc->style_set_connection.~connection();
175     if (tc->dilate_area) {
176         gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
177         tc->dilate_area = NULL;
178     }
180     if (tc->_message_context) {
181         delete tc->_message_context;
182     }
184     G_OBJECT_CLASS(parent_class)->dispose(object);
187 void
188 sp_tweak_update_cursor (SPTweakContext *tc, gint mode)
190     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
191    switch (mode) {
192        case TWEAK_MODE_PUSH:
193            event_context->cursor_shape = cursor_push_xpm;
194            break;
195        case TWEAK_MODE_SHRINK:
196            event_context->cursor_shape = cursor_thin_xpm;
197            break;
198        case TWEAK_MODE_GROW:
199            event_context->cursor_shape = cursor_thicken_xpm;
200            break;
201        case TWEAK_MODE_ATTRACT:
202            event_context->cursor_shape = cursor_attract_xpm;
203            break;
204        case TWEAK_MODE_REPEL:
205            event_context->cursor_shape = cursor_repel_xpm;
206            break;
207        case TWEAK_MODE_ROUGHEN:
208            event_context->cursor_shape = cursor_roughen_xpm;
209            break;
210        case TWEAK_MODE_COLORPAINT:
211        case TWEAK_MODE_COLORJITTER:
212            event_context->cursor_shape = cursor_color_xpm;
213            break;
214    }
215    sp_event_context_update_cursor(event_context);
218 static bool
219 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
221     if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
222         // we cannot store properties with uris
223         css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
225         sp_repr_css_change (inkscape_get_repr (INKSCAPE, "tools.tweak"), (SPCSSAttr *) css, "style");
227         return true;
228     }
229     return false;
233 static void
234 sp_tweak_context_setup(SPEventContext *ec)
236     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
238     if (((SPEventContextClass *) parent_class)->setup)
239         ((SPEventContextClass *) parent_class)->setup(ec);
241     {
242         SPCurve *c = sp_curve_new_from_foreign_bpath(hatch_area_circle);
243         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
244         sp_curve_unref(c);
245         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
246         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
247         sp_canvas_item_hide(tc->dilate_area);
248     }
250     sp_event_context_read(ec, "width");
251     sp_event_context_read(ec, "mode");
252     sp_event_context_read(ec, "fidelity");
253     sp_event_context_read(ec, "force");
254     sp_event_context_read(ec, "usepressure");
255     sp_event_context_read(ec, "doh");
256     sp_event_context_read(ec, "dol");
257     sp_event_context_read(ec, "dos");
258     sp_event_context_read(ec, "doo");
260     tc->is_drawing = false;
262     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
264     tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
265         sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
266     );
269 static void
270 sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
272     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
274     if (!strcmp(key, "width")) {
275         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
276         tc->width = CLAMP(dval, -1000.0, 1000.0);
277     } else if (!strcmp(key, "mode")) {
278         gint64 const dval = ( val ? g_ascii_strtoll (val, NULL, 10) : 0 );
279         tc->mode = dval;
280         sp_tweak_update_cursor(tc, tc->mode);
281     } else if (!strcmp(key, "fidelity")) {
282         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
283         tc->fidelity = CLAMP(dval, 0.0, 1.0);
284     } else if (!strcmp(key, "force")) {
285         double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
286         tc->force = CLAMP(dval, 0, 1.0);
287     } else if (!strcmp(key, "usepressure")) {
288         tc->usepressure = (val && strcmp(val, "0"));
289     } else if (!strcmp(key, "doh")) {
290         tc->do_h = (val && strcmp(val, "0"));
291     } else if (!strcmp(key, "dos")) {
292         tc->do_s = (val && strcmp(val, "0"));
293     } else if (!strcmp(key, "dol")) {
294         tc->do_l = (val && strcmp(val, "0"));
295     } else if (!strcmp(key, "doo")) {
296         tc->do_o = (val && strcmp(val, "0"));
297     }
300 static void
301 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
303     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
304         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
305     else
306         tc->pressure = TC_DEFAULT_PRESSURE;
309 double
310 get_dilate_radius (SPTweakContext *tc)
312     // 10 times the pen width:
313     return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
316 double
317 get_dilate_force (SPTweakContext *tc)
319     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
320         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
321     if (force > 3) {
322         force += 4 * (force - 3);
323     }
324     return force * tc->force;
327 bool
328 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, NR::Point p, NR::Point vector, gint mode, double radius, double force, double fidelity)
330     bool did = false;
332     if (SP_IS_GROUP(item)) {
333         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
334             if (SP_IS_ITEM(child)) {
335                 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity))
336                     did = true;
337             }
338         }
340     } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
342         Inkscape::XML::Node *newrepr = NULL;
343         gint pos = 0;
344         Inkscape::XML::Node *parent = NULL;
345         char const *id = NULL;
346         if (!SP_IS_PATH(item)) {
347             newrepr = sp_selected_item_to_curved_repr(item, 0);
348             if (!newrepr)
349                 return false;
351             // remember the position of the item
352             pos = SP_OBJECT_REPR(item)->position();
353             // remember parent
354             parent = SP_OBJECT_REPR(item)->parent();
355             // remember id
356             id = SP_OBJECT_REPR(item)->attribute("id");
357         }
360         // skip those paths whose bboxes are entirely out of reach with our radius
361         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item));
362         if (bbox) {
363             bbox->growBy(radius);
364             if (!bbox->contains(p)) {
365                 return false;
366             }
367         }
369         Path *orig = Path_for_item(item, false);
370         if (orig == NULL) {
371             return false;
372         }
374         Path *res = new Path;
375         res->SetBackData(false);
377         Shape *theShape = new Shape;
378         Shape *theRes = new Shape;
380         orig->ConvertWithBackData(0.08 - (0.07 * fidelity)); // default 0.059
381         orig->Fill(theShape, 0);
383         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
384         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
385         if (val && strcmp(val, "nonzero") == 0)
386         {
387             theRes->ConvertToShape(theShape, fill_nonZero);
388         }
389         else if (val && strcmp(val, "evenodd") == 0)
390         {
391             theRes->ConvertToShape(theShape, fill_oddEven);
392         }
393         else
394         {
395             theRes->ConvertToShape(theShape, fill_nonZero);
396         }
398         if (NR::L2(vector) != 0)
399             vector = 1/NR::L2(vector) * vector;
401         bool did_this = false;
402         NR::Matrix i2doc(sp_item_i2doc_affine(item));
403         if (mode == TWEAK_MODE_SHRINK || mode == TWEAK_MODE_GROW) {
404             if (theShape->MakeTweak(tweak_mode_grow, theRes,
405                                  mode == TWEAK_MODE_GROW? force : -force,
406                                  join_straight, 4.0,
407                                  true, p, NR::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
408               did_this = true;
409         } else if (mode == TWEAK_MODE_ATTRACT || mode == TWEAK_MODE_REPEL) {
410             if (theShape->MakeTweak(tweak_mode_repel, theRes,
411                                  mode == TWEAK_MODE_REPEL? force : -force,
412                                  join_straight, 4.0,
413                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
414               did_this = true;
415         } else if (mode == TWEAK_MODE_PUSH) {
416             if (theShape->MakeTweak(tweak_mode_push, theRes,
417                                  0,
418                                  join_straight, 4.0,
419                                  true, p, force*2*vector, radius, &i2doc) == 0)
420               did_this = true;
421         } else if (mode == TWEAK_MODE_ROUGHEN) {
422             if (theShape->MakeTweak(tweak_mode_roughen, theRes,
423                                  force,
424                                  join_straight, 4.0,
425                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
426               did_this = true;
427         }
429         // the rest only makes sense if we actually changed the path
430         if (did_this) {
431             theRes->ConvertToShape(theShape, fill_positive);
433             res->Reset();
434             theRes->ConvertToForme(res);
436             double th_max = 0.6 - 0.59*sqrt(fidelity);
437             double threshold = MAX(th_max, th_max*force);
438             res->ConvertEvenLines(threshold);
439             res->Simplify(threshold);
441             if (newrepr) { // converting to path, need to replace the repr
442                 bool is_selected = selection->includes(item);
443                 if (is_selected)
444                     selection->remove(item);
446                 // It's going to resurrect, so we delete without notifying listeners.
447                 SP_OBJECT(item)->deleteObject(false);
449                 // restore id
450                 newrepr->setAttribute("id", id);
451                 // add the new repr to the parent
452                 parent->appendChild(newrepr);
453                 // move to the saved position
454                 newrepr->setPosition(pos > 0 ? pos : 0);
456                 if (is_selected)
457                     selection->add(newrepr);
458             }
460             if (res->descr_cmd.size() > 1) {
461                 gchar *str = res->svg_dump_path();
462                 if (newrepr) {
463                     newrepr->setAttribute("d", str);
464                 } else {
465                     if (SP_IS_SHAPE(item) && SP_SHAPE(item)->path_effect_href) {
466                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
467                     } else {
468                         SP_OBJECT_REPR(item)->setAttribute("d", str);
469                     }
470                 }
471                 g_free(str);
472             } else {
473                 // TODO: if there's 0 or 1 node left, delete this path altogether
474             }
476             if (newrepr) {
477                 Inkscape::GC::release(newrepr);
478                 newrepr = NULL;
479             }
480         }
482         delete theShape;
483         delete theRes;
484         delete orig;
485         delete res;
487         if (did_this)
488             did = true;
489     }
491     return did;
494 void
495 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
497     float rgb_g[3];
499     if (!do_h || !do_s || !do_l) {
500         float hsl_g[3];
501         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
502         float hsl_c[3];
503         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
504         if (!do_h)
505             hsl_g[0] = hsl_c[0];
506         if (!do_s)
507             hsl_g[1] = hsl_c[1];
508         if (!do_l)
509             hsl_g[2] = hsl_c[2];
510         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
511     } else {
512         rgb_g[0] = SP_RGBA32_R_F(goal);
513         rgb_g[1] = SP_RGBA32_G_F(goal);
514         rgb_g[2] = SP_RGBA32_B_F(goal);
515     }
517     for (int i = 0; i < 3; i++) {
518         double d = rgb_g[i] - color[i];
519         color[i] += d * force;
520     }
523 void
524 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
526     float hsl_c[3];
527     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
529     if (do_h) {
530         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
531         if (hsl_c[0] > 1)
532             hsl_c[0] -= 1;
533         if (hsl_c[0] < 0)
534             hsl_c[0] += 1;
535     }
536     if (do_s) {
537         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
538     }
539     if (do_l) {
540         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
541     }
543     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
546 void
547 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
549     if (mode == TWEAK_MODE_COLORPAINT) {
550         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
551     } else if (mode == TWEAK_MODE_COLORJITTER) {
552         tweak_colorjitter (color, force, do_h, do_s, do_l);
553     }
556 void
557 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
559     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
561     if (mode == TWEAK_MODE_COLORPAINT) {
562         double d = opacity_goal - opacity;
563         opacity += d * force;
564     } else if (mode == TWEAK_MODE_COLORJITTER) {
565         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
566     }
568     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
572 double
573 tweak_profile (double dist, double radius)
575     if (radius == 0)
576         return 0;
577     double x = dist / radius;
578     double alpha = 1;
579     if (x >= 1) {
580         return 0;
581     } else if (x <= 0) {
582         return 1;
583     } else {
584         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
585     }
588 void
589 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
590                           guint32 const rgb_goal, NR::Point p_w, double radius, double force, guint mode,
591                           bool do_h, bool do_s, bool do_l, bool /*do_o*/)
593     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
595     if (!gradient || !SP_IS_GRADIENT(gradient))
596         return;
598     NR::Matrix i2d = sp_item_i2doc_affine (item);
599     NR::Point p = p_w * i2d.inverse();
600     p *= (gradient->gradientTransform).inverse();
601     // now p is in gradient's original coordinates
603     double pos = 0;
604     double r = 0;
605     if (SP_IS_LINEARGRADIENT(gradient)) {
606         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
608         NR::Point p1(lg->x1.computed, lg->y1.computed);
609         NR::Point p2(lg->x2.computed, lg->y2.computed);
610         NR::Point pdiff(p2 - p1);
611         double vl = NR::L2(pdiff);
613         // This is the matrix which moves and rotates the gradient line
614         // so it's oriented along the X axis:
615         NR::Matrix norm = NR::Matrix(NR::translate(-p1)) * NR::Matrix(NR::rotate(-atan2(pdiff[NR::Y], pdiff[NR::X])));
617         // Transform the mouse point by it to find out its projection onto the gradient line:
618         NR::Point pnorm = p * norm;
620         // Scale its X coordinate to match the length of the gradient line:
621         pos = pnorm[NR::X] / vl;
622         // Calculate radius in lenfth-of-gradient-line units
623         r = radius / vl;
625     } else if (SP_IS_RADIALGRADIENT(gradient)) {
626         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
627         NR::Point c (rg->cx.computed, rg->cy.computed);
628         pos = NR::L2(p - c) / rg->r.computed;
629         r = radius / rg->r.computed;
630     }
632     // Normalize pos to 0..1, taking into accound gradient spread:
633     double pos_e = pos;
634     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
635         if (pos > 1)
636             pos_e = 1;
637         if (pos < 0)
638             pos_e = 0;
639     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
640         if (pos > 1 || pos < 0)
641             pos_e = pos - floor(pos);
642     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
643         if (pos > 1 || pos < 0) {
644             bool odd = ((int)(floor(pos)) % 2 == 1);
645             pos_e = pos - floor(pos);
646             if (odd)
647                 pos_e = 1 - pos_e;
648         }
649     }
651     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
653     double offset_l = 0;
654     double offset_h = 0;
655     SPObject *child_prev = NULL;
656     for (SPObject *child = sp_object_first_child(vector);
657          child != NULL; child = SP_OBJECT_NEXT(child)) {
658         if (!SP_IS_STOP(child))
659             continue;
660         SPStop *stop = SP_STOP (child);
662         offset_h = stop->offset;
664         if (child_prev) {
666             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
667                 // the summit falls in this interstop, and the radius is small,
668                 // so it only affects the ends of this interstop;
669                 // distribute the force between the two endstops so that they
670                 // get all the painting even if they are not touched by the brush
671                 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
672                                   force * (pos_e - offset_l) / (offset_h - offset_l),
673                                   do_h, do_s, do_l);
674                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
675                                   force * (offset_h - pos_e) / (offset_h - offset_l),
676                                   do_h, do_s, do_l);
677                 SP_OBJECT(stop)->updateRepr();
678                 child_prev->updateRepr();
679                 break;
680             } else {
681                 // wide brush, may affect more than 2 stops,
682                 // paint each stop by the force from the profile curve
683                 if (offset_l <= pos_e && offset_l > pos_e - r) {
684                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
685                                  force * tweak_profile (fabs (pos_e - offset_l), r),
686                                  do_h, do_s, do_l);
687                     child_prev->updateRepr();
688                 }
690                 if (offset_h >= pos_e && offset_h < pos_e + r) {
691                     tweak_color (mode, stop->specified_color.v.c, rgb_goal,
692                                  force * tweak_profile (fabs (pos_e - offset_h), r),
693                                  do_h, do_s, do_l);
694                     SP_OBJECT(stop)->updateRepr();
695                 }
696             }
697         }
699         offset_l = offset_h;
700         child_prev = child;
701     }
704 bool
705 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
706                           guint32 fill_goal, bool do_fill,
707                           guint32 stroke_goal, bool do_stroke,
708                           float opacity_goal, bool do_opacity,
709                           NR::Point p, double radius, double force,
710                           bool do_h, bool do_s, bool do_l, bool do_o)
712     bool did = false;
714     if (SP_IS_GROUP(item)) {
715         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
716             if (SP_IS_ITEM(child)) {
717                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
718                                           fill_goal, do_fill,
719                                           stroke_goal, do_stroke,
720                                           opacity_goal, do_opacity,
721                                           p, radius, force, do_h, do_s, do_l, do_o));
722                     did = true;
723             }
724         }
726     } else {
727         SPStyle *style = SP_OBJECT_STYLE(item);
728         if (!style) {
729             return false;
730         }
731         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item),
732                                                         SPItem::GEOMETRIC_BBOX);
733         if (!bbox) {
734             return false;
735         }
737         NR::Rect brush(p - NR::Point(radius, radius), p + NR::Point(radius, radius));
739         NR::Point center = bbox->midpoint();
740         double this_force;
742 // if item == item_at_point, use max force
743         if (item == item_at_point) {
744             this_force = force;
745 // else if no overlap of bbox and brush box, skip:
746         } else if (!bbox->intersects(brush)) {
747             return false;
748 //TODO:
749 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
750         //} else if (bbox->maxExtent() > 3 * radius) {
751         //}
752 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
753 // else if still smaller, then check only the object center:
754         } else {
755             this_force = force * tweak_profile (NR::L2 (p - center), radius);
756         }
758         if (this_force > 0.002) {
760             if (do_fill) {
761                 if (style->fill.isPaintserver()) {
762                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
763                     did = true;
764                 } else if (style->fill.isColor()) {
765                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
766                     item->updateRepr();
767                     did = true;
768                 }
769             }
770             if (do_stroke) {
771                 if (style->stroke.isPaintserver()) {
772                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
773                     did = true;
774                 } else if (style->stroke.isColor()) {
775                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
776                     item->updateRepr();
777                     did = true;
778                 }
779             }
780             if (do_opacity && do_o) {
781                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
782             }
783         }
784     }
786     return did;
790 bool
791 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector)
793     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
795     if (selection->isEmpty()) {
796         return false;
797     }
799     bool did = false;
800     double radius = get_dilate_radius(tc);
801     double force = get_dilate_force(tc);
802     if (radius == 0 || force == 0) {
803         return false;
804     }
805     double color_force = MIN(sqrt(force)/20.0, 1);
807     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
808     Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.tweak");
809     SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
810     if (tc->mode == TWEAK_MODE_COLORPAINT && !css)
811         return false;
813     bool do_fill = false, do_stroke = false, do_opacity = false;
814     guint32 fill_goal = 0, stroke_goal = 0;
815     float opacity_goal = 1;
817     const gchar *fill_prop = sp_repr_css_property(css, "fill", NULL);
818     if (fill_prop && strcmp(fill_prop, "none")) {
819         do_fill = true;
820         fill_goal = sp_svg_read_color(fill_prop, 0);
821     }
822     const gchar *stroke_prop = sp_repr_css_property(css, "stroke", NULL);
823     if (stroke_prop && strcmp(stroke_prop, "none")) {
824         do_stroke = true;
825         stroke_goal = sp_svg_read_color(stroke_prop, 0);
826     }
827     const gchar *opacity_prop = sp_repr_css_property(css, "opacity", NULL);
828     if (opacity_prop) {
829         do_opacity = true;
830         sp_svg_number_read_f(opacity_prop, &opacity_goal);
831     }
833     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
834          items != NULL;
835          items = items->next) {
837         SPItem *item = (SPItem *) items->data;
839         if (SP_IS_BOX3D(item)) {
840             // convert 3D boxes to ordinary groups before tweaking their shapes
841             item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
842             selection->add(item);
843         }
845         if (tc->mode == TWEAK_MODE_COLORPAINT || tc->mode == TWEAK_MODE_COLORJITTER) {
846             if (do_fill || do_stroke || do_opacity) {
847                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
848                                           fill_goal, do_fill,
849                                           stroke_goal, do_stroke,
850                                           opacity_goal, do_opacity,
851                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
852                 did = true;
853             }
854         } else {
855             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, force, tc->fidelity))
856                 did = true;
857         }
858     }
860     return did;
863 void
864 sp_tweak_update_area (SPTweakContext *tc)
866         double radius = get_dilate_radius(tc);
867         NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
868         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
869         sp_canvas_item_show(tc->dilate_area);
872 void
873 sp_tweak_switch_mode (SPTweakContext *tc, gint mode)
875     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
876     // need to set explicitly, because the prefs may not have changed by the previous
877     tc->mode = mode;
878     sp_tweak_update_cursor (tc, mode);
881 void
882 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode)
884    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
885    gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
886    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
887    // button has changed prefs, restore
888    prefs_set_int_attribute("tools.tweak", "mode", now_mode);
889    // changing prefs changed tc->mode, restore back :)
890    tc->mode = mode;
891    sp_tweak_update_cursor (tc, mode);
894 gint
895 sp_tweak_context_root_handler(SPEventContext *event_context,
896                                   GdkEvent *event)
898     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
899     SPDesktop *desktop = event_context->desktop;
901     gint ret = FALSE;
903     switch (event->type) {
904         case GDK_ENTER_NOTIFY:
905             sp_canvas_item_show(tc->dilate_area);
906             break;
907         case GDK_LEAVE_NOTIFY:
908             sp_canvas_item_hide(tc->dilate_area);
909             break;
910         case GDK_BUTTON_PRESS:
911             if (event->button.button == 1 && !event_context->space_panning) {
913                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
914                     return TRUE;
915                 }
917                 NR::Point const button_w(event->button.x,
918                                          event->button.y);
919                 NR::Point const button_dt(desktop->w2d(button_w));
920                 tc->last_push = desktop->dt2doc(button_dt);
922                 sp_tweak_extinput(tc, event);
924                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
925                 tc->is_drawing = true;
926                 tc->is_dilating = true;
927                 tc->has_dilated = false;
929                 ret = TRUE;
930             }
931             break;
932         case GDK_MOTION_NOTIFY:
933         {
934             NR::Point const motion_w(event->motion.x,
935                                      event->motion.y);
936             NR::Point motion_dt(desktop->w2d(motion_w));
937             NR::Point motion_doc(desktop->dt2doc(motion_dt));
938             sp_tweak_extinput(tc, event);
940             // draw the dilating cursor
941                 double radius = get_dilate_radius(tc);
942                 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
943                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
944                 sp_canvas_item_show(tc->dilate_area);
946                 guint num = 0;
947                 if (!desktop->selection->isEmpty()) {
948                     num = g_slist_length((GSList *) desktop->selection->itemList());
949                 }
950                 if (num == 0) {
951                     tc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
952                 } else {
953                     switch (tc->mode) {
954                         case TWEAK_MODE_PUSH:
955                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
956                                                       ngettext("<b>Pushing %d</b> selected object",
957                                                       "<b>Pushing %d</b> selected objects", num), num);
958                            break;
959                         case TWEAK_MODE_SHRINK:
960                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
961                                                       ngettext("<b>Shrinking %d</b> selected object",
962                                                       "<b>Shrinking %d</b> selected objects", num), num);
963                            break;
964                         case TWEAK_MODE_GROW:
965                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
966                                                       ngettext("<b>Growing %d</b> selected object",
967                                                       "<b>Growing %d</b> selected objects", num), num);
968                            break;
969                         case TWEAK_MODE_ATTRACT:
970                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
971                                                       ngettext("<b>Attracting %d</b> selected object",
972                                                       "<b>Attracting %d</b> selected objects", num), num);
973                            break;
974                         case TWEAK_MODE_REPEL:
975                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
976                                                       ngettext("<b>Repelling %d</b> selected object",
977                                                       "<b>Repelling %d</b> selected objects", num), num);
978                            break;
979                         case TWEAK_MODE_ROUGHEN:
980                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
981                                                       ngettext("<b>Roughening %d</b> selected object",
982                                                       "<b>Roughening %d</b> selected objects", num), num);
983                         case TWEAK_MODE_COLORPAINT:
984                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
985                                                       ngettext("<b>Painting %d</b> selected object",
986                                                       "<b>Painting %d</b> selected objects", num), num);
987                            break;
988                         case TWEAK_MODE_COLORJITTER:
989                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
990                                                       ngettext("<b>Jittering colors in %d</b> selected object",
991                                                       "<b>Jittering colors in %d</b> selected objects", num), num);
992                            break;
993                     }
994                 }
997             // dilating:
998             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
999                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push);
1000                 //tc->last_push = motion_doc;
1001                 tc->has_dilated = true;
1002                 // it's slow, so prevent clogging up with events
1003                 gobble_motion_events(GDK_BUTTON1_MASK);
1004                 return TRUE;
1005             }
1007         }
1008         break;
1011     case GDK_BUTTON_RELEASE:
1012     {
1013         NR::Point const motion_w(event->button.x, event->button.y);
1014         NR::Point const motion_dt(desktop->w2d(motion_w));
1016         sp_canvas_end_forced_full_redraws(desktop->canvas);
1017         tc->is_drawing = false;
1019         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1020             if (!tc->has_dilated) {
1021                 // if we did not rub, do a light tap
1022                 tc->pressure = 0.03;
1023                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0));
1024             }
1025             tc->is_dilating = false;
1026             tc->has_dilated = false;
1027             switch (tc->mode) {
1028                 case TWEAK_MODE_PUSH:
1029                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1030                                      SP_VERB_CONTEXT_TWEAK, _("Push tweak"));
1031                     break;
1032                 case TWEAK_MODE_SHRINK:
1033                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1034                                      SP_VERB_CONTEXT_TWEAK, _("Shrink tweak"));
1035                     break;
1036                 case TWEAK_MODE_GROW:
1037                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1038                                      SP_VERB_CONTEXT_TWEAK, _("Grow tweak"));
1039                     break;
1040                 case TWEAK_MODE_ATTRACT:
1041                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1042                                      SP_VERB_CONTEXT_TWEAK, _("Attract tweak"));
1043                     break;
1044                 case TWEAK_MODE_REPEL:
1045                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1046                                      SP_VERB_CONTEXT_TWEAK, _("Repel tweak"));
1047                     break;
1048                 case TWEAK_MODE_ROUGHEN:
1049                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1050                                      SP_VERB_CONTEXT_TWEAK, _("Roughen tweak"));
1051                     break;
1052                 case TWEAK_MODE_COLORPAINT:
1053                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1054                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1055                 case TWEAK_MODE_COLORJITTER:
1056                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1057                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1058                     break;
1059             }
1060         }
1061         break;
1062     }
1064     case GDK_KEY_PRESS:
1065         switch (get_group0_keyval (&event->key)) {
1066         case GDK_p:
1067         case GDK_P:
1068         case GDK_1:
1069             if (MOD__SHIFT_ONLY) {
1070                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH);
1071                 ret = TRUE;
1072             }
1073             break;
1074         case GDK_s:
1075         case GDK_S:
1076         case GDK_2:
1077             if (MOD__SHIFT_ONLY) {
1078                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK);
1079                 ret = TRUE;
1080             }
1081             break;
1082         case GDK_g:
1083         case GDK_G:
1084         case GDK_3:
1085             if (MOD__SHIFT_ONLY) {
1086                 sp_tweak_switch_mode(tc, TWEAK_MODE_GROW);
1087                 ret = TRUE;
1088             }
1089             break;
1090         case GDK_a:
1091         case GDK_A:
1092         case GDK_4:
1093             if (MOD__SHIFT_ONLY) {
1094                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT);
1095                 ret = TRUE;
1096             }
1097             break;
1098         case GDK_e:
1099         case GDK_E:
1100         case GDK_5:
1101             if (MOD__SHIFT_ONLY) {
1102                 sp_tweak_switch_mode(tc, TWEAK_MODE_REPEL);
1103                 ret = TRUE;
1104             }
1105             break;
1106         case GDK_r:
1107         case GDK_R:
1108         case GDK_6:
1109             if (MOD__SHIFT_ONLY) {
1110                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN);
1111                 ret = TRUE;
1112             }
1113             break;
1114         case GDK_c:
1115         case GDK_C:
1116         case GDK_7:
1117             if (MOD__SHIFT_ONLY) {
1118                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT);
1119                 ret = TRUE;
1120             }
1121             break;
1122         case GDK_j:
1123         case GDK_J:
1124         case GDK_8:
1125             if (MOD__SHIFT_ONLY) {
1126                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER);
1127                 ret = TRUE;
1128             }
1129             break;
1131         case GDK_Up:
1132         case GDK_KP_Up:
1133             if (!MOD__CTRL_ONLY) {
1134                 tc->force += 0.05;
1135                 if (tc->force > 1.0)
1136                     tc->force = 1.0;
1137                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1138                 ret = TRUE;
1139             }
1140             break;
1141         case GDK_Down:
1142         case GDK_KP_Down:
1143             if (!MOD__CTRL_ONLY) {
1144                 tc->force -= 0.05;
1145                 if (tc->force < 0.0)
1146                     tc->force = 0.0;
1147                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1148                 ret = TRUE;
1149             }
1150             break;
1151         case GDK_Right:
1152         case GDK_KP_Right:
1153             if (!MOD__CTRL_ONLY) {
1154                 tc->width += 0.01;
1155                 if (tc->width > 1.0)
1156                     tc->width = 1.0;
1157                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1158                 sp_tweak_update_area(tc);
1159                 ret = TRUE;
1160             }
1161             break;
1162         case GDK_Left:
1163         case GDK_KP_Left:
1164             if (!MOD__CTRL_ONLY) {
1165                 tc->width -= 0.01;
1166                 if (tc->width < 0.01)
1167                     tc->width = 0.01;
1168                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1169                 sp_tweak_update_area(tc);
1170                 ret = TRUE;
1171             }
1172             break;
1173         case GDK_Home:
1174         case GDK_KP_Home:
1175             tc->width = 0.01;
1176             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1177             sp_tweak_update_area(tc);
1178             ret = TRUE;
1179             break;
1180         case GDK_End:
1181         case GDK_KP_End:
1182             tc->width = 1.0;
1183             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1184             sp_tweak_update_area(tc);
1185             ret = TRUE;
1186             break;
1187         case GDK_x:
1188         case GDK_X:
1189             if (MOD__ALT_ONLY) {
1190                 desktop->setToolboxFocusTo ("altx-tweak");
1191                 ret = TRUE;
1192             }
1193             break;
1195         case GDK_Control_L:
1196         case GDK_Control_R:
1197             if (MOD__SHIFT) {
1198                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1199             } else {
1200                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1201             }
1202             break;
1203         case GDK_Shift_L:
1204         case GDK_Shift_R:
1205             if (MOD__CTRL) {
1206                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1207             }
1208             break;
1209         default:
1210             break;
1211         }
1212         break;
1214     case GDK_KEY_RELEASE:
1215         switch (get_group0_keyval(&event->key)) {
1216             case GDK_Control_L:
1217             case GDK_Control_R:
1218                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1219                 tc->_message_context->clear();
1220                 break;
1221             case GDK_Shift_L:
1222             case GDK_Shift_R:
1223                 if (MOD__CTRL) {
1224                     sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1225                 }
1226                 break;
1227             break;
1228             default:
1229                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1230                 break;
1231         }
1233     default:
1234         break;
1235     }
1237     if (!ret) {
1238         if (((SPEventContextClass *) parent_class)->root_handler) {
1239             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1240         }
1241     }
1243     return ret;
1247 /*
1248   Local Variables:
1249   mode:c++
1250   c-file-style:"stroustrup"
1251   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1252   indent-tabs-mode:nil
1253   fill-column:99
1254   End:
1255 */
1256 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :