UIComponent.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/BillboardSet.h"
  25. #include "../Graphics/Graphics.h"
  26. #include "../Graphics/Octree.h"
  27. #include "../Graphics/Technique.h"
  28. #include "../Graphics/Material.h"
  29. #include "../Graphics/Texture2D.h"
  30. #include "../Graphics/StaticModel.h"
  31. #include "../Graphics/Renderer.h"
  32. #include "../Graphics/Camera.h"
  33. #include "../Graphics/VertexBuffer.h"
  34. #include "../Scene/Scene.h"
  35. #include "../Scene/SceneEvents.h"
  36. #include "../Resource/ResourceCache.h"
  37. #include "../IO/Log.h"
  38. #include "../UI/UI.h"
  39. #include "../UI/UIComponent.h"
  40. #include "../UI/UIEvents.h"
  41. #include "../DebugNew.h"
  42. namespace Urho3D
  43. {
  44. static int const UICOMPONENT_DEFAULT_TEXTURE_SIZE = 512;
  45. static int const UICOMPONENT_MIN_TEXTURE_SIZE = 64;
  46. static int const UICOMPONENT_MAX_TEXTURE_SIZE = 4096;
  47. class UIElement3D : public UIElement
  48. {
  49. URHO3D_OBJECT(UIElement3D, UIElement);
  50. public:
  51. /// Construct.
  52. explicit UIElement3D(Context* context) : UIElement(context) { }
  53. /// Destruct.
  54. ~UIElement3D() override = default;
  55. /// Set UIComponent which is using this element as root element.
  56. void SetNode(Node* node) { node_ = node; }
  57. /// Set active viewport through which this element is rendered. If viewport is not set, it defaults to first viewport.
  58. void SetViewport(Viewport* viewport) { viewport_ = viewport; }
  59. /// Convert element coordinates to screen coordinates.
  60. IntVector2 ElementToScreen(const IntVector2& position) override
  61. {
  62. URHO3D_LOGERROR("UIElement3D::ElementToScreen is not implemented.");
  63. return {-1, -1};
  64. }
  65. /// Convert screen coordinates to element coordinates.
  66. IntVector2 ScreenToElement(const IntVector2& screenPos) override
  67. {
  68. IntVector2 result(-1, -1);
  69. if (node_.Expired())
  70. return result;
  71. Scene* scene = node_->GetScene();
  72. auto* model = node_->GetComponent<StaticModel>();
  73. if (scene == nullptr || model == nullptr)
  74. return result;
  75. auto* renderer = GetSubsystem<Renderer>();
  76. if (renderer == nullptr)
  77. return result;
  78. // \todo Always uses the first viewport, in case there are multiple
  79. auto* octree = scene->GetComponent<Octree>();
  80. if (viewport_ == nullptr)
  81. viewport_ = renderer->GetViewportForScene(scene, 0);
  82. if (viewport_.Expired() || octree == nullptr)
  83. return result;
  84. if (viewport_->GetScene() != scene)
  85. {
  86. URHO3D_LOGERROR("UIComponent and Viewport set to component's root element belong to different scenes.");
  87. return result;
  88. }
  89. Camera* camera = viewport_->GetCamera();
  90. if (camera == nullptr)
  91. return result;
  92. IntRect rect = viewport_->GetRect();
  93. if (rect == IntRect::ZERO)
  94. {
  95. auto* graphics = GetSubsystem<Graphics>();
  96. rect.right_ = graphics->GetWidth();
  97. rect.bottom_ = graphics->GetHeight();
  98. }
  99. Ray ray(camera->GetScreenRay((float)screenPos.x_ / rect.Width(), (float)screenPos.y_ / rect.Height()));
  100. PODVector<RayQueryResult> queryResultVector;
  101. RayOctreeQuery query(queryResultVector, ray, RAY_TRIANGLE_UV, M_INFINITY, DRAWABLE_GEOMETRY, DEFAULT_VIEWMASK);
  102. octree->Raycast(query);
  103. if (queryResultVector.Empty())
  104. return result;
  105. for (unsigned i = 0; i < queryResultVector.Size(); i++)
  106. {
  107. RayQueryResult& queryResult = queryResultVector[i];
  108. if (queryResult.drawable_ != model)
  109. {
  110. // ignore billboard sets by default
  111. if (queryResult.drawable_->GetTypeInfo()->IsTypeOf(BillboardSet::GetTypeStatic()))
  112. continue;
  113. return result;
  114. }
  115. Vector2& uv = queryResult.textureUV_;
  116. result = IntVector2(static_cast<int>(uv.x_ * GetWidth()),
  117. static_cast<int>(uv.y_ * GetHeight()));
  118. return result;
  119. }
  120. return result;
  121. }
  122. protected:
  123. /// A UIComponent which owns this element.
  124. WeakPtr<Node> node_;
  125. /// Viewport which renders this element.
  126. WeakPtr<Viewport> viewport_;
  127. };
  128. UIComponent::UIComponent(Context* context)
  129. : Component(context),
  130. viewportIndex_(0)
  131. {
  132. texture_ = context_->CreateObject<Texture2D>();
  133. texture_->SetFilterMode(FILTER_BILINEAR);
  134. texture_->SetAddressMode(COORD_U, ADDRESS_CLAMP);
  135. texture_->SetAddressMode(COORD_V, ADDRESS_CLAMP);
  136. texture_->SetNumLevels(1); // No mipmaps
  137. rootElement_ = context_->CreateObject<UIElement3D>();
  138. rootElement_->SetTraversalMode(TM_BREADTH_FIRST);
  139. rootElement_->SetEnabled(true);
  140. material_ = context_->CreateObject<Material>();
  141. material_->SetTechnique(0, GetSubsystem<ResourceCache>()->GetResource<Technique>("Techniques/Diff.xml"));
  142. material_->SetTexture(TU_DIFFUSE, texture_);
  143. SubscribeToEvent(rootElement_, E_RESIZED, URHO3D_HANDLER(UIComponent, OnElementResized));
  144. // Triggers resizing of texture.
  145. rootElement_->SetRenderTexture(texture_);
  146. rootElement_->SetSize(UICOMPONENT_DEFAULT_TEXTURE_SIZE, UICOMPONENT_DEFAULT_TEXTURE_SIZE);
  147. }
  148. UIComponent::~UIComponent() = default;
  149. void UIComponent::RegisterObject(Context* context)
  150. {
  151. context->RegisterFactory<UIComponent>();
  152. context->RegisterFactory<UIElement3D>();
  153. }
  154. UIElement* UIComponent::GetRoot() const
  155. {
  156. return rootElement_;
  157. }
  158. Material* UIComponent::GetMaterial() const
  159. {
  160. return material_;
  161. }
  162. Texture2D* UIComponent::GetTexture() const
  163. {
  164. return texture_;
  165. }
  166. void UIComponent::OnNodeSet(Node* node)
  167. {
  168. rootElement_->SetNode(node);
  169. if (node)
  170. {
  171. auto* renderer = GetSubsystem<Renderer>();
  172. auto* model = node->GetComponent<StaticModel>();
  173. rootElement_->SetViewport(renderer->GetViewportForScene(GetScene(), viewportIndex_));
  174. if (model == nullptr)
  175. model_ = model = node->CreateComponent<StaticModel>();
  176. model->SetMaterial(material_);
  177. rootElement_->SetRenderTexture(texture_);
  178. }
  179. else
  180. {
  181. rootElement_->SetRenderTexture(nullptr);
  182. if (model_.NotNull())
  183. {
  184. model_->Remove();
  185. model_ = nullptr;
  186. }
  187. }
  188. }
  189. void UIComponent::OnElementResized(StringHash eventType, VariantMap& args)
  190. {
  191. int width = args[Resized::P_WIDTH].GetInt();
  192. int height = args[Resized::P_HEIGHT].GetInt();
  193. if (width < UICOMPONENT_MIN_TEXTURE_SIZE || width > UICOMPONENT_MAX_TEXTURE_SIZE ||
  194. height < UICOMPONENT_MIN_TEXTURE_SIZE || height > UICOMPONENT_MAX_TEXTURE_SIZE)
  195. {
  196. URHO3D_LOGERRORF("UIComponent: Texture size %dx%d is not valid. Width and height should be between %d and %d.",
  197. width, height, UICOMPONENT_MIN_TEXTURE_SIZE, UICOMPONENT_MAX_TEXTURE_SIZE);
  198. return;
  199. }
  200. if (texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_RENDERTARGET))
  201. texture_->GetRenderSurface()->SetUpdateMode(SURFACE_MANUALUPDATE);
  202. else
  203. URHO3D_LOGERROR("UIComponent: resizing texture failed.");
  204. }
  205. void UIComponent::SetViewportIndex(unsigned int index)
  206. {
  207. viewportIndex_ = index;
  208. if (Scene* scene = GetScene())
  209. {
  210. auto* renderer = GetSubsystem<Renderer>();
  211. Viewport* viewport = renderer->GetViewportForScene(scene, index);
  212. rootElement_->SetViewport(viewport);
  213. }
  214. }
  215. }