skia2/tools/sk_app/CommandSet.h

118 lines
3.8 KiB
C
Raw Normal View History

/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef CommandSet_DEFINED
#define CommandSet_DEFINED
#include "include/core/SkString.h"
#include "tools/sk_app/Window.h"
#include <functional>
#include <vector>
class SkCanvas;
namespace sk_app {
/**
* Helper class used by applications that want to hook keypresses to trigger events.
*
* An app can simply store an instance of CommandSet and then use it as follows:
Integrate the ImGui library with viewer Code and docs are at: https://github.com/ocornut/imgui ImGui is an open source immediate mode GUI library that's lightweight and fairly simply to integrate. Widget functions return their state, and the library emits vertex and index data to render everything. It's got a huge set of built-in widgets and really robust layout control. For the initial integration, I had to fix up event handling in the viewer's app framework (to get mouse wheel and more keys, etc...). The new viewer 'Debug' window is toggled with the space bar. For this change, I've added one feature to that window: the slide picker. It's got a list of all slides, with filtering support, and the ability to click to switch slides. I also included the ImGui 'Demo' window (toggled with 'g'). This is nicely laid out, and includes examples of pretty much everything the library can do. It also serves as good documentation - find something that looks like what you want, and then go look at the corresponding code (all of it is in imgui_demo.cpp). I have other CLs with other features (like directly editing the primaries of the working color space), but I wanted to land this chunk first, then start adding more features. Other than adding new debugging features, there are few more outstanding work items: 1) Raster doesn't render the GUI correctly, due to non- invertible pos -> UV matrices. Florin is working on that. 2) Touch inputs aren't being routed yet, so the GUI isn't usable on Android yet. Might also be tough to work with, given the size. 3) ImGui has clipboard integration (that's why it wants the C, X, and V keys), but we need to wire it up to the OS' clipboard functions. 4) Draw commands can carry a void* payload to support drawing images (using whatever mechanism the engine has). I'd like to set that up (probably using SkImage*), which makes it really easy to add visualization of off-screen images in GMs, etc... BUG=skia: Change-Id: Iac2a63e37228d33141cb55b7e4d60bf11b7e9ae1 Reviewed-on: https://skia-review.googlesource.com/7702 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
2017-02-10 18:36:16 +00:00
* 1) Attach to the Window at initialization time.
* 2) Register commands to be executed for characters or keys. Each command needs a Group and a
* description (both just strings). Commands attached to Keys (rather than characters) also need
* a displayable name for the Key. Finally, a function to execute when the key or character is
* pressed must be supplied. The easiest option to is pass in a lambda that captures [this]
* (your application object), and performs whatever action is desired.
Integrate the ImGui library with viewer Code and docs are at: https://github.com/ocornut/imgui ImGui is an open source immediate mode GUI library that's lightweight and fairly simply to integrate. Widget functions return their state, and the library emits vertex and index data to render everything. It's got a huge set of built-in widgets and really robust layout control. For the initial integration, I had to fix up event handling in the viewer's app framework (to get mouse wheel and more keys, etc...). The new viewer 'Debug' window is toggled with the space bar. For this change, I've added one feature to that window: the slide picker. It's got a list of all slides, with filtering support, and the ability to click to switch slides. I also included the ImGui 'Demo' window (toggled with 'g'). This is nicely laid out, and includes examples of pretty much everything the library can do. It also serves as good documentation - find something that looks like what you want, and then go look at the corresponding code (all of it is in imgui_demo.cpp). I have other CLs with other features (like directly editing the primaries of the working color space), but I wanted to land this chunk first, then start adding more features. Other than adding new debugging features, there are few more outstanding work items: 1) Raster doesn't render the GUI correctly, due to non- invertible pos -> UV matrices. Florin is working on that. 2) Touch inputs aren't being routed yet, so the GUI isn't usable on Android yet. Might also be tough to work with, given the size. 3) ImGui has clipboard integration (that's why it wants the C, X, and V keys), but we need to wire it up to the OS' clipboard functions. 4) Draw commands can carry a void* payload to support drawing images (using whatever mechanism the engine has). I'd like to set that up (probably using SkImage*), which makes it really easy to add visualization of off-screen images in GMs, etc... BUG=skia: Change-Id: Iac2a63e37228d33141cb55b7e4d60bf11b7e9ae1 Reviewed-on: https://skia-review.googlesource.com/7702 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
2017-02-10 18:36:16 +00:00
* 3) Register key and char handlers with the Window, and - depending on your state - forward those
* events to the CommandSet's onKey, onChar, and onSoftKey.
* 4) At the end of your onPaint, call drawHelp, and pass in the application's canvas.
* The CommandSet always binds 'h' to cycle through two different help screens. The first shows
* all commands, organized by Group (with headings for each Group). The second shows all commands
* alphabetically by key/character.
*/
class CommandSet {
public:
CommandSet();
void attach(Window* window);
bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers);
bool onChar(SkUnichar, skui::ModifierKey modifiers);
bool onSoftkey(const SkString& softkey);
void addCommand(SkUnichar c, const char* group, const char* description,
std::function<void(void)> function);
void addCommand(skui::Key k, const char* keyName, const char* group, const char* description,
std::function<void(void)> function);
void drawHelp(SkCanvas* canvas);
std::vector<SkString> getCommandsAsSoftkeys() const;
private:
struct Command {
enum CommandType {
kChar_CommandType,
kKey_CommandType,
};
Command(SkUnichar c, const char* group, const char* description,
std::function<void(void)> function)
: fType(kChar_CommandType)
, fChar(c)
Integrate the ImGui library with viewer Code and docs are at: https://github.com/ocornut/imgui ImGui is an open source immediate mode GUI library that's lightweight and fairly simply to integrate. Widget functions return their state, and the library emits vertex and index data to render everything. It's got a huge set of built-in widgets and really robust layout control. For the initial integration, I had to fix up event handling in the viewer's app framework (to get mouse wheel and more keys, etc...). The new viewer 'Debug' window is toggled with the space bar. For this change, I've added one feature to that window: the slide picker. It's got a list of all slides, with filtering support, and the ability to click to switch slides. I also included the ImGui 'Demo' window (toggled with 'g'). This is nicely laid out, and includes examples of pretty much everything the library can do. It also serves as good documentation - find something that looks like what you want, and then go look at the corresponding code (all of it is in imgui_demo.cpp). I have other CLs with other features (like directly editing the primaries of the working color space), but I wanted to land this chunk first, then start adding more features. Other than adding new debugging features, there are few more outstanding work items: 1) Raster doesn't render the GUI correctly, due to non- invertible pos -> UV matrices. Florin is working on that. 2) Touch inputs aren't being routed yet, so the GUI isn't usable on Android yet. Might also be tough to work with, given the size. 3) ImGui has clipboard integration (that's why it wants the C, X, and V keys), but we need to wire it up to the OS' clipboard functions. 4) Draw commands can carry a void* payload to support drawing images (using whatever mechanism the engine has). I'd like to set that up (probably using SkImage*), which makes it really easy to add visualization of off-screen images in GMs, etc... BUG=skia: Change-Id: Iac2a63e37228d33141cb55b7e4d60bf11b7e9ae1 Reviewed-on: https://skia-review.googlesource.com/7702 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
2017-02-10 18:36:16 +00:00
, fKeyName(' ' == c ? SkString("Space") : SkStringPrintf("%c", c))
, fGroup(group)
, fDescription(description)
, fFunction(function) {}
Command(skui::Key k, const char* keyName, const char* group, const char* description,
std::function<void(void)> function)
: fType(kKey_CommandType)
, fKey(k)
, fKeyName(keyName)
, fGroup(group)
, fDescription(description)
, fFunction(function) {}
CommandType fType;
// For kChar_CommandType
SkUnichar fChar;
// For kKey_CommandType
skui::Key fKey;
// Common to all command types
SkString fKeyName;
SkString fGroup;
SkString fDescription;
std::function<void(void)> fFunction;
SkString getSoftkeyString() const {
return SkStringPrintf("%s (%s)", fKeyName.c_str(), fDescription.c_str());
}
};
static bool compareCommandKey(const Command& first, const Command& second);
static bool compareCommandGroup(const Command& first, const Command& second);
enum HelpMode {
kNone_HelpMode,
kGrouped_HelpMode,
kAlphabetical_HelpMode,
};
Window* fWindow;
SkTArray<Command> fCommands;
HelpMode fHelpMode;
};
} // namespace sk_app
#endif