blob: 8b1378a80fdb6669e24102d8cbce7af2ef1f7f81 [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;
jvanverth9f372462016-04-06 06:08:59 -070018
jvanverth34524262016-05-04 13:49:13 -070019namespace sk_app {
20
jvanvertha8d0d6c2016-05-05 12:32:03 -070021class WindowContext;
22
jvanverth9f372462016-04-06 06:08:59 -070023class Window {
24public:
25 static Window* CreateNativeWindow(void* platformData);
26
27 virtual ~Window() {};
28
29 virtual void setTitle(const char*) = 0;
30 virtual void show() = 0;
liyuqiane5a6cd92016-05-27 08:52:52 -070031 virtual void setUIState(const Json::Value& state) {} // do nothing in default
liyuqian566c8e42016-05-23 10:52:34 -070032
33 // Shedules an invalidation event for window if one is not currently pending.
34 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
35 // the the inval event. They unset fIsContentInvalided which allow future onInval.
36 void inval();
jvanverth9f372462016-04-06 06:08:59 -070037
djsollen12d62a72016-04-21 07:59:44 -070038 virtual bool scaleContentToFit() const { return false; }
39 virtual bool supportsContentRect() const { return false; }
40 virtual SkRect getContentRect() { return SkRect::MakeEmpty(); }
41
jvanverthaf236b52016-05-20 06:01:06 -070042 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070043 kNativeGL_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070044 kVulkan_BackendType,
liyuqiand94ad582016-06-07 14:22:37 -070045 kRaster_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070046
liyuqiand94ad582016-06-07 14:22:37 -070047 kLast_BackendType = kRaster_BackendType
jvanverthaf236b52016-05-20 06:01:06 -070048 };
49 enum {
50 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070051 };
52
jvanverthaf236b52016-05-20 06:01:06 -070053 virtual bool attach(BackendType attachType, const DisplayParams& params) = 0;
jvanverth9f372462016-04-06 06:08:59 -070054 void detach();
55
56 // input handling
brianosman622c8d52016-05-10 06:50:49 -070057 enum class Key {
58 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070059
brianosman622c8d52016-05-10 06:50:49 -070060 kLeftSoftKey,
61 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070062
brianosman622c8d52016-05-10 06:50:49 -070063 kHome, //!< the home key - added to match android
64 kBack, //!< (CLR)
65 kSend, //!< the green (talk) key
66 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070067
brianosman622c8d52016-05-10 06:50:49 -070068 k0,
69 k1,
70 k2,
71 k3,
72 k4,
73 k5,
74 k6,
75 k7,
76 k8,
77 k9,
78 kStar, //!< the * key
79 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070080
brianosman622c8d52016-05-10 06:50:49 -070081 kUp,
82 kDown,
83 kLeft,
84 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070085
brianosman622c8d52016-05-10 06:50:49 -070086 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -070087
brianosman622c8d52016-05-10 06:50:49 -070088 kVolUp, //!< volume up - match android
89 kVolDown, //!< volume down - same
90 kPower, //!< power button - same
91 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -070092
brianosman622c8d52016-05-10 06:50:49 -070093 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -070094 };
brianosman622c8d52016-05-10 06:50:49 -070095 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -070096
97 enum ModifierKeys {
98 kShift_ModifierKey = 1 << 0,
99 kControl_ModifierKey = 1 << 1,
100 kOption_ModifierKey = 1 << 2, // same as ALT
101 kCommand_ModifierKey = 1 << 3,
102 kFirstPress_ModifierKey = 1 << 4,
103 };
104
105 enum InputState {
106 kDown_InputState,
107 kUp_InputState,
108 kMove_InputState // only valid for mouse
109 };
110
111 // return value of 'true' means 'I have handled this event'
112 typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
113 typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
114 typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
jvanverth814e38d2016-06-06 08:48:47 -0700115 typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData);
liyuqiane5a6cd92016-05-27 08:52:52 -0700116 typedef void(*OnUIStateChangedFunc)(
117 const SkString& stateName, const SkString& stateValue, void* userData);
jvanverth9f372462016-04-06 06:08:59 -0700118 typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
119
jvanverth9fab59d2016-04-06 12:08:51 -0700120 void registerCharFunc(OnCharFunc func, void* userData) {
121 fCharFunc = func;
122 fCharUserData = userData;
123 }
124
jvanverth9f372462016-04-06 06:08:59 -0700125 void registerKeyFunc(OnKeyFunc func, void* userData) {
126 fKeyFunc = func;
127 fKeyUserData = userData;
128 }
129
130 void registerMouseFunc(OnMouseFunc func, void* userData) {
131 fMouseFunc = func;
132 fMouseUserData = userData;
133 }
134
135 void registerPaintFunc(OnPaintFunc func, void* userData) {
136 fPaintFunc = func;
137 fPaintUserData = userData;
138 }
139
liyuqiand3cdbca2016-05-17 12:44:20 -0700140 void registerTouchFunc(OnTouchFunc func, void* userData) {
141 fTouchFunc = func;
142 fTouchUserData = userData;
143 }
144
liyuqiane5a6cd92016-05-27 08:52:52 -0700145 void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
146 fUIStateChangedFunc = func;
147 fUIStateChangedUserData = userData;
148 }
149
jvanverth9fab59d2016-04-06 12:08:51 -0700150 bool onChar(SkUnichar c, uint32_t modifiers);
151 bool onKey(Key key, InputState state, uint32_t modifiers);
152 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
jvanverth814e38d2016-06-06 08:48:47 -0700153 bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch
liyuqiane5a6cd92016-05-27 08:52:52 -0700154 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
jvanverth9f372462016-04-06 06:08:59 -0700155 void onPaint();
jvanverth9fab59d2016-04-06 12:08:51 -0700156 void onResize(uint32_t width, uint32_t height);
jvanverth9f372462016-04-06 06:08:59 -0700157
jvanverthc265a922016-04-08 12:51:45 -0700158 uint32_t width() { return fWidth; }
159 uint32_t height() { return fHeight; }
160
liyuqian40d21de2016-05-12 09:17:04 -0700161 virtual const DisplayParams& getDisplayParams();
brianosman05de2162016-05-06 13:28:57 -0700162 void setDisplayParams(const DisplayParams& params);
163
jvanverth9f372462016-04-06 06:08:59 -0700164protected:
165 Window();
166
jvanverthc265a922016-04-08 12:51:45 -0700167 uint32_t fWidth;
168 uint32_t fHeight;
169
jvanverth9fab59d2016-04-06 12:08:51 -0700170 OnCharFunc fCharFunc;
171 void* fCharUserData;
jvanverth9f372462016-04-06 06:08:59 -0700172 OnKeyFunc fKeyFunc;
173 void* fKeyUserData;
174 OnMouseFunc fMouseFunc;
175 void* fMouseUserData;
liyuqiand3cdbca2016-05-17 12:44:20 -0700176 OnTouchFunc fTouchFunc;
177 void* fTouchUserData;
liyuqiane5a6cd92016-05-27 08:52:52 -0700178 OnUIStateChangedFunc
179 fUIStateChangedFunc;
180 void* fUIStateChangedUserData;
jvanverth9f372462016-04-06 06:08:59 -0700181 OnPaintFunc fPaintFunc;
182 void* fPaintUserData;
183
liyuqiand3cdbca2016-05-17 12:44:20 -0700184 WindowContext* fWindowContext = nullptr;
liyuqian566c8e42016-05-23 10:52:34 -0700185
186 virtual void onInval() = 0;
187
188 // Uncheck fIsContentInvalided to allow future inval/onInval.
189 void markInvalProcessed();
190
191 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
jvanverth9f372462016-04-06 06:08:59 -0700192};
193
jvanverth34524262016-05-04 13:49:13 -0700194} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700195#endif