forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
moving_pixels_state.cpp
932 lines (781 loc) · 27.5 KB
/
moving_pixels_state.cpp
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
// Aseprite
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#define MOVPIXS_TRACE(...) // TRACE(__VA_ARGS__)
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/ui/editor/moving_pixels_state.h"
#include "app/app.h"
#include "app/color_utils.h"
#include "app/commands/cmd_flip.h"
#include "app/commands/cmd_move_mask.h"
#include "app/commands/cmd_rotate.h"
#include "app/commands/command.h"
#include "app/commands/commands.h"
#include "app/commands/move_thing.h"
#include "app/console.h"
#include "app/modules/gui.h"
#include "app/pref/preferences.h"
#include "app/tools/ink.h"
#include "app/tools/tool.h"
#include "app/transformation.h"
#include "app/ui/context_bar.h"
#include "app/ui/editor/editor.h"
#include "app/ui/editor/editor_customization_delegate.h"
#include "app/ui/editor/pixels_movement.h"
#include "app/ui/editor/standby_state.h"
#include "app/ui/editor/transform_handles.h"
#include "app/ui/keyboard_shortcuts.h"
#include "app/ui/main_window.h"
#include "app/ui/status_bar.h"
#include "app/ui/timeline/timeline.h"
#include "app/ui_context.h"
#include "app/util/clipboard.h"
#include "app/util/layer_utils.h"
#include "base/gcd.h"
#include "base/pi.h"
#include "doc/algorithm/flip_image.h"
#include "doc/mask.h"
#include "doc/sprite.h"
#include "fmt/format.h"
#include "gfx/rect.h"
#include "ui/manager.h"
#include "ui/message.h"
#include "ui/system.h"
#include "ui/view.h"
#include <cstring>
namespace app {
using namespace ui;
MovingPixelsState::MovingPixelsState(Editor* editor,
MouseMessage* msg,
PixelsMovementPtr pixelsMovement,
HandleType handle)
: m_pixelsMovement(pixelsMovement)
, m_delayedMouseMove(this, editor, 5)
, m_editor(editor)
, m_observingEditor(false)
, m_discarded(false)
, m_renderTimer(50)
{
m_pixelsMovement->setDelegate(this);
// MovingPixelsState needs a selection tool to avoid problems
// sharing the extra cel between the drawing cursor preview and the
// pixels movement/transformation preview.
//ASSERT(!editor->getCurrentEditorInk()->isSelection());
UIContext* context = UIContext::instance();
if (handle != NoHandle) {
gfx::Point pt = editor->screenToEditor(msg->position());
m_pixelsMovement->catchImage(gfx::PointF(pt), handle);
editor->captureMouse();
}
// Setup transparent mode/mask color
if (Preferences::instance().selection.autoOpaque()) {
Preferences::instance().selection.opaque(
editor->layer()->isBackground());
}
onTransparentColorChange();
m_renderTimer.Tick.connect([this]{ onRenderTimer(); });
// Hook BeforeCommandExecution signal so we know if the user wants
// to execute other command, so we can drop pixels.
m_ctxConn =
context->BeforeCommandExecution.connect(&MovingPixelsState::onBeforeCommandExecution, this);
// Listen to any change to the transparent color from the ContextBar.
m_opaqueConn =
Preferences::instance().selection.opaque.AfterChange.connect(
[this]{ onTransparentColorChange(); });
m_transparentConn =
Preferences::instance().selection.transparentColor.AfterChange.connect(
[this]{ onTransparentColorChange(); });
// Add the current editor as filter for key message of the manager
// so we can catch the Enter key, and avoid to execute the
// PlayAnimation command.
m_editor->manager()->addMessageFilter(kKeyDownMessage, m_editor);
m_editor->manager()->addMessageFilter(kKeyUpMessage, m_editor);
m_editor->add_observer(this);
m_observingEditor = true;
ContextBar* contextBar = App::instance()->contextBar();
contextBar->updateForMovingPixels(getTransformation(editor));
contextBar->add_observer(this);
App::instance()->mainWindow()->getTimeline()->add_observer(this);
}
MovingPixelsState::~MovingPixelsState()
{
App::instance()->mainWindow()->getTimeline()->remove_observer(this);
ContextBar* contextBar = App::instance()->contextBar();
contextBar->remove_observer(this);
contextBar->updateForActiveTool();
removePixelsMovement();
removeAsEditorObserver();
m_renderTimer.stop();
m_editor->manager()->removeMessageFilter(kKeyDownMessage, m_editor);
m_editor->manager()->removeMessageFilter(kKeyUpMessage, m_editor);
m_editor->document()->generateMaskBoundaries();
}
void MovingPixelsState::translate(const gfx::PointF& delta)
{
if (m_pixelsMovement->isDragging())
m_pixelsMovement->dropImageTemporarily();
m_pixelsMovement->catchImageAgain(gfx::PointF(0, 0), MovePixelsHandle);
m_pixelsMovement->moveImage(delta, PixelsMovement::NormalMovement);
m_pixelsMovement->dropImageTemporarily();
m_editor->updateStatusBar();
}
void MovingPixelsState::rotate(double angle)
{
m_pixelsMovement->rotate(angle);
m_editor->updateStatusBar();
}
void MovingPixelsState::flip(doc::algorithm::FlipType flipType)
{
m_pixelsMovement->flipImage(flipType);
m_editor->updateStatusBar();
}
void MovingPixelsState::shift(int dx, int dy)
{
m_pixelsMovement->shift(dx, dy);
m_editor->updateStatusBar();
}
void MovingPixelsState::updateTransformation(const Transformation& t)
{
m_pixelsMovement->setTransformation(t);
m_editor->updateStatusBar();
}
void MovingPixelsState::onEnterState(Editor* editor)
{
StandbyState::onEnterState(editor);
// Get the active key action so we can lock the FineControl action
// until we release/press again the Ctrl key just in case if we
// started moving the pixels with the Ctrl key pressed.
m_lockedKeyAction = getCurrentKeyAction();
update_screen_for_document(editor->document());
}
void MovingPixelsState::onEditorGotFocus(Editor* editor)
{
ContextBar* contextBar = App::instance()->contextBar();
// Make the DropPixelsField widget visible again in the ContextBar
// when we are back to an editor in MovingPixelsState. Without this
// we would see the SelectionModeField instead which doesn't make
// sense on MovingPixelsState).
contextBar->updateForMovingPixels(getTransformation(editor));
}
EditorState::LeaveAction MovingPixelsState::onLeaveState(Editor* editor, EditorState* newState)
{
MOVPIXS_TRACE("MOVPIXS: onLeaveState\n");
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
onRenderTimer();
// If we are changing to another state, we've to drop the image.
if (m_pixelsMovement->isDragging())
m_pixelsMovement->dropImageTemporarily();
// Drop pixels if we are changing to a non-temporary state (a
// temporary state is something like ScrollingState).
if (!newState || !newState->isTemporalState()) {
if (!m_discarded) {
try {
m_pixelsMovement->dropImage();
}
catch (const LockedDocException& ex) {
// This is one of the worst possible scenarios. We want to
// drop pixels because we're leaving this state (e.g. the user
// changed the current frame/layer, so we came from
// onBeforeFrameChanged) and we weren't able to drop those
// pixels.
//
// TODO this problem should be caught before we reach this
// state, or this problem should cancel the frame/layer
// change.
Console::showException(ex);
}
}
editor->document()->resetTransformation();
removePixelsMovement();
editor->releaseMouse();
// Redraw the document without the transformation handles.
editor->document()->notifyGeneralUpdate();
return DiscardState;
}
else {
editor->releaseMouse();
return KeepState;
}
}
void MovingPixelsState::onActiveToolChange(Editor* editor, tools::Tool* tool)
{
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
// If the user changed the tool when he/she is moving pixels,
// we have to drop the pixels only if the new tool is not selection...
if (m_pixelsMovement) {
// We don't want to drop pixels in case the user change the tool
// for scrolling/zooming/picking colors.
if ((!tool->getInk(0)->isSelection() ||
!tool->getInk(1)->isSelection()) &&
(!tool->getInk(0)->isScrollMovement() ||
!tool->getInk(1)->isScrollMovement()) &&
(!tool->getInk(0)->isZoom() ||
!tool->getInk(1)->isZoom()) &&
(!tool->getInk(0)->isEyedropper() ||
!tool->getInk(1)->isEyedropper())) {
// We have to drop pixels
dropPixels();
}
// If we've temporarily gone to a non-selection tool and now we're
// back, we've just to update the context bar to show the "moving
// pixels" controls (e.g. OK/Cancel movement buttons).
else if (tool->getInk(0)->isSelection() ||
tool->getInk(1)->isSelection()) {
ContextBar* contextBar = App::instance()->contextBar();
contextBar->updateForMovingPixels(getTransformation(editor));
}
}
}
bool MovingPixelsState::onMouseDown(Editor* editor, MouseMessage* msg)
{
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
m_delayedMouseMove.onMouseDown(msg);
// Set this editor as the active one and setup the ContextBar for
// moving pixels. This is needed in case that the user is working
// with a couple of Editors, in one is moving pixels and the other
// one not.
UIContext* ctx = UIContext::instance();
ctx->setActiveView(editor->getDocView());
ContextBar* contextBar = App::instance()->contextBar();
contextBar->updateForMovingPixels(getTransformation(editor));
// Start scroll loop
if (editor->checkForScroll(msg) ||
editor->checkForZoom(msg))
return true;
// Call the eyedropper command
tools::Ink* clickedInk = editor->getCurrentEditorInk();
if (clickedInk->isEyedropper()) {
callEyedropper(editor, msg);
return true;
}
Decorator* decorator = static_cast<Decorator*>(editor->decorator());
Doc* document = editor->document();
// Transform selected pixels
if (document->isMaskVisible() &&
decorator->getTransformHandles(editor) &&
(!Preferences::instance().selection.modifiersDisableHandles() ||
msg->modifiers() == kKeyNoneModifier)) {
TransformHandles* transfHandles = decorator->getTransformHandles(editor);
// Get the handle covered by the mouse.
HandleType handle = transfHandles->getHandleAtPoint(editor,
msg->position(),
getTransformation(editor));
if (handle != NoHandle) {
if (layer_is_locked(editor))
return true;
// Re-catch the image
gfx::Point pt = editor->screenToEditor(msg->position());
m_pixelsMovement->catchImageAgain(gfx::PointF(pt), handle);
editor->captureMouse();
return true;
}
}
// Start "moving pixels" loop. Here we check only for left-click as
// right-click can be used to deselect/subtract selection, so we
// should drop the selection in this later case.
if (editor->isInsideSelection() && msg->left()) {
if (layer_is_locked(editor))
return true;
// In case that the user is pressing the copy-selection keyboard shortcut.
if (auto customization = editor->getCustomizationDelegate()) {
if (int(customization->getPressedKeyAction(KeyContext::TranslatingSelection) & KeyAction::CopySelection)) {
// Stamp the pixels to create the copy.
m_pixelsMovement->stampImage();
}
// Set the locked key action again to lock the FineControl until
// with press Ctrl key again.
m_lockedKeyAction = getCurrentKeyAction();
}
// Re-catch the image
m_pixelsMovement->catchImageAgain(
editor->screenToEditorF(msg->position()), MovePixelsHandle);
editor->captureMouse();
return true;
}
// End "moving pixels" loop
else {
// Drop pixels (e.g. to start drawing)
dropPixels();
}
// Use StandbyState implementation
return StandbyState::onMouseDown(editor, msg);
}
bool MovingPixelsState::onMouseUp(Editor* editor, MouseMessage* msg)
{
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
m_delayedMouseMove.onMouseUp(msg);
// Drop the image temporarily in this location (where the user releases the mouse)
m_pixelsMovement->dropImageTemporarily();
// Redraw the new pivot location.
editor->invalidate();
editor->releaseMouse();
return true;
}
bool MovingPixelsState::onMouseMove(Editor* editor, MouseMessage* msg)
{
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
// If there is a button pressed
if (m_pixelsMovement->isDragging()) {
if (m_delayedMouseMove.onMouseMove(msg))
m_renderTimer.start();
return true;
}
// Use StandbyState implementation
return StandbyState::onMouseMove(editor, msg);
}
void MovingPixelsState::onCommitMouseMove(Editor* editor,
const gfx::PointF& spritePos)
{
ASSERT(m_pixelsMovement);
if (!m_pixelsMovement->isDragging())
return;
m_pixelsMovement->setFastMode(true);
const KeyAction action = getCurrentKeyAction();
PixelsMovement::MoveModifier moveModifier = PixelsMovement::NormalMovement;
bool snapToGrid = (Preferences::instance().selection.snapToGrid() &&
m_editor->docPref().grid.snap());
if (bool(action & KeyAction::SnapToGrid))
snapToGrid = !snapToGrid;
if (snapToGrid)
moveModifier |= PixelsMovement::SnapToGridMovement;
if (int(action & KeyAction::AngleSnap))
moveModifier |= PixelsMovement::AngleSnapMovement;
if (int(action & KeyAction::MaintainAspectRatio))
moveModifier |= PixelsMovement::MaintainAspectRatioMovement;
if (int(action & KeyAction::ScaleFromCenter))
moveModifier |= PixelsMovement::ScaleFromPivot;
if (int(action & KeyAction::LockAxis))
moveModifier |= PixelsMovement::LockAxisMovement;
// Enable the FineControl only if we've already released and pressed
// the key again (because it )
if (int(action & KeyAction::FineControl) &&
int(m_lockedKeyAction & KeyAction::FineControl) == 0) {
moveModifier |= PixelsMovement::FineControl;
}
// Invalidate handles
Decorator* decorator = static_cast<Decorator*>(m_editor->decorator());
TransformHandles* transfHandles = decorator->getTransformHandles(m_editor);
const Transformation& transformation = m_pixelsMovement->getTransformation();
transfHandles->invalidateHandles(m_editor, transformation);
// Drag the image to that position
m_pixelsMovement->moveImage(spritePos, moveModifier);
// Update context bar and status bar
ContextBar* contextBar = App::instance()->contextBar();
contextBar->updateForMovingPixels(transformation);
m_editor->updateStatusBar();
}
bool MovingPixelsState::onSetCursor(Editor* editor, const gfx::Point& mouseScreenPos)
{
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
// Move selection
if (m_pixelsMovement->isDragging()) {
editor->showMouseCursor(kMoveCursor);
return true;
}
// Use StandbyState implementation
return StandbyState::onSetCursor(editor, mouseScreenPos);
}
bool MovingPixelsState::onKeyDown(Editor* editor, KeyMessage* msg)
{
ASSERT(m_pixelsMovement);
if (!isActiveEditor())
return false;
ASSERT(editor == m_editor);
// Reset the locked action just to indicate that we can use the
// FineControl now (e.g. if we pressed another modifier key).
m_lockedKeyAction = KeyAction::None;
if (msg->scancode() == kKeyEnter || // TODO make this key customizable
msg->scancode() == kKeyEnterPad ||
msg->scancode() == kKeyEsc) {
dropPixels();
// The escape key drop pixels and deselect the mask.
if (msg->scancode() == kKeyEsc) { // TODO make this key customizable
Command* cmd = Commands::instance()->byId(CommandId::DeselectMask());
UIContext::instance()->executeCommandFromMenuOrShortcut(cmd);
}
return true;
}
// Use StandbyState implementation
return StandbyState::onKeyDown(editor, msg);
}
bool MovingPixelsState::onKeyUp(Editor* editor, KeyMessage* msg)
{
ASSERT(m_pixelsMovement);
if (!isActiveEditor())
return false;
ASSERT(editor == m_editor);
// Reset the locked action just to indicate that we can use the
// FineControl now (e.g. if we release the Ctrl key).
m_lockedKeyAction = KeyAction::None;
// Use StandbyState implementation
return StandbyState::onKeyUp(editor, msg);
}
bool MovingPixelsState::onUpdateStatusBar(Editor* editor)
{
MOVPIXS_TRACE("MOVPIXS: onUpdateStatusBar (%p)\n", m_pixelsMovement.get());
ASSERT(m_pixelsMovement);
ASSERT(editor == m_editor);
// We've received a crash report where this is nullptr when
// MovingPixelsState::onLeaveState() generates a general update
// notification (notifyGeneralUpdate()) just after the
// m_pixelsMovement is deleted with removePixelsMovement(). The
// general update signals a scroll update in the view which will ask
// for the status bar content again (Editor::notifyScrollChanged).
//
// We weren't able to reproduce this scenario anyway (which should
// be visible with the ASSERT() above).
if (!m_pixelsMovement)
return false;
const Transformation& transform(getTransformation(editor));
gfx::Size imageSize = m_pixelsMovement->getInitialImageSize();
int w = int(transform.bounds().w);
int h = int(transform.bounds().h);
int gcd = base::gcd(w, h);
StatusBar::instance()->setStatusText(
100,
fmt::format(
":pos: {} {}"
" :size: {} {}"
" :selsize: {} {} [{:.02f}% {:.02f}%]"
" :angle: {:.1f}"
" :aspect_ratio: {}:{}",
int(transform.bounds().x),
int(transform.bounds().y),
imageSize.w,
imageSize.h,
w,
h,
(double)w*100.0/imageSize.w,
(double)h*100.0/imageSize.h,
180.0 * transform.angle() / PI,
w/gcd,
h/gcd));
return true;
}
bool MovingPixelsState::acceptQuickTool(tools::Tool* tool)
{
return
(!m_pixelsMovement ||
tool->getInk(0)->isSelection() ||
tool->getInk(0)->isEyedropper() ||
tool->getInk(0)->isScrollMovement() ||
tool->getInk(0)->isZoom());
}
void MovingPixelsState::onBeforeLayerVisibilityChange(Editor* editor,
doc::Layer* layer,
bool newState)
{
if (!isActiveDocument())
return;
// If the layer visibility of any selected layer changes, we just
// drop the pixels (it's the easiest way to avoid modifying hidden
// pixels).
if (m_pixelsMovement) {
const Site& site = m_pixelsMovement->site();
if (site.layer() == layer ||
site.range().contains(layer)) {
dropPixels();
}
}
}
// Before executing any command, we drop the pixels (go back to standby).
void MovingPixelsState::onBeforeCommandExecution(CommandExecutionEvent& ev)
{
Command* command = ev.command();
MOVPIXS_TRACE("MOVPIXS: onBeforeCommandExecution %s\n", command->id().c_str());
// If the command is for other editor, we don't drop pixels.
if (!isActiveEditor())
return;
if (layer_is_locked(m_editor) &&
(command->id() == CommandId::Flip() ||
command->id() == CommandId::Cut() ||
command->id() == CommandId::Clear() ||
command->id() == CommandId::Rotate())) {
ev.cancel();
return;
}
// We don't need to drop the pixels if a MoveMaskCommand of Content is executed.
if (auto moveMaskCmd = dynamic_cast<MoveMaskCommand*>(command)) {
if (moveMaskCmd->getTarget() == MoveMaskCommand::Content) {
if (layer_is_locked(m_editor)) {
ev.cancel();
return;
}
gfx::Point delta = moveMaskCmd->getMoveThing().getDelta(UIContext::instance());
// Verify Shift condition of the MoveMaskCommand (i.e. wrap = true)
if (moveMaskCmd->isWrap()) {
m_pixelsMovement->shift(delta.x, delta.y);
}
else {
translate(gfx::PointF(delta));
}
// We've processed the selection content movement right here.
ev.cancel();
return;
}
}
// Don't drop pixels if the user zooms/scrolls/picks a color
// using commands.
else if ((command->id() == CommandId::Zoom()) ||
(command->id() == CommandId::Scroll()) ||
(command->id() == CommandId::Eyedropper()) ||
// DiscardBrush is used by Eyedropper command
(command->id() == CommandId::DiscardBrush())) {
// Do not drop pixels
return;
}
// Intercept the "Cut" or "Copy" command to handle them locally
// with the current m_pixelsMovement data.
else if (command->id() == CommandId::Cut() ||
command->id() == CommandId::Copy() ||
command->id() == CommandId::Clear()) {
// Copy the floating image to the clipboard on Cut/Copy.
if (command->id() != CommandId::Clear()) {
Doc* document = m_editor->document();
std::unique_ptr<Image> floatingImage;
std::unique_ptr<Mask> floatingMask;
m_pixelsMovement->getDraggedImageCopy(floatingImage, floatingMask);
if (floatingImage->isTilemap()) {
Site site = m_editor->getSite();
ASSERT(site.tileset());
Clipboard::instance()->
copyTilemap(floatingImage.get(),
floatingMask.get(),
document->sprite()->palette(m_editor->frame()),
site.tileset());
}
else {
Clipboard::instance()->
copyImage(floatingImage.get(),
floatingMask.get(),
document->sprite()->palette(m_editor->frame()));
}
}
// Clear floating pixels on Cut/Clear.
if (command->id() != CommandId::Copy()) {
m_pixelsMovement->trim();
// Should we keep the mask after an Edit > Clear command?
auto keepMask = PixelsMovement::DontKeepMask;
if (command->id() == CommandId::Clear() &&
Preferences::instance().selection.keepSelectionAfterClear()) {
keepMask = PixelsMovement::KeepMask;
}
// Discard the dragged image.
m_pixelsMovement->discardImage(PixelsMovement::CommitChanges, keepMask);
m_discarded = true;
// Quit from MovingPixelsState, back to standby.
m_editor->backToPreviousState();
}
// Cancel the command, we've simulated it.
ev.cancel();
return;
}
// Flip Horizontally/Vertically commands are handled manually to
// avoid dropping the floating region of pixels.
else if (command->id() == CommandId::Flip()) {
if (auto flipCommand = dynamic_cast<FlipCommand*>(command)) {
this->flip(flipCommand->getFlipType());
ev.cancel();
return;
}
}
// Rotate is quite simple, we can add the angle to the current transformation.
else if (command->id() == CommandId::Rotate()) {
if (auto rotate = dynamic_cast<RotateCommand*>(command)) {
if (rotate->flipMask()) {
this->rotate(rotate->angle());
ev.cancel();
return;
}
}
}
// We can use previous/next frames while transforming the selection
// to switch between frames
else if (command->id() == CommandId::GotoPreviousFrame() ||
command->id() == CommandId::GotoPreviousFrameWithSameTag()) {
if (m_pixelsMovement->gotoFrame(-1)) {
ev.cancel();
return;
}
}
else if (command->id() == CommandId::GotoNextFrame() ||
command->id() == CommandId::GotoNextFrameWithSameTag()) {
if (m_pixelsMovement->gotoFrame(+1)) {
ev.cancel();
return;
}
}
if (m_pixelsMovement)
dropPixels();
}
void MovingPixelsState::onDestroyEditor(Editor* editor)
{
// TODO we should call ~MovingPixelsState(), we should delete the
// whole "m_statesHistory" when an editor is deleted.
removeAsEditorObserver();
}
void MovingPixelsState::onBeforeFrameChanged(Editor* editor)
{
if (!isActiveDocument())
return;
if (m_pixelsMovement &&
!m_pixelsMovement->canHandleFrameChange()) {
dropPixels();
}
}
void MovingPixelsState::onBeforeLayerChanged(Editor* editor)
{
if (!isActiveDocument())
return;
if (m_pixelsMovement)
dropPixels();
}
void MovingPixelsState::onBeforeRangeChanged(Timeline* timeline)
{
if (!isActiveDocument())
return;
if (m_pixelsMovement)
dropPixels();
}
void MovingPixelsState::onTransparentColorChange()
{
ASSERT(m_pixelsMovement);
bool opaque = Preferences::instance().selection.opaque();
setTransparentColor(
opaque,
opaque ?
app::Color::fromMask():
Preferences::instance().selection.transparentColor());
}
void MovingPixelsState::onRenderTimer()
{
m_pixelsMovement->setFastMode(false);
m_renderTimer.stop();
}
void MovingPixelsState::onDropPixels(ContextBarObserver::DropAction action)
{
if (!isActiveEditor())
return;
switch (action) {
case ContextBarObserver::DropPixels:
dropPixels();
break;
case ContextBarObserver::CancelDrag:
m_pixelsMovement->discardImage(PixelsMovement::DontCommitChanges);
m_discarded = true;
// Quit from MovingPixelsState, back to standby.
m_editor->backToPreviousState();
break;
}
}
void MovingPixelsState::onPivotChange()
{
ContextBar* contextBar = App::instance()->contextBar();
contextBar->updateForMovingPixels(getTransformation(m_editor));
}
void MovingPixelsState::setTransparentColor(bool opaque, const app::Color& color)
{
ASSERT(m_pixelsMovement);
Layer* layer = m_editor->layer();
ASSERT(layer);
try {
m_pixelsMovement->setMaskColor(
opaque, color_utils::color_for_target_mask(color, ColorTarget(layer)));
}
catch (const LockedDocException& ex) {
Console::showException(ex);
}
}
void MovingPixelsState::dropPixels()
{
MOVPIXS_TRACE("MOVPIXS: dropPixels\n");
// Just change to default state (StandbyState generally). We'll
// receive an onLeaveState() event after this call.
m_editor->backToPreviousState();
}
Transformation MovingPixelsState::getTransformation(Editor* editor)
{
// m_pixelsMovement can be null in the final onMouseDown(), after we
// called dropPixels() and we're just going to the previous state.
if (m_pixelsMovement)
return m_pixelsMovement->getTransformation();
else
return StandbyState::getTransformation(editor);
}
bool MovingPixelsState::isActiveDocument() const
{
Doc* doc = UIContext::instance()->activeDocument();
return (m_editor->document() == doc);
}
bool MovingPixelsState::isActiveEditor() const
{
Editor* targetEditor = UIContext::instance()->activeEditor();
return (targetEditor == m_editor);
}
void MovingPixelsState::removeAsEditorObserver()
{
if (m_observingEditor) {
m_observingEditor = false;
m_editor->remove_observer(this);
}
}
void MovingPixelsState::removePixelsMovement()
{
// Avoid receiving a onCommitMouseMove() message when
// m_pixelsMovement is already nullptr.
m_delayedMouseMove.stopTimer();
m_pixelsMovement.reset();
m_ctxConn.disconnect();
m_opaqueConn.disconnect();
m_transparentConn.disconnect();
}
KeyAction MovingPixelsState::getCurrentKeyAction() const
{
KeyContext keyContext = KeyContext::Normal;
switch (m_pixelsMovement->handle()) {
case MovePixelsHandle:
keyContext = KeyContext::TranslatingSelection;
break;
case ScaleNWHandle:
case ScaleNHandle:
case ScaleNEHandle:
case ScaleWHandle:
case ScaleEHandle:
case ScaleSWHandle:
case ScaleSHandle:
case ScaleSEHandle:
keyContext = KeyContext::ScalingSelection;
break;
case RotateNWHandle:
case RotateNEHandle:
case RotateSWHandle:
case RotateSEHandle:
keyContext = KeyContext::RotatingSelection;
break;
case SkewNHandle:
case SkewWHandle:
case SkewEHandle:
case SkewSHandle:
keyContext = KeyContext::ScalingSelection;
break;
}
// Ask to the editor the customization delegate for the active key
// action/modifier for the pixels movement (snap to grid, angle
// snap, etc.).
if (auto customizable = m_editor->getCustomizationDelegate())
return customizable->getPressedKeyAction(keyContext);
else
return KeyAction::None;
}
} // namespace app