UIWebView.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/UI/UIRenderer.h>
  23. #include "WebClient.h"
  24. #include "WebTexture2D.h"
  25. #include "WebBrowserHost.h"
  26. #include "UIWebView.h"
  27. using namespace tb;
  28. namespace Atomic
  29. {
  30. class WebViewWidget : public tb::TBWidget
  31. {
  32. friend class UIWebView;
  33. public:
  34. // For safe typecasting
  35. TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
  36. WebViewWidget()
  37. {
  38. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  39. float color;
  40. ((unsigned&)color) = 0xFFFFFFFF;
  41. float* data = &vertexData_[0];
  42. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  43. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  44. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  45. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  46. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  47. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  48. }
  49. void OnPaint(const PaintProps &paint_props)
  50. {
  51. if (webView_.Null())
  52. return;
  53. TBRect rect = GetRect();
  54. rect.x = rect.y = 0;
  55. ConvertToRoot(rect.x, rect.y);
  56. IntRect size = webView_->GetRect();
  57. float* data = &vertexData_[0];
  58. UI* ui = webView_->GetSubsystem<UI>();
  59. WebTexture2D* tex = webView_->GetWebTexture2D();
  60. tex->SetCurrentSize(rect.w, rect.h);
  61. float umax = (float)tex->GetCurrentWidth()/(float)tex->GetMaxWidth();
  62. float vmax = (float)tex->GetCurrentHeight()/(float)tex->GetMaxHeight();
  63. float color;
  64. float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
  65. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  66. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  67. data[3] = color;
  68. data[9] = color;
  69. data[15] = color;
  70. data[21] = color;
  71. data[27] = color;
  72. data[33] = color;
  73. // UV
  74. data[4] = 0; data[5] = 0;
  75. data[10] = umax; data[11] = 0;
  76. data[16] = umax; data[17] = vmax;
  77. data[22] = 0; data[23] = 0;
  78. data[28] = umax; data[29] = vmax;
  79. data[34] = 0; data[35] = vmax;
  80. data[0] = rect.x;
  81. data[1] = rect.y;
  82. data[6] = rect.x + rect.w;
  83. data[7] = rect.y;
  84. data[12] = rect.x + rect.w;
  85. data[13] = rect.y + rect.h;
  86. data[18] = rect.x;
  87. data[19] = rect.y;
  88. data[24] = rect.x + rect.w;
  89. data[25] = rect.y + rect.h;
  90. data[30] = rect.x;
  91. data[31] = rect.y + rect.h;
  92. ui->SubmitBatchVertexData(tex->GetTexture2D(), vertexData_);
  93. }
  94. private:
  95. WeakPtr<UIWebView> webView_;
  96. PODVector<float> vertexData_;
  97. };
  98. UIWebView::UIWebView(Context* context) : UIWidget(context, false),
  99. resizeRequired_(false)
  100. {
  101. widget_ = new WebViewWidget();
  102. widget_->SetDelegate(this);
  103. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  104. ((WebViewWidget*)widget_)->webView_ = this;
  105. UI* ui = GetSubsystem<UI>();
  106. ui->WrapWidget(this, widget_);
  107. webClient_ = new WebClient(context);
  108. webTexture_ = new WebTexture2D(context);
  109. webClient_->SetWebRenderHandler(webTexture_);
  110. webClient_->CreateBrowser();
  111. }
  112. UIWebView::~UIWebView()
  113. {
  114. }
  115. bool UIWebView::OnEvent(const TBWidgetEvent &ev)
  116. {
  117. if (ev.type == EVENT_TYPE_POINTER_DOWN || ev.type == EVENT_TYPE_POINTER_UP)
  118. {
  119. webClient_->SendMouseClickEvent(ev.target_x, ev.target_y, 0, ev.type == EVENT_TYPE_POINTER_UP, 0);
  120. return true;
  121. }
  122. else if (ev.type == EVENT_TYPE_POINTER_MOVE)
  123. {
  124. webClient_->SendMouseMoveEvent(ev.target_x, ev.target_y, 0);
  125. return true;
  126. }
  127. return UIWidget::OnEvent(ev);
  128. }
  129. WebTexture2D* UIWebView::GetWebTexture2D() const
  130. {
  131. return webTexture_;
  132. }
  133. }