Menu.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "Context.h"
  25. #include "InputEvents.h"
  26. #include "Menu.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. static const ShortStringHash originHash("Origin");
  30. OBJECTTYPESTATIC(Menu);
  31. Menu::Menu(Context* context) :
  32. Button(context),
  33. popupOffset_(IntVector2::ZERO),
  34. showPopup_(false),
  35. acceleratorKey_(0),
  36. acceleratorQualifiers_(0)
  37. {
  38. SubscribeToEvent(this, E_PRESSED, HANDLER(Menu, HandlePressedReleased));
  39. SubscribeToEvent(this, E_RELEASED, HANDLER(Menu, HandlePressedReleased));
  40. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(Menu, HandleFocusChanged));
  41. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(Menu, HandleFocusChanged));
  42. }
  43. Menu::~Menu()
  44. {
  45. if (popup_)
  46. {
  47. ShowPopup(false);
  48. // Make sure the popup is removed from hierarchy if still visible
  49. UIElement* parent = popup_->GetParent();
  50. if (parent)
  51. {
  52. popup_->vars_[originHash].Clear();
  53. parent->RemoveChild(popup_);
  54. }
  55. }
  56. }
  57. void Menu::RegisterObject(Context* context)
  58. {
  59. context->RegisterFactory<Menu>();
  60. }
  61. void Menu::SetStyle(const XMLElement& element)
  62. {
  63. Button::SetStyle(element);
  64. XMLElement popupElem = element.GetChildElement("popup");
  65. if ((popupElem) && (popupElem.HasAttribute("name")))
  66. {
  67. UIElement* root = GetRootElement();
  68. if (root)
  69. SetPopup(root->GetChild(popupElem.GetString("name"), true));
  70. }
  71. if (element.HasChildElement("popupoffset"))
  72. SetPopupOffset(element.GetChildElement("popupoffset").GetIntVector2("value"));
  73. }
  74. void Menu::OnShowPopup()
  75. {
  76. }
  77. void Menu::SetPopup(UIElement* popup)
  78. {
  79. if (popup == this)
  80. return;
  81. if ((popup_) && (!popup))
  82. ShowPopup(false);
  83. popup_ = popup;
  84. // Detach from current parent (if any) to only show when it is time
  85. if (popup_)
  86. {
  87. UIElement* parent = popup_->GetParent();
  88. if (parent)
  89. parent->RemoveChild(popup_);
  90. }
  91. }
  92. void Menu::SetPopupOffset(const IntVector2& offset)
  93. {
  94. popupOffset_ = offset;
  95. }
  96. void Menu::SetPopupOffset(int x, int y)
  97. {
  98. popupOffset_ = IntVector2(x, y);
  99. }
  100. void Menu::ShowPopup(bool enable)
  101. {
  102. if (!popup_)
  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. OnShowPopup();
  111. popup_->SetPosition(GetScreenPosition() + popupOffset_);
  112. popup_->SetVisible(true);
  113. popup_->vars_[originHash] = (void*)this;
  114. root->AddChild(popup_);
  115. // Set fixed high priority
  116. popup_->SetBringToFront(false);
  117. popup_->SetBringToBack(false);
  118. popup_->BringToFront();
  119. }
  120. else
  121. {
  122. popup_->vars_[originHash].Clear();
  123. root->RemoveChild(popup_);
  124. }
  125. showPopup_ = enable;
  126. selected_ = enable;
  127. }
  128. void Menu::SetAccelerator(int key, int qualifiers)
  129. {
  130. acceleratorKey_ = key;
  131. acceleratorQualifiers_ = qualifiers;
  132. if (key)
  133. SubscribeToEvent(E_KEYDOWN, HANDLER(Menu, HandleKeyDown));
  134. else
  135. UnsubscribeFromEvent(E_KEYDOWN);
  136. }
  137. void Menu::HandlePressedReleased(StringHash eventType, VariantMap& eventData)
  138. {
  139. // If this menu shows a sublevel popup, react to button press. Else react to release
  140. if (eventType == E_PRESSED)
  141. {
  142. if (!popup_)
  143. return;
  144. }
  145. if (eventType == E_RELEASED)
  146. {
  147. if (popup_)
  148. return;
  149. }
  150. // Toggle popup visibility if exists
  151. ShowPopup(!showPopup_);
  152. // Send event on each click if no popup, or whenever the popup is opened
  153. if ((!popup_) || (showPopup_))
  154. {
  155. using namespace MenuSelected;
  156. VariantMap newEventData;
  157. newEventData[P_ELEMENT] = (void*)this;
  158. SendEvent(E_MENUSELECTED, newEventData);
  159. }
  160. }
  161. void Menu::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  162. {
  163. if (!showPopup_)
  164. return;
  165. using namespace FocusChanged;
  166. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  167. UIElement* root = GetRootElement();
  168. // If another element was focused due to the menu button being clicked, do not hide the popup
  169. if ((eventType == E_FOCUSCHANGED) && (static_cast<UIElement*>(eventData[P_ORIGINALELEMENT].GetPtr())))
  170. return;
  171. // If clicked emptiness or defocused, hide the popup
  172. if (!element)
  173. {
  174. ShowPopup(false);
  175. return;
  176. }
  177. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  178. // In that case, do not hide
  179. while (element)
  180. {
  181. if ((element == this) || (element == popup_))
  182. return;
  183. if (element->GetParent() == root)
  184. element = static_cast<UIElement*>(element->vars_[originHash].GetPtr());
  185. else
  186. element = element->GetParent();
  187. }
  188. ShowPopup(false);
  189. }
  190. void Menu::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  191. {
  192. if (!active_)
  193. return;
  194. using namespace KeyDown;
  195. // Activate if accelerator key pressed
  196. if ((eventData[P_KEY].GetInt() == acceleratorKey_) && (eventData[P_QUALIFIERS].GetInt() == acceleratorQualifiers_) &&
  197. (eventData[P_REPEAT].GetBool() == false))
  198. HandlePressedReleased(eventType, eventData);
  199. }