| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef CommandSet_DEFINED | 8 #ifndef CommandSet_DEFINED |
| 9 #define CommandSet_DEFINED | 9 #define CommandSet_DEFINED |
| 10 | 10 |
| 11 #include "SkString.h" | 11 #include "SkString.h" |
| 12 #include "Window.h" | 12 #include "Window.h" |
| 13 | 13 |
| 14 #include <functional> | 14 #include <functional> |
| 15 #include <vector> |
| 15 | 16 |
| 16 class SkCanvas; | 17 class SkCanvas; |
| 17 | 18 |
| 18 namespace sk_app { | 19 namespace sk_app { |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * Helper class used by applications that want to hook keypresses to trigger eve
nts. | 22 * Helper class used by applications that want to hook keypresses to trigger eve
nts. |
| 22 * | 23 * |
| 23 * An app can simply store an instance of CommandSet and then use it as follows: | 24 * An app can simply store an instance of CommandSet and then use it as follows: |
| 24 * 1) Attach to the Window at initialization time. This registers key handlers o
n the window. | 25 * 1) Attach to the Window at initialization time. This registers key handlers o
n the window. |
| 25 * 2) Register commands to be executed for characters or keys. Each command need
s a Group and a | 26 * 2) Register commands to be executed for characters or keys. Each command need
s a Group and a |
| 26 * description (both just strings). Commands attached to Keys (rather than ch
aracters) also need | 27 * description (both just strings). Commands attached to Keys (rather than ch
aracters) also need |
| 27 * a displayable name for the Key. Finally, a function to execute when the ke
y or character is | 28 * a displayable name for the Key. Finally, a function to execute when the ke
y or character is |
| 28 * pressed must be supplied. The easiest option to is pass in a lambda that c
aptures [this] | 29 * pressed must be supplied. The easiest option to is pass in a lambda that c
aptures [this] |
| 29 * (your application object), and performs whatever action is desired. | 30 * (your application object), and performs whatever action is desired. |
| 30 * 3) At the end of your onPaint, call drawHelp, and pass in the application's c
anvas. | 31 * 3) At the end of your onPaint, call drawHelp, and pass in the application's c
anvas. |
| 31 | 32 |
| 32 * The CommandSet always binds 'h' to cycle through two different help screens.
The first shows | 33 * The CommandSet always binds 'h' to cycle through two different help screens.
The first shows |
| 33 * all commands, organized by Group (with headings for each Group). The second s
hows all commands | 34 * all commands, organized by Group (with headings for each Group). The second s
hows all commands |
| 34 * alphabetically by key/character. | 35 * alphabetically by key/character. |
| 35 */ | 36 */ |
| 36 class CommandSet { | 37 class CommandSet { |
| 37 public: | 38 public: |
| 38 CommandSet(); | 39 CommandSet(); |
| 39 | 40 |
| 40 void attach(Window* window); | 41 void attach(Window* window); |
| 41 bool onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32
_t modifiers); | 42 bool onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32
_t modifiers); |
| 42 bool onChar(SkUnichar, uint32_t modifiers); | 43 bool onChar(SkUnichar, uint32_t modifiers); |
| 44 bool onSoftkey(const SkString& softkey); |
| 43 | 45 |
| 44 void addCommand(SkUnichar c, const char* group, const char* description, | 46 void addCommand(SkUnichar c, const char* group, const char* description, |
| 45 std::function<void(void)> function); | 47 std::function<void(void)> function); |
| 46 void addCommand(Window::Key k, const char* keyName, const char* group, const
char* description, | 48 void addCommand(Window::Key k, const char* keyName, const char* group, const
char* description, |
| 47 std::function<void(void)> function); | 49 std::function<void(void)> function); |
| 48 | 50 |
| 49 void drawHelp(SkCanvas* canvas); | 51 void drawHelp(SkCanvas* canvas); |
| 50 | 52 |
| 53 std::vector<SkString> getCommandsAsSoftkeys() const; |
| 54 |
| 51 private: | 55 private: |
| 52 struct Command { | 56 struct Command { |
| 53 enum CommandType { | 57 enum CommandType { |
| 54 kChar_CommandType, | 58 kChar_CommandType, |
| 55 kKey_CommandType, | 59 kKey_CommandType, |
| 56 }; | 60 }; |
| 57 | 61 |
| 58 Command(SkUnichar c, const char* group, const char* description, | 62 Command(SkUnichar c, const char* group, const char* description, |
| 59 std::function<void(void)> function) | 63 std::function<void(void)> function) |
| 60 : fType(kChar_CommandType) | 64 : fType(kChar_CommandType) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 79 SkUnichar fChar; | 83 SkUnichar fChar; |
| 80 | 84 |
| 81 // For kKey_CommandType | 85 // For kKey_CommandType |
| 82 Window::Key fKey; | 86 Window::Key fKey; |
| 83 | 87 |
| 84 // Common to all command types | 88 // Common to all command types |
| 85 SkString fKeyName; | 89 SkString fKeyName; |
| 86 SkString fGroup; | 90 SkString fGroup; |
| 87 SkString fDescription; | 91 SkString fDescription; |
| 88 std::function<void(void)> fFunction; | 92 std::function<void(void)> fFunction; |
| 93 |
| 94 SkString getSoftkeyString() const { |
| 95 return SkStringPrintf("%s (%s)", fKeyName.c_str(), fDescription.c_st
r()); |
| 96 } |
| 89 }; | 97 }; |
| 90 | 98 |
| 91 static bool compareCommandKey(const Command& first, const Command& second); | 99 static bool compareCommandKey(const Command& first, const Command& second); |
| 92 static bool compareCommandGroup(const Command& first, const Command& second)
; | 100 static bool compareCommandGroup(const Command& first, const Command& second)
; |
| 93 | 101 |
| 94 enum HelpMode { | 102 enum HelpMode { |
| 95 kNone_HelpMode, | 103 kNone_HelpMode, |
| 96 kGrouped_HelpMode, | 104 kGrouped_HelpMode, |
| 97 kAlphabetical_HelpMode, | 105 kAlphabetical_HelpMode, |
| 98 }; | 106 }; |
| 99 | 107 |
| 100 Window* fWindow; | 108 Window* fWindow; |
| 101 SkTArray<Command> fCommands; | 109 SkTArray<Command> fCommands; |
| 102 HelpMode fHelpMode; | 110 HelpMode fHelpMode; |
| 103 }; | 111 }; |
| 104 | 112 |
| 105 } // namespace sk_app | 113 } // namespace sk_app |
| 106 | 114 |
| 107 #endif | 115 #endif |
| OLD | NEW |