UIWebView.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/Input/InputEvents.h>
  24. #include <Atomic/Input/Input.h>
  25. #include <Atomic/UI/UIRenderer.h>
  26. #include "WebClient.h"
  27. #include "WebTexture2D.h"
  28. #include "WebBrowserHost.h"
  29. #include "UIWebView.h"
  30. using namespace tb;
  31. namespace Atomic
  32. {
  33. class WebViewWidget : public tb::TBWidget
  34. {
  35. friend class UIWebView;
  36. public:
  37. // For safe typecasting
  38. TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
  39. WebViewWidget() : browserCreated_(false)
  40. {
  41. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  42. float color;
  43. ((unsigned&)color) = 0xFFFFFFFF;
  44. float* data = &vertexData_[0];
  45. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  46. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  47. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  48. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  49. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  50. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  51. }
  52. void OnPaint(const PaintProps &paint_props)
  53. {
  54. if (webView_.Null())
  55. return;
  56. TBRect rect = GetRect();
  57. rect.x = rect.y = 0;
  58. ConvertToRoot(rect.x, rect.y);
  59. IntRect size = webView_->GetRect();
  60. float* data = &vertexData_[0];
  61. UI* ui = webView_->GetSubsystem<UI>();
  62. if (!browserCreated_)
  63. {
  64. browserCreated_ = true;
  65. webView_->webClient_->CreateBrowser(webView_->initialURL_, rect.w, rect.h);
  66. }
  67. webView_->webClient_->SetSize(rect.w, rect.h);
  68. float color;
  69. float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
  70. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  71. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  72. data[3] = color;
  73. data[9] = color;
  74. data[15] = color;
  75. data[21] = color;
  76. data[27] = color;
  77. data[33] = color;
  78. data[0] = rect.x;
  79. data[1] = rect.y;
  80. data[6] = rect.x + rect.w;
  81. data[7] = rect.y;
  82. data[12] = rect.x + rect.w;
  83. data[13] = rect.y + rect.h;
  84. data[18] = rect.x;
  85. data[19] = rect.y;
  86. data[24] = rect.x + rect.w;
  87. data[25] = rect.y + rect.h;
  88. data[30] = rect.x;
  89. data[31] = rect.y + rect.h;
  90. ui->SubmitBatchVertexData(webView_->GetWebTexture2D()->GetTexture2D(), vertexData_);
  91. }
  92. private:
  93. bool browserCreated_;
  94. WeakPtr<UIWebView> webView_;
  95. PODVector<float> vertexData_;
  96. };
  97. UIWebView::UIWebView(Context* context, const String &initialURL) : UIWidget(context, false)
  98. {
  99. widget_ = new WebViewWidget();
  100. widget_->SetDelegate(this);
  101. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  102. widget_->SetIsFocusable(true);
  103. ((WebViewWidget*)widget_)->webView_ = this;
  104. UI* ui = GetSubsystem<UI>();
  105. ui->WrapWidget(this, widget_);
  106. webClient_ = new WebClient(context);
  107. webTexture_ = new WebTexture2D(context);
  108. webClient_->SetWebRenderHandler(webTexture_);
  109. initialURL_ = initialURL;
  110. //SubscribeToEvent(E_KEYDOWN, HANDLER(UIWebView, HandleKeyDown));
  111. //SubscribeToEvent(E_KEYUP, HANDLER(UIWebView, HandleKeyUp));
  112. SubscribeToEvent(E_TEXTINPUT, HANDLER(UIWebView, HandleTextInput));
  113. }
  114. UIWebView::~UIWebView()
  115. {
  116. }
  117. void UIWebView::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  118. {
  119. if (!widget_ || !widget_->GetIsFocused())
  120. return;
  121. //if (eventData[KeyDown::P_REPEAT].GetBool())
  122. // return;
  123. int key = eventData[KeyDown::P_KEY].GetInt();
  124. int scanCode = eventData[KeyDown::P_SCANCODE].GetInt();
  125. unsigned raw = eventData[KeyDown::P_RAW].GetUInt();
  126. int buttons = eventData[KeyDown::P_BUTTONS].GetInt();
  127. int qual = eventData[KeyDown::P_QUALIFIERS].GetInt();
  128. webClient_->SendKeyEvent(scanCode, qual, false);
  129. }
  130. void UIWebView::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  131. {
  132. if (!widget_ || !widget_->GetIsFocused())
  133. return;
  134. int key = eventData[KeyUp::P_KEY].GetInt();
  135. int scanCode = eventData[KeyUp::P_SCANCODE].GetInt();
  136. unsigned raw = eventData[KeyUp::P_RAW].GetUInt();
  137. int buttons = eventData[KeyUp::P_BUTTONS].GetInt();
  138. int qual = eventData[KeyUp::P_QUALIFIERS].GetInt();
  139. webClient_->SendKeyEvent(scanCode, qual, true);
  140. }
  141. void UIWebView::HandleTextInput(StringHash eventType, VariantMap& eventData)
  142. {
  143. if (!widget_ || !widget_->GetIsFocused())
  144. return;
  145. String text = eventData[TextInput::P_TEXT].GetString();
  146. webClient_->SendTextEvent(text, 0);
  147. }
  148. bool UIWebView::HandleKeyEvent(const TBWidgetEvent &ev, bool keyDown)
  149. {
  150. if (!keyDown)
  151. return true;
  152. if (ev.special_key == TB_KEY_UNDEFINED)
  153. return true;
  154. int qual = 0;
  155. if (ev.modifierkeys & TB_CTRL)
  156. qual |= QUAL_CTRL;
  157. if (ev.modifierkeys & TB_SHIFT)
  158. qual |= QUAL_SHIFT;
  159. if (ev.modifierkeys & TB_ALT)
  160. qual |= QUAL_ALT;
  161. int scanCode = SDL_SCANCODE_UNKNOWN;
  162. switch (ev.special_key)
  163. {
  164. case TB_KEY_UP:
  165. scanCode = SDL_SCANCODE_UP;
  166. break;
  167. case TB_KEY_DOWN:
  168. scanCode = SDL_SCANCODE_DOWN;
  169. break;
  170. case TB_KEY_RIGHT:
  171. scanCode = SDL_SCANCODE_RIGHT;
  172. break;
  173. case TB_KEY_LEFT:
  174. scanCode = SDL_SCANCODE_LEFT;
  175. break;
  176. case TB_KEY_ENTER:
  177. scanCode = SDL_SCANCODE_RETURN;
  178. break;
  179. case TB_KEY_DELETE:
  180. case TB_KEY_BACKSPACE:
  181. scanCode = SDL_SCANCODE_BACKSPACE;
  182. break;
  183. default:
  184. break;
  185. }
  186. if (scanCode == SDL_SCANCODE_UNKNOWN)
  187. return true;
  188. webClient_->SendKeyEvent(scanCode, qual, !keyDown);
  189. return true;
  190. /*
  191. TB_KEY_UNDEFINED = 0,
  192. TB_KEY_UP, TB_KEY_DOWN, TB_KEY_LEFT, TB_KEY_RIGHT,
  193. TB_KEY_PAGE_UP, TB_KEY_PAGE_DOWN, TB_KEY_HOME, TB_KEY_END,
  194. TB_KEY_TAB, TB_KEY_BACKSPACE, TB_KEY_INSERT, TB_KEY_DELETE,
  195. TB_KEY_ENTER, TB_KEY_ESC,
  196. TB_KEY_F1, TB_KEY_F2, TB_KEY_F3, TB_KEY_F4, TB_KEY_F5, TB_KEY_F6,
  197. TB_KEY_F7, TB_KEY_F8, TB_KEY_F9, TB_KEY_F10, TB_KEY_F11, TB_KEY_F12
  198. */
  199. }
  200. bool UIWebView::OnEvent(const TBWidgetEvent &ev)
  201. {
  202. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_POINTER_UP)
  203. {
  204. webClient_->SendMouseClickEvent(ev.target_x, ev.target_y, 0, ev.type == EVENT_TYPE_POINTER_UP, 0);
  205. return true;
  206. }
  207. else if (ev.type == EVENT_TYPE_POINTER_MOVE)
  208. {
  209. webClient_->SendMouseMoveEvent(ev.target_x, ev.target_y, 0);
  210. return true;
  211. }
  212. else if (ev.type == EVENT_TYPE_WHEEL)
  213. {
  214. webClient_->SendMouseWheelEvent(ev.target_x, ev.target_y, 0, ev.delta_x, ev.delta_y);
  215. return true;
  216. }
  217. else if (ev.type == EVENT_TYPE_KEY_DOWN)
  218. {
  219. return HandleKeyEvent(ev, true);
  220. }
  221. else if (ev.type == EVENT_TYPE_KEY_UP)
  222. {
  223. return HandleKeyEvent(ev, false);
  224. }
  225. else if (ev.type == EVENT_TYPE_SHORTCUT)
  226. {
  227. if (ev.ref_id == TBIDC("copy"))
  228. {
  229. webClient_->ShortcutCopy();
  230. return true;
  231. }
  232. else if (ev.ref_id == TBIDC("paste"))
  233. {
  234. webClient_->ShortcutPaste();
  235. return true;
  236. }
  237. if (ev.ref_id == TBIDC("cut"))
  238. {
  239. webClient_->ShortcutCut();
  240. return true;
  241. }
  242. else if (ev.ref_id == TBIDC("selectall"))
  243. {
  244. webClient_->ShortcutSelectAll();
  245. return true;
  246. }
  247. else if (ev.ref_id == TBIDC("undo"))
  248. {
  249. webClient_->ShortcutUndo();
  250. return true;
  251. }
  252. else if (ev.ref_id == TBIDC("redo"))
  253. {
  254. webClient_->ShortcutRedo();
  255. return true;
  256. }
  257. }
  258. return UIWidget::OnEvent(ev);
  259. }
  260. WebTexture2D* UIWebView::GetWebTexture2D() const
  261. {
  262. return webTexture_;
  263. }
  264. }