Menu.h 4.1 KB

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