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