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