• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkFont.h"
12 #include "SkTSort.h"
13 
14 namespace sk_app {
15 
CommandSet()16 CommandSet::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 
attach(Window * window)34 void CommandSet::attach(Window* window) {
35     fWindow = window;
36 }
37 
onKey(Window::Key key,Window::InputState state,uint32_t modifiers)38 bool CommandSet::onKey(Window::Key key, Window::InputState state, uint32_t modifiers) {
39     if (Window::kDown_InputState == state) {
40         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 
onChar(SkUnichar c,uint32_t modifiers)51 bool CommandSet::onChar(SkUnichar c, uint32_t modifiers) {
52     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 
onSoftkey(const SkString & softkey)62 bool 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 
addCommand(SkUnichar c,const char * group,const char * description,std::function<void (void)> function)72 void 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 
addCommand(Window::Key k,const char * keyName,const char * group,const char * description,std::function<void (void)> function)77 void CommandSet::addCommand(Window::Key k, const char* keyName, const char* group,
78                             const char* description, std::function<void(void)> function) {
79     fCommands.push_back(Command(k, keyName, group, description, function));
80 }
81 
82 #if defined(SK_BUILD_FOR_WIN)
83     #define SK_strcasecmp   _stricmp
84 #else
85     #define SK_strcasecmp   strcasecmp
86 #endif
87 
compareCommandKey(const Command & first,const Command & second)88 bool CommandSet::compareCommandKey(const Command& first, const Command& second) {
89     return SK_strcasecmp(first.fKeyName.c_str(), second.fKeyName.c_str()) < 0;
90 }
91 
compareCommandGroup(const Command & first,const Command & second)92 bool CommandSet::compareCommandGroup(const Command& first, const Command& second) {
93     return SK_strcasecmp(first.fGroup.c_str(), second.fGroup.c_str()) < 0;
94 }
95 
drawHelp(SkCanvas * canvas)96 void 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 
105     SkFont font;
106     font.setSize(16);
107 
108     SkFont groupFont;
109     groupFont.setSize(18);
110 
111     SkPaint bgPaint;
112     bgPaint.setColor(0xC0000000);
113     canvas->drawPaint(bgPaint);
114 
115     SkPaint paint;
116     paint.setAntiAlias(true);
117     paint.setColor(0xFFFFFFFF);
118 
119     SkPaint groupPaint;
120     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,
130                                font.measureText(cmd.fKeyName.c_str(), cmd.fKeyName.size(),
131                                                 kUTF8_SkTextEncoding));
132     }
133     keyWidth += font.measureText(" ", 1, kUTF8_SkTextEncoding);
134 
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) {
138         y += font.getSize();
139     }
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:
146             y += font.getSize();
147             canvas->drawSimpleText(cmd.fGroup.c_str(), cmd.fGroup.size(), kUTF8_SkTextEncoding,
148                                    x, y, groupFont, groupPaint);
149             y += groupFont.getSize() + 2;
150             lastGroup = cmd.fGroup;
151         }
152 
153         canvas->drawSimpleText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), kUTF8_SkTextEncoding,
154                                x, y, font, paint);
155         SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str());
156         canvas->drawString(text, x + keyWidth, y, font, paint);
157         y += font.getSize() + 2;
158     }
159 }
160 
getCommandsAsSoftkeys() const161 std::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 
169 }   // namespace sk_app
170