Code

Updating to current trunk
[inkscape.git] / src / spray-context.cpp
1 #define __SP_SPRAY_CONTEXT_C__
3 /*
4  * Spray Tool
5  *
6  * Authors:
7  *   Pierre-Antoine MARC
8  *   Pierre CACLIN
9  *   Aurel-Aimé MARMION
10  *   Julien LERAY
11  *   Benoît LAVORATA
12  *   Vincent MONTAGNE
13  *   Pierre BARBRY-BLOT
14  *
15  * Copyright (C) 2009 authors
16  *
17  * Released under GNU GPL, read the file 'COPYING' for more information
18  */
20 #include "config.h"
22 #include <gtk/gtk.h>
23 #include <gdk/gdkkeysyms.h>
24 #include <glibmm/i18n.h>
26 #include <numeric>
28 #include "svg/svg.h"
29 #include "display/canvas-bpath.h"
31 #include <glib/gmem.h>
32 #include "macros.h"
33 #include "document.h"
34 #include "selection.h"
35 #include "desktop.h"
36 #include "desktop-events.h"
37 #include "desktop-handles.h"
38 #include "unistd.h"
39 #include "desktop-style.h"
40 #include "message-context.h"
41 #include "pixmaps/cursor-spray.xpm"
42 #include "pixmaps/cursor-spray-move.xpm"
43 #include "pixmaps/cursor-thin.xpm"
44 #include "pixmaps/cursor-thicken.xpm"
45 #include "pixmaps/cursor-attract.xpm"
46 #include "pixmaps/cursor-repel.xpm"
47 #include "pixmaps/cursor-push.xpm"
48 #include "pixmaps/cursor-roughen.xpm"
49 #include "pixmaps/cursor-color.xpm"
50 #include <boost/optional.hpp>
51 #include "libnr/nr-matrix-ops.h"
52 #include "libnr/nr-scale-translate-ops.h"
53 #include "xml/repr.h"
54 #include "context-fns.h"
55 #include "sp-item.h"
56 #include "inkscape.h"
57 #include "color.h"
58 #include "svg/svg-color.h"
59 #include "splivarot.h"
60 #include "sp-item-group.h"
61 #include "sp-shape.h"
62 #include "sp-path.h"
63 #include "path-chemistry.h"
64 #include "sp-gradient.h"
65 #include "sp-stop.h"
66 #include "sp-stop-fns.h"
67 #include "sp-gradient-reference.h"
68 #include "sp-linear-gradient.h"
69 #include "sp-radial-gradient.h"
70 #include "gradient-chemistry.h"
71 #include "sp-text.h"
72 #include "sp-flowtext.h"
73 #include "display/canvas-bpath.h"
74 #include "display/canvas-arena.h"
75 #include "display/curve.h"
76 #include "livarot/Shape.h"
77 #include <2geom/isnan.h>
78 #include <2geom/transforms.h>
79 #include "preferences.h"
80 #include "style.h"
81 #include "box3d.h"
82 #include "sp-item-transform.h"
83 #include "filter-chemistry.h"
84 #include "sp-gaussian-blur-fns.h"
85 #include "sp-gaussian-blur.h"
87 #include "spray-context.h"
88 #include "ui/dialog/dialog-manager.h"
89 #include "helper/action.h"
91 #include <iostream>
92 using namespace std;
95 #define DDC_RED_RGBA 0xff0000ff
97 #define DYNA_MIN_WIDTH 1.0e-6
99 static void sp_spray_context_class_init(SPSprayContextClass *klass);
100 static void sp_spray_context_init(SPSprayContext *ddc);
101 static void sp_spray_context_dispose(GObject *object);
103 static void sp_spray_context_setup(SPEventContext *ec);
104 static void sp_spray_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
105 static gint sp_spray_context_root_handler(SPEventContext *ec, GdkEvent *event);
107 static SPEventContextClass *parent_class = 0;
111 // The following code implements NormalDistribution wich is used for the density of the spray
114 /*
115    RAND is a macro which returns a pseudo-random numbers from a uniform
116    distribution on the interval [0 1]
117 */
118 #define RAND ((double) rand())/((double) RAND_MAX)
120 /*
121    TWOPI = 2.0*pi
122 */
123 #define TWOPI 2.0*3.141592653589793238462643383279502884197169399375
125 /*
126    RANDN is a macro which returns a pseudo-random numbers from a normal
127    distribution with mean zero and standard deviation one. This macro uses Box
128    Muller's algorithm
129 */
130 #define RANDN sqrt(-2.0*log(RAND))*cos(TWOPI*RAND)
133 double NormalDistribution(double mu,double sigma)
135 /*
136    This function returns a pseudo-random numbers from a normal distribution with
137    mean equal at mu and standard deviation equal at sigma > 0
138 */
140   return (mu+sigma*RANDN);
144 //Fin de la création de NormalDistribution
146 GtkType sp_spray_context_get_type(void)
148     static GType type = 0;
149     if (!type) {
150         GTypeInfo info = {
151             sizeof(SPSprayContextClass),
152             NULL, NULL,
153             (GClassInitFunc) sp_spray_context_class_init,
154             NULL, NULL,
155             sizeof(SPSprayContext),
156             4,
157             (GInstanceInitFunc) sp_spray_context_init,
158             NULL,   /* value_table */
159         };
160         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPSprayContext", &info, (GTypeFlags)0);
161     }
162     return type;
165 static void sp_spray_context_class_init(SPSprayContextClass *klass)
167     GObjectClass *object_class = (GObjectClass *) klass;
168     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
170     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
172     object_class->dispose = sp_spray_context_dispose;
174     event_context_class->setup = sp_spray_context_setup;
175     event_context_class->set = sp_spray_context_set;
176     event_context_class->root_handler = sp_spray_context_root_handler;
179 /*Method to rotate items*/
180 void sp_spray_rotate_rel(Geom::Point c,SPDesktop */*desktop*/,SPItem *item, Geom::Rotate const &rotation)
183     Geom::Point center = c;
184     Geom::Translate const s(c);
185     Geom::Matrix affine = Geom::Matrix(s).inverse() * Geom::Matrix(rotation) * Geom::Matrix(s);
187     // Rotate item.
188     sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * (Geom::Matrix)affine);
189     // Use each item's own transform writer, consistent with sp_selection_apply_affine()
190     sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
192     // Restore the center position (it's changed because the bbox center changed)
193     if (item->isCenterSet()) {
194         item->setCenter(c);
195         item->updateRepr();
196     }
199 /*Method to scale items*/
200 void sp_spray_scale_rel(Geom::Point c, SPDesktop */*desktop*/, SPItem *item, Geom::Scale  const &scale)
202         Geom::Translate const s(c);
205         sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * s.inverse() * scale * s  );
206         sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
211 static void sp_spray_context_init(SPSprayContext *tc)
213    SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
217     event_context->cursor_shape = cursor_spray_xpm;
218     event_context->hot_x = 4;
219     event_context->hot_y = 4;
221     /* attributes */
222     tc->dragging = FALSE;
223     tc->distrib = 1;
224     tc->width = 0.2;
225     tc->force = 0.2;
226     tc->ratio = 0;
227     tc->tilt=0;
228     tc->mean = 0.2;
229     tc->rot_min=0;
230     tc->rot_max=0;
231     tc->standard_deviation=0.2;
232     tc->scale=1;
233     tc->scale_min = 1;
234     tc->scale_max=1;
235     tc->pressure = TC_DEFAULT_PRESSURE;
237     tc->is_dilating = false;
238     tc->has_dilated = false;
240     tc->do_h = true;
241     tc->do_s = true;
242     tc->do_l = true;
243     tc->do_o = false;
245     new (&tc->style_set_connection) sigc::connection();
248 static void sp_spray_context_dispose(GObject *object)
250     SPSprayContext *tc = SP_SPRAY_CONTEXT(object);
252     tc->style_set_connection.disconnect();
253     tc->style_set_connection.~connection();
255     if (tc->dilate_area) {
256         gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
257         tc->dilate_area = NULL;
258     }
260     if (tc->_message_context) {
261         delete tc->_message_context;
262     }
264     G_OBJECT_CLASS(parent_class)->dispose(object);
267 bool is_transform_modes(gint mode)
269     return (mode == SPRAY_MODE_COPY ||
270             mode == SPRAY_MODE_CLONE ||
271             mode == SPRAY_MODE_SINGLE_PATH ||
272             mode == SPRAY_OPTION);
275 void sp_spray_update_cursor(SPSprayContext *tc, bool /*with_shift*/)
277    SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
278    SPDesktop *desktop = event_context->desktop;
280                 guint num = 0;
281                 gchar *sel_message = NULL;
282                 if (!desktop->selection->isEmpty()) {
283                     num = g_slist_length((GSList *) desktop->selection->itemList());
284                     sel_message = g_strdup_printf(ngettext("<b>%i</b> object selected","<b>%i</b> objects selected",num), num);
285                 } else {
286                     sel_message = g_strdup_printf(_("<b>Nothing</b> selected"));
287                 }
290    switch (tc->mode) {
291        case SPRAY_MODE_COPY:
292            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag, click or scroll to spray <b>copies</b> of the initial selection"), sel_message);
293            break;
294        case SPRAY_MODE_CLONE:
295            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag, click or scroll to spray <b>clones</b> of the initial selection"), sel_message);
296            break;
297        case SPRAY_MODE_SINGLE_PATH:
298            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag, click or scroll to spray in a <b>single path</b> of the initial selection"), sel_message);
299            break;
300        case SPRAY_OPTION:
301            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Modify <b>spray</b> options"), sel_message);
302            break;
303    }
304    sp_event_context_update_cursor(event_context);
305    g_free(sel_message);
308 static void sp_spray_context_setup(SPEventContext *ec)
310     SPSprayContext *tc = SP_SPRAY_CONTEXT(ec);
312     if (((SPEventContextClass *) parent_class)->setup)
313         ((SPEventContextClass *) parent_class)->setup(ec);
315     {
316         /* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
317         SPCurve *c = new SPCurve();
318         const double C1 = 0.552;
319         c->moveto(-1,0);
320         c->curveto(-1, C1, -C1, 1, 0, 1 );
321         c->curveto(C1, 1, 1, C1, 1, 0 );
322         c->curveto(1, -C1, C1, -1, 0, -1 );
323         c->curveto(-C1, -1, -1, -C1, -1, 0 );
324         c->closepath();
325         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
326         c->unref();
327         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
328         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
329         sp_canvas_item_hide(tc->dilate_area);
330     }
332     tc->is_drawing = false;
334     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
336     sp_event_context_read(ec, "distrib");
337     sp_event_context_read(ec, "width");
338     sp_event_context_read(ec, "ratio");
339     sp_event_context_read(ec, "tilt");
340     sp_event_context_read(ec, "rot_min");
341     sp_event_context_read(ec, "rot_max");
342     sp_event_context_read(ec, "scale_min");
343     sp_event_context_read(ec, "scale_max");
344     sp_event_context_read(ec, "mode");
345     sp_event_context_read(ec, "population");
346     sp_event_context_read(ec, "force");
347     sp_event_context_read(ec, "mean");
348     sp_event_context_read(ec, "standard_deviation");
349     sp_event_context_read(ec, "usepressure");
350     sp_event_context_read(ec, "Rotation min");
351     sp_event_context_read(ec, "Rotation max");
352     sp_event_context_read(ec, "Scale");
353     sp_event_context_read(ec, "doh");
354     sp_event_context_read(ec, "dol");
355     sp_event_context_read(ec, "dos");
356     sp_event_context_read(ec, "doo");
358     ;
360     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
361     if (prefs->getBool("/tools/spray/selcue")) {
362         ec->enableSelectionCue();
363     }
365     if (prefs->getBool("/tools/spray/gradientdrag")) {
366         ec->enableGrDrag();
367     }
370 static void sp_spray_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
372     SPSprayContext *tc = SP_SPRAY_CONTEXT(ec);
373     Glib::ustring path = val->getEntryName();
375     if (path == "width") {
376         tc->width = 0.01 * CLAMP(val->getInt(10), 1, 100);
377     } else if (path == "mode") {
378         tc->mode = val->getInt();
379         sp_spray_update_cursor(tc, false);
380     } else if (path == "distribution") {
381         tc->distrib = val->getInt(1);
382     } else if (path == "population") {
383         tc->population = 0.01 * CLAMP(val->getInt(10), 1, 100);
384      } else if (path == "tilt") {
385         tc->tilt = CLAMP(val->getDouble(0.1), 0, 1000.0);
386      } else if (path == "ratio") {
387         tc->ratio = CLAMP(val->getDouble(), 0.0, 0.9);
388     } else if (path == "force") {
389         tc->force = CLAMP(val->getDouble(1.0), 0, 1.0);
390     } else if (path == "rot_min") {
391         tc->rot_min = CLAMP(val->getDouble(0), 0, 10.0);
392     } else if (path == "rot_max") {
393         tc->rot_max = CLAMP(val->getDouble(0), 0, 10.0);
394     } else if (path == "scale_min") {
395         tc->scale_min = CLAMP(val->getDouble(1.0), 0, 10.0);
396     } else if (path == "scale_max") {
397         tc->scale_max = CLAMP(val->getDouble(1.0), 0, 10.0);
398     } else if (path == "mean") {
399         tc->mean = 0.01 * CLAMP(val->getInt(10), 1, 100);
400     } else if (path == "standard_deviation") {
401         tc->standard_deviation = 0.01 * CLAMP(val->getInt(10), 1, 100);
402     } else if (path == "usepressure") {
403         tc->usepressure = val->getBool();
404     } else if (path == "doh") {
405         tc->do_h = val->getBool();
406     } else if (path == "dos") {
407         tc->do_s = val->getBool();
408     } else if (path == "dol") {
409         tc->do_l = val->getBool();
410     } else if (path == "doo") {
411         tc->do_o = val->getBool();
412     }
415 static void sp_spray_extinput(SPSprayContext *tc, GdkEvent *event)
417     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
418         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
419     else
420         tc->pressure = TC_DEFAULT_PRESSURE;
423 double get_dilate_radius(SPSprayContext *tc)
426     return 250 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
431 double get_path_force(SPSprayContext *tc)
433     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
434         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
435     if (force > 3) {
436         force += 4 * (force - 3);
437     }
438     return force * tc->force;
441 double get_path_mean(SPSprayContext *tc)
443     return tc->mean;
446 double get_path_standard_deviation(SPSprayContext *tc)
448     return tc->standard_deviation;
451 double get_move_force(SPSprayContext *tc)
453     double force = (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE);
454     return force * tc->force;
457 double get_move_mean(SPSprayContext *tc)
459     return tc->mean;
462 double get_move_standard_deviation(SPSprayContext *tc)
464     return tc->standard_deviation;
467 /* Method to handle the distribution of the items */
470 void random_position( double &r, double &p, double &a, double &s, int choix)
472        if (choix == 0) // Mode 1 : uniform repartition
473     {
474         r = (1-pow(g_random_double_range(0, 1),2));
475         p = g_random_double_range(0, M_PI*2);
476     }
477     if (choix == 1) //Mode 0 : gaussian repartition
478     {
479         double r_temp =-1;
480 while(!((r_temp>=0)&&(r_temp<=1)))
482          r_temp = NormalDistribution(a,s/4);
484 // generates a number following a normal distribution
485         p = g_random_double_range(0, M_PI*2);
486         r=r_temp;
487        /* if (r_temp<=0) r=0;
488         else
489         {
490             if (r_temp>1) r=1;
491             else r = r_temp;
492         }*/
493     }
500 bool sp_spray_dilate_recursive(SPDesktop *desktop,
501                                Inkscape::Selection *selection,
502                                SPItem *item,
503                                Geom::Point p,
504                                Geom::Point /*vector*/,
505                                gint mode,
506                                double radius,
507                                double /*force*/,
508                                double population,
509                                double &scale,
510                                double scale_min,
511                                double scale_max,
512                                bool /*reverse*/,
513                                double mean,
514                                double standard_deviation,
515                                double ratio,
516                                double tilt,
517                                double rot_min,
518                                double rot_max,
519                                gint _distrib )
524     bool did = false;
526     if (SP_IS_BOX3D(item) /*&& !is_transform_modes(mode)*/) {
527         // convert 3D boxes to ordinary groups before spraying their shapes
528         item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
529         selection->add(item);
530     }
532 /*if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
533         GSList *items = g_slist_prepend (NULL, item);
534         GSList *selected = NULL;
535         GSList *to_select = NULL;
536         SPDocument *doc = SP_OBJECT_DOCUMENT(item);
537         sp_item_list_to_curves (items, &selected, &to_select);
538         g_slist_free (items);
539         SPObject* newObj = doc->getObjectByRepr((Inkscape::XML::Node *) to_select->data);
540         g_slist_free (to_select);
541         item = (SPItem *) newObj;
542       //  selection->add(item);
543     }
544 */
545    /*if (SP_IS_GROUP(item) && !SP_IS_BOX3D(item)) {
546         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
547             if (SP_IS_ITEM(child)) {
548                 if (sp_spray_dilate_recursive (desktop,selection, SP_ITEM(child), p, vector, mode, radius, force, population, scale, scale_min, scale_max, reverse, mean, standard_deviation,ratio,tilt, rot_min, rot_max,_distrib))
549                     did = true;
550             }
551         }
553     } else {*/
554         if (mode == SPRAY_MODE_COPY) {
556             Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
557             if (a) {
558                 double dr; double dp;
559                 random_position(dr,dp,mean,standard_deviation,_distrib);
560                 dr=dr*radius;
561                 double _fid = g_random_double_range(0,1);
562                 SPItem *item_copied;
563                 double angle = g_random_double_range(rot_min, rot_max);
564                 double _scale = g_random_double_range(scale_min, scale_max);
565                 if(_fid<=population)
566                 {
567                           // duplicate
568                             SPDocument *doc = SP_OBJECT_DOCUMENT(item);
569                             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
570                             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
571                             Inkscape::XML::Node *parent = old_repr->parent();
572                             Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
573                             parent->appendChild(copy);
575                             SPObject *new_obj = doc->getObjectByRepr(copy);
576                             item_copied = (SPItem *) new_obj;   //convertion object->item
577                             Geom::Point center=item->getCenter();
578                             sp_spray_scale_rel(center,desktop,item_copied, Geom::Scale(_scale,_scale));
579                             sp_spray_scale_rel(center,desktop,item_copied, Geom::Scale(scale,scale));
581                             sp_spray_rotate_rel(center,desktop,item_copied, Geom::Rotate(angle));
582                             Geom::Point move = (Geom::Point(cos(tilt)*cos(dp)*dr/(1-ratio)+sin(tilt)*sin(dp)*dr/(1+ratio),-sin(tilt)*cos(dp)*dr/(1-ratio)+cos(tilt)*sin(dp)*dr/(1+ratio)))+(p-a->midpoint());//Move the cursor p
583                             sp_item_move_rel(item_copied, Geom::Translate(move[Geom::X], -move[Geom::Y]));
589                         did = true;
590                         }
591             }
593         } else if (mode == SPRAY_MODE_SINGLE_PATH) {
596             SPItem *Pere; //Objet initial
597             SPItem *item_copied;//Objet projeté
598             SPItem *Union;//Union précédente
599             SPItem *fils;//Copie du père
601            // GSList *items = g_slist_copy((GSList *) selection->itemList()); //Récupère la liste des objects sélectionnés
602 //Pere = (SPItem *) items->data;//Le premier objet est le père du spray
604             int i=1;
605             for (GSList *items = g_slist_copy((GSList *) selection->itemList());
606                     items != NULL;
607                     items = items->next) {
609                 SPItem *item1 = (SPItem *) items->data;
610                 if (i==1) {
611                     Pere=item1;
612                 }
613                 if (i==2) {
614                     Union=item1;
615                 }
616                 i++;
617             }
618             SPDocument *doc = SP_OBJECT_DOCUMENT(Pere);
619             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
620             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(Pere);
621             //SPObject *old_obj = doc->getObjectByRepr(old_repr);
622             Inkscape::XML::Node *parent = old_repr->parent();
624             Geom::OptRect a = Pere->getBounds(sp_item_i2doc_affine(Pere));
625             if (a) {
626                 double dr; double dp; //initialisation des variables
627                 random_position(dr,dp,mean,standard_deviation,_distrib);
628                 dr=dr*radius;
629                 double _fid = g_random_double_range(0,1);
630                 double angle = (g_random_double_range(rot_min, rot_max));
631                 double _scale = g_random_double_range(scale_min, scale_max);
632                 if (i==2) {
633                     Inkscape::XML::Node *copy1 = old_repr->duplicate(xml_doc);
634                     parent->appendChild(copy1);
635                     SPObject *new_obj1 = doc->getObjectByRepr(copy1);
636                     fils = (SPItem *) new_obj1;   //conversion object->item
637                     Union=fils;
638                     Inkscape::GC::release(copy1);
639                    }
641                 if (_fid<=population) { //Rules the population of objects sprayed
642                     // duplicates the father
643                     Inkscape::XML::Node *copy2 = old_repr->duplicate(xml_doc);
644                     parent->appendChild(copy2);
645                     SPObject *new_obj2 = doc->getObjectByRepr(copy2);
646                     item_copied = (SPItem *) new_obj2;
648                     Geom::Point move = (Geom::Point(cos(tilt)*cos(dp)*dr/(1-ratio)+sin(tilt)*sin(dp)*dr/(1+ratio),-sin(tilt)*cos(dp)*dr/(1-ratio)+cos(tilt)*sin(dp)*dr/(1+ratio)))+(p-a->midpoint());//Move around the cursor
650                             Geom::Point center=Pere->getCenter();
651                             sp_spray_scale_rel(center,desktop,item_copied, Geom::Scale(_scale,_scale));
652                             sp_spray_scale_rel(center,desktop,item_copied, Geom::Scale(scale,scale));
653                             sp_spray_rotate_rel(center,desktop,item_copied, Geom::Rotate(angle));
654                             sp_item_move_rel(item_copied, Geom::Translate(move[Geom::X], -move[Geom::Y]));
656 //UNION et surduplication
657                     selection->clear();
658                     selection->add(item_copied);
659                     selection->add(Union);
660                     sp_selected_path_union(selection->desktop());
661                     selection->add(Pere);
662                     Inkscape::GC::release(copy2);
663                     did = true;
664                 }
666             }
667         } else if (mode == SPRAY_MODE_CLONE) {
669         Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
670                 if (a) {
671                 double dr; double dp;
672                 random_position(dr,dp,mean,standard_deviation,_distrib);
673                 dr=dr*radius;
674                 double _fid = g_random_double_range(0,1);
675                 double angle = (g_random_double_range(rot_min, rot_max));
676                 double _scale = g_random_double_range(scale_min, scale_max);
678         if(_fid<=population)
679                 {
680                             SPItem *item_copied;
681                             SPDocument *doc = SP_OBJECT_DOCUMENT(item);
682                             Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
683                             Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
684                             Inkscape::XML::Node *parent = old_repr->parent();
686                             //Creation of the clone
687                             Inkscape::XML::Node *clone = xml_doc->createElement("svg:use");
688                             parent->appendChild(clone); //Ajout du clone à la liste d'enfants du père (selection initiale
689                             clone->setAttribute("xlink:href", g_strdup_printf("#%s", old_repr->attribute("id")), false); //Génère le lien entre les attributs du père et du fils
691                             SPObject *clone_object = doc->getObjectByRepr(clone);
692                             item_copied = (SPItem *) clone_object;//conversion object->item
693                               Geom::Point center=item->getCenter();
694                             sp_spray_scale_rel(center,desktop,item_copied, Geom::Scale(_scale,_scale));
695                             sp_spray_scale_rel(center,desktop,item_copied, Geom::Scale(scale,scale));
696                             sp_spray_rotate_rel(center,desktop,item_copied, Geom::Rotate(angle));
697                             Geom::Point move = (Geom::Point(cos(tilt)*cos(dp)*dr/(1-ratio)+sin(tilt)*sin(dp)*dr/(1+ratio),-sin(tilt)*cos(dp)*dr/(1-ratio)+cos(tilt)*sin(dp)*dr/(1+ratio)))+(p-a->midpoint());
698                             sp_item_move_rel(item_copied, Geom::Translate(move[Geom::X], -move[Geom::Y]));
700                             Inkscape::GC::release(clone);
702                             did = true;
703                 } }}
704                 return did;
709 bool sp_spray_color_recursive(guint /*mode*/,
710                               SPItem */*item*/,
711                               SPItem */*item_at_point*/,
712                               guint32 /*fill_goal*/,
713                               bool /*do_fill*/,
714                               guint32 /*stroke_goal*/,
715                               bool /*do_stroke*/,
716                               float /*opacity_goal*/,
717                               bool /*do_opacity*/,
718                               bool /*do_blur*/,
719                               bool /*reverse*/,
720                               Geom::Point /*p*/,
721                               double /*radius*/,
722                               double /*force*/,
723                               bool /*do_h*/,
724                               bool /*do_s*/,
725                               bool /*do_l*/,
726                               bool /*do_o*/)
728     bool did = false;
730     return did;
734 bool sp_spray_dilate(SPSprayContext *tc, Geom::Point /*event_p*/, Geom::Point p, Geom::Point vector, bool reverse)
736     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
737     SPDesktop *desktop = SP_EVENT_CONTEXT(tc)->desktop;
740     if (selection->isEmpty()) {
741         return false;
742     }
744     bool did = false;
745     double radius = get_dilate_radius(tc);
749     bool do_fill = false, do_stroke = false, do_opacity = false;
750     guint32 fill_goal = sp_desktop_get_color_tool(desktop, "/tools/spray", true, &do_fill);
751     guint32 stroke_goal = sp_desktop_get_color_tool(desktop, "/tools/spray", false, &do_stroke);
752     double opacity_goal = sp_desktop_get_master_opacity_tool(desktop, "/tools/spray", &do_opacity);
753     if (reverse) {
754 #if 0
755         // HSL inversion
756         float hsv[3];
757         float rgb[3];
758         sp_color_rgb_to_hsv_floatv (hsv,
759                                     SP_RGBA32_R_F(fill_goal),
760                                     SP_RGBA32_G_F(fill_goal),
761                                     SP_RGBA32_B_F(fill_goal));
762         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
763         fill_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
764         sp_color_rgb_to_hsv_floatv (hsv,
765                                     SP_RGBA32_R_F(stroke_goal),
766                                     SP_RGBA32_G_F(stroke_goal),
767                                     SP_RGBA32_B_F(stroke_goal));
768         sp_color_hsv_to_rgb_floatv (rgb, hsv[0]<.5? hsv[0]+.5 : hsv[0]-.5, 1 - hsv[1], 1 - hsv[2]);
769         stroke_goal = SP_RGBA32_F_COMPOSE(rgb[0], rgb[1], rgb[2], 1);
770 #else
771         // RGB inversion
772         fill_goal = SP_RGBA32_U_COMPOSE(
773             (255 - SP_RGBA32_R_U(fill_goal)),
774             (255 - SP_RGBA32_G_U(fill_goal)),
775             (255 - SP_RGBA32_B_U(fill_goal)),
776             (255 - SP_RGBA32_A_U(fill_goal)));
777         stroke_goal = SP_RGBA32_U_COMPOSE(
778             (255 - SP_RGBA32_R_U(stroke_goal)),
779             (255 - SP_RGBA32_G_U(stroke_goal)),
780             (255 - SP_RGBA32_B_U(stroke_goal)),
781             (255 - SP_RGBA32_A_U(stroke_goal)));
782 #endif
783         opacity_goal = 1 - opacity_goal;
784     }
786     double path_force = get_path_force(tc);
787     if (radius == 0 || path_force == 0) {
788         return false;
789     }
790     double path_mean = get_path_mean(tc);
791     if (radius == 0 || path_mean == 0) {
792         return false;
793     }
794     double path_standard_deviation = get_path_standard_deviation(tc);
795     if (radius == 0 || path_standard_deviation == 0) {
796         return false;
797     }
798     double move_force = get_move_force(tc);
799     double move_mean = get_move_mean(tc);
800     double move_standard_deviation = get_move_standard_deviation(tc);
803     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
804          items != NULL;
805          items = items->next) {
807         SPItem *item = (SPItem *) items->data;
809         /*if (is_color_modes (tc->mode)) {
810             if (do_fill || do_stroke || do_opacity) {
811                 if (sp_spray_color_recursive (tc->mode, item, item_at_point,
812                                           fill_goal, do_fill,
813                                           stroke_goal, do_stroke,
814                                           opacity_goal, do_opacity,
815                                           tc->mode == SPRAY_MODE_BLUR, reverse,
816                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
817                 did = true;
818             }
819         }else*/ if (is_transform_modes(tc->mode)) {
820             if (sp_spray_dilate_recursive (desktop,selection, item, p, vector, tc->mode, radius, move_force, tc->population,tc->scale, tc->scale_min, tc->scale_max, reverse, move_mean, move_standard_deviation,tc->ratio,tc->tilt, tc->rot_min, tc->rot_max, tc->distrib))
821                 did = true;
822         } else {
823             if (sp_spray_dilate_recursive (desktop,selection, item, p, vector, tc->mode, radius, path_force, tc->population,tc->scale, tc->scale_min, tc->scale_max, reverse, path_mean, path_standard_deviation,tc->ratio,tc->tilt, tc->rot_min, tc->rot_max, tc->distrib))
824                 did = true;
825         }
826     }
828     return did;
831 void sp_spray_update_area(SPSprayContext *tc)
833         double radius = get_dilate_radius(tc);
834         Geom::Matrix const sm ( Geom::Scale(radius/(1-tc->ratio), radius/(1+tc->ratio)) );
835         sp_canvas_item_affine_absolute(tc->dilate_area,   (sm* Geom::Rotate(tc->tilt))* Geom::Translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
836         sp_canvas_item_show(tc->dilate_area);
839 void sp_spray_switch_mode(SPSprayContext *tc, gint mode, bool with_shift)
841     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("spray_tool_mode", mode); //sélectionne le bouton numéro "mode"
842     // need to set explicitly, because the prefs may not have changed by the previous
843     tc->mode = mode;
844     sp_spray_update_cursor (tc, with_shift);
847 void sp_spray_switch_mode_temporarily(SPSprayContext *tc, gint mode, bool with_shift)
849     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
850    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
851    gint now_mode = prefs->getInt("/tools/spray/mode", 0);
852    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("spray_tool_mode", mode);
853    // button has changed prefs, restore
854    prefs->setInt("/tools/spray/mode", now_mode);
855    // changing prefs changed tc->mode, restore back :)
856    tc->mode = mode;
857    sp_spray_update_cursor (tc, with_shift);
860 gint sp_spray_context_root_handler(SPEventContext *event_context,
861                                   GdkEvent *event)
863     SPSprayContext *tc = SP_SPRAY_CONTEXT(event_context);
864     SPDesktop *desktop = event_context->desktop;
866     gint ret = FALSE;
868     switch (event->type) {
869         case GDK_ENTER_NOTIFY:
870             sp_canvas_item_show(tc->dilate_area);
871             break;
872         case GDK_LEAVE_NOTIFY:
873             sp_canvas_item_hide(tc->dilate_area);
874             break;
875         case GDK_BUTTON_PRESS:
876             if (event->button.button == 1 && !event_context->space_panning) {
878                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
879                     return TRUE;
880                 }
882                 Geom::Point const motion_w(event->button.x,
883                                          event->button.y);
884                 Geom::Point const motion_dt(desktop->w2d(motion_w));
885                 tc->last_push = desktop->dt2doc(motion_dt);
887                 sp_spray_extinput(tc, event);
889                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
890                 tc->is_drawing = true;
891                 tc->is_dilating = true;
892                                 tc->has_dilated = false;
896                 if(tc->is_dilating && event->button.button == 1 && !event_context->space_panning)
898                                 sp_spray_dilate (tc, motion_w, desktop->dt2doc(motion_dt), Geom::Point(0,0), MOD__SHIFT);
902                                                           tc->has_dilated=true;
904                 ret = TRUE;
905             }
906             break;
907         case GDK_MOTION_NOTIFY:
908         {
909             Geom::Point const motion_w(event->motion.x,
910                                      event->motion.y);
911             Geom::Point motion_dt(desktop->w2d(motion_w));
912             Geom::Point motion_doc(desktop->dt2doc(motion_dt));
913             sp_spray_extinput(tc, event);
915             // draw the dilating cursor
916                 double radius = get_dilate_radius(tc);
917                 Geom::Matrix const sm (Geom::Scale(radius/(1-tc->ratio), radius/(1+tc->ratio)) );
918                 sp_canvas_item_affine_absolute(tc->dilate_area, (sm*Geom::Rotate(tc->tilt))*Geom::Translate(desktop->w2d(motion_w)));
919                 sp_canvas_item_show(tc->dilate_area);
921                 guint num = 0;
922                 if (!desktop->selection->isEmpty()) {
923                     num = g_slist_length((GSList *) desktop->selection->itemList());
924                 }
925                 if (num == 0) {
926                     tc->_message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to spray."));
927                 }
929             // dilating:
930             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
931                 sp_spray_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
932                 //tc->last_push = motion_doc;
933                 tc->has_dilated = true;
935                 // it's slow, so prevent clogging up with events
936                 gobble_motion_events(GDK_BUTTON1_MASK);
937                 return TRUE;
938             }
940         }
941         break;
942 /*Spray with the scroll*/
943         case GDK_SCROLL:
944                {
945               if (event->scroll.state & GDK_BUTTON1_MASK)
946                                   {
947                   double temp ;
948                   temp=tc->population;
949                   tc->population=1.0;
950                   desktop->setToolboxAdjustmentValue ("population", tc->population * 100);
951                   Geom::Point const scroll_w(event->button.x,event->button.y);
952                   Geom::Point const scroll_dt = desktop->point();;
953                   Geom::Point motion_doc(desktop->dt2doc(scroll_dt));
954                        switch (event->scroll.direction)
955                        {
956                          case GDK_SCROLL_UP:
957                            {
958                                    if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false)
959                                    {
960                                     return TRUE;
961                                    }
962                                    tc->last_push = desktop->dt2doc(scroll_dt);
963                                    sp_spray_extinput(tc, event);
964                                    sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
965                                    tc->is_drawing = true;
966                                    tc->is_dilating = true;
967                                    tc->has_dilated = false;
968                                    if(tc->is_dilating && !event_context->space_panning)
970                                                         sp_spray_dilate (tc, scroll_w, desktop->dt2doc(scroll_dt), Geom::Point(0,0),false);
974                                    tc->has_dilated=true;
975                                    tc->population=temp;
977                                    desktop->setToolboxAdjustmentValue ("population", tc->population * 100);
979                                    ret = TRUE;
980                            }
981                                break;
982                            case GDK_SCROLL_DOWN:
983                            {
984                                    if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false)
985                                                                    {
986                                                                     return TRUE;
987                                                                    }
988                                                                    tc->last_push = desktop->dt2doc(scroll_dt);
989                                                                    sp_spray_extinput(tc, event);
990                                                                    sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
991                                                                    tc->is_drawing = true;
992                                                                    tc->is_dilating = true;
993                                                                    tc->has_dilated = false;
994                                                                    if(tc->is_dilating && !event_context->space_panning)
995                                                                                         sp_spray_dilate (tc, scroll_w, desktop->dt2doc(scroll_dt), Geom::Point(0,0), false);
997                                                                    tc->has_dilated=true;
999                                                                    ret = TRUE;
1002                            }
1003                                break;
1004 case GDK_SCROLL_RIGHT:
1005                            {} break;
1006 case GDK_SCROLL_LEFT:
1007                            {} break;
1008                                                                                                                    }
1009                                   }
1012                                break;
1014                }
1015     case GDK_BUTTON_RELEASE:
1016     {
1017         Geom::Point const motion_w(event->button.x, event->button.y);
1018         Geom::Point const motion_dt(desktop->w2d(motion_w));
1020         sp_canvas_end_forced_full_redraws(desktop->canvas);
1021         tc->is_drawing = false;
1023         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1024             if (!tc->has_dilated) {
1025                 // if we did not rub, do a light tap
1026                 tc->pressure = 0.03;
1027                 sp_spray_dilate (tc, motion_w, desktop->dt2doc(motion_dt), Geom::Point(0,0), MOD__SHIFT);
1028             }
1029             tc->is_dilating = false;
1030             tc->has_dilated = false;
1031             switch (tc->mode) {
1032                 case SPRAY_MODE_COPY:
1033                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1034                                      SP_VERB_CONTEXT_SPRAY, _("Spray with copies"));
1035                     break;
1036                 case SPRAY_MODE_CLONE:
1037                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1038                                      SP_VERB_CONTEXT_SPRAY, _("Spray with clones"));
1039                     break;
1040                 case SPRAY_MODE_SINGLE_PATH:
1041                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1042                                      SP_VERB_CONTEXT_SPRAY, _("Spray in single path"));
1043                     break;
1044             }
1045         }
1046         break;
1047     }
1049     case GDK_KEY_PRESS:
1050         switch (get_group0_keyval (&event->key)) {
1051 case GDK_j:  if (MOD__SHIFT_ONLY) {
1052                 sp_spray_switch_mode(tc, SPRAY_MODE_COPY, MOD__SHIFT);
1053                 ret = TRUE;
1054             }
1055 case GDK_J: if (MOD__SHIFT_ONLY) {
1056                 sp_spray_switch_mode(tc, SPRAY_MODE_COPY, MOD__SHIFT);
1057                 ret = TRUE;
1058             }
1060 break;
1061         case GDK_m:
1062         case GDK_M:
1063         case GDK_0:
1065             break;
1066         case GDK_i:
1067         case GDK_I:
1068         case GDK_k:    if (MOD__SHIFT_ONLY) {
1069                 sp_spray_switch_mode(tc, SPRAY_MODE_SINGLE_PATH, MOD__SHIFT);
1070                 ret = TRUE;
1071             }
1072         case GDK_K:if (MOD__SHIFT_ONLY) {
1073                 sp_spray_switch_mode(tc, SPRAY_MODE_SINGLE_PATH, MOD__SHIFT);
1074                 ret = TRUE;
1075             }
1076 break;
1078         case GDK_l: if (MOD__SHIFT_ONLY) {
1079                 sp_spray_switch_mode(tc, SPRAY_MODE_CLONE, MOD__SHIFT);
1080                 ret = TRUE;
1081             }
1083         case GDK_L:
1084  if (MOD__SHIFT_ONLY) {
1085                 sp_spray_switch_mode(tc, SPRAY_MODE_CLONE, MOD__SHIFT);
1086                 ret = TRUE;
1087             }
1088             break;
1089         case GDK_Up:
1090         case GDK_KP_Up:
1091             if (!MOD__CTRL_ONLY) {
1092                 tc->scale += 0.05;
1094                 //desktop->setToolboxAdjustmentValue ("spray-force", tc->force * 100);
1095                 ret = TRUE;
1096             }
1097             break;
1098         case GDK_Down:
1099         case GDK_KP_Down:
1100             if (!MOD__CTRL_ONLY) {
1102                 tc->scale -= 0.05;
1103                 if (tc->scale < 0.0)
1104                     tc->scale = 0.0;
1105                 //desktop->setToolboxAdjustmentValue ("spray-force", tc->force * 100);
1107                 ret = TRUE;
1109             }
1110             break;
1111         case GDK_Right:
1112         case GDK_KP_Right:
1113             if (!MOD__CTRL_ONLY) {
1114                 tc->width += 0.01;
1115                 if (tc->width > 1.0)
1116                     tc->width = 1.0;
1117                 desktop->setToolboxAdjustmentValue ("altx-spray", tc->width * 100); // the same spinbutton is for alt+x
1118                 sp_spray_update_area(tc);
1119                 ret = TRUE;
1120             }
1121             break;
1122         case GDK_Left:
1123         case GDK_KP_Left:
1124             if (!MOD__CTRL_ONLY) {
1125                 tc->width -= 0.01;
1126                 if (tc->width < 0.01)
1127                     tc->width = 0.01;
1128                 desktop->setToolboxAdjustmentValue ("altx-spray", tc->width * 100);
1129                 sp_spray_update_area(tc);
1130                 ret = TRUE;
1131             }
1132             break;
1133         case GDK_Home:
1134         case GDK_KP_Home:
1135             tc->width = 0.01;
1136             desktop->setToolboxAdjustmentValue ("altx-spray", tc->width * 100);
1137             sp_spray_update_area(tc);
1138             ret = TRUE;
1139             break;
1140         case GDK_End:
1141         case GDK_KP_End:
1142             tc->width = 1.0;
1143             desktop->setToolboxAdjustmentValue ("altx-spray", tc->width * 100);
1144             sp_spray_update_area(tc);
1145             ret = TRUE;
1146             break;
1147         case GDK_x:
1148         case GDK_X:
1149             if (MOD__ALT_ONLY) {
1150                 desktop->setToolboxFocusTo ("altx-spray");
1151                 ret = TRUE;
1152             }
1153             break;
1155         case GDK_Shift_L:
1156         case GDK_Shift_R:
1157             sp_spray_update_cursor(tc, true);
1158             break;
1159 /*Set the scale to 1*/
1160         case GDK_Control_L:
1161                 tc->scale=1;
1162         default:
1163             break;
1164         }
1165         break;
1167     case GDK_KEY_RELEASE: {
1168         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1169         switch (get_group0_keyval(&event->key)) {
1170             case GDK_Shift_L:
1171             case GDK_Shift_R:
1172                 sp_spray_update_cursor(tc, false);
1173                 break;
1174             case GDK_Control_L:
1175             case GDK_Control_R:
1176                 sp_spray_switch_mode (tc, prefs->getInt("/tools/spray/mode"), MOD__SHIFT);
1177                 tc->_message_context->clear();
1178                 break;
1179             default:
1180                 sp_spray_switch_mode (tc, prefs->getInt("/tools/spray/mode"), MOD__SHIFT);
1181                 break;
1182         }
1183     }
1185     default:
1186         break;
1187     }
1189     if (!ret) {
1190         if (((SPEventContextClass *) parent_class)->root_handler) {
1191             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1192         }
1193     }
1195     return ret;
1198 /*
1199   Local Variables:
1200   mode:c++
1201   c-file-style:"stroustrup"
1202   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1203   indent-tabs-mode:nil
1204   fill-column:99
1205   End:
1206 */
1207 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :