Menu.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright (c) 2008-2022 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. #pragma once
  23. #include "../UI/Button.h"
  24. namespace Urho3D
  25. {
  26. /// %Menu %UI element that optionally shows a popup.
  27. class URHO3D_API Menu : public Button
  28. {
  29. URHO3D_OBJECT(Menu, Button);
  30. public:
  31. /// Construct.
  32. explicit Menu(Context* context);
  33. /// Destruct.
  34. ~Menu() override;
  35. /// Register object factory.
  36. /// @nobind
  37. static void RegisterObject(Context* context);
  38. using UIElement::LoadXML;
  39. using UIElement::SaveXML;
  40. /// Load from XML data with style. Return true if successful.
  41. bool LoadXML(const XMLElement& source, XMLFile* styleFile) override;
  42. /// Save as XML data. Return true if successful.
  43. bool SaveXML(XMLElement& dest) const override;
  44. /// Perform UI element update.
  45. void Update(float timeStep) override;
  46. /// React to mouse hover.
  47. void OnHover(const IntVector2& position, const IntVector2& screenPosition, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor) override;
  48. /// React to the popup being shown.
  49. virtual void OnShowPopup();
  50. /// React to the popup being hidden.
  51. virtual void OnHidePopup() { }
  52. /// Set popup element to show on selection.
  53. /// @property
  54. void SetPopup(UIElement* popup);
  55. /// Set popup element offset.
  56. /// @property
  57. void SetPopupOffset(const IntVector2& offset);
  58. /// Set popup element offset.
  59. void SetPopupOffset(int x, int y);
  60. /// Force the popup to show or hide.
  61. /// @property
  62. void ShowPopup(bool enable);
  63. /// Set accelerator key (set zero key code to disable).
  64. void SetAccelerator(int key, int qualifiers);
  65. /// Return popup element.
  66. /// @property
  67. UIElement* GetPopup() const { return popup_; }
  68. /// Return popup element offset.
  69. /// @property
  70. const IntVector2& GetPopupOffset() const { return popupOffset_; }
  71. /// Return whether popup is open.
  72. /// @property
  73. bool GetShowPopup() const { return showPopup_; }
  74. /// Return accelerator key code, 0 if disabled.
  75. /// @property
  76. int GetAcceleratorKey() const { return acceleratorKey_; }
  77. /// Return accelerator qualifiers.
  78. /// @property
  79. int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
  80. protected:
  81. /// Filter implicit attributes in serialization process.
  82. virtual bool FilterPopupImplicitAttributes(XMLElement& dest) const;
  83. /// Popup element.
  84. SharedPtr<UIElement> popup_;
  85. /// Popup element offset.
  86. IntVector2 popupOffset_;
  87. /// Show popup flag.
  88. bool showPopup_;
  89. /// Accelerator key code.
  90. int acceleratorKey_;
  91. /// Accelerator qualifiers.
  92. int acceleratorQualifiers_;
  93. private:
  94. /// Handle press and release for selection and toggling popup visibility.
  95. void HandlePressedReleased(StringHash eventType, VariantMap& eventData);
  96. /// Handle global focus change to check for hiding the popup.
  97. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  98. /// Handle keypress for checking accelerator.
  99. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  100. /// Auto popup flag.
  101. bool autoPopup_;
  102. };
  103. }