UIComponent.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  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. #pragma once
  23. #include "../Scene/Component.h"
  24. namespace Atomic
  25. {
  26. class Viewport;
  27. class UIView;
  28. class StaticModel;
  29. class Material;
  30. // UI Component which can be attached to a scene node with a StaticModel
  31. class ATOMIC_API UIComponent : public Component
  32. {
  33. ATOMIC_OBJECT(UIComponent, Component)
  34. public:
  35. /// Construct.
  36. UIComponent(Context* context);
  37. /// Destruct.
  38. virtual ~UIComponent();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Handle enabled/disabled state change.
  42. virtual void OnSetEnabled();
  43. /// Set the UIView for this UIComponent, note this is optional as one will be autocreated
  44. void SetUIView(UIView* view);
  45. /// Get the UIView for this UIComponent
  46. UIView* GetUIView() const { return uiView_; }
  47. /// Get the viewport associated with this UIComponent
  48. Viewport* GetViewport() const;
  49. /// Set the static model used for rendering and ray casting
  50. void SetStaticModel(StaticModel* staticModel);
  51. /// Get the static model used for rendering and ray casting
  52. StaticModel* GetStaticModel() const;
  53. /// Calculate position in UI space given a screen position
  54. bool CalcUIViewPos(const IntVector2& screenPos, IntVector2& viewPos);
  55. protected:
  56. /// Handle scene node being assigned at creation.
  57. virtual void OnNodeSet(Node* node);
  58. /// Handle scene being assigned. This may happen several times during the component's lifetime. Scene-wide subsystems and events are subscribed to here.
  59. virtual void OnSceneSet(Scene* scene);
  60. private:
  61. const IntVector2& GetSizeAttr() const;
  62. void SetSizeAttr(const IntVector2& value);
  63. void UpdateEventSubscriptions(bool subscribe);
  64. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  65. bool FilterInput() const;
  66. void UpdateMouseEventSubscriptions(bool unsubscribe = false);
  67. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData);
  68. void HandleMouseButtonUp(StringHash eventType, VariantMap& eventData);
  69. void HandleMouseMove(StringHash eventType, VariantMap& eventData);
  70. void HandleMouseWheel(StringHash eventType, VariantMap& eventData);
  71. /// WeakPtr to UIView as we don't want to hold after UI subsystem has released
  72. WeakPtr<UIView> uiView_;
  73. WeakPtr<StaticModel> staticModel_;
  74. IntVector2 viewSize_;
  75. IntRect mouseScreenRect_;
  76. SharedPtr<Material> defaultMaterial_;
  77. };
  78. }