Menu.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. {
  34. subscribeToEvent(EVENT_UIMOUSECLICK, EVENT_HANDLER(Menu, handleUIMouseClick));
  35. }
  36. Menu::~Menu()
  37. {
  38. if (mPopup)
  39. mPopup->getUserData()[originHash].clear();
  40. }
  41. void Menu::setStyle(const XMLElement& element, ResourceCache* cache)
  42. {
  43. Button::setStyle(element, cache);
  44. XMLElement popupElem = element.getChildElement("popup");
  45. if ((popupElem) && (popupElem.hasAttribute("name")))
  46. {
  47. UIElement* root = getRootElement();
  48. if (root)
  49. setPopup(root->getChild(popupElem.getString("name"), true));
  50. }
  51. if (element.hasChildElement("popupoffset"))
  52. setPopupOffset(element.getChildElement("popupoffset").getIntVector2("value"));
  53. }
  54. void Menu::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  55. {
  56. setPressed(true);
  57. // Toggle popup visibility if exists
  58. showPopup(!mShowPopup);
  59. // Send event on each click if no popup, or whenever the popup is opened
  60. if ((!mPopup) || (mShowPopup))
  61. {
  62. using namespace MenuSelected;
  63. VariantMap eventData;
  64. eventData[P_ELEMENT] = (void*)this;
  65. sendEvent(EVENT_MENUSELECTED, eventData);
  66. }
  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::handleUIMouseClick(StringHash eventType, VariantMap& eventData)
  123. {
  124. if (!mShowPopup)
  125. return;
  126. using namespace UIMouseClick;
  127. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].getPtr());
  128. UIElement* root = getRootElement();
  129. // If clicked emptiness, hide the popup
  130. if (!element)
  131. {
  132. showPopup(false);
  133. return;
  134. }
  135. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  136. // In that case, do not hide
  137. while (element)
  138. {
  139. if ((element == this) || (element == mPopup))
  140. return;
  141. if (element->getParent() == root)
  142. element = static_cast<UIElement*>(element->getUserData()[originHash].getPtr());
  143. else
  144. element = element->getParent();
  145. }
  146. showPopup(false);
  147. }