Menu.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Button.h"
  25. /// %Menu %UI element that optionally shows a popup.
  26. class Menu : public Button
  27. {
  28. OBJECT(Menu);
  29. using UIElement::SetStyle;
  30. public:
  31. /// Construct.
  32. Menu(Context* context);
  33. /// Destruct.
  34. virtual ~Menu();
  35. /// Register object factory.
  36. static void RegisterObject(Context* context);
  37. /// %Set UI element style from XML data.
  38. virtual void SetStyle(const XMLElement& element);
  39. /// React to the popup being shown.
  40. virtual void OnShowPopup();
  41. /// %Set popup element to show on selection.
  42. void SetPopup(UIElement* element);
  43. /// %Set popup element offset.
  44. void SetPopupOffset(const IntVector2& offset);
  45. /// %Set popup element offset.
  46. void SetPopupOffset(int x, int y);
  47. /// Force the popup to show or hide.
  48. void ShowPopup(bool enable);
  49. /// %Set accelerator key (set zero key code to disable.)
  50. void SetAccelerator(int key, int qualifiers);
  51. /// Return popup element.
  52. UIElement* GetPopup() const { return popup_; }
  53. /// Return popup element offset.
  54. const IntVector2& GetPopupOffset() const { return popupOffset_; }
  55. /// Return whether popup is open.
  56. bool GetShowPopup() const { return showPopup_; }
  57. /// Return accelerator key code, 0 if disabled.
  58. int GetAcceleratorKey() const { return acceleratorKey_; }
  59. /// Return accelerator qualifiers.
  60. int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
  61. protected:
  62. /// Popup element.
  63. SharedPtr<UIElement> popup_;
  64. /// Popup element offset.
  65. IntVector2 popupOffset_;
  66. /// Show popup flag.
  67. bool showPopup_;
  68. /// Accelerator key code.
  69. int acceleratorKey_;
  70. /// Accelerator qualifiers.
  71. int acceleratorQualifiers_;
  72. private:
  73. /// Handle press and release for selection and toggling popup visibility.
  74. void HandlePressedReleased(StringHash eventType, VariantMap& eventData);
  75. /// Handle global focus change to check for hiding the popup.
  76. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  77. /// Handle keypress for checking accelerator.
  78. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  79. };