Menu.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #include "Precompiled.h"
  24. #include "InputEvents.h"
  25. #include "Menu.h"
  26. #include "UIEvents.h"
  27. #include "DebugNew.h"
  28. static const ShortStringHash originHash("origin");
  29. Menu::Menu(const std::string& name) :
  30. Button(name),
  31. mPopupOffset(IntVector2::sZero),
  32. mShowPopup(false),
  33. mAcceleratorKey(0),
  34. mAcceleratorQualifiers(0)
  35. {
  36. subscribeToEvent(EVENT_UIMOUSECLICK, EVENT_HANDLER(Menu, handleFocusChanged));
  37. subscribeToEvent(EVENT_FOCUSCHANGED, EVENT_HANDLER(Menu, handleFocusChanged));
  38. }
  39. Menu::~Menu()
  40. {
  41. if (mPopup)
  42. showPopup(false);
  43. }
  44. void Menu::setStyle(const XMLElement& element, ResourceCache* cache)
  45. {
  46. Button::setStyle(element, cache);
  47. XMLElement popupElem = element.getChildElement("popup");
  48. if ((popupElem) && (popupElem.hasAttribute("name")))
  49. {
  50. UIElement* root = getRootElement();
  51. if (root)
  52. setPopup(root->getChild(popupElem.getString("name"), true));
  53. }
  54. if (element.hasChildElement("popupoffset"))
  55. setPopupOffset(element.getChildElement("popupoffset").getIntVector2("value"));
  56. }
  57. void Menu::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  58. {
  59. setPressed(true);
  60. // Toggle popup visibility if exists
  61. showPopup(!mShowPopup);
  62. // Send event on each click if no popup, or whenever the popup is opened
  63. if ((!mPopup) || (mShowPopup))
  64. {
  65. using namespace MenuSelected;
  66. VariantMap eventData;
  67. eventData[P_ELEMENT] = (void*)this;
  68. sendEvent(EVENT_MENUSELECTED, eventData);
  69. }
  70. }
  71. void Menu::onShowPopup()
  72. {
  73. }
  74. void Menu::setPopup(UIElement* popup)
  75. {
  76. if (popup == this)
  77. return;
  78. if ((mPopup) && (!popup))
  79. showPopup(false);
  80. mPopup = popup;
  81. // Detach from current parent (if any) to only show when it is time
  82. if (mPopup)
  83. {
  84. UIElement* parent = mPopup->getParent();
  85. if (parent)
  86. parent->removeChild(mPopup);
  87. }
  88. }
  89. void Menu::setPopupOffset(const IntVector2& offset)
  90. {
  91. mPopupOffset = offset;
  92. }
  93. void Menu::setPopupOffset(int x, int y)
  94. {
  95. mPopupOffset = IntVector2(x, y);
  96. }
  97. void Menu::showPopup(bool enable)
  98. {
  99. if (!mPopup)
  100. return;
  101. // Find the UI root element for showing the popup. If we are already detached, try to find it through the popup
  102. UIElement* root = getRootElement();
  103. if (!root)
  104. root = mPopup->getRootElement();
  105. if (!root)
  106. return;
  107. if (enable)
  108. {
  109. onShowPopup();
  110. mPopup->setPosition(getScreenPosition() + mPopupOffset);
  111. mPopup->setVisible(true);
  112. mPopup->getUserData()[originHash] = (void*)this;
  113. root->addChild(mPopup);
  114. // Set fixed high priority
  115. mPopup->setBringToFront(false);
  116. mPopup->setBringToBack(false);
  117. mPopup->bringToFront();
  118. }
  119. else
  120. {
  121. mPopup->getUserData()[originHash].clear();
  122. root->removeChild(mPopup);
  123. }
  124. mShowPopup = enable;
  125. mSelected = enable;
  126. }
  127. void Menu::setAccelerator(int key, int qualifiers)
  128. {
  129. mAcceleratorKey = key;
  130. mAcceleratorQualifiers = qualifiers;
  131. if (key)
  132. subscribeToEvent(EVENT_KEYDOWN, EVENT_HANDLER(Menu, handleKeyDown));
  133. else
  134. unsubscribeFromEvent(EVENT_KEYDOWN);
  135. }
  136. void Menu::handleFocusChanged(StringHash eventType, VariantMap& eventData)
  137. {
  138. if (!mShowPopup)
  139. return;
  140. using namespace FocusChanged;
  141. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].getPtr());
  142. UIElement* root = getRootElement();
  143. // If clicked emptiness, hide the popup
  144. if (!element)
  145. {
  146. showPopup(false);
  147. return;
  148. }
  149. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  150. // In that case, do not hide
  151. while (element)
  152. {
  153. if ((element == this) || (element == mPopup))
  154. return;
  155. if (element->getParent() == root)
  156. element = static_cast<UIElement*>(element->getUserData()[originHash].getPtr());
  157. else
  158. element = element->getParent();
  159. }
  160. showPopup(false);
  161. }
  162. void Menu::handleKeyDown(StringHash eventType, VariantMap& eventData)
  163. {
  164. if ((!mEnabled) || (!mVisible))
  165. return;
  166. using namespace KeyDown;
  167. // Simulate a click if accelerator key pressed
  168. if ((eventData[P_KEY].getInt() == mAcceleratorKey) && (eventData[P_QUALIFIERS].getInt() == mAcceleratorQualifiers) &&
  169. (eventData[P_REPEAT].getBool() == false))
  170. onClick(getPosition(), getScreenPosition(), eventData[P_BUTTONS].getInt(), eventData[P_QUALIFIERS].getInt(), 0);
  171. }