1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* 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 v2+, read the file 'COPYING' for more information.
*/
//#define DEBUG_MESH
// Libraries
#include <gdk/gdkkeysyms.h>
#include <glibmm/i18n.h>
// General
#include "desktop.h"
#include "document-undo.h"
#include "document.h"
#include "gradient-drag.h"
#include "gradient-chemistry.h"
#include "include/macros.h"
#include "message-context.h"
#include "message-stack.h"
#include "rubberband.h"
#include "selection.h"
#include "snap.h"
#include "verbs.h"
#include "display/sp-ctrlcurve.h"
#include "display/curve.h"
#include "object/sp-defs.h"
#include "object/sp-mesh-gradient.h"
#include "object/sp-namedview.h"
#include "object/sp-text.h"
#include "style.h"
#include "ui/pixmaps/cursor-gradient.xpm"
#include "ui/pixmaps/cursor-gradient-add.xpm"
#include "ui/control-manager.h"
#include "ui/tools/mesh-tool.h"
using Inkscape::DocumentUndo;
namespace Inkscape {
namespace UI {
namespace Tools {
static void sp_mesh_new_default(MeshTool &rc);
const std::string& MeshTool::getPrefsPath() {
return MeshTool::prefsPath;
}
const std::string MeshTool::prefsPath = "/tools/mesh";
// TODO: The gradient tool class looks like a 1:1 copy.
MeshTool::MeshTool()
: ToolBase(cursor_gradient_xpm)
// TODO: Why are these connections stored as pointers?
, selcon(nullptr)
, subselcon(nullptr)
, cursor_addnode(false)
, node_added(false)
, show_handles(true)
, edit_fill(true)
, edit_stroke(true)
{
// TODO: This value is overwritten in the root handler
this->tolerance = 6;
}
MeshTool::~MeshTool() {
this->enableGrDrag(false);
this->selcon->disconnect();
delete this->selcon;
this->subselcon->disconnect();
delete this->subselcon;
}
// This must match GrPointType enum sp-gradient.h
// We should move this to a shared header (can't simply move to gradient.h since that would require
// including <glibmm/i18n.h> which messes up "N_" in extensions... argh!).
const gchar *ms_handle_descr [] = {
N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
N_("Linear gradient <b>end</b>"),
N_("Linear gradient <b>mid stop</b>"),
N_("Radial gradient <b>center</b>"),
N_("Radial gradient <b>radius</b>"),
N_("Radial gradient <b>radius</b>"),
N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
N_("Radial gradient <b>mid stop</b>"),
N_("Radial gradient <b>mid stop</b>"),
N_("Mesh gradient <b>corner</b>"),
N_("Mesh gradient <b>handle</b>"),
N_("Mesh gradient <b>tensor</b>")
};
void MeshTool::selection_changed(Inkscape::Selection* /*sel*/) {
GrDrag *drag = this->_grdrag;
Inkscape::Selection *selection = this->desktop->getSelection();
if (selection == nullptr) {
return;
}
guint n_obj = (guint) boost::distance(selection->items());
if (!drag->isNonEmpty() || selection->isEmpty()) {
return;
}
guint n_tot = drag->numDraggers();
guint n_sel = drag->numSelected();
//The use of ngettext in the following code is intentional even if the English singular form would never be used
if (n_sel == 1) {
if (drag->singleSelectedDraggerNumDraggables() == 1) {
gchar * message = g_strconcat(
//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
ngettext(" out of %d mesh handle"," out of %d mesh handles",n_tot),
ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
this->message_context->setF(Inkscape::NORMAL_MESSAGE,
message,_(ms_handle_descr[drag->singleSelectedDraggerSingleDraggableType()]), n_tot, n_obj);
} else {
gchar * message =
g_strconcat(
//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",
drag->singleSelectedDraggerNumDraggables()),
ngettext(" out of %d mesh handle"," out of %d mesh handles",n_tot),
ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
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)
gchar * message =
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
ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
this->message_context->setF(Inkscape::NORMAL_MESSAGE,message, n_sel, n_tot, n_obj);
} else if (n_sel == 0) {
this->message_context->setF(Inkscape::NORMAL_MESSAGE,
//TRANSLATORS: The plural refers to number of selected objects
ngettext("<b>No</b> mesh handles selected out of %d on %d selected object",
"<b>No</b> mesh handles selected out of %d on %d selected objects",n_obj), n_tot, n_obj);
}
// FIXME
// We need to update mesh gradient handles.
// Get gradient this drag belongs too..
}
void MeshTool::setup() {
ToolBase::setup();
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
if (prefs->getBool("/tools/mesh/selcue", true)) {
this->enableSelectionCue();
}
this->enableGrDrag();
Inkscape::Selection *selection = this->desktop->getSelection();
this->selcon = new sigc::connection(selection->connectChanged(
sigc::mem_fun(this, &MeshTool::selection_changed)
));
this->subselcon = new sigc::connection(this->desktop->connectToolSubselectionChanged(
sigc::hide(sigc::bind(
sigc::mem_fun(*this, &MeshTool::selection_changed),
(Inkscape::Selection*)nullptr)
)
));
sp_event_context_read(this, "show_handles");
sp_event_context_read(this, "edit_fill");
sp_event_context_read(this, "edit_stroke");
this->selection_changed(selection);
}
void MeshTool::set(const Inkscape::Preferences::Entry& value) {
Glib::ustring entry_name = value.getEntryName();
if (entry_name == "show_handles") {
this->show_handles = value.getBool(true);
} else if (entry_name == "edit_fill") {
this->edit_fill = value.getBool(true);
} else if (entry_name == "edit_stroke") {
this->edit_stroke = value.getBool(true);
} else {
ToolBase::set(value);
}
}
void
sp_mesh_context_select_next (ToolBase *event_context)
{
GrDrag *drag = event_context->_grdrag;
g_assert (drag);
GrDragger *d = drag->select_next();
event_context->desktop->scroll_to_point(d->point, 1.0);
}
void
sp_mesh_context_select_prev (ToolBase *event_context)
{
GrDrag *drag = event_context->_grdrag;
g_assert (drag);
GrDragger *d = drag->select_prev();
event_context->desktop->scroll_to_point(d->point, 1.0);
}
/**
Returns vector of control lines mouse is over. Returns only first if 'first' is true.
*/
static std::vector<SPCtrlCurve *>
sp_mesh_context_over_line (MeshTool *rc, Geom::Point event_p, bool first = true)
{
SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
//Translate mouse point into proper coord system
rc->mousepoint_doc = desktop->w2d(event_p);
double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
GrDrag *drag = rc->_grdrag;
std::vector<SPCtrlCurve *> selected;
for (std::vector<SPCtrlLine *>::const_iterator l = drag->lines.begin(); l != drag->lines.end(); ++l) {
if (!SP_IS_CTRLCURVE(*l)) continue;
SPCtrlCurve *curve = SP_CTRLCURVE(*l);
Geom::BezierCurveN<3> b( curve->p0, curve->p1, curve->p2, curve->p3 );
Geom::Coord coord = b.nearestTime( rc->mousepoint_doc ); // Coord == double
Geom::Point nearest = b( coord );
double dist_screen = Geom::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
if (dist_screen < tolerance) {
selected.push_back(curve);
if (first) {
break;
}
}
}
return selected;
}
/**
Split row/column near the mouse point.
*/
static void sp_mesh_context_split_near_point(MeshTool *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
{
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_split_near_point: entrance: " << mouse_p << std::endl;
#endif
// item is the selected item. mouse_p the location in doc coordinates of where to add the stop
ToolBase *ec = SP_EVENT_CONTEXT(rc);
SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
double tolerance = (double) ec->tolerance;
ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MESH,
_("Split mesh row/column"));
ec->get_drag()->updateDraggers();
}
/**
Wrapper for various mesh operations that require a list of selected corner nodes.
*/
void
sp_mesh_context_corner_operation (MeshTool *rc, MeshCornerOperation operation )
{
#ifdef DEBUG_MESH
std::cout << "sp_mesh_corner_operation: entrance: " << operation << std::endl;
#endif
SPDocument *doc = nullptr;
GrDrag *drag = rc->_grdrag;
std::map<SPMeshGradient*, std::vector<guint> > points;
std::map<SPMeshGradient*, SPItem*> items;
std::map<SPMeshGradient*, Inkscape::PaintTarget> fill_or_stroke;
// Get list of selected draggers for each mesh.
// For all selected draggers (a dragger may include draggerables from different meshes).
for (auto dragger : drag->selected) {
// For all draggables of dragger (a draggable corresponds to a unique mesh).
for (auto d : dragger->draggables) {
// Only mesh corners
if( d->point_type != POINT_MG_CORNER ) continue;
// Find the gradient
SPMeshGradient *gradient = SP_MESHGRADIENT( getGradient (d->item, d->fill_or_stroke) );
// Collect points together for same gradient
points[gradient].push_back( d->point_i );
items[gradient] = d->item;
fill_or_stroke[gradient] = d->fill_or_stroke ? Inkscape::FOR_FILL: Inkscape::FOR_STROKE;
}
}
// Loop over meshes.
for( std::map<SPMeshGradient*, std::vector<guint> >::const_iterator iter = points.begin(); iter != points.end(); ++iter) {
SPMeshGradient *mg = SP_MESHGRADIENT( iter->first );
if( iter->second.size() > 0 ) {
guint noperation = 0;
switch (operation) {
case MG_CORNER_SIDE_TOGGLE:
// std::cout << "SIDE_TOGGLE" << std::endl;
noperation += mg->array.side_toggle( iter->second );
break;
case MG_CORNER_SIDE_ARC:
// std::cout << "SIDE_ARC" << std::endl;
noperation += mg->array.side_arc( iter->second );
break;
case MG_CORNER_TENSOR_TOGGLE:
// std::cout << "TENSOR_TOGGLE" << std::endl;
noperation += mg->array.tensor_toggle( iter->second );
break;
case MG_CORNER_COLOR_SMOOTH:
// std::cout << "COLOR_SMOOTH" << std::endl;
noperation += mg->array.color_smooth( iter->second );
break;
case MG_CORNER_COLOR_PICK:
// std::cout << "COLOR_PICK" << std::endl;
noperation += mg->array.color_pick( iter->second, items[iter->first] );
break;
case MG_CORNER_INSERT:
// std::cout << "INSERT" << std::endl;
noperation += mg->array.insert( iter->second );
break;
default:
std::cout << "sp_mesh_corner_operation: unknown operation" << std::endl;
}
if( noperation > 0 ) {
mg->array.write( mg );
mg->requestModified(SP_OBJECT_MODIFIED_FLAG);
doc = mg->document;
switch (operation) {
case MG_CORNER_SIDE_TOGGLE:
DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Toggled mesh path type."));
drag->local_change = true; // Don't create new draggers.
break;
case MG_CORNER_SIDE_ARC:
DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Approximated arc for mesh side."));
drag->local_change = true; // Don't create new draggers.
break;
case MG_CORNER_TENSOR_TOGGLE:
DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Toggled mesh tensors."));
drag->local_change = true; // Don't create new draggers.
break;
case MG_CORNER_COLOR_SMOOTH:
DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Smoothed mesh corner color."));
drag->local_change = true; // Don't create new draggers.
break;
case MG_CORNER_COLOR_PICK:
DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Picked mesh corner color."));
drag->local_change = true; // Don't create new draggers.
break;
case MG_CORNER_INSERT:
DocumentUndo::done(doc, SP_VERB_CONTEXT_MESH, _("Inserted new row or column."));
break;
default:
std::cout << "sp_mesh_corner_operation: unknown operation" << std::endl;
}
}
}
}
// Not needed. Update is done via gr_drag_sel_modified().
// drag->updateDraggers();
}
/**
* Scale mesh to just fit into bbox of selected items.
*/
void
sp_mesh_context_fit_mesh_in_bbox (MeshTool *rc)
{
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_fit_mesh_in_bbox: entrance: Entrance"<< std::endl;
#endif
SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
Inkscape::Selection *selection = desktop->getSelection();
if (selection == nullptr) {
return;
}
bool changed = false;
auto itemlist = selection->items();
for (auto i=itemlist.begin(); i!=itemlist.end(); ++i) {
SPItem *item = *i;
SPStyle *style = item->style;
if (style) {
if (style->fill.isPaintserver()) {
SPPaintServer *server = item->style->getFillPaintServer();
if ( SP_IS_MESHGRADIENT(server) ) {
Geom::OptRect item_bbox = item->geometricBounds();
SPMeshGradient *gradient = SP_MESHGRADIENT(server);
if (gradient->array.fill_box( item_bbox )) {
changed = true;
}
}
}
if (style->stroke.isPaintserver()) {
SPPaintServer *server = item->style->getStrokePaintServer();
if ( SP_IS_MESHGRADIENT(server) ) {
Geom::OptRect item_bbox = item->visualBounds();
SPMeshGradient *gradient = SP_MESHGRADIENT(server);
if (gradient->array.fill_box( item_bbox )) {
changed = true;
}
}
}
}
}
if (changed) {
DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MESH,
_("Fit mesh inside bounding box."));
}
}
/**
Handles all keyboard and mouse input for meshs.
Note: node/handle events are take care of elsewhere.
*/
bool MeshTool::root_handler(GdkEvent* event) {
static bool dragging;
Inkscape::Selection *selection = desktop->getSelection();
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
this->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
double const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000, "px"); // in px
// Get value of fill or stroke preference
Inkscape::PaintTarget fill_or_stroke_pref =
static_cast<Inkscape::PaintTarget>(prefs->getInt("/tools/mesh/newfillorstroke"));
GrDrag *drag = this->_grdrag;
g_assert (drag);
gint ret = FALSE;
auto move_handle = [&](int x_dir, int y_dir) {
gint mul = 1 + gobble_key_events(get_latin_keyval(&event->key), 0); // with any mask
if (MOD__SHIFT(event)) {
mul *= 10;
}
y_dir *= -desktop->yaxisdir();
if (MOD__ALT(event)) {
drag->selected_move_screen(mul * x_dir, mul * y_dir);
} else {
mul *= nudge;
drag->selected_move(mul * x_dir, mul * y_dir);
}
};
switch (event->type) {
case GDK_2BUTTON_PRESS:
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_root_handler: GDK_2BUTTON_PRESS" << std::endl;
#endif
// Double click:
// If over a mesh line, divide mesh row/column
// If not over a line and no mesh, create new mesh for top selected object.
if ( event->button.button == 1 ) {
// Are we over a mesh line?
std::vector<SPCtrlCurve *> over_line =
sp_mesh_context_over_line(this, Geom::Point(event->motion.x, event->motion.y));
if (!over_line.empty()) {
// 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, selection->items().front(), this->mousepoint_doc, event->button.time);
} else {
// Create a new gradient with default coordinates.
// Check if object already has mesh... if it does,
// don't create new mesh with click-drag.
bool has_mesh = false;
if (!selection->isEmpty()) {
SPStyle *style = selection->items().front()->style;
if (style) {
SPPaintServer *server =
(fill_or_stroke_pref == Inkscape::FOR_FILL) ?
style->getFillPaintServer():
style->getStrokePaintServer();
if (server && SP_IS_MESHGRADIENT(server))
has_mesh = true;
}
}
if (!has_mesh) {
sp_mesh_new_default(*this);
}
}
ret = TRUE;
}
break;
case GDK_BUTTON_PRESS:
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_root_handler: GDK_BUTTON_PRESS" << std::endl;
#endif
// Button down
// If mesh already exists, do rubber band selection.
// Else set origin for drag which will create a new gradient.
if ( event->button.button == 1 && !this->space_panning ) {
// Are we over a mesh line?
std::vector<SPCtrlCurve *> over_line =
sp_mesh_context_over_line(this, Geom::Point(event->motion.x, event->motion.y), false);
if (!over_line.empty()) {
for (auto it : over_line) {
SPItem *item = it->item;
Inkscape::PaintTarget fill_or_stroke =
it->is_fill ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
GrDragger* dragger0 = drag->getDraggerFor(item, POINT_MG_CORNER, it->corner0, fill_or_stroke);
GrDragger* dragger1 = drag->getDraggerFor(item, POINT_MG_CORNER, it->corner1, fill_or_stroke);
bool add = (event->button.state & GDK_SHIFT_MASK);
bool toggle = (event->button.state & GDK_CONTROL_MASK);
if ( !add && !toggle ) {
drag->deselectAll();
}
drag->setSelected( dragger0, true, !toggle );
drag->setSelected( dragger1, true, !toggle );
}
ret = true;
break; // To avoid putting the following code in an else block.
}
Geom::Point button_w(event->button.x, event->button.y);
// save drag origin
this->xp = (gint) button_w[Geom::X];
this->yp = (gint) button_w[Geom::Y];
this->within_tolerance = true;
dragging = true;
Geom::Point button_dt = desktop->w2d(button_w);
// Check if object already has mesh... if it does,
// don't create new mesh with click-drag.
bool has_mesh = false;
if (!selection->isEmpty()) {
SPStyle *style = selection->items().front()->style;
if (style) {
SPPaintServer *server =
(fill_or_stroke_pref == Inkscape::FOR_FILL) ?
style->getFillPaintServer():
style->getStrokePaintServer();
if (server && SP_IS_MESHGRADIENT(server))
has_mesh = true;
}
}
if (has_mesh) {
Inkscape::Rubberband::get(desktop)->start(desktop, button_dt);
}
// remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
// enable Ctrl+doubleclick of exactly the selected item(s)
if (!(event->button.state & GDK_CONTROL_MASK)) {
this->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
}
if (!selection->isEmpty()) {
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
m.unSetup();
}
this->origin = button_dt;
ret = TRUE;
}
break;
case GDK_MOTION_NOTIFY:
// Mouse move
if ( dragging && ( event->motion.state & GDK_BUTTON1_MASK ) && !this->space_panning ) {
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_root_handler: GDK_MOTION_NOTIFY: Dragging" << std::endl;
#endif
if ( this->within_tolerance
&& ( abs( (gint) event->motion.x - this->xp ) < this->tolerance )
&& ( abs( (gint) event->motion.y - this->yp ) < this->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;
Geom::Point const motion_w(event->motion.x,
event->motion.y);
Geom::Point const motion_dt = this->desktop->w2d(motion_w);
if (Inkscape::Rubberband::get(desktop)->is_started()) {
Inkscape::Rubberband::get(desktop)->move(motion_dt);
this->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
} else {
// Do nothing. For a linear/radial gradient we follow the drag, updating the
// gradient as the end node is dragged. For a mesh gradient, the gradient is always
// created to fill the object when the drag ends.
}
gobble_motion_events(GDK_BUTTON1_MASK);
ret = TRUE;
} else {
// Not dragging
// Do snapping
if (!drag->mouseOver() && !selection->isEmpty()) {
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
Geom::Point const motion_w(event->motion.x, event->motion.y);
Geom::Point const motion_dt = this->desktop->w2d(motion_w);
m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_OTHER_HANDLE));
m.unSetup();
}
// Highlight corner node corresponding to side or tensor node
if( drag->mouseOver() ) {
// 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
std::vector<SPCtrlCurve *> over_line =
sp_mesh_context_over_line(this, Geom::Point(event->motion.x, event->motion.y));
if (this->cursor_addnode && over_line.empty()) {
this->cursor_shape = cursor_gradient_xpm;
this->sp_event_context_update_cursor();
this->cursor_addnode = false;
} else if (!this->cursor_addnode && !over_line.empty()) {
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
std::cout << "sp_mesh_context_root_handler: GDK_BUTTON_RELEASE" << std::endl;
#endif
this->xp = this->yp = 0;
if ( event->button.button == 1 && !this->space_panning ) {
// Check if over line
std::vector<SPCtrlCurve *> over_line =
sp_mesh_context_over_line(this, Geom::Point(event->motion.x, event->motion.y));
if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
if (!over_line.empty()) {
sp_mesh_context_split_near_point(this, over_line[0]->item,
this->mousepoint_doc, 0);
ret = TRUE;
}
} else {
dragging = false;
// unless clicked with Ctrl (to enable Ctrl+doubleclick).
if (event->button.state & GDK_CONTROL_MASK) {
ret = TRUE;
break;
}
if (!this->within_tolerance) {
// Check if object already has mesh... if it does,
// don't create new mesh with click-drag.
bool has_mesh = false;
if (!selection->isEmpty()) {
SPStyle *style = selection->items().front()->style;
if (style) {
SPPaintServer *server =
(fill_or_stroke_pref == Inkscape::FOR_FILL) ?
style->getFillPaintServer():
style->getStrokePaintServer();
if (server && SP_IS_MESHGRADIENT(server))
has_mesh = true;
}
}
if (!has_mesh) {
sp_mesh_new_default(*this);
} else {
// we've been dragging, either create a new gradient
// or rubberband-select if we have rubberband
Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
if (r->is_started() && !this->within_tolerance) {
// this was a rubberband drag
if (r->getMode() == RUBBERBAND_MODE_RECT) {
Geom::OptRect const b = r->getRectangle();
if (!(event->button.state & GDK_SHIFT_MASK)) {
drag->deselectAll();
}
drag->selectRect(*b);
}
}
}
} else if (this->item_to_select) {
if (!over_line.empty()) {
// 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
if (event->button.state & GDK_SHIFT_MASK) {
selection->toggle(this->item_to_select);
} else {
drag->deselectAll();
selection->set(this->item_to_select);
}
}
} else {
if (!over_line.empty()) {
// Clicked on an existing mesh line, don't change selection. This stops
// possible change in selection during a double click with overlapping objects
} else {
// click in an empty space; do the same as Esc
if (!drag->selected.empty()) {
drag->deselectAll();
} else {
selection->clear();
}
}
}
this->item_to_select = nullptr;
ret = TRUE;
}
Inkscape::Rubberband::get(desktop)->stop();
}
break;
case GDK_KEY_PRESS:
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_root_handler: GDK_KEY_PRESS" << std::endl;
#endif
// FIXME: tip
switch (get_latin_keyval (&event->key)) {
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:
// sp_event_show_modifier_tip (this->defaultMessageContext(), event,
// _("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:
if (MOD__CTRL_ONLY(event) && drag->isNonEmpty()) {
drag->selectAll();
ret = TRUE;
}
break;
case GDK_KEY_Escape:
if (!drag->selected.empty()) {
drag->deselectAll();
} else {
selection->clear();
}
ret = TRUE;
//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__CTRL(event)) { // not ctrl
move_handle(-1, 0);
ret = TRUE;
}
break;
case GDK_KEY_Up: // move handle up
case GDK_KEY_KP_Up:
case GDK_KEY_KP_8:
if (!MOD__CTRL(event)) { // not ctrl
move_handle(0, 1);
ret = TRUE;
}
break;
case GDK_KEY_Right: // move handle right
case GDK_KEY_KP_Right:
case GDK_KEY_KP_6:
if (!MOD__CTRL(event)) { // not ctrl
move_handle(1, 0);
ret = TRUE;
}
break;
case GDK_KEY_Down: // move handle down
case GDK_KEY_KP_Down:
case GDK_KEY_KP_2:
if (!MOD__CTRL(event)) { // not ctrl
move_handle(0, -1);
ret = TRUE;
}
break;
// Mesh Operations --------------------------------------------
case GDK_KEY_Insert:
case GDK_KEY_KP_Insert:
// with any modifiers:
sp_mesh_context_corner_operation ( this, MG_CORNER_INSERT );
ret = TRUE;
break;
case GDK_KEY_i:
case GDK_KEY_I:
if (MOD__SHIFT_ONLY(event)) {
// Shift+I - insert corners (alternate keybinding for keyboards
// that don't have the Insert key)
sp_mesh_context_corner_operation ( this, MG_CORNER_INSERT );
ret = TRUE;
}
break;
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
case GDK_KEY_BackSpace:
if ( !drag->selected.empty() ) {
ret = TRUE;
}
break;
case GDK_KEY_b: // Toggle mesh side between lineto and curveto.
case GDK_KEY_B:
if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
sp_mesh_context_corner_operation ( this, MG_CORNER_SIDE_TOGGLE );
ret = TRUE;
}
break;
case GDK_KEY_c: // Convert mesh side from generic Bezier to Bezier approximating arc,
case GDK_KEY_C: // preserving handle direction.
if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
sp_mesh_context_corner_operation ( this, MG_CORNER_SIDE_ARC );
ret = TRUE;
}
break;
case GDK_KEY_g: // Toggle mesh tensor points on/off
case GDK_KEY_G:
if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
sp_mesh_context_corner_operation ( this, MG_CORNER_TENSOR_TOGGLE );
ret = TRUE;
}
break;
case GDK_KEY_j: // Smooth corner color
case GDK_KEY_J:
if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
sp_mesh_context_corner_operation ( this, MG_CORNER_COLOR_SMOOTH );
ret = TRUE;
}
break;
case GDK_KEY_k: // Pick corner color
case GDK_KEY_K:
if (MOD__ALT(event) && drag->isNonEmpty() && drag->hasSelection()) {
sp_mesh_context_corner_operation ( this, MG_CORNER_COLOR_PICK );
ret = TRUE;
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
#ifdef DEBUG_MESH
std::cout << "sp_mesh_context_root_handler: GDK_KEY_RELEASE" << std::endl;
#endif
switch (get_latin_keyval (&event->key)) {
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) {
ret = ToolBase::root_handler(event);
}
return ret;
}
// Creates a new mesh gradient.
static void sp_mesh_new_default(MeshTool &rc) {
SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
Inkscape::Selection *selection = desktop->getSelection();
SPDocument *document = desktop->getDocument();
if (!selection->isEmpty()) {
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
Inkscape::PaintTarget fill_or_stroke_pref =
static_cast<Inkscape::PaintTarget>(prefs->getInt("/tools/mesh/newfillorstroke"));
// Ensure mesh is immediately editable.
// Editing both fill and stroke at same time doesn't work well so avoid.
if (fill_or_stroke_pref == Inkscape::FOR_FILL) {
prefs->setBool("/tools/mesh/edit_fill", true );
prefs->setBool("/tools/mesh/edit_stroke", false);
} else {
prefs->setBool("/tools/mesh/edit_fill", false);
prefs->setBool("/tools/mesh/edit_stroke", true );
}
// HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
SPCSSAttr *css = sp_repr_css_attr_new();
sp_repr_css_set_property(css, "fill-opacity", "1.0");
Inkscape::XML::Document *xml_doc = document->getReprDoc();
SPDefs *defs = document->getDefs();
auto items= selection->items();
for(auto i=items.begin();i!=items.end();++i){
//FIXME: see above
sp_repr_css_change_recursive((*i)->getRepr(), css, "style");
// Create mesh element
Inkscape::XML::Node *repr = xml_doc->createElement("svg:meshgradient");
// privates are garbage-collectable
repr->setAttribute("inkscape:collect", "always");
// Attach to document
defs->getRepr()->appendChild(repr);
Inkscape::GC::release(repr);
// Get corresponding object
SPMeshGradient *mg = static_cast<SPMeshGradient *>(document->getObjectByRepr(repr));
mg->array.create(mg, *i, (fill_or_stroke_pref == Inkscape::FOR_FILL) ?
(*i)->geometricBounds() : (*i)->visualBounds());
bool isText = SP_IS_TEXT(*i);
sp_style_set_property_url(*i,
((fill_or_stroke_pref == Inkscape::FOR_FILL) ? "fill":"stroke"),
mg, isText);
(*i)->requestModified(SP_OBJECT_MODIFIED_FLAG|SP_OBJECT_STYLE_MODIFIED_FLAG);
}
if (css) {
sp_repr_css_attr_unref(css);
css = nullptr;
}
DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MESH, _("Create mesh"));
// status text; we do not track coords because this branch is run once, not all the time
// during drag
int n_objects = (int) boost::distance(selection->items());
rc.message_context->setF(Inkscape::NORMAL_MESSAGE,
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),
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 :
|