• 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 #ifndef Window_DEFINED
9 #define Window_DEFINED
10 
11 #include "DisplayParams.h"
12 #include "SkRect.h"
13 #include "SkTouchGesture.h"
14 #include "SkTypes.h"
15 #include "SkJSONCPP.h"
16 
17 class GrContext;
18 class SkCanvas;
19 class SkSurface;
20 
21 namespace sk_app {
22 
23 class WindowContext;
24 
25 class Window {
26 public:
27     static Window* CreateNativeWindow(void* platformData);
28 
~Window()29     virtual ~Window() { this->detach(); }
30 
31     virtual void setTitle(const char*) = 0;
32     virtual void show() = 0;
setUIState(const Json::Value & state)33     virtual void setUIState(const Json::Value& state) {}  // do nothing in default
34 
35     // Shedules an invalidation event for window if one is not currently pending.
36     // Make sure that either onPaint or markInvalReceived is called when the client window consumes
37     // the the inval event. They unset fIsContentInvalided which allow future onInval.
38     void inval();
39 
scaleContentToFit()40     virtual bool scaleContentToFit() const { return false; }
41 
42     enum BackendType {
43         kNativeGL_BackendType,
44 #ifdef SK_VULKAN
45         kVulkan_BackendType,
46 #endif
47         kRaster_BackendType,
48 
49         kLast_BackendType = kRaster_BackendType
50     };
51     enum {
52         kBackendTypeCount = kLast_BackendType + 1
53     };
54 
55     virtual bool attach(BackendType) = 0;
56     void detach();
57 
58     // input handling
59     enum class Key {
60         kNONE,    //corresponds to android's UNKNOWN
61 
62         kLeftSoftKey,
63         kRightSoftKey,
64 
65         kHome,    //!< the home key - added to match android
66         kBack,    //!< (CLR)
67         kSend,    //!< the green (talk) key
68         kEnd,     //!< the red key
69 
70         k0,
71         k1,
72         k2,
73         k3,
74         k4,
75         k5,
76         k6,
77         k7,
78         k8,
79         k9,
80         kStar,    //!< the * key
81         kHash,    //!< the # key
82 
83         kUp,
84         kDown,
85         kLeft,
86         kRight,
87 
88         // Keys needed by ImGui
89         kTab,
90         kPageUp,
91         kPageDown,
92         kDelete,
93         kEscape,
94         kShift,
95         kCtrl,
96         kOption, // AKA Alt
97         kA,
98         kC,
99         kV,
100         kX,
101         kY,
102         kZ,
103 
104         kOK,      //!< the center key
105 
106         kVolUp,   //!< volume up    - match android
107         kVolDown, //!< volume down  - same
108         kPower,   //!< power button - same
109         kCamera,  //!< camera       - same
110 
111         kLast = kCamera
112     };
113     static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
114 
115     enum ModifierKeys {
116         kShift_ModifierKey = 1 << 0,
117         kControl_ModifierKey = 1 << 1,
118         kOption_ModifierKey = 1 << 2,   // same as ALT
119         kCommand_ModifierKey = 1 << 3,
120         kFirstPress_ModifierKey = 1 << 4,
121     };
122 
123     enum InputState {
124         kDown_InputState,
125         kUp_InputState,
126         kMove_InputState   // only valid for mouse
127     };
128 
129     // return value of 'true' means 'I have handled this event'
130     typedef void(*OnBackendCreatedFunc)(void* userData);
131     typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
132     typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
133     typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
134     typedef bool(*OnMouseWheelFunc)(float delta, uint32_t modifiers, void* userData);
135     typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData);
136     typedef void(*OnUIStateChangedFunc)(
137             const SkString& stateName, const SkString& stateValue, void* userData);
138     typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
139 
registerBackendCreatedFunc(OnBackendCreatedFunc func,void * userData)140     void registerBackendCreatedFunc(OnBackendCreatedFunc func, void* userData) {
141         fBackendCreatedFunc = func;
142         fBackendCreatedUserData = userData;
143     }
144 
registerCharFunc(OnCharFunc func,void * userData)145     void registerCharFunc(OnCharFunc func, void* userData) {
146         fCharFunc = func;
147         fCharUserData = userData;
148     }
149 
registerKeyFunc(OnKeyFunc func,void * userData)150     void registerKeyFunc(OnKeyFunc func, void* userData) {
151         fKeyFunc = func;
152         fKeyUserData = userData;
153     }
154 
registerMouseFunc(OnMouseFunc func,void * userData)155     void registerMouseFunc(OnMouseFunc func, void* userData) {
156         fMouseFunc = func;
157         fMouseUserData = userData;
158     }
159 
registerMouseWheelFunc(OnMouseWheelFunc func,void * userData)160     void registerMouseWheelFunc(OnMouseWheelFunc func, void* userData) {
161         fMouseWheelFunc = func;
162         fMouseWheelUserData = userData;
163     }
164 
registerPaintFunc(OnPaintFunc func,void * userData)165     void registerPaintFunc(OnPaintFunc func, void* userData) {
166         fPaintFunc = func;
167         fPaintUserData = userData;
168     }
169 
registerTouchFunc(OnTouchFunc func,void * userData)170     void registerTouchFunc(OnTouchFunc func, void* userData) {
171         fTouchFunc = func;
172         fTouchUserData = userData;
173     }
174 
registerUIStateChangedFunc(OnUIStateChangedFunc func,void * userData)175     void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) {
176         fUIStateChangedFunc = func;
177         fUIStateChangedUserData = userData;
178     }
179 
180     void onBackendCreated();
181     bool onChar(SkUnichar c, uint32_t modifiers);
182     bool onKey(Key key, InputState state, uint32_t modifiers);
183     bool onMouse(int x, int y, InputState state, uint32_t modifiers);
184     bool onMouseWheel(float delta, uint32_t modifiers);
185     bool onTouch(intptr_t owner, InputState state, float x, float y);  // multi-owner = multi-touch
186     void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
187     void onPaint();
188     void onResize(int width, int height);
189 
190     int width();
191     int height();
192 
getRequestedDisplayParams()193     virtual const DisplayParams& getRequestedDisplayParams() { return fRequestedDisplayParams; }
194     virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
195 
196     // Actual parameters in effect, obtained from the native window.
197     int sampleCount() const;
198     int stencilBits() const;
199 
200     // Returns null if there is not a GPU backend or if the backend is not yet created.
201     const GrContext* getGrContext() const;
202 
203 protected:
204     Window();
205 
206     OnBackendCreatedFunc   fBackendCreatedFunc;
207     void*                  fBackendCreatedUserData;
208     OnCharFunc             fCharFunc;
209     void*                  fCharUserData;
210     OnKeyFunc              fKeyFunc;
211     void*                  fKeyUserData;
212     OnMouseFunc            fMouseFunc;
213     void*                  fMouseUserData;
214     OnMouseWheelFunc       fMouseWheelFunc;
215     void*                  fMouseWheelUserData;
216     OnTouchFunc            fTouchFunc;
217     void*                  fTouchUserData;
218     OnUIStateChangedFunc   fUIStateChangedFunc;
219     void*                  fUIStateChangedUserData;
220     OnPaintFunc            fPaintFunc;
221     void*                  fPaintUserData;
222     DisplayParams          fRequestedDisplayParams;
223 
224     WindowContext* fWindowContext = nullptr;
225 
226     virtual void onInval() = 0;
227 
228     // Uncheck fIsContentInvalided to allow future inval/onInval.
229     void markInvalProcessed();
230 
231     bool fIsContentInvalidated = false;  // use this to avoid duplicate invalidate events
232 };
233 
234 }   // namespace sk_app
235 #endif
236