Menu.h 3.1 KB

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