Menu.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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::LoadXML;
  32. public:
  33. /// Construct.
  34. Menu(Context* context);
  35. /// Destruct.
  36. virtual ~Menu();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Load from XML data with style. Return true if successful.
  40. virtual bool LoadXML(const XMLElement& source, XMLFile* styleFile);
  41. /// Save as XML data. Return true if successful.
  42. virtual bool SaveXML(XMLElement& dest);
  43. /// React to the popup being shown.
  44. virtual void OnShowPopup();
  45. /// Set popup element to show on selection.
  46. void SetPopup(UIElement* element);
  47. /// Set popup element offset.
  48. void SetPopupOffset(const IntVector2& offset);
  49. /// Set popup element offset.
  50. void SetPopupOffset(int x, int y);
  51. /// Force the popup to show or hide.
  52. void ShowPopup(bool enable);
  53. /// Set accelerator key (set zero key code to disable.)
  54. void SetAccelerator(int key, int qualifiers);
  55. /// Return popup element.
  56. UIElement* GetPopup() const { return popup_; }
  57. /// Return popup element offset.
  58. const IntVector2& GetPopupOffset() const { return popupOffset_; }
  59. /// Return whether popup is open.
  60. bool GetShowPopup() const { return showPopup_; }
  61. /// Return accelerator key code, 0 if disabled.
  62. int GetAcceleratorKey() const { return acceleratorKey_; }
  63. /// Return accelerator qualifiers.
  64. int GetAcceleratorQualifiers() const { return acceleratorQualifiers_; }
  65. protected:
  66. /// Popup element.
  67. SharedPtr<UIElement> popup_;
  68. /// Popup element offset.
  69. IntVector2 popupOffset_;
  70. /// Show popup flag.
  71. bool showPopup_;
  72. /// Accelerator key code.
  73. int acceleratorKey_;
  74. /// Accelerator qualifiers.
  75. int acceleratorQualifiers_;
  76. private:
  77. /// Handle press and release for selection and toggling popup visibility.
  78. void HandlePressedReleased(StringHash eventType, VariantMap& eventData);
  79. /// Handle global focus change to check for hiding the popup.
  80. void HandleFocusChanged(StringHash eventType, VariantMap& eventData);
  81. /// Handle keypress for checking accelerator.
  82. void HandleKeyDown(StringHash eventType, VariantMap& eventData);
  83. };
  84. }