Menu.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Copyright (c) 2008-2013 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 Urho3D
  25. {
  26. /// %Menu %UI element that optionally shows a popup.
  27. class Menu : public Button
  28. {
  29. OBJECT(Menu);
  30. using UIElement::LoadXML;
  31. public:
  32. /// Construct.
  33. Menu(Context* context);
  34. /// Destruct.
  35. virtual ~Menu();
  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);
  40. /// Save as XML data. Return true if successful.
  41. virtual bool SaveXML(XMLElement& dest);
  42. /// React to mouse hover.
  43. virtual void OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  44. /// React to the popup being shown.
  45. virtual void OnShowPopup();
  46. /// Set popup element to show on selection.
  47. void SetPopup(UIElement* element);
  48. /// Set popup element offset.
  49. void SetPopupOffset(const IntVector2& offset);
  50. /// Set popup element offset.
  51. void SetPopupOffset(int x, int y);
  52. /// Force the popup to show or hide.
  53. void ShowPopup(bool enable);
  54. /// Set accelerator key (set zero key code to disable.)
  55. void SetAccelerator(int key, int qualifiers);
  56. /// Return popup element.
  57. UIElement* GetPopup() const { return popup_; }
  58. /// Return popup element offset.
  59. const IntVector2& GetPopupOffset() const { return popupOffset_; }
  60. /// Return whether popup is open.
  61. bool GetShowPopup() const { return showPopup_; }
  62. /// Return accelerator key code, 0 if disabled.
  63. int GetAcceleratorKey() const { return acceleratorKey_; }
  64. /// Return accelerator qualifiers.
  65. int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
  66. protected:
  67. /// Popup element.
  68. SharedPtr<UIElement> popup_;
  69. /// Popup element offset.
  70. IntVector2 popupOffset_;
  71. /// Show popup flag.
  72. bool showPopup_;
  73. /// Accelerator key code.
  74. int acceleratorKey_;
  75. /// Accelerator qualifiers.
  76. int acceleratorQualifiers_;
  77. private:
  78. /// Handle press and release for selection and toggling popup visibility.
  79. void HandlePressedReleased(StringHash eventType, VariantMap& eventData);
  80. /// Handle global focus change to check for hiding the popup.
  81. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  82. /// Handle keypress for checking accelerator.
  83. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  84. };
  85. }