blob: ea0eb137e57b090743bf07e23cc0b2a938052509 [file] [log] [blame]
brianosman622c8d52016-05-10 06:50:49 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/sk_app/CommandSet.h"
brianosman622c8d52016-05-10 06:50:49 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkFont.h"
12#include "src/core/SkTSort.h"
brianosman622c8d52016-05-10 06:50:49 -070013
14namespace sk_app {
15
brianosman622c8d52016-05-10 06:50:49 -070016CommandSet::CommandSet()
17 : fHelpMode(kNone_HelpMode) {
18 this->addCommand('h', "Overlays", "Show help screen", [this]() {
19 switch (this->fHelpMode) {
20 case kNone_HelpMode:
21 this->fHelpMode = kGrouped_HelpMode;
22 break;
23 case kGrouped_HelpMode:
24 this->fHelpMode = kAlphabetical_HelpMode;
25 break;
26 case kAlphabetical_HelpMode:
27 this->fHelpMode = kNone_HelpMode;
28 break;
29 }
30 fWindow->inval();
31 });
32}
33
34void CommandSet::attach(Window* window) {
35 fWindow = window;
brianosman622c8d52016-05-10 06:50:49 -070036}
37
Hal Canaryb1f411a2019-08-29 10:39:22 -040038bool CommandSet::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
39 if (skui::InputState::kDown == state) {
brianosman622c8d52016-05-10 06:50:49 -070040 for (Command& cmd : fCommands) {
41 if (Command::kKey_CommandType == cmd.fType && key == cmd.fKey) {
42 cmd.fFunction();
43 return true;
44 }
45 }
46 }
47
48 return false;
49}
50
Hal Canaryb1f411a2019-08-29 10:39:22 -040051bool CommandSet::onChar(SkUnichar c, skui::ModifierKey modifiers) {
brianosman622c8d52016-05-10 06:50:49 -070052 for (Command& cmd : fCommands) {
53 if (Command::kChar_CommandType == cmd.fType && c == cmd.fChar) {
54 cmd.fFunction();
55 return true;
56 }
57 }
58
59 return false;
60}
61
liyuqianb73c24b2016-06-03 08:47:23 -070062bool CommandSet::onSoftkey(const SkString& softkey) {
63 for (const Command& cmd : fCommands) {
64 if (cmd.getSoftkeyString().equals(softkey)) {
65 cmd.fFunction();
66 return true;
67 }
68 }
69 return false;
70}
71
brianosman622c8d52016-05-10 06:50:49 -070072void CommandSet::addCommand(SkUnichar c, const char* group, const char* description,
73 std::function<void(void)> function) {
74 fCommands.push_back(Command(c, group, description, function));
75}
76
Hal Canaryb1f411a2019-08-29 10:39:22 -040077void CommandSet::addCommand(skui::Key k, const char* keyName, const char* group,
brianosman622c8d52016-05-10 06:50:49 -070078 const char* description, std::function<void(void)> function) {
79 fCommands.push_back(Command(k, keyName, group, description, function));
80}
81
Mike Klein8f11d4d2018-01-24 12:42:55 -050082#if defined(SK_BUILD_FOR_WIN)
brianosman622c8d52016-05-10 06:50:49 -070083 #define SK_strcasecmp _stricmp
84#else
85 #define SK_strcasecmp strcasecmp
86#endif
87
88bool CommandSet::compareCommandKey(const Command& first, const Command& second) {
89 return SK_strcasecmp(first.fKeyName.c_str(), second.fKeyName.c_str()) < 0;
90}
91
92bool CommandSet::compareCommandGroup(const Command& first, const Command& second) {
93 return SK_strcasecmp(first.fGroup.c_str(), second.fGroup.c_str()) < 0;
94}
95
96void CommandSet::drawHelp(SkCanvas* canvas) {
97 if (kNone_HelpMode == fHelpMode) {
98 return;
99 }
100
101 // Sort commands for current mode:
102 SkTQSort(fCommands.begin(), fCommands.end() - 1,
103 kAlphabetical_HelpMode == fHelpMode ? compareCommandKey : compareCommandGroup);
104
Mike Reed91919132019-01-02 12:21:01 -0500105 SkFont font;
106 font.setSize(16);
107
108 SkFont groupFont;
109 groupFont.setSize(18);
110
brianosman622c8d52016-05-10 06:50:49 -0700111 SkPaint bgPaint;
112 bgPaint.setColor(0xC0000000);
113 canvas->drawPaint(bgPaint);
114
115 SkPaint paint;
brianosman622c8d52016-05-10 06:50:49 -0700116 paint.setAntiAlias(true);
117 paint.setColor(0xFFFFFFFF);
118
119 SkPaint groupPaint;
brianosman622c8d52016-05-10 06:50:49 -0700120 groupPaint.setAntiAlias(true);
121 groupPaint.setColor(0xFFFFFFFF);
122
123 SkScalar x = SkIntToScalar(10);
124 SkScalar y = SkIntToScalar(10);
125
126 // Measure all key strings:
127 SkScalar keyWidth = 0;
128 for (Command& cmd : fCommands) {
129 keyWidth = SkMaxScalar(keyWidth,
Mike Reed91919132019-01-02 12:21:01 -0500130 font.measureText(cmd.fKeyName.c_str(), cmd.fKeyName.size(),
Ben Wagner51e15a62019-05-07 15:38:46 -0400131 SkTextEncoding::kUTF8));
brianosman622c8d52016-05-10 06:50:49 -0700132 }
Ben Wagner51e15a62019-05-07 15:38:46 -0400133 keyWidth += font.measureText(" ", 1, SkTextEncoding::kUTF8);
brianosman622c8d52016-05-10 06:50:49 -0700134
135 // If we're grouping by category, we'll be adding text height on every new group (including the
136 // first), so no need to do that here. Otherwise, skip down so the first line is where we want.
137 if (kGrouped_HelpMode != fHelpMode) {
Mike Reed91919132019-01-02 12:21:01 -0500138 y += font.getSize();
brianosman622c8d52016-05-10 06:50:49 -0700139 }
140
141 // Print everything:
142 SkString lastGroup;
143 for (Command& cmd : fCommands) {
144 if (kGrouped_HelpMode == fHelpMode && lastGroup != cmd.fGroup) {
145 // Group change. Advance and print header:
Mike Reed91919132019-01-02 12:21:01 -0500146 y += font.getSize();
Ben Wagner51e15a62019-05-07 15:38:46 -0400147 canvas->drawSimpleText(cmd.fGroup.c_str(), cmd.fGroup.size(), SkTextEncoding::kUTF8,
Mike Reed91919132019-01-02 12:21:01 -0500148 x, y, groupFont, groupPaint);
149 y += groupFont.getSize() + 2;
brianosman622c8d52016-05-10 06:50:49 -0700150 lastGroup = cmd.fGroup;
151 }
152
Ben Wagner51e15a62019-05-07 15:38:46 -0400153 canvas->drawSimpleText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), SkTextEncoding::kUTF8,
Mike Reed91919132019-01-02 12:21:01 -0500154 x, y, font, paint);
brianosman622c8d52016-05-10 06:50:49 -0700155 SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str());
Hal Canary89a644b2019-01-07 09:36:09 -0500156 canvas->drawString(text, x + keyWidth, y, font, paint);
Mike Reed91919132019-01-02 12:21:01 -0500157 y += font.getSize() + 2;
brianosman622c8d52016-05-10 06:50:49 -0700158 }
159}
160
liyuqianb73c24b2016-06-03 08:47:23 -0700161std::vector<SkString> CommandSet::getCommandsAsSoftkeys() const {
162 std::vector<SkString> result;
163 for(const Command& command : fCommands) {
164 result.push_back(command.getSoftkeyString());
165 }
166 return result;
167}
168
brianosman622c8d52016-05-10 06:50:49 -0700169} // namespace sk_app