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;
111 }
113 static void
114 sp_tweak_context_class_init(SPTweakContextClass *klass)
115 {
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;
126 }
128 static void
129 sp_tweak_context_init(SPTweakContext *tc)
130 {
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();
153 }
155 static void
156 sp_tweak_context_dispose(GObject *object)
157 {
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);
173 }
175 void
176 sp_tweak_update_cursor (SPTweakContext *tc, gint mode)
177 {
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);
204 }
206 static bool
207 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
208 {
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;
218 }
221 static void
222 sp_tweak_context_setup(SPEventContext *ec)
223 {
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 }
271 }
273 static void
274 sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
275 {
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 }
302 }
304 static void
305 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
306 {
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;
311 }
313 double
314 get_dilate_radius (SPTweakContext *tc)
315 {
316 // 10 times the pen width:
317 return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
318 }
320 double
321 get_dilate_force (SPTweakContext *tc)
322 {
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;
329 }
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)
333 {
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;
502 }
504 void
505 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
506 {
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 }
531 }
533 void
534 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
535 {
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]);
554 }
556 void
557 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
558 {
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 }
564 }
566 void
567 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
568 {
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);
579 }
582 double
583 tweak_profile (double dist, double radius)
584 {
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 }
596 }
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*/)
602 {
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 }
712 }
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)
721 {
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;
797 }
800 bool
801 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector)
802 {
803 Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
805 if (selection->isEmpty()) {
806 return false;
807 }
809 bool did = false;
810 double radius = get_dilate_radius(tc);
811 double force = get_dilate_force(tc);
812 if (radius == 0 || force == 0) {
813 return false;
814 }
815 double color_force = MIN(sqrt(force)/20.0, 1);
817 SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
818 Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.tweak");
819 SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
820 if (tc->mode == TWEAK_MODE_COLORPAINT && !css)
821 return false;
823 bool do_fill = false, do_stroke = false, do_opacity = false;
824 guint32 fill_goal = 0, stroke_goal = 0;
825 float opacity_goal = 1;
827 const gchar *fill_prop = sp_repr_css_property(css, "fill", NULL);
828 if (fill_prop && strcmp(fill_prop, "none")) {
829 do_fill = true;
830 fill_goal = sp_svg_read_color(fill_prop, 0);
831 }
832 const gchar *stroke_prop = sp_repr_css_property(css, "stroke", NULL);
833 if (stroke_prop && strcmp(stroke_prop, "none")) {
834 do_stroke = true;
835 stroke_goal = sp_svg_read_color(stroke_prop, 0);
836 }
837 const gchar *opacity_prop = sp_repr_css_property(css, "opacity", NULL);
838 if (opacity_prop) {
839 do_opacity = true;
840 sp_svg_number_read_f(opacity_prop, &opacity_goal);
841 }
843 for (GSList *items = g_slist_copy((GSList *) selection->itemList());
844 items != NULL;
845 items = items->next) {
847 SPItem *item = (SPItem *) items->data;
849 if (tc->mode == TWEAK_MODE_COLORPAINT || tc->mode == TWEAK_MODE_COLORJITTER) {
850 if (do_fill || do_stroke || do_opacity) {
851 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
852 fill_goal, do_fill,
853 stroke_goal, do_stroke,
854 opacity_goal, do_opacity,
855 p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
856 did = true;
857 }
858 } else {
859 if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, force, tc->fidelity))
860 did = true;
861 }
862 }
864 return did;
865 }
867 void
868 sp_tweak_update_area (SPTweakContext *tc)
869 {
870 double radius = get_dilate_radius(tc);
871 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
872 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
873 sp_canvas_item_show(tc->dilate_area);
874 }
876 void
877 sp_tweak_switch_mode (SPTweakContext *tc, gint mode)
878 {
879 SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
880 // need to set explicitly, because the prefs may not have changed by the previous
881 tc->mode = mode;
882 sp_tweak_update_cursor (tc, mode);
883 }
885 void
886 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode)
887 {
888 // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
889 gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
890 SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
891 // button has changed prefs, restore
892 prefs_set_int_attribute("tools.tweak", "mode", now_mode);
893 // changing prefs changed tc->mode, restore back :)
894 tc->mode = mode;
895 sp_tweak_update_cursor (tc, mode);
896 }
898 gint
899 sp_tweak_context_root_handler(SPEventContext *event_context,
900 GdkEvent *event)
901 {
902 SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
903 SPDesktop *desktop = event_context->desktop;
905 gint ret = FALSE;
907 switch (event->type) {
908 case GDK_ENTER_NOTIFY:
909 sp_canvas_item_show(tc->dilate_area);
910 break;
911 case GDK_LEAVE_NOTIFY:
912 sp_canvas_item_hide(tc->dilate_area);
913 break;
914 case GDK_BUTTON_PRESS:
915 if (event->button.button == 1 && !event_context->space_panning) {
917 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
918 return TRUE;
919 }
921 NR::Point const button_w(event->button.x,
922 event->button.y);
923 NR::Point const button_dt(desktop->w2d(button_w));
924 tc->last_push = desktop->dt2doc(button_dt);
926 sp_tweak_extinput(tc, event);
928 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
929 tc->is_drawing = true;
930 tc->is_dilating = true;
931 tc->has_dilated = false;
933 ret = TRUE;
934 }
935 break;
936 case GDK_MOTION_NOTIFY:
937 {
938 NR::Point const motion_w(event->motion.x,
939 event->motion.y);
940 NR::Point motion_dt(desktop->w2d(motion_w));
941 NR::Point motion_doc(desktop->dt2doc(motion_dt));
942 sp_tweak_extinput(tc, event);
944 // draw the dilating cursor
945 double radius = get_dilate_radius(tc);
946 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
947 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
948 sp_canvas_item_show(tc->dilate_area);
950 guint num = 0;
951 if (!desktop->selection->isEmpty()) {
952 num = g_slist_length((GSList *) desktop->selection->itemList());
953 }
954 if (num == 0) {
955 tc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
956 } else {
957 switch (tc->mode) {
958 case TWEAK_MODE_PUSH:
959 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
960 ngettext("<b>Pushing %d</b> selected object",
961 "<b>Pushing %d</b> selected objects", num), num);
962 break;
963 case TWEAK_MODE_SHRINK:
964 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
965 ngettext("<b>Shrinking %d</b> selected object",
966 "<b>Shrinking %d</b> selected objects", num), num);
967 break;
968 case TWEAK_MODE_GROW:
969 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
970 ngettext("<b>Growing %d</b> selected object",
971 "<b>Growing %d</b> selected objects", num), num);
972 break;
973 case TWEAK_MODE_ATTRACT:
974 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
975 ngettext("<b>Attracting %d</b> selected object",
976 "<b>Attracting %d</b> selected objects", num), num);
977 break;
978 case TWEAK_MODE_REPEL:
979 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
980 ngettext("<b>Repelling %d</b> selected object",
981 "<b>Repelling %d</b> selected objects", num), num);
982 break;
983 case TWEAK_MODE_ROUGHEN:
984 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
985 ngettext("<b>Roughening %d</b> selected object",
986 "<b>Roughening %d</b> selected objects", num), num);
987 break;
988 case TWEAK_MODE_COLORPAINT:
989 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
990 ngettext("<b>Painting %d</b> selected object",
991 "<b>Painting %d</b> selected objects", num), num);
992 break;
993 case TWEAK_MODE_COLORJITTER:
994 tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
995 ngettext("<b>Jittering colors in %d</b> selected object",
996 "<b>Jittering colors in %d</b> selected objects", num), num);
997 break;
998 }
999 }
1002 // dilating:
1003 if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1004 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push);
1005 //tc->last_push = motion_doc;
1006 tc->has_dilated = true;
1007 // it's slow, so prevent clogging up with events
1008 gobble_motion_events(GDK_BUTTON1_MASK);
1009 return TRUE;
1010 }
1012 }
1013 break;
1016 case GDK_BUTTON_RELEASE:
1017 {
1018 NR::Point const motion_w(event->button.x, event->button.y);
1019 NR::Point const motion_dt(desktop->w2d(motion_w));
1021 sp_canvas_end_forced_full_redraws(desktop->canvas);
1022 tc->is_drawing = false;
1024 if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1025 if (!tc->has_dilated) {
1026 // if we did not rub, do a light tap
1027 tc->pressure = 0.03;
1028 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0));
1029 }
1030 tc->is_dilating = false;
1031 tc->has_dilated = false;
1032 switch (tc->mode) {
1033 case TWEAK_MODE_PUSH:
1034 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1035 SP_VERB_CONTEXT_TWEAK, _("Push tweak"));
1036 break;
1037 case TWEAK_MODE_SHRINK:
1038 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1039 SP_VERB_CONTEXT_TWEAK, _("Shrink tweak"));
1040 break;
1041 case TWEAK_MODE_GROW:
1042 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1043 SP_VERB_CONTEXT_TWEAK, _("Grow tweak"));
1044 break;
1045 case TWEAK_MODE_ATTRACT:
1046 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1047 SP_VERB_CONTEXT_TWEAK, _("Attract tweak"));
1048 break;
1049 case TWEAK_MODE_REPEL:
1050 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1051 SP_VERB_CONTEXT_TWEAK, _("Repel tweak"));
1052 break;
1053 case TWEAK_MODE_ROUGHEN:
1054 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1055 SP_VERB_CONTEXT_TWEAK, _("Roughen tweak"));
1056 break;
1057 case TWEAK_MODE_COLORPAINT:
1058 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1059 SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1060 break;
1061 case TWEAK_MODE_COLORJITTER:
1062 sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1063 SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1064 break;
1065 }
1066 }
1067 break;
1068 }
1070 case GDK_KEY_PRESS:
1071 switch (get_group0_keyval (&event->key)) {
1072 case GDK_p:
1073 case GDK_P:
1074 case GDK_1:
1075 if (MOD__SHIFT_ONLY) {
1076 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH);
1077 ret = TRUE;
1078 }
1079 break;
1080 case GDK_s:
1081 case GDK_S:
1082 case GDK_2:
1083 if (MOD__SHIFT_ONLY) {
1084 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK);
1085 ret = TRUE;
1086 }
1087 break;
1088 case GDK_g:
1089 case GDK_G:
1090 case GDK_3:
1091 if (MOD__SHIFT_ONLY) {
1092 sp_tweak_switch_mode(tc, TWEAK_MODE_GROW);
1093 ret = TRUE;
1094 }
1095 break;
1096 case GDK_a:
1097 case GDK_A:
1098 case GDK_4:
1099 if (MOD__SHIFT_ONLY) {
1100 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT);
1101 ret = TRUE;
1102 }
1103 break;
1104 case GDK_e:
1105 case GDK_E:
1106 case GDK_5:
1107 if (MOD__SHIFT_ONLY) {
1108 sp_tweak_switch_mode(tc, TWEAK_MODE_REPEL);
1109 ret = TRUE;
1110 }
1111 break;
1112 case GDK_r:
1113 case GDK_R:
1114 case GDK_6:
1115 if (MOD__SHIFT_ONLY) {
1116 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN);
1117 ret = TRUE;
1118 }
1119 break;
1120 case GDK_c:
1121 case GDK_C:
1122 case GDK_7:
1123 if (MOD__SHIFT_ONLY) {
1124 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT);
1125 ret = TRUE;
1126 }
1127 break;
1128 case GDK_j:
1129 case GDK_J:
1130 case GDK_8:
1131 if (MOD__SHIFT_ONLY) {
1132 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER);
1133 ret = TRUE;
1134 }
1135 break;
1137 case GDK_Up:
1138 case GDK_KP_Up:
1139 if (!MOD__CTRL_ONLY) {
1140 tc->force += 0.05;
1141 if (tc->force > 1.0)
1142 tc->force = 1.0;
1143 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1144 ret = TRUE;
1145 }
1146 break;
1147 case GDK_Down:
1148 case GDK_KP_Down:
1149 if (!MOD__CTRL_ONLY) {
1150 tc->force -= 0.05;
1151 if (tc->force < 0.0)
1152 tc->force = 0.0;
1153 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1154 ret = TRUE;
1155 }
1156 break;
1157 case GDK_Right:
1158 case GDK_KP_Right:
1159 if (!MOD__CTRL_ONLY) {
1160 tc->width += 0.01;
1161 if (tc->width > 1.0)
1162 tc->width = 1.0;
1163 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1164 sp_tweak_update_area(tc);
1165 ret = TRUE;
1166 }
1167 break;
1168 case GDK_Left:
1169 case GDK_KP_Left:
1170 if (!MOD__CTRL_ONLY) {
1171 tc->width -= 0.01;
1172 if (tc->width < 0.01)
1173 tc->width = 0.01;
1174 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1175 sp_tweak_update_area(tc);
1176 ret = TRUE;
1177 }
1178 break;
1179 case GDK_Home:
1180 case GDK_KP_Home:
1181 tc->width = 0.01;
1182 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1183 sp_tweak_update_area(tc);
1184 ret = TRUE;
1185 break;
1186 case GDK_End:
1187 case GDK_KP_End:
1188 tc->width = 1.0;
1189 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1190 sp_tweak_update_area(tc);
1191 ret = TRUE;
1192 break;
1193 case GDK_x:
1194 case GDK_X:
1195 if (MOD__ALT_ONLY) {
1196 desktop->setToolboxFocusTo ("altx-tweak");
1197 ret = TRUE;
1198 }
1199 break;
1201 case GDK_Control_L:
1202 case GDK_Control_R:
1203 if (MOD__SHIFT) {
1204 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1205 } else {
1206 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1207 }
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_GROW);
1213 }
1214 break;
1215 default:
1216 break;
1217 }
1218 break;
1220 case GDK_KEY_RELEASE:
1221 switch (get_group0_keyval(&event->key)) {
1222 case GDK_Control_L:
1223 case GDK_Control_R:
1224 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1225 tc->_message_context->clear();
1226 break;
1227 case GDK_Shift_L:
1228 case GDK_Shift_R:
1229 if (MOD__CTRL) {
1230 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1231 }
1232 break;
1233 break;
1234 default:
1235 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1236 break;
1237 }
1239 default:
1240 break;
1241 }
1243 if (!ret) {
1244 if (((SPEventContextClass *) parent_class)->root_handler) {
1245 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1246 }
1247 }
1249 return ret;
1250 }
1253 /*
1254 Local Variables:
1255 mode:c++
1256 c-file-style:"stroustrup"
1257 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1258 indent-tabs-mode:nil
1259 fill-column:99
1260 End:
1261 */
1262 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :