Menu.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2008-2015 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 "Button.h"
  24. namespace Atomic
  25. {
  26. namespace SystemUI
  27. {
  28. /// %Menu %UI element that optionally shows a popup.
  29. class ATOMIC_API Menu : public Button
  30. {
  31. OBJECT(Menu);
  32. using UIElement::LoadXML;
  33. public:
  34. /// Construct.
  35. Menu(Context* context);
  36. /// Destruct.
  37. virtual ~Menu();
  38. /// Register object factory.
  39. static void RegisterObject(Context* context);
  40. /// Load from XML data with style. Return true if successful.
  41. virtual bool LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault = false);
  42. /// Save as XML data. Return true if successful.
  43. virtual bool SaveXML(XMLElement& dest) const;
  44. /// Perform UI element update.
  45. virtual void Update(float timeStep);
  46. /// React to mouse hover.
  47. virtual void OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  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. void SetPopup(UIElement* element);
  54. /// Set popup element offset.
  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. void ShowPopup(bool enable);
  60. /// Set accelerator key (set zero key code to disable.)
  61. void SetAccelerator(int key, int qualifiers);
  62. /// Return popup element.
  63. UIElement* GetPopup() const { return popup_; }
  64. /// Return popup element offset.
  65. const IntVector2& GetPopupOffset() const { return popupOffset_; }
  66. /// Return whether popup is open.
  67. bool GetShowPopup() const { return showPopup_; }
  68. /// Return accelerator key code, 0 if disabled.
  69. int GetAcceleratorKey() const { return acceleratorKey_; }
  70. /// Return accelerator qualifiers.
  71. int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
  72. protected:
  73. /// Filter implicit attributes in serialization process.
  74. virtual bool FilterPopupImplicitAttributes(XMLElement& dest) const;
  75. /// Popup element.
  76. SharedPtr<UIElement> popup_;
  77. /// Popup element offset.
  78. IntVector2 popupOffset_;
  79. /// Show popup flag.
  80. bool showPopup_;
  81. /// Accelerator key code.
  82. int acceleratorKey_;
  83. /// Accelerator qualifiers.
  84. int acceleratorQualifiers_;
  85. private:
  86. /// Handle press and release for selection and toggling popup visibility.
  87. void HandlePressedReleased(StringHash eventType, VariantMap& eventData);
  88. /// Handle global focus change to check for hiding the popup.
  89. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  90. /// Handle keypress for checking accelerator.
  91. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  92. /// Auto popup flag.
  93. bool autoPopup_;
  94. };
  95. }
  96. }