jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 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 | |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 11 | #include "DisplayParams.h" |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 12 | #include "SkRect.h" |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 13 | #include "SkTouchGesture.h" |
| 14 | #include "SkTypes.h" |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 15 | #include "SkJSONCPP.h" |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 16 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | class SkCanvas; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 18 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 19 | namespace sk_app { |
| 20 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 21 | class WindowContext; |
| 22 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 23 | class Window { |
| 24 | public: |
| 25 | static Window* CreateNativeWindow(void* platformData); |
| 26 | |
| 27 | virtual ~Window() {}; |
| 28 | |
| 29 | virtual void setTitle(const char*) = 0; |
| 30 | virtual void show() = 0; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 31 | virtual void setUIState(const Json::Value& state) {} // do nothing in default |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 32 | |
| 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(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 37 | |
djsollen | 12d62a7 | 2016-04-21 07:59:44 -0700 | [diff] [blame] | 38 | virtual bool scaleContentToFit() const { return false; } |
| 39 | virtual bool supportsContentRect() const { return false; } |
| 40 | virtual SkRect getContentRect() { return SkRect::MakeEmpty(); } |
| 41 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 42 | enum BackendType { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 43 | kNativeGL_BackendType, |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 44 | kVulkan_BackendType, |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame^] | 45 | kRaster_BackendType, |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 46 | |
liyuqian | d94ad58 | 2016-06-07 14:22:37 -0700 | [diff] [blame^] | 47 | kLast_BackendType = kRaster_BackendType |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 48 | }; |
| 49 | enum { |
| 50 | kBackendTypeCount = kLast_BackendType + 1 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 53 | virtual bool attach(BackendType attachType, const DisplayParams& params) = 0; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 54 | void detach(); |
| 55 | |
| 56 | // input handling |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 57 | enum class Key { |
| 58 | kNONE, //corresponds to android's UNKNOWN |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 59 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 60 | kLeftSoftKey, |
| 61 | kRightSoftKey, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 62 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 63 | kHome, //!< the home key - added to match android |
| 64 | kBack, //!< (CLR) |
| 65 | kSend, //!< the green (talk) key |
| 66 | kEnd, //!< the red key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 67 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 68 | 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 |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 80 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 81 | kUp, |
| 82 | kDown, |
| 83 | kLeft, |
| 84 | kRight, |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 85 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 86 | kOK, //!< the center key |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 87 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 88 | kVolUp, //!< volume up - match android |
| 89 | kVolDown, //!< volume down - same |
| 90 | kPower, //!< power button - same |
| 91 | kCamera, //!< camera - same |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 92 | |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 93 | kLast = kCamera |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 94 | }; |
brianosman | 622c8d5 | 2016-05-10 06:50:49 -0700 | [diff] [blame] | 95 | static const int kKeyCount = static_cast<int>(Key::kLast) + 1; |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 96 | |
| 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); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 115 | typedef bool(*OnTouchFunc)(intptr_t owner, InputState state, float x, float y, void* userData); |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 116 | typedef void(*OnUIStateChangedFunc)( |
| 117 | const SkString& stateName, const SkString& stateValue, void* userData); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 118 | typedef void(*OnPaintFunc)(SkCanvas*, void* userData); |
| 119 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 120 | void registerCharFunc(OnCharFunc func, void* userData) { |
| 121 | fCharFunc = func; |
| 122 | fCharUserData = userData; |
| 123 | } |
| 124 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 125 | 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 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 140 | void registerTouchFunc(OnTouchFunc func, void* userData) { |
| 141 | fTouchFunc = func; |
| 142 | fTouchUserData = userData; |
| 143 | } |
| 144 | |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 145 | void registerUIStateChangedFunc(OnUIStateChangedFunc func, void* userData) { |
| 146 | fUIStateChangedFunc = func; |
| 147 | fUIStateChangedUserData = userData; |
| 148 | } |
| 149 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 150 | 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); |
jvanverth | 814e38d | 2016-06-06 08:48:47 -0700 | [diff] [blame] | 153 | bool onTouch(intptr_t owner, InputState state, float x, float y); // multi-owner = multi-touch |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 154 | void onUIStateChanged(const SkString& stateName, const SkString& stateValue); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 155 | void onPaint(); |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 156 | void onResize(uint32_t width, uint32_t height); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 157 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 158 | uint32_t width() { return fWidth; } |
| 159 | uint32_t height() { return fHeight; } |
| 160 | |
liyuqian | 40d21de | 2016-05-12 09:17:04 -0700 | [diff] [blame] | 161 | virtual const DisplayParams& getDisplayParams(); |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 162 | void setDisplayParams(const DisplayParams& params); |
| 163 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 164 | protected: |
| 165 | Window(); |
| 166 | |
jvanverth | c265a92 | 2016-04-08 12:51:45 -0700 | [diff] [blame] | 167 | uint32_t fWidth; |
| 168 | uint32_t fHeight; |
| 169 | |
jvanverth | 9fab59d | 2016-04-06 12:08:51 -0700 | [diff] [blame] | 170 | OnCharFunc fCharFunc; |
| 171 | void* fCharUserData; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 172 | OnKeyFunc fKeyFunc; |
| 173 | void* fKeyUserData; |
| 174 | OnMouseFunc fMouseFunc; |
| 175 | void* fMouseUserData; |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 176 | OnTouchFunc fTouchFunc; |
| 177 | void* fTouchUserData; |
liyuqian | e5a6cd9 | 2016-05-27 08:52:52 -0700 | [diff] [blame] | 178 | OnUIStateChangedFunc |
| 179 | fUIStateChangedFunc; |
| 180 | void* fUIStateChangedUserData; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 181 | OnPaintFunc fPaintFunc; |
| 182 | void* fPaintUserData; |
| 183 | |
liyuqian | d3cdbca | 2016-05-17 12:44:20 -0700 | [diff] [blame] | 184 | WindowContext* fWindowContext = nullptr; |
liyuqian | 566c8e4 | 2016-05-23 10:52:34 -0700 | [diff] [blame] | 185 | |
| 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 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 192 | }; |
| 193 | |
jvanverth | 3452426 | 2016-05-04 13:49:13 -0700 | [diff] [blame] | 194 | } // namespace sk_app |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 195 | #endif |