| 1 | // Aseprite |
| 2 | // Copyright (C) 2019-2020 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2016 David Capello |
| 4 | // |
| 5 | // This program is distributed under the terms of |
| 6 | // the End-User License Agreement for Aseprite. |
| 7 | |
| 8 | #ifndef APP_UI_EDITOR_EDITOR_STATE_H_INCLUDED |
| 9 | #define APP_UI_EDITOR_EDITOR_STATE_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "base/disable_copying.h" |
| 13 | #include "gfx/fwd.h" |
| 14 | #include "gfx/point.h" |
| 15 | |
| 16 | #include <memory> |
| 17 | |
| 18 | namespace gfx { |
| 19 | class Region; |
| 20 | } |
| 21 | |
| 22 | namespace ui { |
| 23 | class KeyMessage; |
| 24 | class MouseMessage; |
| 25 | class TouchMessage; |
| 26 | } |
| 27 | |
| 28 | namespace doc { |
| 29 | class Tag; |
| 30 | } |
| 31 | |
| 32 | namespace app { |
| 33 | class Editor; |
| 34 | class EditorDecorator; |
| 35 | |
| 36 | namespace tools { |
| 37 | class Ink; |
| 38 | class Tool; |
| 39 | } |
| 40 | |
| 41 | // Represents one state of the sprite's editor (Editor class). This |
| 42 | // is a base class, a dummy state that ignores all events from the |
| 43 | // Editor. Subclasses overrides these methods to customize the |
| 44 | // behavior of the Editor to do different tasks (e.g. scrolling, |
| 45 | // drawing in the active sprite, etc.). |
| 46 | class EditorState { |
| 47 | public: |
| 48 | enum LeaveAction { |
| 49 | DiscardState, |
| 50 | KeepState |
| 51 | }; |
| 52 | |
| 53 | EditorState() { } |
| 54 | virtual ~EditorState() { } |
| 55 | |
| 56 | // Returns true if this state is a "temporal" state. It means that |
| 57 | // this state doesn't go to other state than the previous one. |
| 58 | virtual bool isTemporalState() const { return false; } |
| 59 | |
| 60 | // Called just before this state is replaced by a new state in the |
| 61 | // Editor::setState() method. Returns true if this state should be |
| 62 | // kept in the EditorStatesHistory. |
| 63 | virtual LeaveAction onLeaveState(Editor* editor, EditorState* newState) { |
| 64 | return KeepState; |
| 65 | } |
| 66 | |
| 67 | // Called when this instance is set as the new Editor's state when |
| 68 | // Editor::setState() method is used. |
| 69 | virtual void onEnterState(Editor* editor) { } |
| 70 | |
| 71 | // Called just before the state will be removed from the |
| 72 | // EditorStatesHistory. This event is useful to remove the |
| 73 | // decorator from the editor. |
| 74 | virtual void onBeforePopState(Editor* editor) { } |
| 75 | |
| 76 | // Called when the current tool in the tool bar changes. It is |
| 77 | // useful for states which depends on the selected current tool (as |
| 78 | // MovingPixelsState which drops the pixels in case the user selects |
| 79 | // other drawing tool). |
| 80 | virtual void onActiveToolChange(Editor* editor, tools::Tool* tool) { } |
| 81 | |
| 82 | // Called when the editor gets the focus. |
| 83 | virtual void onEditorGotFocus(Editor* editor) { } |
| 84 | |
| 85 | // Called when the user presses a mouse button over the editor. |
| 86 | virtual bool onMouseDown(Editor* editor, ui::MouseMessage* msg) { return false; } |
| 87 | |
| 88 | // Called when the user releases a mouse button. |
| 89 | virtual bool onMouseUp(Editor* editor, ui::MouseMessage* msg) { return false; } |
| 90 | |
| 91 | // Called when the user moves the mouse over the editor. |
| 92 | virtual bool onMouseMove(Editor* editor, ui::MouseMessage* msg) { return false; } |
| 93 | |
| 94 | // Called when the user moves the mouse wheel over the editor. |
| 95 | virtual bool onMouseWheel(Editor* editor, ui::MouseMessage* msg) { return false; } |
| 96 | |
| 97 | // Called when the user wants to zoom in/out using a pinch gesture in the trackpad. |
| 98 | virtual bool onTouchMagnify(Editor* editor, ui::TouchMessage* msg) { return false; } |
| 99 | |
| 100 | // Called when the user moves the mouse wheel over the editor. |
| 101 | virtual bool onDoubleClick(Editor* editor, ui::MouseMessage* msg) { return false; } |
| 102 | |
| 103 | // Called each time the mouse changes its position so we can set an |
| 104 | // appropiated cursor depending on the new coordinates of the mouse |
| 105 | // pointer. |
| 106 | virtual bool onSetCursor(Editor* editor, const gfx::Point& mouseScreenPos) { return false; } |
| 107 | |
| 108 | // Called when a key is pressed over the current editor. |
| 109 | virtual bool onKeyDown(Editor* editor, ui::KeyMessage* msg) { return false; } |
| 110 | |
| 111 | // Called when a key is released. |
| 112 | virtual bool onKeyUp(Editor* editor, ui::KeyMessage* msg) { return false; } |
| 113 | |
| 114 | // Called when the editor scroll is changed. |
| 115 | virtual bool onScrollChange(Editor* editor) { return false; } |
| 116 | |
| 117 | // Called when status bar needs to be updated. |
| 118 | virtual bool onUpdateStatusBar(Editor* editor) { return false; } |
| 119 | |
| 120 | // When a part of the sprite will be exposed. |
| 121 | virtual void onExposeSpritePixels(const gfx::Region& rgn) { } |
| 122 | |
| 123 | // Returns true if the this state requires the brush-preview as |
| 124 | // drawing cursor. |
| 125 | virtual bool requireBrushPreview() { return false; } |
| 126 | |
| 127 | // Returns true if this state allow layer edges and cel guides |
| 128 | virtual bool allowLayerEdges() { return false; } |
| 129 | |
| 130 | // Returns true if this state accept the given quicktool. |
| 131 | virtual bool acceptQuickTool(tools::Tool* tool) { return true; } |
| 132 | |
| 133 | // Custom ink in this state. |
| 134 | virtual tools::Ink* getStateInk() const { return nullptr; } |
| 135 | |
| 136 | // Called when a tag is deleted. |
| 137 | virtual void onRemoveTag(Editor* editor, doc::Tag* tag) { } |
| 138 | |
| 139 | // Used to adjust the grid origin point for temporal cels created |
| 140 | // by states like DrawingState + ExpandCelCanvas. |
| 141 | virtual bool getGridBounds(Editor* editor, gfx::Rect& gridBounds) { return false; } |
| 142 | |
| 143 | private: |
| 144 | DISABLE_COPYING(EditorState); |
| 145 | }; |
| 146 | |
| 147 | using EditorStatePtr = std::shared_ptr<EditorState>; |
| 148 | |
| 149 | } // namespace app |
| 150 | |
| 151 | #endif // APP_UI_EDITOR_EDITOR_STATE_H_INCLUDED |
| 152 | |