UIWebView.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "UIWebView.h"
  24. using namespace tb;
  25. namespace Atomic
  26. {
  27. class WebViewWidget : public tb::TBWidget
  28. {
  29. friend class UIWebView;
  30. public:
  31. // For safe typecasting
  32. TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
  33. WebViewWidget()
  34. {
  35. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  36. float color;
  37. ((unsigned&)color) = 0xFFFFFFFF;
  38. float* data = &vertexData_[0];
  39. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  40. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  41. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  42. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  43. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  44. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  45. }
  46. void OnPaint(const PaintProps &paint_props)
  47. {
  48. if (webView_.Null())
  49. return;
  50. TBRect rect = GetRect();
  51. rect.x = rect.y = 0;
  52. ConvertToRoot(rect.x, rect.y);
  53. IntVector2 size; /* = webView_->GetSize();*/
  54. if (size.x_ != rect.w || size.y_ != rect.h)
  55. {
  56. size.x_ = rect.w;
  57. size.y_ = rect.h;
  58. webView_->SetResizeRequired();
  59. // early out here, responsible for flicker
  60. // https://github.com/AtomicGameEngine/AtomicGameEngine/issues/115
  61. return;
  62. }
  63. float* data = &vertexData_[0];
  64. UI* ui = webView_->GetSubsystem<UI>();
  65. float color;
  66. float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
  67. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  68. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  69. data[3] = color;
  70. data[9] = color;
  71. data[15] = color;
  72. data[21] = color;
  73. data[27] = color;
  74. data[33] = color;
  75. data[0] = rect.x;
  76. data[1] = rect.y;
  77. data[6] = rect.x + rect.w;
  78. data[7] = rect.y;
  79. data[12] = rect.x + rect.w;
  80. data[13] = rect.y + rect.h;
  81. data[18] = rect.x;
  82. data[19] = rect.y;
  83. data[24] = rect.x + rect.w;
  84. data[25] = rect.y + rect.h;
  85. data[30] = rect.x;
  86. data[31] = rect.y + rect.h;
  87. /*
  88. webView_->GetSubsystem<UI>()->SubmitBatchVertexData(webView_->GetTexture(), vertexData_);
  89. */
  90. }
  91. private:
  92. WeakPtr<UIWebView> webView_;
  93. PODVector<float> vertexData_;
  94. };
  95. UIWebView::UIWebView(Context* context) : UIWidget(context, false),
  96. resizeRequired_(false)
  97. {
  98. widget_ = new WebViewWidget();
  99. widget_->SetDelegate(this);
  100. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  101. ((WebViewWidget*)widget_)->webView_ = this;
  102. UI* ui = GetSubsystem<UI>();
  103. ui->WrapWidget(this, widget_);
  104. }
  105. UIWebView::~UIWebView()
  106. {
  107. }
  108. }