• 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 "tools/sk_app/CommandSet.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "src/core/SkStringUtils.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(skui::Key key,skui::InputState state,skui::ModifierKey modifiers)38 bool CommandSet::onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) {
39     if (skui::InputState::kDown == 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,skui::ModifierKey modifiers)51 bool CommandSet::onChar(SkUnichar c, skui::ModifierKey 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(skui::Key k,const char * keyName,const char * group,const char * description,std::function<void (void)> function)77 void CommandSet::addCommand(skui::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 
compareCommandKey(const Command & first,const Command & second)82 bool CommandSet::compareCommandKey(const Command& first, const Command& second) {
83     return SK_strcasecmp(first.fKeyName.c_str(), second.fKeyName.c_str()) < 0;
84 }
85 
compareCommandGroup(const Command & first,const Command & second)86 bool CommandSet::compareCommandGroup(const Command& first, const Command& second) {
87     return SK_strcasecmp(first.fGroup.c_str(), second.fGroup.c_str()) < 0;
88 }
89 
drawHelp(SkCanvas * canvas)90 void CommandSet::drawHelp(SkCanvas* canvas) {
91     if (kNone_HelpMode == fHelpMode) {
92         return;
93     }
94 
95     // Sort commands for current mode:
96     std::stable_sort(fCommands.begin(), fCommands.end(),
97                      kAlphabetical_HelpMode == fHelpMode ? compareCommandKey : compareCommandGroup);
98 
99     SkFont font;
100     font.setSize(16);
101 
102     SkFont groupFont;
103     groupFont.setSize(18);
104 
105     SkPaint bgPaint;
106     bgPaint.setColor(0xC0000000);
107     canvas->drawPaint(bgPaint);
108 
109     SkPaint paint;
110     paint.setAntiAlias(true);
111     paint.setColor(0xFFFFFFFF);
112 
113     SkPaint groupPaint;
114     groupPaint.setAntiAlias(true);
115     groupPaint.setColor(0xFFFFFFFF);
116 
117     SkScalar x = SkIntToScalar(10);
118     SkScalar y = SkIntToScalar(10);
119 
120     // Measure all key strings:
121     SkScalar keyWidth = 0;
122     for (Command& cmd : fCommands) {
123         keyWidth = std::max(keyWidth,
124                                font.measureText(cmd.fKeyName.c_str(), cmd.fKeyName.size(),
125                                                 SkTextEncoding::kUTF8));
126     }
127     keyWidth += font.measureText(" ", 1, SkTextEncoding::kUTF8);
128 
129     // If we're grouping by category, we'll be adding text height on every new group (including the
130     // first), so no need to do that here. Otherwise, skip down so the first line is where we want.
131     if (kGrouped_HelpMode != fHelpMode) {
132         y += font.getSize();
133     }
134 
135     // Print everything:
136     SkString lastGroup;
137     for (Command& cmd : fCommands) {
138         if (kGrouped_HelpMode == fHelpMode && lastGroup != cmd.fGroup) {
139             // Group change. Advance and print header:
140             y += font.getSize();
141             canvas->drawSimpleText(cmd.fGroup.c_str(), cmd.fGroup.size(), SkTextEncoding::kUTF8,
142                                    x, y, groupFont, groupPaint);
143             y += groupFont.getSize() + 2;
144             lastGroup = cmd.fGroup;
145         }
146 
147         canvas->drawSimpleText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), SkTextEncoding::kUTF8,
148                                x, y, font, paint);
149         SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str());
150         canvas->drawString(text, x + keyWidth, y, font, paint);
151         y += font.getSize() + 2;
152     }
153 }
154 
getCommandsAsSoftkeys() const155 std::vector<SkString> CommandSet::getCommandsAsSoftkeys() const {
156     std::vector<SkString> result;
157     for(const Command& command : fCommands) {
158         result.push_back(command.getSoftkeyString());
159     }
160     return result;
161 }
162 
163 }   // namespace sk_app
164