UISceneView.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/UI/UIView.h>
  26. #include <Atomic/IO/Log.h>
  27. #include <Atomic/Engine/Engine.h>
  28. #include <Atomic/Graphics/Graphics.h>
  29. #include <Atomic/Graphics/Camera.h>
  30. #include <Atomic/Graphics/RenderPath.h>
  31. #include <Atomic/Graphics/Renderer.h>
  32. #include <Atomic/Core/CoreEvents.h>
  33. #include "UIRenderer.h"
  34. #include "UISceneView.h"
  35. using namespace tb;
  36. namespace Atomic
  37. {
  38. UISceneView::UISceneView(Context* context, bool createWidget) : UIWidget(context, false),
  39. rttFormat_(Graphics::GetRGBFormat()),
  40. autoUpdate_(false),
  41. size_(-1, -1),
  42. resizeRequired_(false)
  43. {
  44. UI* ui= GetSubsystem<UI>();
  45. if (createWidget)
  46. {
  47. renderTexture_ = new Texture2D(context_);
  48. depthTexture_ = new Texture2D(context_);
  49. viewport_ = new Viewport(context_);
  50. widget_ = new SceneViewWidget();
  51. widget_->SetDelegate(this);
  52. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  53. ((SceneViewWidget*)widget_)->sceneView_ = this;
  54. ui->WrapWidget(this, widget_);
  55. }
  56. renderer_ = ui->GetRenderer();
  57. SubscribeToEvent(E_ENDFRAME, ATOMIC_HANDLER(UISceneView, HandleEndFrame));
  58. }
  59. UISceneView::~UISceneView()
  60. {
  61. }
  62. bool UISceneView::OnEvent(const TBWidgetEvent &ev)
  63. {
  64. return UIWidget::OnEvent(ev);
  65. }
  66. void UISceneView::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  67. {
  68. if (resizeRequired_)
  69. {
  70. TBRect rect = widget_->GetRect();
  71. OnResize(IntVector2(rect.w, rect.h));
  72. resizeRequired_ = false;
  73. }
  74. }
  75. void UISceneView::OnResize(const IntVector2 &newSize)
  76. {
  77. if (newSize.x_ == size_.x_ && newSize.y_ == size_.y_)
  78. return;
  79. int width = newSize.x_;
  80. int height = newSize.y_;
  81. if (width > 0 && height > 0)
  82. {
  83. viewport_->SetRect(IntRect(0, 0, width, height));
  84. renderTexture_->SetSize(width, height, rttFormat_, TEXTURE_RENDERTARGET);
  85. depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  86. RenderSurface* surface = renderTexture_->GetRenderSurface();
  87. surface->SetViewport(0, viewport_);
  88. surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE);
  89. surface->SetLinkedDepthStencil(depthTexture_->GetRenderSurface());
  90. size_ = newSize;
  91. }
  92. }
  93. void UISceneView::SetView(Scene* scene, Camera* camera)
  94. {
  95. scene_ = scene;
  96. cameraNode_ = camera ? camera->GetNode() : 0;
  97. viewport_->SetScene(scene_);
  98. viewport_->SetCamera(camera);
  99. QueueUpdate();
  100. }
  101. void UISceneView::SetFormat(unsigned format)
  102. {
  103. if (format != rttFormat_)
  104. {
  105. rttFormat_ = format;
  106. }
  107. }
  108. void UISceneView::SetAutoUpdate(bool enable)
  109. {
  110. if (enable != autoUpdate_)
  111. {
  112. autoUpdate_ = enable;
  113. RenderSurface* surface = renderTexture_->GetRenderSurface();
  114. if (surface)
  115. surface->SetUpdateMode(autoUpdate_ ? SURFACE_UPDATEALWAYS : SURFACE_MANUALUPDATE);
  116. }
  117. }
  118. void UISceneView::QueueUpdate()
  119. {
  120. if (!autoUpdate_)
  121. {
  122. RenderSurface* surface = renderTexture_->GetRenderSurface();
  123. if (surface)
  124. surface->QueueUpdate();
  125. }
  126. }
  127. Scene* UISceneView::GetScene() const
  128. {
  129. return scene_;
  130. }
  131. Node* UISceneView::GetCameraNode() const
  132. {
  133. return cameraNode_;
  134. }
  135. Texture2D* UISceneView::GetRenderTexture() const
  136. {
  137. return renderTexture_;
  138. }
  139. Texture2D* UISceneView::GetDepthTexture() const
  140. {
  141. return depthTexture_;
  142. }
  143. Viewport* UISceneView::GetViewport() const
  144. {
  145. return viewport_;
  146. }
  147. SceneViewWidget::SceneViewWidget()
  148. {
  149. vertexData_.Resize(6 * UI_VERTEX_SIZE);
  150. float color;
  151. ((unsigned&)color) = 0xFFFFFFFF;
  152. float* data = &vertexData_[0];
  153. data[2] = 0; data[3] = color; data[4] = 0; data[5] = 0;
  154. data[8] = 0; data[9] = color; data[10] = 1; data[11] = 0;
  155. data[14] = 0; data[15] = color; data[16] = 1; data[17] = 1;
  156. data[20] = 0; data[21] = color; data[22] = 0; data[23] = 0;
  157. data[26] = 0; data[27] = color; data[28] = 1; data[29] = 1;
  158. data[32] = 0; data[33] = color; data[34] = 0; data[35] = 1;
  159. }
  160. void SceneViewWidget::OnPaint(const PaintProps &paint_props)
  161. {
  162. if (sceneView_.Null())
  163. return;
  164. TBRect rect = GetRect();
  165. rect.x = rect.y = 0;
  166. ConvertToRoot(rect.x, rect.y);
  167. IntVector2 size = sceneView_->GetSize();
  168. if (size.x_ != rect.w || size.y_ != rect.h)
  169. {
  170. size.x_ = rect.w;
  171. size.y_ = rect.h;
  172. sceneView_->SetResizeRequired();
  173. // early out here, responsible for flicker
  174. // https://github.com/AtomicGameEngine/AtomicGameEngine/issues/115
  175. return;
  176. }
  177. float* data = &vertexData_[0];
  178. float color;
  179. float fopacity = GetOpacity() * sceneView_->renderer_->GetOpacity();
  180. unsigned char opacity = (unsigned char) (fopacity* 255.0f);
  181. ((unsigned&)color) = (0x00FFFFFF + (((uint32)opacity) << 24));
  182. float x = (float) rect.x;
  183. float y = (float) rect.y;
  184. float w = (float) rect.w;
  185. float h = (float) rect.h;
  186. #ifdef ATOMIC_PLATFORM_WINDOWS
  187. #ifndef ATOMIC_D3D11
  188. //Direct3D9 Adjustment
  189. x += 0.5f;
  190. y += 0.5f;
  191. #endif
  192. #endif
  193. data[3] = color;
  194. data[9] = color;
  195. data[15] = color;
  196. data[21] = color;
  197. data[27] = color;
  198. data[33] = color;
  199. data[0] = x;
  200. data[1] = y;
  201. data[6] = x + w;
  202. data[7] = y;
  203. data[12] = x + w;
  204. data[13] = y + h;
  205. data[18] = x;
  206. data[19] = y;
  207. data[24] = x + w;
  208. data[25] = y + h;
  209. data[30] = x;
  210. data[31] = y + h;
  211. UIView *view = sceneView_->GetView();
  212. if (view)
  213. {
  214. view->SubmitBatchVertexData(sceneView_->GetRenderTexture(), vertexData_);
  215. }
  216. }
  217. }