Code

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