mesh-tool.cpp revision 769a6887551cf7ff7bce4b48d3ac303cbea69507
1N/A/*
1N/A * Mesh drawing and editing tool
1N/A *
1N/A * Authors:
1N/A * bulia byak <buliabyak@users.sf.net>
1N/A * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
1N/A * Abhishek Sharma
1N/A * Tavmjong Bah <tavmjong@free.fr>
1N/A *
1N/A * Copyright (C) 2012 Tavmjong Bah
1N/A * Copyright (C) 2007 Johan Engelen
1N/A * Copyright (C) 2005 Authors
1N/A *
1N/A * Released under GNU GPL, read the file 'COPYING' for more information
1N/A */
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include "config.h"
1N/A#endif
1N/A
1N/A//#define DEBUG_MESH
1N/A
1N/A
1N/A// Libraries
1N/A#include <gdk/gdkkeysyms.h>
1N/A#include <glibmm/i18n.h>
1N/A
1N/A// General
1N/A#include "desktop.h"
1N/A#include "desktop-handles.h"
1N/A#include "document.h"
1N/A#include "document-undo.h"
1N/A#include "macros.h"
1N/A#include "message-context.h"
1N/A#include "message-stack.h"
1N/A#include "preferences.h"
1N/A#include "rubberband.h"
1N/A#include "selection.h"
1N/A#include "snap.h"
1N/A#include "sp-namedview.h"
1N/A#include "verbs.h"
1N/A
1N/A// Gradient specific
1N/A#include "gradient-drag.h"
1N/A#include "gradient-chemistry.h"
1N/A#include "pixmaps/cursor-gradient.xpm"
1N/A#include "pixmaps/cursor-gradient-add.xpm"
1N/A
1N/A// Mesh specific
1N/A#include "ui/tools/mesh-tool.h"
1N/A#include "sp-mesh-gradient.h"
1N/A#include "display/sp-ctrlcurve.h"
1N/A
1N/Ausing Inkscape::DocumentUndo;
1N/A
1N/A#include "ui/tool-factory.h"
1N/A
1N/Anamespace Inkscape {
1N/Anamespace UI {
1N/Anamespace Tools {
1N/A
1N/Astatic void sp_mesh_drag(MeshTool &rc, Geom::Point const pt, guint state, guint32 etime);
1N/A
1N/Anamespace {
1N/A ToolBase* createMeshContext() {
1N/A return new MeshTool();
1N/A }
1N/A
1N/A bool meshContextRegistered = ToolFactory::instance().registerObject("/tools/mesh", createMeshContext);
1N/A}
1N/A
1N/Aconst std::string& MeshTool::getPrefsPath() {
1N/A return MeshTool::prefsPath;
1N/A}
1N/A
1N/Aconst std::string MeshTool::prefsPath = "/tools/mesh";
1N/A
1N/A// TODO: The gradient tool class looks like a 1:1 copy.
1N/A
1N/AMeshTool::MeshTool()
1N/A : ToolBase(cursor_gradient_xpm, 4, 4)
1N/A , cursor_addnode(false)
1N/A , node_added(false)
1N/A// TODO: Why are these connections stored as pointers?
1N/A , selcon(NULL)
1N/A , subselcon(NULL)
1N/A{
1N/A // TODO: This value is overwritten in the root handler
1N/A this->tolerance = 6;
1N/A}
1N/A
1N/AMeshTool::~MeshTool() {
1N/A this->enableGrDrag(false);
1N/A
1N/A this->selcon->disconnect();
1N/A delete this->selcon;
1N/A
1N/A this->subselcon->disconnect();
1N/A delete this->subselcon;
1N/A}
1N/A
1N/Aconst gchar *ms_handle_descr [] = {
1N/A N_("Mesh gradient <b>corner</b>"),
1N/A N_("Mesh gradient <b>handle</b>"),
1N/A N_("Mesh gradient <b>tensor</b>")
1N/A};
1N/A
1N/Avoid MeshTool::selection_changed(Inkscape::Selection* /*sel*/) {
1N/A GrDrag *drag = this->_grdrag;
1N/A Inkscape::Selection *selection = this->desktop->getSelection();
1N/A
1N/A if (selection == NULL) {
1N/A return;
1N/A }
1N/A
1N/A guint n_obj = g_slist_length((GSList *) selection->itemList());
1N/A
1N/A if (!drag->isNonEmpty() || selection->isEmpty()) {
1N/A return;
1N/A }
1N/A
1N/A guint n_tot = drag->numDraggers();
1N/A guint n_sel = drag->numSelected();
1N/A
1N/A //The use of ngettext in the following code is intentional even if the English singular form would never be used
1N/A if (n_sel == 1) {
1N/A if (drag->singleSelectedDraggerNumDraggables() == 1) {
1N/A gchar * message = g_strconcat(
1N/A //TRANSLATORS: %s will be substituted with the point name (see previous messages); This is part of a compound message
1N/A _("%s selected"),
1N/A //TRANSLATORS: Mind the space in front. This is part of a compound message
1N/A ngettext(" out of %d mesh handle"," out of %d mesh handles",n_tot),
1N/A ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
1N/A this->message_context->setF(Inkscape::NORMAL_MESSAGE,
1N/A message,_(ms_handle_descr[drag->singleSelectedDraggerSingleDraggableType()]), n_tot, n_obj);
1N/A } else {
1N/A gchar * message =
1N/A g_strconcat(
1N/A //TRANSLATORS: This is a part of a compound message (out of two more indicating: grandint handle count & object count)
1N/A ngettext("One handle merging %d stop (drag with <b>Shift</b> to separate) selected",
1N/A "One handle merging %d stops (drag with <b>Shift</b> to separate) selected",
1N/A drag->singleSelectedDraggerNumDraggables()),
1N/A ngettext(" out of %d mesh handle"," out of %d mesh handles",n_tot),
1N/A ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
1N/A this->message_context->setF(Inkscape::NORMAL_MESSAGE,message,drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
1N/A }
1N/A } else if (n_sel > 1) {
1N/A //TRANSLATORS: The plural refers to number of selected mesh handles. This is part of a compound message (part two indicates selected object count)
1N/A gchar * message =
1N/A g_strconcat(ngettext("<b>%d</b> mesh handle selected out of %d","<b>%d</b> mesh handles selected out of %d",n_sel),
1N/A //TRANSLATORS: Mind the space in front. (Refers to gradient handles selected). This is part of a compound message
1N/A ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
1N/A this->message_context->setF(Inkscape::NORMAL_MESSAGE,message, n_sel, n_tot, n_obj);
1N/A } else if (n_sel == 0) {
1N/A this->message_context->setF(Inkscape::NORMAL_MESSAGE,
1N/A //TRANSLATORS: The plural refers to number of selected objects
1N/A ngettext("<b>No</b> mesh handles selected out of %d on %d selected object",
1N/A "<b>No</b> mesh handles selected out of %d on %d selected objects",n_obj), n_tot, n_obj);
1N/A }
1N/A
1N/A // FIXME
1N/A // We need to update mesh gradient handles.
1N/A // Get gradient this drag belongs too..
1N/A // std::cout << "mesh_selection_changed: selection: objects: " << n_obj << std::endl;
1N/A // GSList *itemList = (GSList *) selection->itemList();
1N/A // while( itemList ) {
1N/A
1N/A // SPItem *item = SP_ITEM( itemList->data );
1N/A // // std::cout << " item: " << SP_OBJECT(item)->getId() << std::endl;
1N/A
1N/A // SPStyle *style = item->style;
1N/A // if (style && (style->fill.isPaintserver())) {
1N/A
1N/A // SPPaintServer *server = item->style->getFillPaintServer();
1N/A // if ( SP_IS_MESHGRADIENT(server) ) {
1N/A
1N/A // SPMeshGradient *mg = SP_MESHGRADIENT(server);
1N/A
1N/A // guint rows = 0;//mg->array.patches.size();
1N/A // for ( guint i = 0; i < rows; ++i ) {
1N/A // guint columns = 0;//mg->array.patches[0].size();
1N/A // for ( guint j = 0; j < columns; ++j ) {
1N/A // }
1N/A // }
1N/A // }
1N/A // }
1N/A // itemList = itemList->next;
1N/A // }
1N/A
1N/A // GList* dragger_ptr = drag->draggers; // Points to GrDragger class (group of GrDraggable)
1N/A // guint count = 0;
1N/A // while( dragger_ptr ) {
1N/A
1N/A // std::cout << "mesh_selection_changed: dragger: " << ++count << std::endl;
1N/A // GSList* draggable_ptr = ((GrDragger *) dragger_ptr->data)->draggables;
1N/A
1N/A // while( draggable_ptr ) {
1N/A
1N/A // std::cout << "mesh_selection_changed: draggable: " << draggable_ptr << std::endl;
1N/A // GrDraggable *draggable = (GrDraggable *) draggable_ptr->data;
1N/A
1N/A // gint point_type = draggable->point_type;
1N/A // gint point_i = draggable->point_i;
1N/A // bool fill_or_stroke = draggable->fill_or_stroke;
1N/A
1N/A // if( point_type == POINT_MG_CORNER ) {
1N/A
1N/A // //std::cout << "mesh_selection_changed: POINT_MG_CORNER: " << point_i << std::endl;
1N/A // // Now we must create or destroy corresponding handles.
1N/A
1N/A // if( g_list_find( drag->selected, dragger_ptr->data ) ) {
1N/A // //std::cout << "gradient_selection_changed: Selected: " << point_i << std::endl;
1N/A // // Which meshes does this point belong to?
1N/A
1N/A // } else {
1N/A // //std::cout << "mesh_selection_changed: Not Selected: " << point_i << std::endl;
1N/A // }
1N/A // }
1N/A
1N/A // draggable_ptr = draggable_ptr->next;
1N/A
1N/A // }
1N/A
1N/A // dragger_ptr = dragger_ptr->next;
1N/A // }
1N/A}
1N/A
1N/Avoid MeshTool::setup() {
1N/A ToolBase::setup();
1N/A
1N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1N/A if (prefs->getBool("/tools/mesh/selcue", true)) {
1N/A this->enableSelectionCue();
1N/A }
1N/A
1N/A this->enableGrDrag();
1N/A Inkscape::Selection *selection = this->desktop->getSelection();
1N/A
1N/A this->selcon = new sigc::connection(selection->connectChanged(
1N/A sigc::mem_fun(this, &MeshTool::selection_changed)
1N/A ));
1N/A
1N/A this->subselcon = new sigc::connection(this->desktop->connectToolSubselectionChanged(
1N/A sigc::hide(sigc::bind(
1N/A sigc::mem_fun(*this, &MeshTool::selection_changed),
1N/A (Inkscape::Selection*)NULL)
1N/A )
1N/A ));
1N/A
1N/A this->selection_changed(selection);
1N/A}
1N/A
1N/Avoid
1N/Asp_mesh_context_select_next (ToolBase *event_context)
1N/A{
1N/A GrDrag *drag = event_context->_grdrag;
1N/A g_assert (drag);
1N/A
1N/A GrDragger *d = drag->select_next();
1N/A
1N/A event_context->desktop->scroll_to_point(d->point, 1.0);
1N/A}
1N/A
1N/Avoid
1N/Asp_mesh_context_select_prev (ToolBase *event_context)
1N/A{
1N/A GrDrag *drag = event_context->_grdrag;
1N/A g_assert (drag);
1N/A
1N/A GrDragger *d = drag->select_prev();
1N/A
1N/A event_context->desktop->scroll_to_point(d->point, 1.0);
1N/A}
1N/A
1N/A/**
1N/AReturns true if mouse cursor over mesh edge.
1N/A*/
1N/Astatic bool
1N/Asp_mesh_context_is_over_line (MeshTool *rc, SPItem *item, Geom::Point event_p)
1N/A{
1N/A SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
1N/A
1N/A //Translate mouse point into proper coord system
1N/A rc->mousepoint_doc = desktop->w2d(event_p);
1N/A
1N/A SPCtrlCurve *curve = SP_CTRLCURVE(item);
1N/A Geom::BezierCurveN<3> b( curve->p0, curve->p1, curve->p2, curve->p3 );
1N/A Geom::Coord coord = b.nearestPoint( rc->mousepoint_doc ); // Coord == double
1N/A Geom::Point nearest = b( coord );
1N/A
1N/A double dist_screen = Geom::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
1N/A
1N/A double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
1N/A
1N/A bool close = (dist_screen < tolerance);
1N/A
1N/A return close;
1N/A}
1N/A
1N/A
1N/A/**
1N/ASplit row/column near the mouse point.
1N/A*/
1N/Astatic void sp_mesh_context_split_near_point(MeshTool *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
1N/A{
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_split_near_point: entrance: " << mouse_p << std::endl;
1N/A#endif
1N/A
1N/A // item is the selected item. mouse_p the location in doc coordinates of where to add the stop
1N/A
1N/A ToolBase *ec = SP_EVENT_CONTEXT(rc);
1N/A SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
1N/A
1N/A double tolerance = (double) ec->tolerance;
1N/A
1N/A ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
1N/A
1N/A DocumentUndo::done(sp_desktop_document (desktop), SP_VERB_CONTEXT_MESH,
1N/A _("Split mesh row/column"));
1N/A
1N/A ec->get_drag()->updateDraggers();
1N/A}
1N/A
1N/A/**
1N/AWrapper for various mesh operations that require a list of selected corner nodes.
1N/A */
1N/Astatic void
1N/Asp_mesh_context_corner_operation (MeshTool *rc, MeshCornerOperation operation )
1N/A{
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_corner_operation: entrance: " << operation << std::endl;
1N/A#endif
1N/A
1N/A SPDocument *doc = NULL;
1N/A GrDrag *drag = rc->_grdrag;
1N/A
1N/A std::map<SPMeshGradient*, std::vector<guint> > points;
1N/A std::map<SPMeshGradient*, SPItem*> items;
1N/A
1N/A // Get list of selected draggers for each mesh.
1N/A // For all selected draggers
1N/A for (GList *i = drag->selected; i != NULL; i = i->next) {
1N/A GrDragger *dragger = (GrDragger *) i->data;
1N/A // For all draggables of dragger
1N/A for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
1N/A GrDraggable *d = (GrDraggable *) j->data;
1N/A
1N/A // Only mesh corners
1N/A if( d->point_type != POINT_MG_CORNER ) continue;
1N/A
1N/A // Find the gradient
1N/A SPMeshGradient *gradient = SP_MESHGRADIENT( getGradient (d->item, d->fill_or_stroke) );
1N/A
1N/A // Collect points together for same gradient
1N/A points[gradient].push_back( d->point_i );
1N/A items[gradient] = d->item;
1N/A }
1N/A }
1N/A
1N/A // Loop over meshes.
1N/A for( std::map<SPMeshGradient*, std::vector<guint> >::const_iterator iter = points.begin(); iter != points.end(); ++iter) {
1N/A SPMeshGradient *mg = SP_MESHGRADIENT( iter->first );
1N/A if( iter->second.size() > 0 ) {
1N/A guint noperation = 0;
1N/A switch (operation) {
1N/A
1N/A case MG_CORNER_SIDE_TOGGLE:
1N/A // std::cout << "SIDE_TOGGLE" << std::endl;
1N/A noperation += mg->array.side_toggle( iter->second );
1N/A break;
1N/A
1N/A case MG_CORNER_SIDE_ARC:
1N/A // std::cout << "SIDE_ARC" << std::endl;
1N/A noperation += mg->array.side_arc( iter->second );
1N/A break;
1N/A
1N/A case MG_CORNER_TENSOR_TOGGLE:
1N/A // std::cout << "TENSOR_TOGGLE" << std::endl;
1N/A noperation += mg->array.tensor_toggle( iter->second );
1N/A break;
1N/A
1N/A case MG_CORNER_COLOR_SMOOTH:
1N/A // std::cout << "COLOR_SMOOTH" << std::endl;
1N/A noperation += mg->array.color_smooth( iter->second );
1N/A break;
1N/A
1N/A case MG_CORNER_COLOR_PICK:
1N/A // std::cout << "COLOR_PICK" << std::endl;
1N/A noperation += mg->array.color_pick( iter->second, items[iter->first] );
1N/A break;
1N/A
1N/A default:
1N/A std::cout << "sp_mesh_corner_operation: unknown operation" << std::endl;
1N/A }
1N/A
1N/A if( noperation > 0 ) {
1N/A mg->array.write( mg );
1N/A mg->requestModified(SP_OBJECT_MODIFIED_FLAG);
1N/A doc = mg->document;
1N/A
1N/A switch (operation) {
1N/A
1N/A case MG_CORNER_SIDE_TOGGLE:
1N/A DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Toggled mesh path type."));
1N/A break;
1N/A
1N/A case MG_CORNER_SIDE_ARC:
1N/A DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Approximated arc for mesh side."));
1N/A break;
1N/A
1N/A case MG_CORNER_TENSOR_TOGGLE:
1N/A DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Toggled mesh tensors."));
1N/A break;
1N/A
1N/A case MG_CORNER_COLOR_SMOOTH:
1N/A DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Smoothed mesh corner color."));
1N/A break;
1N/A
1N/A case MG_CORNER_COLOR_PICK:
1N/A DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Picked mesh corner color."));
1N/A break;
1N/A
1N/A default:
1N/A std::cout << "sp_mesh_corner_operation: unknown operation" << std::endl;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A drag->updateDraggers();
1N/A
1N/A}
1N/A
1N/A
1N/A/**
1N/AHandles all keyboard and mouse input for meshs.
1N/A*/
1N/Abool MeshTool::root_handler(GdkEvent* event) {
1N/A static bool dragging;
1N/A
1N/A Inkscape::Selection *selection = desktop->getSelection();
1N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1N/A
1N/A this->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
1N/A double const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000, "px"); // in px
1N/A
1N/A GrDrag *drag = this->_grdrag;
1N/A g_assert (drag);
1N/A
1N/A gint ret = FALSE;
1N/A
1N/A switch (event->type) {
1N/A case GDK_2BUTTON_PRESS:
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: GDK_2BUTTON_PRESS" << std::endl;
1N/A#endif
1N/A
1N/A // Double click:
1N/A // If over a mesh line, divide mesh row/column
1N/A // If not over a line, create new gradients for selected objects.
1N/A
1N/A if ( event->button.button == 1 ) {
1N/A // Are we over a mesh line?
1N/A bool over_line = false;
1N/A SPCtrlCurve *line = NULL;
1N/A
1N/A if (drag->lines) {
1N/A for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
1N/A line = (SPCtrlCurve*) l->data;
1N/A over_line |= sp_mesh_context_is_over_line (this, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
1N/A }
1N/A }
1N/A
1N/A if (over_line) {
1N/A // We take the first item in selection, because with doubleclick, the first click
1N/A // always resets selection to the single object under cursor
1N/A sp_mesh_context_split_near_point(this, SP_ITEM(selection->itemList()->data), this->mousepoint_doc, event->button.time);
1N/A } else {
1N/A // Create a new gradient with default coordinates.
1N/A for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
1N/A SPItem *item = SP_ITEM(i->data);
1N/A SPGradientType new_type = SP_GRADIENT_TYPE_MESH;
1N/A Inkscape::PaintTarget fsmode = (prefs->getInt("/tools/gradient/newfillorstroke", 1) != 0) ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: creating new mesh on: " << (fsmode == Inkscape::FOR_FILL ? "Fill" : "Stroke") << std::endl;
1N/A#endif
1N/A SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, item, fsmode);
1N/A
1N/A SPGradient *priv = sp_item_set_gradient(item, vector, new_type, fsmode);
1N/A sp_gradient_reset_to_userspace(priv, item);
1N/A }
1N/A
1N/A DocumentUndo::done(sp_desktop_document (desktop), SP_VERB_CONTEXT_MESH,
1N/A _("Create default mesh"));
1N/A }
1N/A
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_BUTTON_PRESS:
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: GDK_BUTTON_PRESS" << std::endl;
1N/A#endif
1N/A // Button down
1N/A // If Shift key down: do rubber band selection
1N/A // Else set origin for drag. A drag creates a new gradient if one does not exist
1N/A if ( event->button.button == 1 && !this->space_panning ) {
1N/A Geom::Point button_w(event->button.x, event->button.y);
1N/A
1N/A // save drag origin
1N/A this->xp = (gint) button_w[Geom::X];
1N/A this->yp = (gint) button_w[Geom::Y];
1N/A this->within_tolerance = true;
1N/A
1N/A dragging = true;
1N/A
1N/A Geom::Point button_dt = desktop->w2d(button_w);
1N/A if (event->button.state & GDK_SHIFT_MASK) {
1N/A Inkscape::Rubberband::get(desktop)->start(desktop, button_dt);
1N/A } else {
1N/A // remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
1N/A // enable Ctrl+doubleclick of exactly the selected item(s)
1N/A if (!(event->button.state & GDK_CONTROL_MASK)) {
1N/A this->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
1N/A }
1N/A
1N/A if (!selection->isEmpty()) {
1N/A SnapManager &m = desktop->namedview->snap_manager;
1N/A m.setup(desktop);
1N/A m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
1N/A m.unSetup();
1N/A }
1N/A
1N/A this->origin = button_dt;
1N/A }
1N/A
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_MOTION_NOTIFY:
1N/A // Mouse move
1N/A if ( dragging && ( event->motion.state & GDK_BUTTON1_MASK ) && !this->space_panning ) {
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: GDK_MOTION_NOTIFY: Dragging" << std::endl;
1N/A#endif
1N/A if ( this->within_tolerance
1N/A && ( abs( (gint) event->motion.x - this->xp ) < this->tolerance )
1N/A && ( abs( (gint) event->motion.y - this->yp ) < this->tolerance ) ) {
1N/A break; // do not drag if we're within tolerance from origin
1N/A }
1N/A // Once the user has moved farther than tolerance from the original location
1N/A // (indicating they intend to draw, not click), then always process the
1N/A // motion notify coordinates as given (no snapping back to origin)
1N/A this->within_tolerance = false;
1N/A
1N/A Geom::Point const motion_w(event->motion.x,
1N/A event->motion.y);
1N/A Geom::Point const motion_dt = this->desktop->w2d(motion_w);
1N/A
1N/A if (Inkscape::Rubberband::get(desktop)->is_started()) {
1N/A Inkscape::Rubberband::get(desktop)->move(motion_dt);
1N/A this->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
1N/A } else {
1N/A // Create new gradient with coordinates determined by drag.
1N/A sp_mesh_drag(*this, motion_dt, event->motion.state, event->motion.time);
1N/A }
1N/A
1N/A gobble_motion_events(GDK_BUTTON1_MASK);
1N/A
1N/A ret = TRUE;
1N/A } else {
1N/A // Not dragging
1N/A
1N/A // Do snapping
1N/A if (!drag->mouseOver() && !selection->isEmpty()) {
1N/A SnapManager &m = desktop->namedview->snap_manager;
1N/A m.setup(desktop);
1N/A
1N/A Geom::Point const motion_w(event->motion.x, event->motion.y);
1N/A Geom::Point const motion_dt = this->desktop->w2d(motion_w);
1N/A
1N/A m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_OTHER_HANDLE));
1N/A m.unSetup();
1N/A }
1N/A
1N/A // Highlight corner node corresponding to side or tensor node
1N/A if( drag->mouseOver() ) {
1N/A // MESH FIXME: Light up corresponding corner node corresponding to node we are over.
1N/A // See "pathflash" in ui/tools/node-tool.cpp for ideas.
1N/A // Use desktop->add_temporary_canvasitem( SPCanvasItem, milliseconds );
1N/A }
1N/A
1N/A // Change cursor shape if over line
1N/A bool over_line = false;
1N/A
1N/A if (drag->lines) {
1N/A for (GSList *l = drag->lines; l != NULL; l = l->next) {
1N/A over_line |= sp_mesh_context_is_over_line (this, (SPItem*) l->data, Geom::Point(event->motion.x, event->motion.y));
1N/A }
1N/A }
1N/A
1N/A if (this->cursor_addnode && !over_line) {
1N/A this->cursor_shape = cursor_gradient_xpm;
1N/A this->sp_event_context_update_cursor();
1N/A this->cursor_addnode = false;
1N/A } else if (!this->cursor_addnode && over_line) {
1N/A this->cursor_shape = cursor_gradient_add_xpm;
1N/A this->sp_event_context_update_cursor();
1N/A this->cursor_addnode = true;
1N/A }
1N/A }
1N/A break;
1N/A
1N/A case GDK_BUTTON_RELEASE:
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: GDK_BUTTON_RELEASE" << std::endl;
1N/A#endif
1N/A
1N/A this->xp = this->yp = 0;
1N/A
1N/A if ( event->button.button == 1 && !this->space_panning ) {
1N/A // Check if over line
1N/A bool over_line = false;
1N/A SPCtrlLine *line = NULL;
1N/A
1N/A if (drag->lines) {
1N/A for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
1N/A line = (SPCtrlLine*) l->data;
1N/A over_line = sp_mesh_context_is_over_line (this, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
1N/A
1N/A if (over_line) {
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A
1N/A if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
1N/A if (over_line && line) {
1N/A sp_mesh_context_split_near_point(this, line->item, this->mousepoint_doc, 0);
1N/A ret = TRUE;
1N/A }
1N/A } else {
1N/A dragging = false;
1N/A
1N/A // unless clicked with Ctrl (to enable Ctrl+doubleclick).
1N/A if (event->button.state & GDK_CONTROL_MASK) {
1N/A ret = TRUE;
1N/A break;
1N/A }
1N/A
1N/A if (!this->within_tolerance) {
1N/A // we've been dragging, either do nothing (grdrag handles that),
1N/A // or rubberband-select if we have rubberband
1N/A Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
1N/A
1N/A if (r->is_started() && !this->within_tolerance) {
1N/A // this was a rubberband drag
1N/A if (r->getMode() == RUBBERBAND_MODE_RECT) {
1N/A Geom::OptRect const b = r->getRectangle();
1N/A drag->selectRect(*b);
1N/A }
1N/A }
1N/A } else if (this->item_to_select) {
1N/A if (over_line && line) {
1N/A // Clicked on an existing mesh line, don't change selection. This stops
1N/A // possible change in selection during a double click with overlapping objects
1N/A } else {
1N/A // no dragging, select clicked item if any
1N/A if (event->button.state & GDK_SHIFT_MASK) {
1N/A selection->toggle(this->item_to_select);
1N/A } else {
1N/A selection->set(this->item_to_select);
1N/A }
1N/A }
1N/A } else {
1N/A // click in an empty space; do the same as Esc
1N/A if (drag->selected) {
1N/A drag->deselectAll();
1N/A } else {
1N/A selection->clear();
1N/A }
1N/A }
1N/A
1N/A this->item_to_select = NULL;
1N/A ret = TRUE;
1N/A }
1N/A
1N/A Inkscape::Rubberband::get(desktop)->stop();
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_PRESS:
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: GDK_KEY_PRESS" << std::endl;
1N/A#endif
1N/A
1N/A // FIXME: tip
1N/A switch (get_group0_keyval (&event->key)) {
1N/A case GDK_KEY_Alt_L:
1N/A case GDK_KEY_Alt_R:
1N/A case GDK_KEY_Control_L:
1N/A case GDK_KEY_Control_R:
1N/A case GDK_KEY_Shift_L:
1N/A case GDK_KEY_Shift_R:
1N/A case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
1N/A case GDK_KEY_Meta_R:
1N/A sp_event_show_modifier_tip (this->defaultMessageContext(), event,
1N/A _("FIXME<b>Ctrl</b>: snap mesh angle"),
1N/A _("FIXME<b>Shift</b>: draw mesh around the starting point"),
1N/A NULL);
1N/A break;
1N/A
1N/A case GDK_KEY_A:
1N/A case GDK_KEY_a:
1N/A if (MOD__CTRL_ONLY(event) && drag->isNonEmpty()) {
1N/A drag->selectAll();
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_Escape:
1N/A if (drag->selected) {
1N/A drag->deselectAll();
1N/A } else {
1N/A selection->clear();
1N/A }
1N/A
1N/A ret = TRUE;
1N/A //TODO: make dragging escapable by Esc
1N/A break;
1N/A
1N/A case GDK_KEY_Left: // move handle left
1N/A case GDK_KEY_KP_Left:
1N/A case GDK_KEY_KP_4:
1N/A if (!MOD__CTRL(event)) { // not ctrl
1N/A gint mul = 1 + gobble_key_events(get_group0_keyval(&event->key), 0); // with any mask
1N/A
1N/A if (MOD__ALT(event)) { // alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move_screen(mul*-10, 0); // shift
1N/A } else {
1N/A drag->selected_move_screen(mul*-1, 0); // no shift
1N/A }
1N/A } else { // no alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move(mul*-10*nudge, 0); // shift
1N/A } else {
1N/A drag->selected_move(mul*-nudge, 0); // no shift
1N/A }
1N/A }
1N/A
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_Up: // move handle up
1N/A case GDK_KEY_KP_Up:
1N/A case GDK_KEY_KP_8:
1N/A if (!MOD__CTRL(event)) { // not ctrl
1N/A gint mul = 1 + gobble_key_events(get_group0_keyval(&event->key), 0); // with any mask
1N/A
1N/A if (MOD__ALT(event)) { // alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move_screen(0, mul*10); // shift
1N/A } else {
1N/A drag->selected_move_screen(0, mul*1); // no shift
1N/A }
1N/A } else { // no alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move(0, mul*10*nudge); // shift
1N/A } else {
1N/A drag->selected_move(0, mul*nudge); // no shift
1N/A }
1N/A }
1N/A
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_Right: // move handle right
1N/A case GDK_KEY_KP_Right:
1N/A case GDK_KEY_KP_6:
1N/A if (!MOD__CTRL(event)) { // not ctrl
1N/A gint mul = 1 + gobble_key_events(get_group0_keyval(&event->key), 0); // with any mask
1N/A
1N/A if (MOD__ALT(event)) { // alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move_screen(mul*10, 0); // shift
1N/A } else {
1N/A drag->selected_move_screen(mul*1, 0); // no shift
1N/A }
1N/A } else { // no alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move(mul*10*nudge, 0); // shift
1N/A } else {
1N/A drag->selected_move(mul*nudge, 0); // no shift
1N/A }
1N/A }
1N/A
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_Down: // move handle down
1N/A case GDK_KEY_KP_Down:
1N/A case GDK_KEY_KP_2:
1N/A if (!MOD__CTRL(event)) { // not ctrl
1N/A gint mul = 1 + gobble_key_events(get_group0_keyval(&event->key), 0); // with any mask
1N/A
1N/A if (MOD__ALT(event)) { // alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move_screen(0, mul*-10); // shift
1N/A } else {
1N/A drag->selected_move_screen(0, mul*-1); // no shift
1N/A }
1N/A } else { // no alt
1N/A if (MOD__SHIFT(event)) {
1N/A drag->selected_move(0, mul*-10*nudge); // shift
1N/A } else {
1N/A drag->selected_move(0, mul*-nudge); // no shift
1N/A }
1N/A }
1N/A
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_Insert:
1N/A case GDK_KEY_KP_Insert:
1N/A // with any modifiers:
1N/A //sp_gradient_context_add_stops_between_selected_stops (rc);
1N/A std::cout << "Inserting stops between selected stops not implemented yet" << std::endl;
1N/A ret = TRUE;
1N/A break;
1N/A
1N/A case GDK_KEY_Delete:
1N/A case GDK_KEY_KP_Delete:
1N/A case GDK_KEY_BackSpace:
1N/A if ( drag->selected ) {
1N/A std::cout << "Deleting mesh stops not implemented yet" << std::endl;
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A // Mesh Operations --------------------------------------------
1N/A
1N/A case GDK_KEY_b: // Toggle mesh side between lineto and curveto.
1N/A case GDK_KEY_B:
1N/A if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
1N/A sp_mesh_context_corner_operation ( this, MG_CORNER_SIDE_TOGGLE );
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_c: // Convert mesh side from generic Bezier to Bezier approximating arc,
1N/A case GDK_KEY_C: // preserving handle direction.
1N/A if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
1N/A sp_mesh_context_corner_operation ( this, MG_CORNER_SIDE_ARC );
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_g: // Toggle mesh tensor points on/off
1N/A case GDK_KEY_G:
1N/A if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
1N/A sp_mesh_context_corner_operation ( this, MG_CORNER_TENSOR_TOGGLE );
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_j: // Smooth corner color
1N/A case GDK_KEY_J:
1N/A if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
1N/A sp_mesh_context_corner_operation ( this, MG_CORNER_COLOR_SMOOTH );
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A case GDK_KEY_k: // Pick corner color
1N/A case GDK_KEY_K:
1N/A if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
1N/A sp_mesh_context_corner_operation ( this, MG_CORNER_COLOR_PICK );
1N/A ret = TRUE;
1N/A }
1N/A break;
1N/A
1N/A default:
1N/A break;
1N/A }
1N/A
1N/A break;
1N/A
1N/A case GDK_KEY_RELEASE:
1N/A
1N/A#ifdef DEBUG_MESH
1N/A std::cout << "sp_mesh_context_root_handler: GDK_KEY_RELEASE" << std::endl;
1N/A#endif
1N/A switch (get_group0_keyval (&event->key)) {
1N/A case GDK_KEY_Alt_L:
1N/A case GDK_KEY_Alt_R:
1N/A case GDK_KEY_Control_L:
1N/A case GDK_KEY_Control_R:
1N/A case GDK_KEY_Shift_L:
1N/A case GDK_KEY_Shift_R:
1N/A case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt
1N/A case GDK_KEY_Meta_R:
1N/A this->defaultMessageContext()->clear();
1N/A break;
1N/A default:
1N/A break;
1N/A }
1N/A break;
1N/A default:
1N/A break;
1N/A }
1N/A
1N/A if (!ret) {
1N/A ret = ToolBase::root_handler(event);
1N/A }
1N/A
1N/A return ret;
1N/A}
1N/A
1N/Astatic void sp_mesh_drag(MeshTool &rc, Geom::Point const /*pt*/, guint /*state*/, guint32 /*etime*/) {
1N/A SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
1N/A Inkscape::Selection *selection = desktop->getSelection();
1N/A SPDocument *document = sp_desktop_document(desktop);
1N/A ToolBase *ec = SP_EVENT_CONTEXT(&rc);
1N/A
1N/A if (!selection->isEmpty()) {
1N/A
1N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1N/A int type = SP_GRADIENT_TYPE_MESH;
1N/A Inkscape::PaintTarget fill_or_stroke = (prefs->getInt("/tools/gradient/newfillorstroke", 1) != 0) ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
1N/A
1N/A SPGradient *vector;
1N/A if (ec->item_to_select) {
1N/A // pick color from the object where drag started
1N/A vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
1N/A } else {
1N/A // Starting from empty space:
1N/A // Sort items so that the topmost comes last
1N/A GSList *items = g_slist_copy ((GSList *) selection->itemList());
1N/A items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
1N/A // take topmost
1N/A vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
1N/A g_slist_free (items);
1N/A }
1N/A
1N/A // HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
1N/A SPCSSAttr *css = sp_repr_css_attr_new();
1N/A sp_repr_css_set_property(css, "fill-opacity", "1.0");
1N/A
1N/A for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
1N/A
1N/A //FIXME: see above
1N/A sp_repr_css_change_recursive(SP_OBJECT(i->data)->getRepr(), css, "style");
1N/A
1N/A sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
1N/A
1N/A // We don't need to do anything. Mesh is already sized appropriately.
1N/A
1N/A SP_OBJECT(i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1N/A }
1N/A // if (ec->_grdrag) {
1N/A // ec->_grdrag->updateDraggers();
1N/A // // prevent regenerating draggers by selection modified signal, which sometimes
1N/A // // comes too late and thus destroys the knot which we will now grab:
1N/A // ec->_grdrag->local_change = true;
1N/A // // give the grab out-of-bounds values of xp/yp because we're already dragging
1N/A // // and therefore are already out of tolerance
1N/A // ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
1N/A // type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
1N/A // -1, // ignore number (though it is always 1)
1N/A // fill_or_stroke, 99999, 99999, etime);
1N/A // }
1N/A // We did an undoable action, but SPDocumentUndo::done will be called by the knot when released
1N/A
1N/A // status text; we do not track coords because this branch is run once, not all the time
1N/A // during drag
1N/A int n_objects = g_slist_length((GSList *) selection->itemList());
1N/A rc.message_context->setF(Inkscape::NORMAL_MESSAGE,
1N/A ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
1N/A "<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
1N/A n_objects);
1N/A } else {
1N/A sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
1N/A }
1N/A
1N/A}
1N/A
1N/A}
1N/A}
1N/A}
1N/A
1N/A/*
1N/A Local Variables:
1N/A mode:c++
1N/A c-file-style:"stroustrup"
1N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1N/A indent-tabs-mode:nil
1N/A fill-column:99
1N/A End:
1N/A*/
1N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
1N/A