Menu.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(this, EVENT_PRESSED, EVENT_HANDLER(Menu, handlePressedReleased));
  37. subscribeToEvent(this, EVENT_RELEASED, EVENT_HANDLER(Menu, handlePressedReleased));
  38. subscribeToEvent(EVENT_UIMOUSECLICK, EVENT_HANDLER(Menu, handleFocusChanged));
  39. subscribeToEvent(EVENT_FOCUSCHANGED, EVENT_HANDLER(Menu, handleFocusChanged));
  40. }
  41. Menu::~Menu()
  42. {
  43. if (mPopup)
  44. {
  45. showPopup(false);
  46. // Make sure the popup is removed from hierarchy if still visible
  47. UIElement* parent = mPopup->getParent();
  48. if (parent)
  49. {
  50. mPopup->getUserData()[originHash].clear();
  51. parent->removeChild(mPopup);
  52. }
  53. }
  54. }
  55. void Menu::setStyle(const XMLElement& element, ResourceCache* cache)
  56. {
  57. Button::setStyle(element, cache);
  58. XMLElement popupElem = element.getChildElement("popup");
  59. if ((popupElem) && (popupElem.hasAttribute("name")))
  60. {
  61. UIElement* root = getRootElement();
  62. if (root)
  63. setPopup(root->getChild(popupElem.getString("name"), true));
  64. }
  65. if (element.hasChildElement("popupoffset"))
  66. setPopupOffset(element.getChildElement("popupoffset").getIntVector2("value"));
  67. }
  68. void Menu::onShowPopup()
  69. {
  70. }
  71. void Menu::setPopup(UIElement* popup)
  72. {
  73. if (popup == this)
  74. return;
  75. if ((mPopup) && (!popup))
  76. showPopup(false);
  77. mPopup = popup;
  78. // Detach from current parent (if any) to only show when it is time
  79. if (mPopup)
  80. {
  81. UIElement* parent = mPopup->getParent();
  82. if (parent)
  83. parent->removeChild(mPopup);
  84. }
  85. }
  86. void Menu::setPopupOffset(const IntVector2& offset)
  87. {
  88. mPopupOffset = offset;
  89. }
  90. void Menu::setPopupOffset(int x, int y)
  91. {
  92. mPopupOffset = IntVector2(x, y);
  93. }
  94. void Menu::showPopup(bool enable)
  95. {
  96. if (!mPopup)
  97. return;
  98. // Find the UI root element for showing the popup
  99. UIElement* root = getRootElement();
  100. if (!root)
  101. return;
  102. if (enable)
  103. {
  104. onShowPopup();
  105. mPopup->setPosition(getScreenPosition() + mPopupOffset);
  106. mPopup->setVisible(true);
  107. mPopup->getUserData()[originHash] = (void*)this;
  108. root->addChild(mPopup);
  109. // Set fixed high priority
  110. mPopup->setBringToFront(false);
  111. mPopup->setBringToBack(false);
  112. mPopup->bringToFront();
  113. }
  114. else
  115. {
  116. mPopup->getUserData()[originHash].clear();
  117. root->removeChild(mPopup);
  118. }
  119. mShowPopup = enable;
  120. mSelected = enable;
  121. }
  122. void Menu::setAccelerator(int key, int qualifiers)
  123. {
  124. mAcceleratorKey = key;
  125. mAcceleratorQualifiers = qualifiers;
  126. if (key)
  127. subscribeToEvent(EVENT_KEYDOWN, EVENT_HANDLER(Menu, handleKeyDown));
  128. else
  129. unsubscribeFromEvent(EVENT_KEYDOWN);
  130. }
  131. void Menu::handlePressedReleased(StringHash eventType, VariantMap& eventData)
  132. {
  133. // If this menu shows a sublevel popup, react to button press. Else react to release
  134. if (eventType == EVENT_PRESSED)
  135. {
  136. if (!mPopup)
  137. return;
  138. }
  139. if (eventType == EVENT_RELEASED)
  140. {
  141. if (mPopup)
  142. return;
  143. }
  144. // Toggle popup visibility if exists
  145. showPopup(!mShowPopup);
  146. // Send event on each click if no popup, or whenever the popup is opened
  147. if ((!mPopup) || (mShowPopup))
  148. {
  149. using namespace MenuSelected;
  150. VariantMap newEventData;
  151. newEventData[P_ELEMENT] = (void*)this;
  152. sendEvent(EVENT_MENUSELECTED, newEventData);
  153. }
  154. }
  155. void Menu::handleFocusChanged(StringHash eventType, VariantMap& eventData)
  156. {
  157. if (!mShowPopup)
  158. return;
  159. using namespace FocusChanged;
  160. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].getPtr());
  161. UIElement* root = getRootElement();
  162. // If another element was focused due to the menu button being clicked, do not hide the popup
  163. if ((eventType == EVENT_FOCUSCHANGED) && (static_cast<UIElement*>(eventData[P_ORIGINALELEMENT].getPtr())))
  164. return;
  165. // If clicked emptiness or defocused, hide the popup
  166. if (!element)
  167. {
  168. showPopup(false);
  169. return;
  170. }
  171. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  172. // In that case, do not hide
  173. while (element)
  174. {
  175. if ((element == this) || (element == mPopup))
  176. return;
  177. if (element->getParent() == root)
  178. element = static_cast<UIElement*>(element->getUserData()[originHash].getPtr());
  179. else
  180. element = element->getParent();
  181. }
  182. showPopup(false);
  183. }
  184. void Menu::handleKeyDown(StringHash eventType, VariantMap& eventData)
  185. {
  186. if (!mEnabled)
  187. return;
  188. using namespace KeyDown;
  189. // Activate if accelerator key pressed
  190. if ((eventData[P_KEY].getInt() == mAcceleratorKey) && (eventData[P_QUALIFIERS].getInt() == mAcceleratorQualifiers) &&
  191. (eventData[P_REPEAT].getBool() == false))
  192. handlePressedReleased(eventType, eventData);
  193. }