blob: 63d5e19e5594716c9bc95742b0fac51fe8b9af75 [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"
jvanverth9fab59d2016-04-06 12:08:51 -070015
jvanverth9f372462016-04-06 06:08:59 -070016class SkCanvas;
jvanverth9f372462016-04-06 06:08:59 -070017
jvanverth34524262016-05-04 13:49:13 -070018namespace sk_app {
19
jvanvertha8d0d6c2016-05-05 12:32:03 -070020class WindowContext;
21
jvanverth9f372462016-04-06 06:08:59 -070022class Window {
23public:
24 static Window* CreateNativeWindow(void* platformData);
25
26 virtual ~Window() {};
27
28 virtual void setTitle(const char*) = 0;
29 virtual void show() = 0;
jvanverthc265a922016-04-08 12:51:45 -070030 virtual void inval() = 0;
jvanverth9f372462016-04-06 06:08:59 -070031
djsollen12d62a72016-04-21 07:59:44 -070032 virtual bool scaleContentToFit() const { return false; }
33 virtual bool supportsContentRect() const { return false; }
34 virtual SkRect getContentRect() { return SkRect::MakeEmpty(); }
35
jvanverthaf236b52016-05-20 06:01:06 -070036 enum BackendType {
jvanverth9f372462016-04-06 06:08:59 -070037 kNativeGL_BackendType,
jvanverthaf236b52016-05-20 06:01:06 -070038 kVulkan_BackendType,
39
40 kLast_BackendType = kVulkan_BackendType
41 };
42 enum {
43 kBackendTypeCount = kLast_BackendType + 1
jvanverth9f372462016-04-06 06:08:59 -070044 };
45
jvanverthaf236b52016-05-20 06:01:06 -070046 virtual bool attach(BackendType attachType, const DisplayParams& params) = 0;
jvanverth9f372462016-04-06 06:08:59 -070047 void detach();
48
49 // input handling
brianosman622c8d52016-05-10 06:50:49 -070050 enum class Key {
51 kNONE, //corresponds to android's UNKNOWN
jvanverth9fab59d2016-04-06 12:08:51 -070052
brianosman622c8d52016-05-10 06:50:49 -070053 kLeftSoftKey,
54 kRightSoftKey,
jvanverth9fab59d2016-04-06 12:08:51 -070055
brianosman622c8d52016-05-10 06:50:49 -070056 kHome, //!< the home key - added to match android
57 kBack, //!< (CLR)
58 kSend, //!< the green (talk) key
59 kEnd, //!< the red key
jvanverth9fab59d2016-04-06 12:08:51 -070060
brianosman622c8d52016-05-10 06:50:49 -070061 k0,
62 k1,
63 k2,
64 k3,
65 k4,
66 k5,
67 k6,
68 k7,
69 k8,
70 k9,
71 kStar, //!< the * key
72 kHash, //!< the # key
jvanverth9fab59d2016-04-06 12:08:51 -070073
brianosman622c8d52016-05-10 06:50:49 -070074 kUp,
75 kDown,
76 kLeft,
77 kRight,
jvanverth9fab59d2016-04-06 12:08:51 -070078
brianosman622c8d52016-05-10 06:50:49 -070079 kOK, //!< the center key
jvanverth9fab59d2016-04-06 12:08:51 -070080
brianosman622c8d52016-05-10 06:50:49 -070081 kVolUp, //!< volume up - match android
82 kVolDown, //!< volume down - same
83 kPower, //!< power button - same
84 kCamera, //!< camera - same
jvanverth9fab59d2016-04-06 12:08:51 -070085
brianosman622c8d52016-05-10 06:50:49 -070086 kLast = kCamera
jvanverth9fab59d2016-04-06 12:08:51 -070087 };
brianosman622c8d52016-05-10 06:50:49 -070088 static const int kKeyCount = static_cast<int>(Key::kLast) + 1;
jvanverth9fab59d2016-04-06 12:08:51 -070089
90 enum ModifierKeys {
91 kShift_ModifierKey = 1 << 0,
92 kControl_ModifierKey = 1 << 1,
93 kOption_ModifierKey = 1 << 2, // same as ALT
94 kCommand_ModifierKey = 1 << 3,
95 kFirstPress_ModifierKey = 1 << 4,
96 };
97
98 enum InputState {
99 kDown_InputState,
100 kUp_InputState,
101 kMove_InputState // only valid for mouse
102 };
103
104 // return value of 'true' means 'I have handled this event'
105 typedef bool(*OnCharFunc)(SkUnichar c, uint32_t modifiers, void* userData);
106 typedef bool(*OnKeyFunc)(Key key, InputState state, uint32_t modifiers, void* userData);
107 typedef bool(*OnMouseFunc)(int x, int y, InputState state, uint32_t modifiers, void* userData);
liyuqiand3cdbca2016-05-17 12:44:20 -0700108 typedef bool(*OnTouchFunc)(int owner, InputState state, float x, float y, void* userData);
jvanverth9f372462016-04-06 06:08:59 -0700109 typedef void(*OnPaintFunc)(SkCanvas*, void* userData);
110
jvanverth9fab59d2016-04-06 12:08:51 -0700111 void registerCharFunc(OnCharFunc func, void* userData) {
112 fCharFunc = func;
113 fCharUserData = userData;
114 }
115
jvanverth9f372462016-04-06 06:08:59 -0700116 void registerKeyFunc(OnKeyFunc func, void* userData) {
117 fKeyFunc = func;
118 fKeyUserData = userData;
119 }
120
121 void registerMouseFunc(OnMouseFunc func, void* userData) {
122 fMouseFunc = func;
123 fMouseUserData = userData;
124 }
125
126 void registerPaintFunc(OnPaintFunc func, void* userData) {
127 fPaintFunc = func;
128 fPaintUserData = userData;
129 }
130
liyuqiand3cdbca2016-05-17 12:44:20 -0700131 void registerTouchFunc(OnTouchFunc func, void* userData) {
132 fTouchFunc = func;
133 fTouchUserData = userData;
134 }
135
jvanverth9fab59d2016-04-06 12:08:51 -0700136 bool onChar(SkUnichar c, uint32_t modifiers);
137 bool onKey(Key key, InputState state, uint32_t modifiers);
138 bool onMouse(int x, int y, InputState state, uint32_t modifiers);
liyuqiand3cdbca2016-05-17 12:44:20 -0700139 bool onTouch(int owner, InputState state, float x, float y); // multi-owner = multi-touch
jvanverth9f372462016-04-06 06:08:59 -0700140 void onPaint();
jvanverth9fab59d2016-04-06 12:08:51 -0700141 void onResize(uint32_t width, uint32_t height);
jvanverth9f372462016-04-06 06:08:59 -0700142
jvanverthc265a922016-04-08 12:51:45 -0700143 uint32_t width() { return fWidth; }
144 uint32_t height() { return fHeight; }
145
liyuqian40d21de2016-05-12 09:17:04 -0700146 virtual const DisplayParams& getDisplayParams();
brianosman05de2162016-05-06 13:28:57 -0700147 void setDisplayParams(const DisplayParams& params);
148
jvanverth9f372462016-04-06 06:08:59 -0700149protected:
150 Window();
151
jvanverthc265a922016-04-08 12:51:45 -0700152 uint32_t fWidth;
153 uint32_t fHeight;
154
jvanverth9fab59d2016-04-06 12:08:51 -0700155 OnCharFunc fCharFunc;
156 void* fCharUserData;
jvanverth9f372462016-04-06 06:08:59 -0700157 OnKeyFunc fKeyFunc;
158 void* fKeyUserData;
159 OnMouseFunc fMouseFunc;
160 void* fMouseUserData;
liyuqiand3cdbca2016-05-17 12:44:20 -0700161 OnTouchFunc fTouchFunc;
162 void* fTouchUserData;
jvanverth9f372462016-04-06 06:08:59 -0700163 OnPaintFunc fPaintFunc;
164 void* fPaintUserData;
165
liyuqiand3cdbca2016-05-17 12:44:20 -0700166 WindowContext* fWindowContext = nullptr;
jvanverth9f372462016-04-06 06:08:59 -0700167};
168
jvanverth34524262016-05-04 13:49:13 -0700169} // namespace sk_app
jvanverth9f372462016-04-06 06:08:59 -0700170#endif