Menu.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. Menu::Menu(const std::string& name) :
  29. Button(name),
  30. mPopupOffset(IntVector2::sZero),
  31. mShowPopup(false)
  32. {
  33. subscribeToEvent(EVENT_TRYFOCUS, EVENT_HANDLER(Menu, handleTryFocus));
  34. }
  35. Menu::~Menu()
  36. {
  37. if (mPopup)
  38. mPopup->setOrigin(0);
  39. }
  40. void Menu::setStyle(const XMLElement& element, ResourceCache* cache)
  41. {
  42. Button::setStyle(element, cache);
  43. XMLElement popupElem = element.getChildElement("popup");
  44. if ((popupElem) && (popupElem.hasAttribute("name")))
  45. {
  46. UIElement* root = getRootElement();
  47. if (root)
  48. setPopup(root->getChild(element.getChildElement("popup").getString("name"), true));
  49. }
  50. if (element.hasChildElement("resizepopup"))
  51. setResizePopup(element.getChildElement("resizepopup").getBool("enable"));
  52. if (element.hasChildElement("popupoffset"))
  53. setPopupOffset(element.getChildElement("popupoffset").getIntVector2("value"));
  54. }
  55. void Menu::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  56. {
  57. setPressed(true);
  58. // Toggle popup visibility if exists
  59. showPopup(!mShowPopup);
  60. // Send event on each click if no popup, or whenever the popup is opened
  61. if ((!mPopup) || (mShowPopup))
  62. {
  63. using namespace MenuSelected;
  64. VariantMap eventData;
  65. eventData[P_ELEMENT] = (void*)this;
  66. sendEvent(EVENT_MENUSELECTED, eventData);
  67. }
  68. }
  69. void Menu::onResize()
  70. {
  71. if ((mPopup) && (mResizePopup))
  72. mPopup->setWidth(getWidth());
  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::setResizePopup(bool enable)
  98. {
  99. if (enable != mResizePopup)
  100. {
  101. mResizePopup = enable;
  102. onResize();
  103. }
  104. }
  105. void Menu::showPopup(bool enable)
  106. {
  107. if (!mPopup)
  108. return;
  109. // Find the UI root element for showing the popup
  110. UIElement* root = getRootElement();
  111. if (!root)
  112. return;
  113. if (enable)
  114. {
  115. mPopup->setPosition(getScreenPosition() + mPopupOffset);
  116. mPopup->setVisible(true);
  117. mPopup->setOrigin(this);
  118. root->addChild(mPopup);
  119. // Set fixed high priority
  120. mPopup->setBringToFront(false);
  121. mPopup->setBringToBack(false);
  122. mPopup->bringToFront();
  123. }
  124. else
  125. {
  126. mPopup->setOrigin(0);
  127. root->removeChild(mPopup);
  128. }
  129. mShowPopup = enable;
  130. mSelected = enable;
  131. }
  132. void Menu::handleTryFocus(StringHash eventType, VariantMap& eventData)
  133. {
  134. if (!mShowPopup)
  135. return;
  136. using namespace TryFocus;
  137. UIElement* focusElement = static_cast<UIElement*>(eventData[P_ELEMENT].getPtr());
  138. UIElement* root = getRootElement();
  139. // If global defocus, hide the popup
  140. if (!focusElement)
  141. {
  142. showPopup(false);
  143. return;
  144. }
  145. // Otherwise see if the focused element has either the menu item or the popup in its parent chain.
  146. // In that case, do not hide
  147. while (focusElement)
  148. {
  149. if ((focusElement == this) || (focusElement == mPopup))
  150. return;
  151. if (focusElement->getParent() == root)
  152. focusElement = focusElement->getOrigin();
  153. else
  154. focusElement = focusElement->getParent();
  155. }
  156. showPopup(false);
  157. }