OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "CommandSet.h" |
| 9 |
| 10 #include "SkCanvas.h" |
| 11 #include "SkTSort.h" |
| 12 |
| 13 namespace sk_app { |
| 14 |
| 15 static bool on_key_handler(Window::Key key, Window::InputState state, uint32_t m
odifiers, |
| 16 void* userData) { |
| 17 CommandSet* cs = reinterpret_cast<CommandSet*>(userData); |
| 18 return cs->onKey(key, state, modifiers); |
| 19 } |
| 20 |
| 21 static bool on_char_handler(SkUnichar c, uint32_t modifiers, void* userData) { |
| 22 CommandSet* cs = reinterpret_cast<CommandSet*>(userData); |
| 23 return cs->onChar(c, modifiers); |
| 24 } |
| 25 |
| 26 CommandSet::CommandSet() |
| 27 : fHelpMode(kNone_HelpMode) { |
| 28 this->addCommand('h', "Overlays", "Show help screen", [this]() { |
| 29 switch (this->fHelpMode) { |
| 30 case kNone_HelpMode: |
| 31 this->fHelpMode = kGrouped_HelpMode; |
| 32 break; |
| 33 case kGrouped_HelpMode: |
| 34 this->fHelpMode = kAlphabetical_HelpMode; |
| 35 break; |
| 36 case kAlphabetical_HelpMode: |
| 37 this->fHelpMode = kNone_HelpMode; |
| 38 break; |
| 39 } |
| 40 fWindow->inval(); |
| 41 }); |
| 42 } |
| 43 |
| 44 void CommandSet::attach(Window* window) { |
| 45 fWindow = window; |
| 46 window->registerKeyFunc(on_key_handler, this); |
| 47 window->registerCharFunc(on_char_handler, this); |
| 48 } |
| 49 |
| 50 bool CommandSet::onKey(Window::Key key, Window::InputState state, uint32_t modif
iers) { |
| 51 if (Window::kDown_InputState == state) { |
| 52 for (Command& cmd : fCommands) { |
| 53 if (Command::kKey_CommandType == cmd.fType && key == cmd.fKey) { |
| 54 cmd.fFunction(); |
| 55 return true; |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 return false; |
| 61 } |
| 62 |
| 63 bool CommandSet::onChar(SkUnichar c, uint32_t modifiers) { |
| 64 for (Command& cmd : fCommands) { |
| 65 if (Command::kChar_CommandType == cmd.fType && c == cmd.fChar) { |
| 66 cmd.fFunction(); |
| 67 return true; |
| 68 } |
| 69 } |
| 70 |
| 71 return false; |
| 72 } |
| 73 |
| 74 void CommandSet::addCommand(SkUnichar c, const char* group, const char* descript
ion, |
| 75 std::function<void(void)> function) { |
| 76 fCommands.push_back(Command(c, group, description, function)); |
| 77 } |
| 78 |
| 79 void CommandSet::addCommand(Window::Key k, const char* keyName, const char* grou
p, |
| 80 const char* description, std::function<void(void)> f
unction) { |
| 81 fCommands.push_back(Command(k, keyName, group, description, function)); |
| 82 } |
| 83 |
| 84 #if defined(SK_BUILD_FOR_WIN32) |
| 85 #define SK_strcasecmp _stricmp |
| 86 #else |
| 87 #define SK_strcasecmp strcasecmp |
| 88 #endif |
| 89 |
| 90 bool CommandSet::compareCommandKey(const Command& first, const Command& second)
{ |
| 91 return SK_strcasecmp(first.fKeyName.c_str(), second.fKeyName.c_str()) < 0; |
| 92 } |
| 93 |
| 94 bool CommandSet::compareCommandGroup(const Command& first, const Command& second
) { |
| 95 return SK_strcasecmp(first.fGroup.c_str(), second.fGroup.c_str()) < 0; |
| 96 } |
| 97 |
| 98 void CommandSet::drawHelp(SkCanvas* canvas) { |
| 99 if (kNone_HelpMode == fHelpMode) { |
| 100 return; |
| 101 } |
| 102 |
| 103 // Sort commands for current mode: |
| 104 SkTQSort(fCommands.begin(), fCommands.end() - 1, |
| 105 kAlphabetical_HelpMode == fHelpMode ? compareCommandKey : compareCo
mmandGroup); |
| 106 |
| 107 SkPaint bgPaint; |
| 108 bgPaint.setColor(0xC0000000); |
| 109 canvas->drawPaint(bgPaint); |
| 110 |
| 111 SkPaint paint; |
| 112 paint.setTextSize(16); |
| 113 paint.setAntiAlias(true); |
| 114 paint.setColor(0xFFFFFFFF); |
| 115 |
| 116 SkPaint groupPaint; |
| 117 groupPaint.setTextSize(18); |
| 118 groupPaint.setUnderlineText(true); |
| 119 groupPaint.setAntiAlias(true); |
| 120 groupPaint.setColor(0xFFFFFFFF); |
| 121 |
| 122 SkScalar x = SkIntToScalar(10); |
| 123 SkScalar y = SkIntToScalar(10); |
| 124 |
| 125 // Measure all key strings: |
| 126 SkScalar keyWidth = 0; |
| 127 for (Command& cmd : fCommands) { |
| 128 keyWidth = SkMaxScalar(keyWidth, |
| 129 paint.measureText(cmd.fKeyName.c_str(), cmd.fKeyN
ame.size())); |
| 130 } |
| 131 keyWidth += paint.measureText(" ", 1); |
| 132 |
| 133 // If we're grouping by category, we'll be adding text height on every new g
roup (including the |
| 134 // first), so no need to do that here. Otherwise, skip down so the first lin
e is where we want. |
| 135 if (kGrouped_HelpMode != fHelpMode) { |
| 136 y += paint.getTextSize(); |
| 137 } |
| 138 |
| 139 // Print everything: |
| 140 SkString lastGroup; |
| 141 for (Command& cmd : fCommands) { |
| 142 if (kGrouped_HelpMode == fHelpMode && lastGroup != cmd.fGroup) { |
| 143 // Group change. Advance and print header: |
| 144 y += paint.getTextSize(); |
| 145 canvas->drawText(cmd.fGroup.c_str(), cmd.fGroup.size(), x, y, groupP
aint); |
| 146 y += groupPaint.getTextSize() + 2; |
| 147 lastGroup = cmd.fGroup; |
| 148 } |
| 149 |
| 150 canvas->drawText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), x, y, paint)
; |
| 151 SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str()); |
| 152 canvas->drawText(text.c_str(), text.size(), x + keyWidth, y, paint); |
| 153 y += paint.getTextSize() + 2; |
| 154 } |
| 155 } |
| 156 |
| 157 } // namespace sk_app |
OLD | NEW |