1 // Aseprite 2 // Copyright (C) 2017 David Capello 3 // 4 // This program is distributed under the terms of 5 // the End-User License Agreement for Aseprite. 6 7 #ifdef HAVE_CONFIG_H 8 #include "config.h" 9 #endif 10 11 #include "app/commands/move_thing.h" 12 13 #include "app/commands/params.h" 14 #include "app/i18n/strings.h" 15 #include "app/pref/preferences.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 23 namespace app { 24 onLoadParams(const Params & params)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 getFriendlyString() const47std::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 getDelta(Context * context) const73gfx::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 DocumentPreferences& docPref = Preferences::instance().document(view->document()); 82 Editor* editor = view->editor(); 83 gfx::Rect vp = view->viewWidget()->viewportBounds(); 84 gfx::Rect gridBounds = docPref.grid.bounds(); 85 int pixels = 0; 86 87 switch (units) { 88 case Pixel: 89 pixels = 1; 90 break; 91 case TileWidth: 92 pixels = gridBounds.w; 93 break; 94 case TileHeight: 95 pixels = gridBounds.h; 96 break; 97 case ZoomedPixel: 98 pixels = editor->zoom().apply(1); 99 break; 100 case ZoomedTileWidth: 101 pixels = editor->zoom().apply(gridBounds.w); 102 break; 103 case ZoomedTileHeight: 104 pixels = editor->zoom().apply(gridBounds.h); 105 break; 106 case ViewportWidth: 107 pixels = vp.h; 108 break; 109 case ViewportHeight: 110 pixels = vp.w; 111 break; 112 } 113 114 switch (direction) { 115 case Left: delta.x = -quantity * pixels; break; 116 case Right: delta.x = +quantity * pixels; break; 117 case Up: delta.y = -quantity * pixels; break; 118 case Down: delta.y = +quantity * pixels; break; 119 } 120 121 return delta; 122 } 123 124 } // namespace app 125