UIWebView.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // Copyright (c) 2014-2016, 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/UIView.h>
  26. #include <Atomic/UI/UIRenderer.h>
  27. #include "WebClient.h"
  28. #include "WebTexture2D.h"
  29. #include "WebBrowserHost.h"
  30. #include "UIWebView.h"
  31. using namespace tb;
  32. namespace Atomic
  33. {
  34. class WebViewWidget : public tb::TBWidget
  35. {
  36. friend class UIWebView;
  37. public:
  38. // For safe typecasting
  39. TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
  40. WebViewWidget() : browserCreated_(false)
  41. {
  42. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  43. float color;
  44. ((unsigned&)color) = 0xFFFFFFFF;
  45. float* data = &vertexData_[0];
  46. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  47. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  48. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  49. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  50. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  51. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  52. }
  53. void OnProcess()
  54. {
  55. TBWidget::OnProcess();
  56. if (webView_.Null())
  57. return;
  58. if (!browserCreated_)
  59. {
  60. browserCreated_ = true;
  61. TBRect rect = GetRect();
  62. webView_->webClient_->CreateBrowser(webView_->initialURL_, rect.w, rect.h);
  63. }
  64. }
  65. void OnResized(int oldW, int oldH)
  66. {
  67. if (webView_.Null())
  68. return;
  69. TBRect rect = GetRect();
  70. if (rect.w <= 0 || rect.h <= 0)
  71. return;
  72. webView_->webClient_->SetSize(rect.w, rect.h);
  73. }
  74. void OnPaint(const PaintProps &paint_props)
  75. {
  76. if (webView_.Null())
  77. return;
  78. TBRect rect = GetRect();
  79. rect.x = rect.y = 0;
  80. ConvertToRoot(rect.x, rect.y);
  81. float* data = &vertexData_[0];
  82. UI* ui = webView_->GetSubsystem<UI>();
  83. float color;
  84. float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
  85. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  86. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  87. float x = (float) rect.x;
  88. float y = (float) rect.y;
  89. float w = (float) rect.w;
  90. float h = (float) rect.h;
  91. #ifdef ATOMIC_PLATFORM_WINDOWS
  92. #if !defined(ATOMIC_D3D11) && !defined(ATOMIC_OPENGL)
  93. //Direct3D9 Adjustment
  94. x += 0.5f;
  95. y += 0.5f;
  96. #endif
  97. #endif
  98. data[3] = color;
  99. data[9] = color;
  100. data[15] = color;
  101. data[21] = color;
  102. data[27] = color;
  103. data[33] = color;
  104. data[0] = x;
  105. data[1] = y;
  106. data[6] = x + w;
  107. data[7] = y;
  108. data[12] = x + w;
  109. data[13] = y + h;
  110. data[18] = x;
  111. data[19] = y;
  112. data[24] = x + w;
  113. data[25] = y + h;
  114. data[30] = x;
  115. data[31] = y + h;
  116. UIView *view = webView_->GetView();
  117. if (view)
  118. {
  119. view->SubmitBatchVertexData(webView_->GetWebTexture2D()->GetTexture2D(), vertexData_);
  120. }
  121. }
  122. private:
  123. bool browserCreated_;
  124. WeakPtr<UIWebView> webView_;
  125. PODVector<float> vertexData_;
  126. };
  127. UIWebView::UIWebView(Context* context, const String &initialURL) : UIWidget(context, false)
  128. {
  129. widget_ = new WebViewWidget();
  130. widget_->SetDelegate(this);
  131. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  132. widget_->SetIsFocusable(true);
  133. ((WebViewWidget*)widget_)->webView_ = this;
  134. UI* ui = GetSubsystem<UI>();
  135. ui->WrapWidget(this, widget_);
  136. webClient_ = new WebClient(context);
  137. webTexture_ = new WebTexture2D(context);
  138. webClient_->SetWebRenderHandler(webTexture_);
  139. initialURL_ = initialURL;
  140. SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(UIWebView, HandleKeyDown));
  141. SubscribeToEvent(E_KEYUP, ATOMIC_HANDLER(UIWebView, HandleKeyUp));
  142. SubscribeToEvent(E_TEXTINPUT, ATOMIC_HANDLER(UIWebView, HandleTextInput));
  143. }
  144. UIWebView::~UIWebView()
  145. {
  146. }
  147. void UIWebView::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  148. {
  149. if (!widget_ || !widget_->GetIsFocused())
  150. return;
  151. webClient_->SendKeyEvent(eventType, eventData);
  152. }
  153. void UIWebView::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  154. {
  155. if (!widget_ || !widget_->GetIsFocused())
  156. return;
  157. webClient_->SendKeyEvent(eventType, eventData);
  158. }
  159. void UIWebView::HandleTextInput(StringHash eventType, VariantMap& eventData)
  160. {
  161. if (!widget_ || !widget_->GetIsFocused())
  162. return;
  163. webClient_->SendTextInputEvent(eventType, eventData);
  164. }
  165. bool UIWebView::OnEvent(const TBWidgetEvent &ev)
  166. {
  167. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_POINTER_UP)
  168. {
  169. webClient_->SendMouseClickEvent(ev.target_x, ev.target_y, 0, ev.type == EVENT_TYPE_POINTER_UP, 0, ev.count);
  170. return true;
  171. }
  172. else if (ev.type == EVENT_TYPE_POINTER_MOVE)
  173. {
  174. webClient_->SendMouseMoveEvent(ev.target_x, ev.target_y, 0);
  175. return true;
  176. }
  177. else if (ev.type == EVENT_TYPE_WHEEL)
  178. {
  179. webClient_->SendMouseWheelEvent(ev.target_x, ev.target_y, 0, ev.delta_x, ev.delta_y);
  180. return true;
  181. }
  182. else if (ev.type == EVENT_TYPE_SHORTCUT)
  183. {
  184. #ifdef ATOMIC_PLATFORM_OSX
  185. // On OSX, these shortcuts are not being invoked with key combinations (Handle KeyDown/KeyUp)
  186. if (ev.ref_id == TBIDC("copy"))
  187. {
  188. webClient_->ShortcutCopy();
  189. return true;
  190. }
  191. else if (ev.ref_id == TBIDC("paste"))
  192. {
  193. webClient_->ShortcutPaste();
  194. return true;
  195. }
  196. else if (ev.ref_id == TBIDC("cut"))
  197. {
  198. webClient_->ShortcutCut();
  199. return true;
  200. }
  201. #endif
  202. // Handled by key combinations in Handle KeyDown/KeyUp
  203. /*
  204. else if (ev.ref_id == TBIDC("selectall"))
  205. {
  206. webClient_->ShortcutSelectAll();
  207. return true;
  208. }
  209. else if (ev.ref_id == TBIDC("undo"))
  210. {
  211. webClient_->ShortcutUndo();
  212. return true;
  213. }
  214. else if (ev.ref_id == TBIDC("redo"))
  215. {
  216. webClient_->ShortcutRedo();
  217. return true;
  218. }
  219. */
  220. }
  221. else if (ev.type == EVENT_TYPE_KEY_DOWN || ev.type == EVENT_TYPE_KEY_UP)
  222. {
  223. // We consume all key events as they are handled by the UI event system directly
  224. return true;
  225. }
  226. return UIWidget::OnEvent(ev);
  227. }
  228. void UIWebView::OnFocusChanged(bool focused)
  229. {
  230. webClient_->SendFocusEvent(focused);
  231. }
  232. WebTexture2D* UIWebView::GetWebTexture2D() const
  233. {
  234. return webTexture_;
  235. }
  236. }