1 // Aseprite
2 // Copyright (C) 2001-2018  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_KEY_H_INCLUDED
8 #define APP_UI_KEY_H_INCLUDED
9 #pragma once
10 
11 #include "app/commands/params.h"
12 #include "app/ui/key_context.h"
13 #include "base/convert_to.h"
14 #include "ui/accelerator.h"
15 
16 #include <memory>
17 #include <vector>
18 
19 namespace ui {
20   class Message;
21 }
22 
23 namespace app {
24   class Command;
25   class KeyboardShortcuts;
26 
27   namespace tools {
28     class Tool;
29   }
30 
31   enum class KeySource {
32     Original,
33     UserDefined
34   };
35 
36   enum class KeyType {
37     Command,
38     Tool,
39     Quicktool,
40     Action,
41     WheelAction,
42   };
43 
44   // TODO This should be called "KeyActionModifier" or something similar
45   enum class KeyAction {
46     None                      = 0x00000000,
47     CopySelection             = 0x00000001,
48     SnapToGrid                = 0x00000002,
49     AngleSnap                 = 0x00000004,
50     MaintainAspectRatio       = 0x00000008,
51     LockAxis                  = 0x00000010,
52     AddSelection              = 0x00000020,
53     SubtractSelection         = 0x00000040,
54     AutoSelectLayer           = 0x00000080,
55     LeftMouseButton           = 0x00000100,
56     RightMouseButton          = 0x00000200,
57     StraightLineFromLastPoint = 0x00000400,
58     MoveOrigin                = 0x00000800,
59     SquareAspect              = 0x00001000,
60     DrawFromCenter            = 0x00002000,
61     ScaleFromCenter           = 0x00004000,
62     AngleSnapFromLastPoint    = 0x00008000,
63     RotateShape               = 0x00010000,
64   };
65 
66   enum class WheelAction {
67     None,
68     Zoom,
69     VScroll,
70     HScroll,
71     FgColor,
72     BgColor,
73     Frame,
74     BrushSize,
75     BrushAngle,
76     ToolSameGroup,
77     ToolOtherGroup,
78     Layer,
79     InkOpacity,
80     LayerOpacity,
81     CelOpacity,
82     Alpha,
83     HslHue,
84     HslSaturation,
85     HslLightness,
86     HsvHue,
87     HsvSaturation,
88     HsvValue,
89 
90     // Range
91     First = Zoom,
92     Last = HsvValue,
93   };
94 
95   inline KeyAction operator&(KeyAction a, KeyAction b) {
96     return KeyAction(int(a) & int(b));
97   }
98 
99   class Key {
100   public:
101     Key(Command* command, const Params& params, KeyContext keyContext);
102     Key(KeyType type, tools::Tool* tool);
103     explicit Key(KeyAction action);
104     explicit Key(WheelAction action);
105 
type()106     KeyType type() const { return m_type; }
accels()107     const ui::Accelerators& accels() const {
108       return (m_useUsers ? m_users: m_accels);
109     }
origAccels()110     const ui::Accelerators& origAccels() const { return m_accels; }
userAccels()111     const ui::Accelerators& userAccels() const { return m_users; }
userRemovedAccels()112     const ui::Accelerators& userRemovedAccels() const { return m_userRemoved; }
113 
114     void add(const ui::Accelerator& accel,
115              const KeySource source,
116              KeyboardShortcuts& globalKeys);
117     const ui::Accelerator* isPressed(const ui::Message* msg,
118                                      KeyboardShortcuts& globalKeys) const;
119     bool isPressed() const;
120     bool isLooselyPressed() const;
121 
122     bool hasAccel(const ui::Accelerator& accel) const;
123     void disableAccel(const ui::Accelerator& accel);
124 
125     // Resets user accelerators to the original ones.
126     void reset();
127 
128     void copyOriginalToUser();
129 
130     // for KeyType::Command
command()131     Command* command() const { return m_command; }
params()132     const Params& params() const { return m_params; }
keycontext()133     KeyContext keycontext() const { return m_keycontext; }
134     // for KeyType::Tool or Quicktool
tool()135     tools::Tool* tool() const { return m_tool; }
136     // for KeyType::Action
action()137     KeyAction action() const { return m_action; }
138     // for KeyType::WheelAction
wheelAction()139     WheelAction wheelAction() const { return m_wheelAction; }
140 
141     std::string triggerString() const;
142 
143   private:
144     KeyType m_type;
145     ui::Accelerators m_accels;      // Default accelerators (from gui.xml)
146     ui::Accelerators m_users;       // User-defined accelerators
147     ui::Accelerators m_userRemoved; // Default accelerators removed by user
148     bool m_useUsers;
149     KeyContext m_keycontext;
150 
151     // for KeyType::Command
152     Command* m_command;
153     Params m_params;
154     // for KeyType::Tool or Quicktool
155     tools::Tool* m_tool;
156     // for KeyType::Action
157     KeyAction m_action;
158     // for KeyType::WheelAction
159     WheelAction m_wheelAction;
160   };
161 
162   typedef std::shared_ptr<Key> KeyPtr;
163   typedef std::vector<KeyPtr> Keys;
164 
165   std::string convertKeyContextToString(KeyContext keyContext);
166   std::string convertKeyContextToUserFriendlyString(KeyContext keyContext);
167 
168 } // namespace app
169 
170 namespace base {
171 
172   template<> app::KeyAction convert_to(const std::string& from);
173   template<> std::string convert_to(const app::KeyAction& from);
174 
175   template<> app::WheelAction convert_to(const std::string& from);
176   template<> std::string convert_to(const app::WheelAction& from);
177 
178 } // namespace base
179 
180 #endif
181