UISceneView.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/UI/UI.h>
  24. #include <Atomic/UI/UIBatch.h>
  25. #include <Atomic/IO/Log.h>
  26. #include <Atomic/Engine/Engine.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #include <Atomic/Graphics/RenderPath.h>
  30. #include <Atomic/Graphics/Renderer.h>
  31. #include <Atomic/Core/CoreEvents.h>
  32. #include "UIRenderer.h"
  33. #include "UISceneView.h"
  34. using namespace tb;
  35. namespace Atomic
  36. {
  37. UISceneView::UISceneView(Context* context, bool createWidget) : UIWidget(context, false),
  38. rttFormat_(Graphics::GetRGBFormat()),
  39. autoUpdate_(false),
  40. size_(-1, -1),
  41. resizeRequired_(false)
  42. {
  43. UI* ui= GetSubsystem<UI>();
  44. if (createWidget)
  45. {
  46. renderTexture_ = new Texture2D(context_);
  47. depthTexture_ = new Texture2D(context_);
  48. viewport_ = new Viewport(context_);
  49. widget_ = new SceneViewWidget();
  50. widget_->SetDelegate(this);
  51. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  52. ((SceneViewWidget*)widget_)->sceneView_ = this;
  53. ui->WrapWidget(this, widget_);
  54. }
  55. renderer_ = ui->GetRenderer();
  56. SubscribeToEvent(E_ENDFRAME, HANDLER(UISceneView, HandleEndFrame));
  57. }
  58. UISceneView::~UISceneView()
  59. {
  60. }
  61. bool UISceneView::OnEvent(const TBWidgetEvent &ev)
  62. {
  63. return UIWidget::OnEvent(ev);
  64. }
  65. void UISceneView::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  66. {
  67. if (resizeRequired_)
  68. {
  69. TBRect rect = widget_->GetRect();
  70. OnResize(IntVector2(rect.w, rect.h));
  71. resizeRequired_ = false;
  72. }
  73. }
  74. void UISceneView::OnResize(const IntVector2 &newSize)
  75. {
  76. if (newSize.x_ == size_.x_ && newSize.y_ == size_.y_)
  77. return;
  78. int width = newSize.x_;
  79. int height = newSize.y_;
  80. if (width > 0 && height > 0)
  81. {
  82. viewport_->SetRect(IntRect(0, 0, width, height));
  83. renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET);
  84. depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  85. RenderSurface* surface = renderTexture_->GetRenderSurface();
  86. surface->SetViewport(0, viewport_);
  87. surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE);
  88. surface->SetLinkedDepthStencil(depthTexture_->GetRenderSurface());
  89. size_ = newSize;
  90. }
  91. }
  92. void UISceneView::SetView(Scene* scene, Camera* camera)
  93. {
  94. scene_ = scene;
  95. cameraNode_ = camera ? camera->GetNode() : 0;
  96. viewport_->SetScene(scene_);
  97. viewport_->SetCamera(camera);
  98. QueueUpdate();
  99. }
  100. void UISceneView::SetFormat(unsigned format)
  101. {
  102. if (format != rttFormat_)
  103. {
  104. rttFormat_ = format;
  105. }
  106. }
  107. void UISceneView::SetAutoUpdate(bool enable)
  108. {
  109. if (enable != autoUpdate_)
  110. {
  111. autoUpdate_ = enable;
  112. RenderSurface* surface = renderTexture_->GetRenderSurface();
  113. if (surface)
  114. surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE);
  115. }
  116. }
  117. void UISceneView::QueueUpdate()
  118. {
  119. if (!autoUpdate_)
  120. {
  121. RenderSurface* surface = renderTexture_->GetRenderSurface();
  122. if (surface)
  123. surface->QueueUpdate();
  124. }
  125. }
  126. Scene* UISceneView::GetScene() const
  127. {
  128. return scene_;
  129. }
  130. Node* UISceneView::GetCameraNode() const
  131. {
  132. return cameraNode_;
  133. }
  134. Texture2D* UISceneView::GetRenderTexture() const
  135. {
  136. return renderTexture_;
  137. }
  138. Texture2D* UISceneView::GetDepthTexture() const
  139. {
  140. return depthTexture_;
  141. }
  142. Viewport* UISceneView::GetViewport() const
  143. {
  144. return viewport_;
  145. }
  146. SceneViewWidget::SceneViewWidget()
  147. {
  148. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  149. float color;
  150. ((unsigned&)color) = 0xFFFFFFFF;
  151. float* data = &vertexData_[0];
  152. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  153. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  154. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  155. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  156. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  157. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  158. }
  159. void SceneViewWidget::OnPaint(const PaintProps &paint_props)
  160. {
  161. if (sceneView_.Null())
  162. return;
  163. TBRect rect = GetRect();
  164. rect.x = rect.y = 0;
  165. ConvertToRoot(rect.x, rect.y);
  166. IntVector2 size = sceneView_->GetSize();
  167. if (size.x_ != rect.w || size.y_ != rect.h)
  168. {
  169. size.x_ = rect.w;
  170. size.y_ = rect.h;
  171. sceneView_->SetResizeRequired();
  172. // early out here, responsible for flicker
  173. // https://github.com/AtomicGameEngine/AtomicGameEngine/issues/115
  174. return;
  175. }
  176. float* data = &vertexData_[0];
  177. float color;
  178. float fopacity = GetOpacity() * sceneView_->renderer_->GetOpacity();
  179. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  180. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  181. float x = (float) rect.x;
  182. float y = (float) rect.y;
  183. float w = (float) rect.w;
  184. float h = (float) rect.h;
  185. #ifdef ATOMIC_PLATFORM_WINDOWS
  186. #ifndef ATOMIC_D3D11
  187. //Direct3D9 Adjustment
  188. x += 0.5f;
  189. y += 0.5f;
  190. #endif
  191. #endif
  192. data[3] = color;
  193. data[9] = color;
  194. data[15] = color;
  195. data[21] = color;
  196. data[27] = color;
  197. data[33] = color;
  198. data[0] = x;
  199. data[1] = y;
  200. data[6] = x + w;
  201. data[7] = y;
  202. data[12] = x + w;
  203. data[13] = y + h;
  204. data[18] = x;
  205. data[19] = y;
  206. data[24] = x + w;
  207. data[25] = y + h;
  208. data[30] = x;
  209. data[31] = y + h;
  210. sceneView_->GetSubsystem<UI>()->SubmitBatchVertexData(sceneView_->GetRenderTexture(), vertexData_);
  211. }
  212. }