UIComponent.cpp 7.6 KB

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