MenuItem.cpp 5.0 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 "MenuItem.h"
  26. #include "UIEvents.h"
  27. #include "DebugNew.h"
  28. MenuItem::MenuItem(const std::string& name) :
  29. Button(name),
  30. mPopupOffset(IntVector2::sZero),
  31. mShowPopup(false)
  32. {
  33. subscribeToEvent(EVENT_TRYFOCUS, EVENT_HANDLER(MenuItem, handleTryFocus));
  34. }
  35. MenuItem::~MenuItem()
  36. {
  37. if (mPopup)
  38. mPopup->setOrigin(0);
  39. }
  40. void MenuItem::setStyle(const XMLElement& element, ResourceCache* cache)
  41. {
  42. Button::setStyle(element, cache);
  43. if (element.hasChildElement("popup"))
  44. {
  45. UIElement* root = getRootElement();
  46. if (root)
  47. setPopup(root->getChild(element.getChildElement("popup").getString("name"), true));
  48. }
  49. if (element.hasChildElement("popupoffset"))
  50. setPopupOffset(element.getChildElement("popupoffset").getIntVector2("value"));
  51. }
  52. void MenuItem::update(float timeStep)
  53. {
  54. // Keep pressed state if showing the popup
  55. if ((!mHovering) && (!mShowPopup))
  56. setPressed(false);
  57. }
  58. void MenuItem::onHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  59. {
  60. // Keep pressed state if showing the popup
  61. setPressed(((buttons & MOUSEB_LEFT) != 0) || (mShowPopup));
  62. mHovering = true;
  63. }
  64. void MenuItem::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  65. {
  66. setPressed(true);
  67. // Toggle popup visibility if exists
  68. showPopup(!mShowPopup);
  69. // Send event on each click if no popup, or whenever the popup is opened
  70. if ((!mPopup) || (mShowPopup))
  71. {
  72. using namespace ItemSelected;
  73. VariantMap eventData;
  74. eventData[P_ELEMENT] = (void*)this;
  75. sendEvent(EVENT_ITEMSELECTED, eventData);
  76. }
  77. }
  78. void MenuItem::setPopup(UIElement* popup)
  79. {
  80. if (popup == this)
  81. return;
  82. if ((mPopup) && (!popup))
  83. showPopup(false);
  84. mPopup = popup;
  85. // Detach from current parent (if any) to only show when it is time
  86. if (mPopup)
  87. {
  88. UIElement* parent = mPopup->getParent();
  89. parent->removeChild(mPopup);
  90. }
  91. }
  92. void MenuItem::setPopupOffset(const IntVector2& offset)
  93. {
  94. mPopupOffset = offset;
  95. }
  96. void MenuItem::setPopupOffset(int x, int y)
  97. {
  98. mPopupOffset = IntVector2(x, y);
  99. }
  100. void MenuItem::showPopup(bool enable)
  101. {
  102. if (!mPopup)
  103. return;
  104. // Find the UI root element for showing the popup
  105. UIElement* root = getRootElement();
  106. if (!root)
  107. return;
  108. if (enable)
  109. {
  110. mPopup->setPosition(getScreenPosition() + mPopupOffset);
  111. mPopup->setVisible(true);
  112. mPopup->setOrigin(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->setOrigin(0);
  122. root->removeChild(mPopup);
  123. }
  124. mShowPopup = enable;
  125. }
  126. void MenuItem::handleTryFocus(StringHash eventType, VariantMap& eventData)
  127. {
  128. if (!mShowPopup)
  129. return;
  130. using namespace TryFocus;
  131. UIElement* focusElement = static_cast<UIElement*>(eventData[P_ELEMENT].getPtr());
  132. UIElement* root = getRootElement();
  133. // If global defocus, hide the popup
  134. if (!focusElement)
  135. {
  136. showPopup(false);
  137. return;
  138. }
  139. // Otherwise see if the focused element has either the menu item or the popup in its parent chain.
  140. // In that case, do not hide
  141. while (focusElement)
  142. {
  143. if ((focusElement == this) || (focusElement == mPopup))
  144. return;
  145. if (focusElement->getParent() == root)
  146. focusElement = focusElement->getOrigin();
  147. else
  148. focusElement = focusElement->getParent();
  149. }
  150. showPopup(false);
  151. }