Menu.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace Urho3D
  26. {
  27. /// %Menu %UI element that optionally shows a popup.
  28. class Menu : public Button
  29. {
  30. OBJECT(Menu);
  31. using UIElement::SetStyle;
  32. public:
  33. /// Construct.
  34. Menu(Context* context);
  35. /// Destruct.
  36. virtual ~Menu();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Set UI element style from XML data.
  40. virtual void SetStyle(const XMLElement& element);
  41. /// React to the popup being shown.
  42. virtual void OnShowPopup();
  43. /// Set popup element to show on selection.
  44. void SetPopup(UIElement* element);
  45. /// Set popup element offset.
  46. void SetPopupOffset(const IntVector2& offset);
  47. /// Set popup element offset.
  48. void SetPopupOffset(int x, int y);
  49. /// Force the popup to show or hide.
  50. void ShowPopup(bool enable);
  51. /// Set accelerator key (set zero key code to disable.)
  52. void SetAccelerator(int key, int qualifiers);
  53. /// Return popup element.
  54. UIElement* GetPopup() const { return popup_; }
  55. /// Return popup element offset.
  56. const IntVector2& GetPopupOffset() const { return popupOffset_; }
  57. /// Return whether popup is open.
  58. bool GetShowPopup() const { return showPopup_; }
  59. /// Return accelerator key code, 0 if disabled.
  60. int GetAcceleratorKey() const { return acceleratorKey_; }
  61. /// Return accelerator qualifiers.
  62. int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
  63. protected:
  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. };
  82. }