CommandSet.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef CommandSet_DEFINED
  8. #define CommandSet_DEFINED
  9. #include "include/core/SkString.h"
  10. #include "tools/sk_app/Window.h"
  11. #include <functional>
  12. #include <vector>
  13. class SkCanvas;
  14. namespace sk_app {
  15. /**
  16. * Helper class used by applications that want to hook keypresses to trigger events.
  17. *
  18. * An app can simply store an instance of CommandSet and then use it as follows:
  19. * 1) Attach to the Window at initialization time.
  20. * 2) Register commands to be executed for characters or keys. Each command needs a Group and a
  21. * description (both just strings). Commands attached to Keys (rather than characters) also need
  22. * a displayable name for the Key. Finally, a function to execute when the key or character is
  23. * pressed must be supplied. The easiest option to is pass in a lambda that captures [this]
  24. * (your application object), and performs whatever action is desired.
  25. * 3) Register key and char handlers with the Window, and - depending on your state - forward those
  26. * events to the CommandSet's onKey, onChar, and onSoftKey.
  27. * 4) At the end of your onPaint, call drawHelp, and pass in the application's canvas.
  28. * The CommandSet always binds 'h' to cycle through two different help screens. The first shows
  29. * all commands, organized by Group (with headings for each Group). The second shows all commands
  30. * alphabetically by key/character.
  31. */
  32. class CommandSet {
  33. public:
  34. CommandSet();
  35. void attach(Window* window);
  36. bool onKey(sk_app::Window::Key key, InputState state, ModifierKey modifiers);
  37. bool onChar(SkUnichar, ModifierKey modifiers);
  38. bool onSoftkey(const SkString& softkey);
  39. void addCommand(SkUnichar c, const char* group, const char* description,
  40. std::function<void(void)> function);
  41. void addCommand(Window::Key k, const char* keyName, const char* group, const char* description,
  42. std::function<void(void)> function);
  43. void drawHelp(SkCanvas* canvas);
  44. std::vector<SkString> getCommandsAsSoftkeys() const;
  45. private:
  46. struct Command {
  47. enum CommandType {
  48. kChar_CommandType,
  49. kKey_CommandType,
  50. };
  51. Command(SkUnichar c, const char* group, const char* description,
  52. std::function<void(void)> function)
  53. : fType(kChar_CommandType)
  54. , fChar(c)
  55. , fKeyName(' ' == c ? SkString("Space") : SkStringPrintf("%c", c))
  56. , fGroup(group)
  57. , fDescription(description)
  58. , fFunction(function) {}
  59. Command(Window::Key k, const char* keyName, const char* group, const char* description,
  60. std::function<void(void)> function)
  61. : fType(kKey_CommandType)
  62. , fKey(k)
  63. , fKeyName(keyName)
  64. , fGroup(group)
  65. , fDescription(description)
  66. , fFunction(function) {}
  67. CommandType fType;
  68. // For kChar_CommandType
  69. SkUnichar fChar;
  70. // For kKey_CommandType
  71. Window::Key fKey;
  72. // Common to all command types
  73. SkString fKeyName;
  74. SkString fGroup;
  75. SkString fDescription;
  76. std::function<void(void)> fFunction;
  77. SkString getSoftkeyString() const {
  78. return SkStringPrintf("%s (%s)", fKeyName.c_str(), fDescription.c_str());
  79. }
  80. };
  81. static bool compareCommandKey(const Command& first, const Command& second);
  82. static bool compareCommandGroup(const Command& first, const Command& second);
  83. enum HelpMode {
  84. kNone_HelpMode,
  85. kGrouped_HelpMode,
  86. kAlphabetical_HelpMode,
  87. };
  88. Window* fWindow;
  89. SkTArray<Command> fCommands;
  90. HelpMode fHelpMode;
  91. };
  92. } // namespace sk_app
  93. #endif