UIComponent.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Copyright (c) 2008-2017 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 "UIComponent.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 "../Resource/ResourceCache.h"
  36. #include "../IO/Log.h"
  37. #include "../UI/UI.h"
  38. #include "../UI/UIEvents.h"
  39. namespace Urho3D
  40. {
  41. static int const UICOMPONENT_DEFAULT_TEXTURE_SIZE = 512;
  42. static int const UICOMPONENT_MIN_TEXTURE_SIZE = 64;
  43. static int const UICOMPONENT_MAX_TEXTURE_SIZE = 4096;
  44. UIComponent::UIComponent(Context* context) :
  45. Component(context),
  46. isStaticModelOwned_(false)
  47. {
  48. vertexBuffer_ = new VertexBuffer(context_);
  49. debugVertexBuffer_ = new VertexBuffer(context_);
  50. texture_ = context_->CreateObject<Texture2D>();
  51. rootElement_ = context_->CreateObject<UIElement>();
  52. rootElement_->SetTraversalMode(TM_BREADTH_FIRST);
  53. material_ = context_->CreateObject<Material>();
  54. material_->SetTechnique(0, GetSubsystem<ResourceCache>()->GetResource<Technique>("Techniques/Diff.xml"));
  55. material_->SetTexture(TU_DIFFUSE, texture_);
  56. SubscribeToEvent(rootElement_, E_RESIZED, URHO3D_HANDLER(UIComponent, OnElementResized));
  57. // Triggers resizing of texture.
  58. rootElement_->SetSize(UICOMPONENT_DEFAULT_TEXTURE_SIZE, UICOMPONENT_DEFAULT_TEXTURE_SIZE);
  59. }
  60. UIComponent::~UIComponent()
  61. {
  62. }
  63. void UIComponent::RegisterObject(Context* context)
  64. {
  65. context->RegisterFactory<UIComponent>();
  66. }
  67. UIElement* UIComponent::GetRoot() const
  68. {
  69. return rootElement_;
  70. }
  71. Material* UIComponent::GetMaterial() const
  72. {
  73. return material_;
  74. }
  75. Texture2D* UIComponent::GetTexture() const
  76. {
  77. return texture_;
  78. }
  79. void UIComponent::OnNodeSet(Node* node)
  80. {
  81. if (node)
  82. {
  83. model_ = node->GetComponent<StaticModel>();
  84. if (model_.Null())
  85. {
  86. isStaticModelOwned_ = true;
  87. model_ = node->CreateComponent<StaticModel>();
  88. }
  89. model_->SetMaterial(material_);
  90. }
  91. else
  92. {
  93. model_->SetMaterial(nullptr);
  94. if (isStaticModelOwned_)
  95. {
  96. model_->GetNode()->RemoveComponent<StaticModel>();
  97. isStaticModelOwned_ = false;
  98. }
  99. model_ = nullptr;
  100. }
  101. UI* ui = GetSubsystem<UI>();
  102. // May be null on shutdown
  103. if (ui)
  104. ui->SetRenderToTexture(this, node != nullptr);
  105. }
  106. void UIComponent::OnElementResized(StringHash eventType, VariantMap& args)
  107. {
  108. int width = args[Resized::P_WIDTH].GetInt();
  109. int height = args[Resized::P_HEIGHT].GetInt();
  110. if (width < UICOMPONENT_MIN_TEXTURE_SIZE || width > UICOMPONENT_MAX_TEXTURE_SIZE ||
  111. height < UICOMPONENT_MIN_TEXTURE_SIZE || height > UICOMPONENT_MAX_TEXTURE_SIZE)
  112. {
  113. URHO3D_LOGERRORF("UIComponent: Texture size %dx%d is not valid. Width and height should be between %d and %d.",
  114. width, height, UICOMPONENT_MIN_TEXTURE_SIZE, UICOMPONENT_MAX_TEXTURE_SIZE);
  115. return;
  116. }
  117. if (texture_->SetSize(width, height, GetSubsystem<Graphics>()->GetRGBAFormat(), TEXTURE_RENDERTARGET))
  118. {
  119. texture_->SetFilterMode(FILTER_BILINEAR);
  120. texture_->SetAddressMode(COORD_U, ADDRESS_CLAMP);
  121. texture_->SetAddressMode(COORD_V, ADDRESS_CLAMP);
  122. texture_->SetNumLevels(1); // No mipmaps
  123. texture_->GetRenderSurface()->SetUpdateMode(SURFACE_MANUALUPDATE);
  124. }
  125. else
  126. URHO3D_LOGERROR("UIComponent: resizing texture failed.");
  127. }
  128. bool UIComponent::ScreenToUIPosition(IntVector2 screenPos, IntVector2& result)
  129. {
  130. Scene* scene = GetScene();
  131. if (!scene)
  132. return false;
  133. Renderer* renderer = GetSubsystem<Renderer>();
  134. if (!renderer)
  135. return false;
  136. // \todo Always uses the first viewport, in case there are multiple
  137. Viewport* viewport = renderer->GetViewportForScene(scene, 0);
  138. Octree* octree = scene->GetComponent<Octree>();
  139. if (!viewport || !octree)
  140. return false;
  141. Camera* camera = viewport->GetCamera();
  142. if (!camera)
  143. return false;
  144. IntRect rect = viewport->GetRect();
  145. if (rect == IntRect::ZERO)
  146. {
  147. Graphics* graphics = GetSubsystem<Graphics>();
  148. rect.right_ = graphics->GetWidth();
  149. rect.bottom_ = graphics->GetHeight();
  150. }
  151. Ray ray(camera->GetScreenRay((float)screenPos.x_ / rect.Width(), (float)screenPos.y_ / rect.Height()));
  152. PODVector<RayQueryResult> queryResultVector;
  153. RayOctreeQuery query(queryResultVector, ray, RAY_TRIANGLE_UV, M_INFINITY, DRAWABLE_GEOMETRY, DEFAULT_VIEWMASK);
  154. octree->Raycast(query);
  155. if (queryResultVector.Empty())
  156. return false;
  157. for (unsigned i = 0; i < queryResultVector.Size(); i++)
  158. {
  159. RayQueryResult& queryResult = queryResultVector[i];
  160. if (queryResult.drawable_ != model_)
  161. {
  162. // ignore billboard sets by default
  163. if (queryResult.drawable_->GetTypeInfo()->IsTypeOf(BillboardSet::GetTypeStatic()))
  164. continue;
  165. return false;
  166. }
  167. Vector2& uv = queryResult.textureUV_;
  168. result = IntVector2(static_cast<int>(uv.x_ * rootElement_->GetWidth()),
  169. static_cast<int>(uv.y_ * rootElement_->GetHeight()));
  170. return true;
  171. }
  172. return false;
  173. }
  174. }