Menu.h 4.2 KB

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