mesh-tool.cpp revision 8ecc5504e6ee53e361e874ddc7d279827839c4e5
/*
* Mesh drawing and editing tool
*
* Authors:
* bulia byak <buliabyak@users.sf.net>
* Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
* Abhishek Sharma
* Tavmjong Bah <tavmjong@free.fr>
*
* Copyright (C) 2012 Tavmjong Bah
* Copyright (C) 2007 Johan Engelen
* Copyright (C) 2005 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
//#define DEBUG_MESH
// Libraries
#include <gdk/gdkkeysyms.h>
// General
#include "desktop.h"
#include "document.h"
#include "document-undo.h"
#include "macros.h"
#include "message-context.h"
#include "message-stack.h"
#include "preferences.h"
#include "rubberband.h"
#include "selection.h"
#include "snap.h"
#include "sp-namedview.h"
#include "verbs.h"
// Gradient specific
#include "gradient-drag.h"
#include "gradient-chemistry.h"
#include "pixmaps/cursor-gradient.xpm"
#include "pixmaps/cursor-gradient-add.xpm"
// Mesh specific
#include "ui/tools/mesh-tool.h"
#include "sp-mesh-gradient.h"
#include "display/sp-ctrlcurve.h"
using Inkscape::DocumentUndo;
namespace Inkscape {
namespace UI {
namespace Tools {
}
// TODO: The gradient tool class looks like a 1:1 copy.
, cursor_addnode(false)
, node_added(false)
// TODO: Why are these connections stored as pointers?
{
// TODO: This value is overwritten in the root handler
this->tolerance = 6;
}
this->enableGrDrag(false);
this->selcon->disconnect();
delete this->selcon;
this->subselcon->disconnect();
delete this->subselcon;
}
const gchar *ms_handle_descr [] = {
N_("Mesh gradient <b>corner</b>"),
N_("Mesh gradient <b>handle</b>"),
N_("Mesh gradient <b>tensor</b>")
};
return;
}
return;
}
//The use of ngettext in the following code is intentional even if the English singular form would never be used
if (n_sel == 1) {
//TRANSLATORS: %s will be substituted with the point name (see previous messages); This is part of a compound message
_("%s selected"),
//TRANSLATORS: Mind the space in front. This is part of a compound message
} else {
//TRANSLATORS: This is a part of a compound message (out of two more indicating: grandint handle count & object count)
ngettext("One handle merging %d stop (drag with <b>Shift</b> to separate) selected",
"One handle merging %d stops (drag with <b>Shift</b> to separate) selected",
this->message_context->setF(Inkscape::NORMAL_MESSAGE,message,drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
}
} else if (n_sel > 1) {
//TRANSLATORS: The plural refers to number of selected mesh handles. This is part of a compound message (part two indicates selected object count)
g_strconcat(ngettext("<b>%d</b> mesh handle selected out of %d","<b>%d</b> mesh handles selected out of %d",n_sel),
//TRANSLATORS: Mind the space in front. (Refers to gradient handles selected). This is part of a compound message
} else if (n_sel == 0) {
//TRANSLATORS: The plural refers to number of selected objects
ngettext("<b>No</b> mesh handles selected out of %d on %d selected object",
}
// FIXME
// We need to update mesh gradient handles.
// Get gradient this drag belongs too..
// std::cout << "mesh_selection_changed: selection: objects: " << n_obj << std::endl;
// GSList *itemList = (GSList *) selection->itemList();
// while( itemList ) {
// SPItem *item = SP_ITEM( itemList->data );
// // std::cout << " item: " << SP_OBJECT(item)->getId() << std::endl;
// SPStyle *style = item->style;
// if (style && (style->fill.isPaintserver())) {
// SPPaintServer *server = item->style->getFillPaintServer();
// if ( SP_IS_MESHGRADIENT(server) ) {
// SPMeshGradient *mg = SP_MESHGRADIENT(server);
// guint rows = 0;//mg->array.patches.size();
// for ( guint i = 0; i < rows; ++i ) {
// guint columns = 0;//mg->array.patches[0].size();
// for ( guint j = 0; j < columns; ++j ) {
// }
// }
// }
// }
// itemList = itemList->next;
// }
// GList* dragger_ptr = drag->draggers; // Points to GrDragger class (group of GrDraggable)
// guint count = 0;
// while( dragger_ptr ) {
// std::cout << "mesh_selection_changed: dragger: " << ++count << std::endl;
// GSList* draggable_ptr = ((GrDragger *) dragger_ptr->data)->draggables;
// while( draggable_ptr ) {
// std::cout << "mesh_selection_changed: draggable: " << draggable_ptr << std::endl;
// GrDraggable *draggable = (GrDraggable *) draggable_ptr->data;
// gint point_type = draggable->point_type;
// gint point_i = draggable->point_i;
// bool fill_or_stroke = draggable->fill_or_stroke;
// if( point_type == POINT_MG_CORNER ) {
// //std::cout << "mesh_selection_changed: POINT_MG_CORNER: " << point_i << std::endl;
// // Now we must create or destroy corresponding handles.
// if( g_list_find( drag->selected, dragger_ptr->data ) ) {
// //std::cout << "gradient_selection_changed: Selected: " << point_i << std::endl;
// // Which meshes does this point belong to?
// } else {
// //std::cout << "mesh_selection_changed: Not Selected: " << point_i << std::endl;
// }
// }
// draggable_ptr = draggable_ptr->next;
// }
// dragger_ptr = dragger_ptr->next;
// }
}
this->enableSelectionCue();
}
this->enableGrDrag();
));
)
));
this->selection_changed(selection);
}
void
{
}
void
{
}
/**
Returns true if mouse cursor over mesh edge.
*/
static bool
{
//Translate mouse point into proper coord system
return close;
}
/**
*/
static void sp_mesh_context_split_near_point(MeshTool *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
{
#ifdef DEBUG_MESH
#endif
// item is the selected item. mouse_p the location in doc coordinates of where to add the stop
}
/**
Wrapper for various mesh operations that require a list of selected corner nodes.
*/
static void
{
#ifdef DEBUG_MESH
#endif
// Get list of selected draggers for each mesh.
// For all selected draggers
// For all draggables of dragger
// Only mesh corners
if( d->point_type != POINT_MG_CORNER ) continue;
// Find the gradient
// Collect points together for same gradient
}
}
// Loop over meshes.
for( std::map<SPMeshGradient*, std::vector<guint> >::const_iterator iter = points.begin(); iter != points.end(); ++iter) {
guint noperation = 0;
switch (operation) {
case MG_CORNER_SIDE_TOGGLE:
// std::cout << "SIDE_TOGGLE" << std::endl;
break;
case MG_CORNER_SIDE_ARC:
// std::cout << "SIDE_ARC" << std::endl;
break;
case MG_CORNER_TENSOR_TOGGLE:
// std::cout << "TENSOR_TOGGLE" << std::endl;
break;
case MG_CORNER_COLOR_SMOOTH:
// std::cout << "COLOR_SMOOTH" << std::endl;
break;
case MG_CORNER_COLOR_PICK:
// std::cout << "COLOR_PICK" << std::endl;
break;
default:
}
if( noperation > 0 ) {
switch (operation) {
case MG_CORNER_SIDE_TOGGLE:
break;
case MG_CORNER_SIDE_ARC:
break;
case MG_CORNER_TENSOR_TOGGLE:
break;
case MG_CORNER_COLOR_SMOOTH:
break;
case MG_CORNER_COLOR_PICK:
break;
default:
}
}
}
}
drag->updateDraggers();
}
/**
Handles all keyboard and mouse input for meshs.
*/
static bool dragging;
double const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000, "px"); // in px
case GDK_2BUTTON_PRESS:
#ifdef DEBUG_MESH
#endif
// Double click:
// If not over a line, create new gradients for selected objects.
// Are we over a mesh line?
bool over_line = false;
over_line |= sp_mesh_context_is_over_line (this, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
}
}
if (over_line) {
// We take the first item in selection, because with doubleclick, the first click
// always resets selection to the single object under cursor
sp_mesh_context_split_near_point(this, SP_ITEM(selection->itemList()->data), this->mousepoint_doc, event->button.time);
} else {
// Create a new gradient with default coordinates.
Inkscape::PaintTarget fsmode = (prefs->getInt("/tools/gradient/newfillorstroke", 1) != 0) ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_root_handler: creating new mesh on: " << (fsmode == Inkscape::FOR_FILL ? "Fill" : "Stroke") << std::endl;
#endif
}
_("Create default mesh"));
}
}
break;
case GDK_BUTTON_PRESS:
#ifdef DEBUG_MESH
#endif
// Button down
// If Shift key down: do rubber band selection
// Else set origin for drag. A drag creates a new gradient if one does not exist
// save drag origin
this->within_tolerance = true;
dragging = true;
} else {
// remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
// enable Ctrl+doubleclick of exactly the selected item(s)
this->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
}
m.unSetup();
}
}
}
break;
case GDK_MOTION_NOTIFY:
// Mouse move
#ifdef DEBUG_MESH
#endif
if ( this->within_tolerance
break; // do not drag if we're within tolerance from origin
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to draw, not click), then always process the
// motion notify coordinates as given (no snapping back to origin)
this->within_tolerance = false;
this->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
} else {
// Create new gradient with coordinates determined by drag.
}
} else {
// Not dragging
// Do snapping
m.unSetup();
}
// Highlight corner node corresponding to side or tensor node
// MESH FIXME: Light up corresponding corner node corresponding to node we are over.
// See "pathflash" in ui/tools/node-tool.cpp for ideas.
// Use desktop->add_temporary_canvasitem( SPCanvasItem, milliseconds );
}
// Change cursor shape if over line
bool over_line = false;
over_line |= sp_mesh_context_is_over_line (this, (SPItem*) l->data, Geom::Point(event->motion.x, event->motion.y));
}
}
if (this->cursor_addnode && !over_line) {
this->cursor_shape = cursor_gradient_xpm;
this->sp_event_context_update_cursor();
this->cursor_addnode = false;
} else if (!this->cursor_addnode && over_line) {
this->cursor_shape = cursor_gradient_add_xpm;
this->sp_event_context_update_cursor();
this->cursor_addnode = true;
}
}
break;
case GDK_BUTTON_RELEASE:
#ifdef DEBUG_MESH
#endif
// Check if over line
bool over_line = false;
over_line = sp_mesh_context_is_over_line (this, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
if (over_line) {
break;
}
}
}
}
} else {
dragging = false;
// unless clicked with Ctrl (to enable Ctrl+doubleclick).
break;
}
if (!this->within_tolerance) {
// we've been dragging, either do nothing (grdrag handles that),
// or rubberband-select if we have rubberband
if (r->is_started() && !this->within_tolerance) {
// this was a rubberband drag
if (r->getMode() == RUBBERBAND_MODE_RECT) {
drag->selectRect(*b);
}
}
} else if (this->item_to_select) {
// Clicked on an existing mesh line, don't change selection. This stops
// possible change in selection during a double click with overlapping objects
} else {
// no dragging, select clicked item if any
} else {
}
}
} else {
// click in an empty space; do the same as Esc
drag->deselectAll();
} else {
}
}
this->item_to_select = NULL;
}
}
break;
case GDK_KEY_PRESS:
#ifdef DEBUG_MESH
#endif
// FIXME: tip
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R:
case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
case GDK_KEY_Meta_R:
_("FIXME<b>Ctrl</b>: snap mesh angle"),
_("FIXME<b>Shift</b>: draw mesh around the starting point"),
NULL);
break;
case GDK_KEY_A:
case GDK_KEY_a:
}
break;
case GDK_KEY_Escape:
drag->deselectAll();
} else {
}
//TODO: make dragging escapable by Esc
break;
case GDK_KEY_Left: // move handle left
case GDK_KEY_KP_Left:
case GDK_KEY_KP_4:
if (MOD__SHIFT(event)) {
} else {
}
} else { // no alt
if (MOD__SHIFT(event)) {
} else {
}
}
}
break;
case GDK_KEY_Up: // move handle up
case GDK_KEY_KP_Up:
case GDK_KEY_KP_8:
if (MOD__SHIFT(event)) {
} else {
}
} else { // no alt
if (MOD__SHIFT(event)) {
} else {
}
}
}
break;
case GDK_KEY_Right: // move handle right
case GDK_KEY_KP_Right:
case GDK_KEY_KP_6:
if (MOD__SHIFT(event)) {
} else {
}
} else { // no alt
if (MOD__SHIFT(event)) {
} else {
}
}
}
break;
case GDK_KEY_Down: // move handle down
case GDK_KEY_KP_Down:
case GDK_KEY_KP_2:
if (MOD__SHIFT(event)) {
} else {
}
} else { // no alt
if (MOD__SHIFT(event)) {
} else {
}
}
}
break;
case GDK_KEY_Insert:
case GDK_KEY_KP_Insert:
// with any modifiers:
//sp_gradient_context_add_stops_between_selected_stops (rc);
break;
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
case GDK_KEY_BackSpace:
}
break;
// Mesh Operations --------------------------------------------
case GDK_KEY_b: // Toggle mesh side between lineto and curveto.
case GDK_KEY_B:
}
break;
case GDK_KEY_c: // Convert mesh side from generic Bezier to Bezier approximating arc,
case GDK_KEY_C: // preserving handle direction.
}
break;
case GDK_KEY_G:
}
break;
case GDK_KEY_j: // Smooth corner color
case GDK_KEY_J:
}
break;
case GDK_KEY_k: // Pick corner color
case GDK_KEY_K:
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
#ifdef DEBUG_MESH
#endif
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R:
case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt
case GDK_KEY_Meta_R:
this->defaultMessageContext()->clear();
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
}
return ret;
}
static void sp_mesh_drag(MeshTool &rc, Geom::Point const /*pt*/, guint /*state*/, guint32 /*etime*/) {
int type = SP_GRADIENT_TYPE_MESH;
Inkscape::PaintTarget fill_or_stroke = (prefs->getInt("/tools/gradient/newfillorstroke", 1) != 0) ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
if (ec->item_to_select) {
// pick color from the object where drag started
} else {
// Starting from empty space:
// Sort items so that the topmost comes last
// take topmost
vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
}
// HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
//FIXME: see above
// We don't need to do anything. Mesh is already sized appropriately.
}
// if (ec->_grdrag) {
// ec->_grdrag->updateDraggers();
// // prevent regenerating draggers by selection modified signal, which sometimes
// // comes too late and thus destroys the knot which we will now grab:
// ec->_grdrag->local_change = true;
// // and therefore are already out of tolerance
// ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
// type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
// -1, // ignore number (though it is always 1)
// fill_or_stroke, 99999, 99999, etime);
// }
// We did an undoable action, but SPDocumentUndo::done will be called by the knot when released
// status text; we do not track coords because this branch is run once, not all the time
// during drag
ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
"<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
} else {
desktop->getMessageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
}
}
}
}
}
/*
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 :