blob: 24919a816bb22437b27a5e4e34676e50526274ab [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -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
8#ifndef Window_DEFINED
9#define Window_DEFINED
10
brianosman05de2162016-05-06 13:28:57 -070011#include "DisplayParams.h"
djsollen12d62a72016-04-21 07:59:44 -070012#include "SkRect.h"
liyuqiand3cdbca2016-05-17 12:44:20 -070013#include "SkTouchGesture.h"
14#include "SkTypes.h"
liyuqiane5a6cd92016-05-27 08:52:52 -070015#include "SkJSONCPP.h"
jvanverth9fab59d2016-04-06 12:08:51 -070016
jvanverth9f372462016-04-06 06:08:59 -070017class SkCanvas;
liyuqian74959a12016-06-16 14:10:34 -070018class SkSurface;
jvanverth9f372462016-04-06 06:08:59 -070019
jvanverth34524262016-05-04 13:49:13 -070020namespace sk_app {
21
jvanvertha8d0d6c2016-05-05 12:32:03 -070022class WindowContext;
23
jvanverth9f372462016-04-06 06:08:59 -070024class Window {
25public:
26 static Window* CreateNativeWindow(void* platformData);
27
Brian Osman2dd96932016-10-18 15:33:53 -040028 virtual ~Window() { this->detach(); }
jvanverth9f372462016-04-06 06:08:59 -070029
30 virtual void setTitle(const char*) = 0;
31 virtual void show() = 0;
liyuqiane5a6cd92016-05-27 08:52:52 -070032 virtual void setUIState(const Json::Value& state) {} // do nothing in default
liyuqian566c8e42016-05-23 10:52:34 -070033
34 // Shedules an invalidation event for window if one is not currently pending.
35 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
36 // the the inval event. They unset fIsContentInvalided which allow future onInval.
37 void inval();
jvanverth9f372462016-04-06 06:08:59 -070038
djsollen12d62a72016-04-21 07:59:44 -070039 virtual bool scaleContentToFit() const { return false; }
40 virtual bool supportsContentRect() const { return false; }
41 virtual SkRect getContentRect() { return SkRect::MakeEmpty(); }
42
jvanverthaf236b52016-05-20 06:01:06 -070043 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070044 kNativeGL_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070045#ifdef SK_VULKAN
jvanverthaf236b52016-05-20 06:01:06 -070046 kVulkan_BackendType,
jvanverth063ece72016-06-17 09:29:14 -070047#endif
liyuqiand94ad582016-06-07 14:22:37 -070048 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070049
liyuqiand94ad582016-06-07 14:22:37 -070050 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070051 };
52 enum {
53 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070054 };
55
jvanverthaf236b52016-05-20 06:01:06 -070056 virtual bool attach(BackendType attachType, const DisplayParams& params) = 0;
jvanverth9f372462016-04-06 06:08:59 -070057 void detach();
58
59 // input handling
brianosman622c8d52016-05-10 06:50:49 -070060 enum class Key {
61 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070062
brianosman622c8d52016-05-10 06:50:49 -070063 kLeftSoftKey,
64 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070065
brianosman622c8d52016-05-10 06:50:49 -070066 kHome, //!< the home key - added to match android
67 kBack, //!< (CLR)
68 kSend, //!< the green (talk) key
69 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070070
brianosman622c8d52016-05-10 06:50:49 -070071 k0,
72 k1,
73 k2,
74 k3,
75 k4,
76 k5,
77 k6,
78 k7,
79 k8,
80 k9,
81 kStar, //!< the * key
82 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070083
brianosman622c8d52016-05-10 06:50:49 -070084 kUp,
85 kDown,
86 kLeft,
87 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070088
Brian Osman79086b92017-02-10 13:36:16 -050089 // Keys needed by ImGui
90 kTab,
91 kPageUp,
92 kPageDown,
93 kDelete,
94 kEscape,
95 kShift,
96 kCtrl,
97 kOption, // AKA Alt
98 kA,
99 kC,
100 kV,
101 kX,
102 kY,
103 kZ,
104
brianosman622c8d52016-05-10 06:50:49 -0700105 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -0700106
brianosman622c8d52016-05-10 06:50:49 -0700107 kVolUp, //!< volume up - match android
108 kVolDown, //!< volume down - same
109 kPower, //!< power button - same
110 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -0700111
brianosman622c8d52016-05-10 06:50:49 -0700112 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -0700113 };
brianosman622c8d52016-05-10 06:50:49 -0700114 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -0700115
116 enum ModifierKeys {
117 kShift_ModifierKey = 1 << 0,
118 kControl_ModifierKey = 1 << 1,
119 kOption_ModifierKey = 1 << 2, // same as ALT
120 kCommand_ModifierKey = 1 << 3,
121 kFirstPress_ModifierKey = 1 << 4,
122 };
123
124 enum InputState {
125 kDown_InputState,
126 kUp_InputState,
127 kMove_InputState // only valid for mouse
128 };
129
130 // return value of 'true' means 'I have handled this event'
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700131 typedef void(*OnBackendCreatedFunc)(void* userData);
jvanverth9fab59d2016-04-06 12:08:51 -0700132 typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
133 typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
134 typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
Brian Osman79086b92017-02-10 13:36:16 -0500135 typedef bool(*OnMouseWheelFunc)(float delta, uint32_t modifiers, void* userData);
jvanverth814e38d2016-06-06 08:48:47 -0700136 typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData);
liyuqiane5a6cd92016-05-27 08:52:52 -0700137 typedef void(*OnUIStateChangedFunc)(
138 const SkString& stateName, const SkString& stateValue, void* userData);
jvanverth9f372462016-04-06 06:08:59 -0700139 typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
140
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700141 void registerBackendCreatedFunc(OnBackendCreatedFunc func, void* userData) {
142 fBackendCreatedFunc = func;
143 fBackendCreatedUserData = userData;
144 }
145
jvanverth9fab59d2016-04-06 12:08:51 -0700146 void registerCharFunc(OnCharFunc func, void* userData) {
147 fCharFunc = func;
148 fCharUserData = userData;
149 }
150
jvanverth9f372462016-04-06 06:08:59 -0700151 void registerKeyFunc(OnKeyFunc func, void* userData) {
152 fKeyFunc = func;
153 fKeyUserData = userData;
154 }
155
156 void registerMouseFunc(OnMouseFunc func, void* userData) {
157 fMouseFunc = func;
158 fMouseUserData = userData;
159 }
160
Brian Osman79086b92017-02-10 13:36:16 -0500161 void registerMouseWheelFunc(OnMouseWheelFunc func, void* userData) {
162 fMouseWheelFunc = func;
163 fMouseWheelUserData = userData;
164 }
165
jvanverth9f372462016-04-06 06:08:59 -0700166 void registerPaintFunc(OnPaintFunc func, void* userData) {
167 fPaintFunc = func;
168 fPaintUserData = userData;
169 }
170
liyuqiand3cdbca2016-05-17 12:44:20 -0700171 void registerTouchFunc(OnTouchFunc func, void* userData) {
172 fTouchFunc = func;
173 fTouchUserData = userData;
174 }
175
liyuqiane5a6cd92016-05-27 08:52:52 -0700176 void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
177 fUIStateChangedFunc = func;
178 fUIStateChangedUserData = userData;
179 }
180
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700181 void onBackendCreated();
jvanverth9fab59d2016-04-06 12:08:51 -0700182 bool onChar(SkUnichar c, uint32_t modifiers);
183 bool onKey(Key key, InputState state, uint32_t modifiers);
184 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
Brian Osman79086b92017-02-10 13:36:16 -0500185 bool onMouseWheel(float delta, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700186 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700187 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700188 void onPaint();
bsalomonccde4ab2016-07-27 08:50:12 -0700189 void onResize(int width, int height);
jvanverth9f372462016-04-06 06:08:59 -0700190
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700191 int width();
192 int height();
jvanverthc265a922016-04-08 12:51:45 -0700193
liyuqian40d21de2016-05-12 09:17:04 -0700194 virtual const DisplayParams& getDisplayParams();
brianosman05de2162016-05-06 13:28:57 -0700195 void setDisplayParams(const DisplayParams& params);
196
jvanverth9f372462016-04-06 06:08:59 -0700197protected:
198 Window();
199
Christopher Dalton443ec1b2017-02-24 13:22:53 -0700200 OnBackendCreatedFunc fBackendCreatedFunc;
201 void* fBackendCreatedUserData;
202 OnCharFunc fCharFunc;
203 void* fCharUserData;
204 OnKeyFunc fKeyFunc;
205 void* fKeyUserData;
206 OnMouseFunc fMouseFunc;
207 void* fMouseUserData;
208 OnMouseWheelFunc fMouseWheelFunc;
209 void* fMouseWheelUserData;
210 OnTouchFunc fTouchFunc;
211 void* fTouchUserData;
212 OnUIStateChangedFunc fUIStateChangedFunc;
213 void* fUIStateChangedUserData;
214 OnPaintFunc fPaintFunc;
215 void* fPaintUserData;
jvanverth9f372462016-04-06 06:08:59 -0700216
liyuqiand3cdbca2016-05-17 12:44:20 -0700217 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700218
219 virtual void onInval() = 0;
220
221 // Uncheck fIsContentInvalided to allow future inval/onInval.
222 void markInvalProcessed();
223
224 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
jvanverth9f372462016-04-06 06:08:59 -0700225};
226
jvanverth34524262016-05-04 13:49:13 -0700227} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700228#endif