1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3// Copyright (C) 2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/commands/move_thing.h"
13
14#include "app/commands/params.h"
15#include "app/i18n/strings.h"
16#include "app/ui/doc_view.h"
17#include "app/ui/editor/editor.h"
18#include "app/ui_context.h"
19#include "fmt/format.h"
20
21#include <algorithm>
22
23namespace app {
24
25void MoveThing::onLoadParams(const Params& params)
26{
27 std::string v = params.get("direction");
28 if (v == "left") direction = Left;
29 else if (v == "right") direction = Right;
30 else if (v == "up") direction = Up;
31 else if (v == "down") direction = Down;
32
33 v = params.get("units");
34 if (v == "pixel") units = Pixel;
35 else if (v == "tile-width") units = TileWidth;
36 else if (v == "tile-height") units = TileHeight;
37 else if (v == "zoomed-pixel") units = ZoomedPixel;
38 else if (v == "zoomed-tile-width") units = ZoomedTileWidth;
39 else if (v == "zoomed-tile-height") units = ZoomedTileHeight;
40 else if (v == "viewport-width") units = ViewportWidth;
41 else if (v == "viewport-height") units = ViewportHeight;
42
43 int q = params.get_as<int>("quantity");
44 quantity = std::max<int>(1, q);
45}
46
47std::string MoveThing::getFriendlyString() const
48{
49 std::string dim, dir;
50
51 switch (units) {
52 case Pixel: dim = Strings::commands_Move_Pixel(); break;
53 case TileWidth: dim = Strings::commands_Move_TileWidth(); break;
54 case TileHeight: dim = Strings::commands_Move_TileHeight(); break;
55 case ZoomedPixel: dim = Strings::commands_Move_ZoomedPixel(); break;
56 case ZoomedTileWidth: dim = Strings::commands_Move_ZoomedTileWidth(); break;
57 case ZoomedTileHeight: dim = Strings::commands_Move_ZoomedTileHeight(); break;
58 case ViewportWidth: dim = Strings::commands_Move_ViewportWidth(); break;
59 case ViewportHeight: dim = Strings::commands_Move_ViewportHeight(); break;
60 }
61
62 switch (direction) {
63 case Left: dir = Strings::commands_Move_Left(); break;
64 case Right: dir = Strings::commands_Move_Right(); break;
65 case Up: dir = Strings::commands_Move_Up(); break;
66 case Down: dir = Strings::commands_Move_Down(); break;
67 }
68
69 return fmt::format(Strings::commands_Move_Thing(),
70 quantity, dim, dir);
71}
72
73gfx::Point MoveThing::getDelta(Context* context) const
74{
75 gfx::Point delta(0, 0);
76
77 DocView* view = static_cast<UIContext*>(context)->activeView();
78 if (!view)
79 return delta;
80
81 Editor* editor = view->editor();
82 gfx::Rect vp = view->viewWidget()->viewportBounds();
83 gfx::Rect gridBounds = view->document()->sprite()->gridBounds();
84 int pixels = 0;
85
86 switch (units) {
87 case Pixel:
88 pixels = 1;
89 break;
90 case TileWidth:
91 pixels = gridBounds.w;
92 break;
93 case TileHeight:
94 pixels = gridBounds.h;
95 break;
96 case ZoomedPixel:
97 pixels = editor->zoom().apply(1);
98 break;
99 case ZoomedTileWidth:
100 pixels = editor->zoom().apply(gridBounds.w);
101 break;
102 case ZoomedTileHeight:
103 pixels = editor->zoom().apply(gridBounds.h);
104 break;
105 case ViewportWidth:
106 pixels = vp.h;
107 break;
108 case ViewportHeight:
109 pixels = vp.w;
110 break;
111 }
112
113 switch (direction) {
114 case Left: delta.x = -quantity * pixels; break;
115 case Right: delta.x = +quantity * pixels; break;
116 case Up: delta.y = -quantity * pixels; break;
117 case Down: delta.y = +quantity * pixels; break;
118 }
119
120 return delta;
121}
122
123} // namespace app
124