spray-tool.cpp revision dfb8610808350871e9cfe90d551b9a0a38bcb4c4
/*
* Spray Tool
*
* Authors:
* Pierre-Antoine MARC
* Pierre CACLIN
* Aurel-Aimé MARMION
* Julien LERAY
* Benoît LAVORATA
* Vincent MONTAGNE
* Pierre BARBRY-BLOT
* Steren GIANNINI (steren.giannini@gmail.com)
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
* Jabiertxo Arraiza <jabier.arraiza@marker.es>
*
* Copyright (C) 2009 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "config.h"
#include <numeric>
#include "ui/dialog/dialog-manager.h"
#include <glib.h>
#include "macros.h"
#include "document.h"
#include "document-undo.h"
#include "selection.h"
#include "desktop.h"
#include "desktop-events.h"
#include "message-context.h"
#include "pixmaps/cursor-spray.xpm"
#include <boost/optional.hpp>
#include "context-fns.h"
#include "sp-item.h"
#include "inkscape.h"
#include "splivarot.h"
#include "sp-item-group.h"
#include "sp-shape.h"
#include "sp-path.h"
#include "path-chemistry.h"
// For color picking
#include "display/drawing-context.h"
#include "display/cairo-utils.h"
#include "desktop-style.h"
#include "svg/svg-color.h"
#include "sp-text.h"
#include "sp-root.h"
#include "sp-flowtext.h"
#include "display/sp-canvas.h"
#include "display/canvas-bpath.h"
#include "display/canvas-arena.h"
#include "preferences.h"
#include "style.h"
#include "box3d.h"
#include "sp-item-transform.h"
#include "filter-chemistry.h"
#include "ui/tools/spray-tool.h"
#include "verbs.h"
#include <iostream>
#include <gdk/gdkkeysyms.h>
using Inkscape::DocumentUndo;
using namespace std;
#define DDC_RED_RGBA 0xff0000ff
#define DYNA_MIN_WIDTH 1.0e-6
// Disabled in 0.91 because of Bug #1274831 (crash, spraying an object
// with the mode: spray object in single path)
// Please enable again when working on 1.0
#define ENABLE_SPRAY_MODE_SINGLE_PATH
namespace Inkscape {
namespace UI {
namespace Tools {
enum {
};
}
/**
* This function returns pseudo-random numbers from a normal distribution
* @param mu : mean
* @param sigma : standard deviation ( > 0 )
*/
{
// use Box Muller's algorithm
return mu + sigma * sqrt( -2.0 * log(g_random_double_range(0, 1)) ) * cos( 2.0*M_PI*g_random_double_range(0, 1) );
}
/* Method to rotate items */
static void sp_spray_rotate_rel(Geom::Point c, SPDesktop */*desktop*/, SPItem *item, Geom::Rotate const &rotation)
{
// Rotate item.
// Use each item's own transform writer, consistent with sp_selection_apply_affine()
// Restore the center position (it's changed because the bbox center changed)
if (item->isCenterSet()) {
item->updateRepr();
}
}
/* Method to scale items */
static void sp_spray_scale_rel(Geom::Point c, SPDesktop */*desktop*/, SPItem *item, Geom::Scale const &scale)
{
}
, dragging(false)
, usepressurewidth(false)
, usepressurepopulation(false)
, usepressurescale(false)
, usetilt(false)
, usetext(false)
, width(0.2)
, ratio(0)
, tilt(0)
, rotation_variation(0)
, population(0)
, scale_variation(1)
, scale(1)
, mean(0.2)
, standard_deviation(0.2)
, distrib(1)
, mode(0)
, is_drawing(false)
, is_dilating(false)
, has_dilated(false)
, dilate_area(NULL)
, nooverlap(false)
, picker(false)
, pickinversesize(false)
, pickfill(false)
, pickstroke(false)
, visible(false)
, offset(0)
{
}
this->enableGrDrag(false);
this->style_set_connection.disconnect();
if (this->dilate_area) {
sp_canvas_item_destroy(this->dilate_area);
this->dilate_area = NULL;
}
}
sel_message = g_strdup_printf(ngettext("<b>%i</b> object selected","<b>%i</b> objects selected",num), num);
} else {
}
switch (this->mode) {
case SPRAY_MODE_COPY:
this->message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag, click or click and scroll to spray <b>copies</b> of the initial selection."), sel_message);
break;
case SPRAY_MODE_CLONE:
this->message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag, click or click and scroll to spray <b>clones</b> of the initial selection."), sel_message);
break;
case SPRAY_MODE_SINGLE_PATH:
this->message_context->setF(Inkscape::NORMAL_MESSAGE, _("%s. Drag, click or click and scroll to spray in a <b>single path</b> of the initial selection."), sel_message);
break;
default:
break;
}
this->sp_event_context_update_cursor();
}
{
/* TODO: have a look at sp_dyna_draw_context_setup where the same is done.. generalize? at least make it an arcto! */
c->unref();
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_item_hide(this->dilate_area);
}
this->is_drawing = false;
sp_event_context_read(this, "distrib");
sp_event_context_read(this, "width");
sp_event_context_read(this, "ratio");
sp_event_context_read(this, "tilt");
sp_event_context_read(this, "rotation_variation");
sp_event_context_read(this, "scale_variation");
sp_event_context_read(this, "mode");
sp_event_context_read(this, "population");
sp_event_context_read(this, "mean");
sp_event_context_read(this, "standard_deviation");
sp_event_context_read(this, "usepressurewidth");
sp_event_context_read(this, "usepressurepopulation");
sp_event_context_read(this, "usepressurescale");
sp_event_context_read(this, "Scale");
sp_event_context_read(this, "offset");
sp_event_context_read(this, "picker");
sp_event_context_read(this, "pickinversesize");
sp_event_context_read(this, "pickfill");
sp_event_context_read(this, "pickstroke");
sp_event_context_read(this, "visible");
sp_event_context_read(this, "nooverlap");
this->enableSelectionCue();
}
this->enableGrDrag();
}
}
if (path == "mode") {
this->update_cursor(false);
} else if (path == "width") {
} else if (path == "usepressurewidth") {
} else if (path == "usepressurepopulation") {
} else if (path == "usepressurescale") {
} else if (path == "population") {
} else if (path == "rotation_variation") {
} else if (path == "scale_variation") {
} else if (path == "standard_deviation") {
} else if (path == "mean") {
// Not implemented in the toolbar and preferences yet
} else if (path == "distribution") {
} else if (path == "tilt") {
} else if (path == "ratio") {
} else if (path == "offset") {
} else if (path == "picker") {
} else if (path == "pickinversesize") {
} else if (path == "pickfill") {
} else if (path == "pickstroke") {
} else if (path == "visible") {
} else if (path == "nooverlap") {
}
}
{
} else {
}
}
{
//g_warning("Pressure, population: %f, %f", pressure, pressure * tc->population);
}
{
}
{
}
{
return tc->standard_deviation;
}
{
//g_warning("Pressure, population: %f, %f", pressure, pressure * tc->population);
}
{
//g_warning("Pressure, population: %f, %f", pressure, pressure * tc->population);
return pressure;
}
{
}
{
return tc->standard_deviation;
}
/**
* Method to handle the distribution of the items
* @param[out] radius : radius of the position of the sprayed object
* @param[out] angle : angle of the position of the sprayed object
* @param[in] a : mean
* @param[in] s : standard deviation
* @param[in] choice :
*/
{
// angle is taken from an uniform distribution
// radius is taken from a Normal Distribution
double radius_temp =-1;
{
radius_temp = NormalDistribution(a, s);
}
// Because we are in polar coordinates, a special treatment has to be done to the radius.
// Otherwise, positions taken from an uniform repartition on radius and angle will not seam to
// be uniformily distributed on the disk (more at the center and less at the boundary).
// We counter this effect with a 0.5 exponent. This is empiric.
}
static void sp_spray_transform_path(SPItem * item, Geom::Path &path, Geom::Affine affine, Geom::Point center){
} else {
}
Geom::Affine i2dt = item->i2dt_affine() * Geom::Translate(center).inverse() * affine * Geom::Translate(center);
}
/**
Randomizes \a val by \a rand, with 0 < val < 1 and all values (including 0, 1) having the same
probability of being displaced.
*/
{
if (base < 0) {
base = 0;
}
return CLAMP(val, 0, 1); // this should be unnecessary with the above provisions, but just in case...
}
double angle,
double &_scale,
double scale,
bool picker,
bool pickinversesize,
bool pickfill,
bool pickstroke,
bool visible,
bool nooverlap,
double offset,
bool trace_scale)
{
if(offset_min < 0 ){
offset_min = 0;
}
Geom::OptRect bbox_procesed = Geom::Rect(Geom::Point(bbox->left() - offset_min, bbox->top() - offset_min),Geom::Point(bbox->right() + offset_min, bbox->bottom() + offset_min));
if(offset < 100 ){
} else {
offset_min = 0;
}
std::vector<SPItem*> items_down = desktop->getDocument()->getItemsPartiallyInBox(desktop->dkey, *bbox_procesed);
return false;
}
SPItem *item_selected = *j;
gchar const * spray_origin;
} else {
}
{
if(nooverlap){
return false;
}
item_down->updateRepr();
}
}
}
}
if(!nooverlap){
doc->ensureUpToDate();
}
double opacity = 1.0;
Geom::IntRect area = Geom::IntRect::from_xywh(floor(mid_point[Geom::X]), floor(mid_point[Geom::Y]), 1, 1);
double R = 0, G = 0, B = 0, A = 0;
ink_cairo_surface_average_color(s, R, G, B, A);
float r = SP_RGBA32_R_F(rgba);
float g = SP_RGBA32_G_F(rgba);
float b = SP_RGBA32_B_F(rgba);
float a = SP_RGBA32_A_F(rgba);
//this can fix the bug #1511998 if confirmed
if( a == 0 && r == 0 && g == 0 && b == 0){
r = 1;
g = 1;
b = 1;
}
if(visible && (a == 0 || a < 1e-6)){
return false;
}
if(picker){
float hsl[3];
sp_color_rgb_to_hsl_floatv (hsl, r, g, b);
switch (pick) {
case PICK_COLOR:
break;
case PICK_OPACITY:
val = a;
break;
case PICK_R:
val = r;
break;
case PICK_G:
val = g;
break;
case PICK_B:
val = b;
break;
case PICK_H:
break;
case PICK_S:
break;
case PICK_L:
break;
default:
break;
}
if (rand_picked > 0) {
r = randomize01 (r, rand_picked);
g = randomize01 (g, rand_picked);
b = randomize01 (b, rand_picked);
}
if (gamma_picked != 0) {
double power;
if (gamma_picked > 0)
else
}
if (invert_picked) {
r = 1 - r;
g = 1 - g;
b = 1 - b;
}
r = CLAMP (r, 0, 1);
g = CLAMP (g, 0, 1);
b = CLAMP (b, 0, 1);
// recompose tweaked color
rgba = SP_RGBA32_F_COMPOSE(r, g, b, a);
if (pick_to_size) {
if(!trace_scale){
if(pickinversesize) {
} else {
}
if(_scale == 0.0) {
return false;
}
item,
bbox,
move,
css,
true)){
return false;
}
}
}
if (pick_to_opacity) {
opacity_str << opacity;
}
if (pick_to_presence) {
//Hidding the element is a way to retain original
//beabiohur of tiled clones for presence option.
}
}
if (pick_to_color) {
if(pickfill){
}
if(pickstroke){
}
}
return false;
}
}
SPItem *item_hidden = *k;
item_hidden->setHidden(false);
}
}
}
return true;
}
double radius,
double population,
double &scale,
double scale_variation,
bool /*reverse*/,
double mean,
double standard_deviation,
double ratio,
double tilt,
double rotation_variation,
bool nooverlap,
bool picker,
bool pickinversesize,
bool pickfill,
bool pickstroke,
bool visible,
double offset,
bool usepressurescale,
double pressure)
{
bool did = false;
{
if (box) {
// convert 3D boxes to ordinary groups before spraying their shapes
}
}
double angle = g_random_double_range( - rotation_variation / 100.0 * M_PI , rotation_variation / 100.0 * M_PI );
double _scale = g_random_double_range( 1.0 - scale_variation / 100.0, 1.0 + scale_variation / 100.0 );
if(usepressurescale){
}
if (mode == SPRAY_MODE_COPY) {
if (a) {
if(_fid <= population)
{
gchar const * spray_origin;
} else {
}
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());
if(!fit_item(desktop, item, a, move, center, angle, _scale, scale, picker, pickinversesize, pickfill, pickstroke, visible, nooverlap, offset, css, false)){
return false;
}
}
// Duplicate
}
// Move the cursor p
if(picker){
}
did = true;
}
}
} else if (mode == SPRAY_MODE_SINGLE_PATH) {
int i=1;
if (i == 1) {
parent_item = item1;
}
if (i == 2) {
unionResult = item1;
}
i++;
}
if (parent_item) {
if (a) {
// Duplicates the parent item
gchar const * spray_origin;
} else {
}
// Move around the cursor
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());
// Union and duplication
if (unionResult) { // No need to add the very first item (initialized with NULL).
}
did = true;
}
}
}
#endif
} else if (mode == SPRAY_MODE_CLONE) {
if (a) {
if(_fid <= population) {
gchar const * spray_origin;
} else {
}
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());
if(!fit_item(desktop, item, a, move, center, angle, _scale, scale, picker, pickinversesize, pickfill, pickstroke, visible, nooverlap, offset, css, false)){
return false;
}
}
// Creation of the clone
// Ad the clone to the list of the parent's children
// Generates the link between parent and child attributes
}
// Conversion object->item
if(picker){
}
did = true;
}
}
}
return did;
}
static bool sp_spray_dilate(SprayTool *tc, Geom::Point /*event_p*/, Geom::Point p, Geom::Point vector, bool reverse)
{
return false;
}
bool did = false;
if (radius == 0 || population == 0) {
return false;
}
return false;
}
if (radius == 0 || path_standard_deviation == 0) {
return false;
}
{
}
if (sp_spray_recursive(desktop, selection, item, p, vector, tc->mode, radius, population, tc->scale, tc->scale_variation, reverse, move_mean, move_standard_deviation, tc->ratio, tc->tilt, tc->rotation_variation, tc->distrib, tc->nooverlap, tc->picker, tc->pickinversesize, tc->pickfill, tc->pickstroke, tc->visible, tc->offset, tc->usepressurescale, get_pressure(tc))) {
did = true;
}
}
}
}
return did;
}
{
sp_canvas_item_affine_absolute(tc->dilate_area, (sm* Geom::Rotate(tc->tilt))* Geom::Translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
}
{
// Select the button mode
// Need to set explicitly, because the prefs may not have changed by the previous
}
case GDK_ENTER_NOTIFY:
sp_canvas_item_show(this->dilate_area);
break;
case GDK_LEAVE_NOTIFY:
sp_canvas_item_hide(this->dilate_area);
break;
case GDK_BUTTON_PRESS:
return TRUE;
}
sp_spray_extinput(this, event);
this->is_drawing = true;
this->is_dilating = true;
this->has_dilated = false;
}
this->has_dilated = true;
}
break;
case GDK_MOTION_NOTIFY: {
sp_spray_extinput(this, event);
// Draw the dilating cursor
double radius = get_dilate_radius(this);
sp_canvas_item_affine_absolute(this->dilate_area, (sm*Geom::Rotate(this->tilt))*Geom::Translate(desktop->w2d(motion_w)));
sp_canvas_item_show(this->dilate_area);
}
if (num == 0) {
this->message_context->flash(Inkscape::ERROR_MESSAGE, _("<b>Nothing selected!</b> Select objects to spray."));
}
// Dilating:
sp_spray_dilate(this, motion_w, motion_doc, motion_doc - this->last_push, event->button.state & GDK_SHIFT_MASK? true : false);
//this->last_push = motion_doc;
this->has_dilated = true;
// It's slow, so prevent clogging up with events
return TRUE;
}
}
break;
/* Spray with the scroll */
case GDK_SCROLL: {
double temp ;
temp = this->population;
this->population = 1.0;
case GDK_SCROLL_DOWN:
case GDK_SCROLL_UP: {
return TRUE;
}
sp_spray_extinput(this, event);
this->is_drawing = true;
this->is_dilating = true;
this->has_dilated = false;
if(this->is_dilating && !this->space_panning) {
}
this->has_dilated = true;
this->population = temp;
}
break;
case GDK_SCROLL_RIGHT:
{} break;
case GDK_SCROLL_LEFT:
{} break;
}
}
break;
}
case GDK_BUTTON_RELEASE: {
this->is_drawing = false;
if (!this->has_dilated) {
// If we did not rub, do a light tap
this->pressure = 0.03;
}
this->is_dilating = false;
this->has_dilated = false;
switch (this->mode) {
case SPRAY_MODE_COPY:
SP_VERB_CONTEXT_SPRAY, _("Spray with copies"));
break;
case SPRAY_MODE_CLONE:
SP_VERB_CONTEXT_SPRAY, _("Spray with clones"));
break;
case SPRAY_MODE_SINGLE_PATH:
SP_VERB_CONTEXT_SPRAY, _("Spray in single path"));
break;
}
}
break;
}
case GDK_KEY_PRESS:
case GDK_KEY_j:
case GDK_KEY_J:
if (MOD__SHIFT_ONLY(event)) {
}
break;
case GDK_KEY_k:
case GDK_KEY_K:
if (MOD__SHIFT_ONLY(event)) {
}
break;
case GDK_KEY_l:
case GDK_KEY_L:
if (MOD__SHIFT_ONLY(event)) {
}
break;
#endif
case GDK_KEY_Up:
case GDK_KEY_KP_Up:
if (!MOD__CTRL_ONLY(event)) {
this->population += 0.01;
if (this->population > 1.0) {
this->population = 1.0;
}
}
break;
case GDK_KEY_Down:
case GDK_KEY_KP_Down:
if (!MOD__CTRL_ONLY(event)) {
this->population -= 0.01;
if (this->population < 0.0) {
this->population = 0.0;
}
}
break;
case GDK_KEY_Right:
case GDK_KEY_KP_Right:
if (!MOD__CTRL_ONLY(event)) {
this->width += 0.01;
if (this->width > 1.0) {
this->width = 1.0;
}
// The same spinbutton is for alt+x
sp_spray_update_area(this);
}
break;
case GDK_KEY_Left:
case GDK_KEY_KP_Left:
if (!MOD__CTRL_ONLY(event)) {
this->width -= 0.01;
if (this->width < 0.01) {
this->width = 0.01;
}
sp_spray_update_area(this);
}
break;
case GDK_KEY_Home:
case GDK_KEY_KP_Home:
this->width = 0.01;
sp_spray_update_area(this);
break;
case GDK_KEY_End:
case GDK_KEY_KP_End:
this->width = 1.0;
sp_spray_update_area(this);
break;
case GDK_KEY_x:
case GDK_KEY_X:
if (MOD__ALT_ONLY(event)) {
}
break;
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R:
this->update_cursor(true);
break;
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
break;
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
case GDK_KEY_BackSpace:
break;
default:
break;
}
break;
case GDK_KEY_RELEASE: {
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R:
this->update_cursor(false);
break;
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
this->message_context->clear();
break;
default:
break;
}
}
default:
break;
}
if (!ret) {
// if ((SP_EVENT_CONTEXT_CLASS(sp_spray_context_parent_class))->root_handler) {
// ret = (SP_EVENT_CONTEXT_CLASS(sp_spray_context_parent_class))->root_handler(event_context, event);
// }
}
return ret;
}
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :