Code

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