UIWebView.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/UI/UIRenderer.h>
  25. #include "WebClient.h"
  26. #include "WebTexture2D.h"
  27. #include "WebBrowserHost.h"
  28. #include "UIWebView.h"
  29. using namespace tb;
  30. namespace Atomic
  31. {
  32. class WebViewWidget : public tb::TBWidget
  33. {
  34. friend class UIWebView;
  35. public:
  36. // For safe typecasting
  37. TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
  38. WebViewWidget() : browserCreated_(false)
  39. {
  40. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  41. float color;
  42. ((unsigned&)color) = 0xFFFFFFFF;
  43. float* data = &vertexData_[0];
  44. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  45. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  46. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  47. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  48. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  49. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  50. }
  51. void OnPaint(const PaintProps &paint_props)
  52. {
  53. if (webView_.Null())
  54. return;
  55. TBRect rect = GetRect();
  56. rect.x = rect.y = 0;
  57. ConvertToRoot(rect.x, rect.y);
  58. IntRect size = webView_->GetRect();
  59. float* data = &vertexData_[0];
  60. UI* ui = webView_->GetSubsystem<UI>();
  61. if (!browserCreated_)
  62. {
  63. browserCreated_ = true;
  64. webView_->webClient_->CreateBrowser(webView_->initialURL_, rect.w, rect.h);
  65. }
  66. webView_->webClient_->SetSize(rect.w, rect.h);
  67. float color;
  68. float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
  69. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  70. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  71. data[3] = color;
  72. data[9] = color;
  73. data[15] = color;
  74. data[21] = color;
  75. data[27] = color;
  76. data[33] = color;
  77. data[0] = rect.x;
  78. data[1] = rect.y;
  79. data[6] = rect.x + rect.w;
  80. data[7] = rect.y;
  81. data[12] = rect.x + rect.w;
  82. data[13] = rect.y + rect.h;
  83. data[18] = rect.x;
  84. data[19] = rect.y;
  85. data[24] = rect.x + rect.w;
  86. data[25] = rect.y + rect.h;
  87. data[30] = rect.x;
  88. data[31] = rect.y + rect.h;
  89. ui->SubmitBatchVertexData(webView_->GetWebTexture2D()->GetTexture2D(), vertexData_);
  90. }
  91. private:
  92. bool browserCreated_;
  93. WeakPtr<UIWebView> webView_;
  94. PODVector<float> vertexData_;
  95. };
  96. UIWebView::UIWebView(Context* context, const String &initialURL) : UIWidget(context, false)
  97. {
  98. widget_ = new WebViewWidget();
  99. widget_->SetDelegate(this);
  100. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  101. widget_->SetIsFocusable(true);
  102. ((WebViewWidget*)widget_)->webView_ = this;
  103. UI* ui = GetSubsystem<UI>();
  104. ui->WrapWidget(this, widget_);
  105. webClient_ = new WebClient(context);
  106. webTexture_ = new WebTexture2D(context);
  107. webClient_->SetWebRenderHandler(webTexture_);
  108. initialURL_ = initialURL;
  109. SubscribeToEvent(E_KEYDOWN, HANDLER(UIWebView, HandleKeyDown));
  110. SubscribeToEvent(E_KEYUP, HANDLER(UIWebView, HandleKeyUp));
  111. SubscribeToEvent(E_TEXTINPUT, HANDLER(UIWebView, HandleTextInput));
  112. }
  113. UIWebView::~UIWebView()
  114. {
  115. }
  116. void UIWebView::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  117. {
  118. if (!widget_ || !widget_->GetIsFocused())
  119. return;
  120. //if (eventData[KeyDown::P_REPEAT].GetBool())
  121. // return;
  122. int key = eventData[KeyDown::P_KEY].GetInt();
  123. int scanCode = eventData[KeyDown::P_SCANCODE].GetInt();
  124. unsigned raw = eventData[KeyDown::P_RAW].GetUInt();
  125. int buttons = eventData[KeyDown::P_BUTTONS].GetInt();
  126. int qual = eventData[KeyDown::P_QUALIFIERS].GetInt();
  127. webClient_->SendKeyEvent(key, false, scanCode, raw, buttons, qual);
  128. }
  129. void UIWebView::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  130. {
  131. if (!widget_ || !widget_->GetIsFocused())
  132. return;
  133. int key = eventData[KeyUp::P_KEY].GetInt();
  134. int scanCode = eventData[KeyUp::P_SCANCODE].GetInt();
  135. unsigned raw = eventData[KeyUp::P_RAW].GetUInt();
  136. int buttons = eventData[KeyUp::P_BUTTONS].GetInt();
  137. int qual = eventData[KeyUp::P_QUALIFIERS].GetInt();
  138. webClient_->SendKeyEvent(key, true, scanCode, raw, buttons, qual);
  139. }
  140. void UIWebView::HandleTextInput(StringHash eventType, VariantMap& eventData)
  141. {
  142. if (!widget_ || !widget_->GetIsFocused())
  143. return;
  144. String text = eventData[TextInput::P_TEXT].GetString();
  145. webClient_->SendTextEvent(text, 0);
  146. }
  147. bool UIWebView::OnEvent(const TBWidgetEvent &ev)
  148. {
  149. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_POINTER_UP)
  150. {
  151. webClient_->SendMouseClickEvent(ev.target_x, ev.target_y, 0, ev.type == EVENT_TYPE_POINTER_UP, 0);
  152. return true;
  153. }
  154. else if (ev.type == EVENT_TYPE_POINTER_MOVE)
  155. {
  156. webClient_->SendMouseMoveEvent(ev.target_x, ev.target_y, 0);
  157. return true;
  158. }
  159. else if (ev.type == EVENT_TYPE_WHEEL)
  160. {
  161. webClient_->SendMouseWheelEvent(ev.target_x, ev.target_y, 0, ev.delta_x, ev.delta_y);
  162. return true;
  163. }
  164. else if (ev.type == EVENT_TYPE_KEY_DOWN)
  165. {
  166. return true;
  167. }
  168. else if (ev.type == EVENT_TYPE_KEY_UP)
  169. {
  170. return true;
  171. }
  172. return UIWidget::OnEvent(ev);
  173. }
  174. WebTexture2D* UIWebView::GetWebTexture2D() const
  175. {
  176. return webTexture_;
  177. }
  178. }