Menu.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "InputEvents.h"
  25. #include "Log.h"
  26. #include "Menu.h"
  27. #include "UI.h"
  28. #include "UIEvents.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. extern ShortStringHash VAR_ORIGIN;
  33. const ShortStringHash VAR_SHOW_POPUP("ShowPopup");
  34. OBJECTTYPESTATIC(Menu);
  35. Menu::Menu(Context* context) :
  36. Button(context),
  37. popupOffset_(IntVector2::ZERO),
  38. showPopup_(false),
  39. acceleratorKey_(0),
  40. acceleratorQualifiers_(0),
  41. autoPopup_(true)
  42. {
  43. SubscribeToEvent(this, E_PRESSED, HANDLER(Menu, HandlePressedReleased));
  44. SubscribeToEvent(this, E_RELEASED, HANDLER(Menu, HandlePressedReleased));
  45. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(Menu, HandleFocusChanged));
  46. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(Menu, HandleFocusChanged));
  47. }
  48. Menu::~Menu()
  49. {
  50. if (popup_)
  51. ShowPopup(false);
  52. }
  53. void Menu::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<Menu>();
  56. COPY_BASE_ATTRIBUTES(Menu, Button);
  57. REF_ACCESSOR_ATTRIBUTE(Menu, VAR_INTVECTOR2, "Popup Offset", GetPopupOffset, SetPopupOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  58. }
  59. void Menu::Update(float timeStep)
  60. {
  61. if (popup_ && showPopup_)
  62. {
  63. const Vector<SharedPtr<UIElement> >& children = popup_->GetChildren();
  64. for (unsigned i = 0; i < children.Size(); ++i)
  65. {
  66. Menu* menu = dynamic_cast<Menu*>(children[i].Get());
  67. if (menu && !menu->autoPopup_ && !menu->IsHovering())
  68. menu->autoPopup_ = true;
  69. }
  70. }
  71. }
  72. void Menu::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  73. {
  74. Button::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  75. Menu* sibling = static_cast<Menu*>(parent_->GetChild(VAR_SHOW_POPUP, true));
  76. if (popup_ && !showPopup_)
  77. {
  78. // Check if popup is shown by one of the siblings
  79. if (sibling)
  80. {
  81. // "Move" the popup from sibling menu to this menu
  82. sibling->ShowPopup(false);
  83. ShowPopup(true);
  84. return;
  85. }
  86. if (autoPopup_)
  87. {
  88. // Show popup when parent menu has its popup shown
  89. Menu* parentMenu = static_cast<Menu*>(parent_->GetVar(VAR_ORIGIN).GetPtr());
  90. if (parentMenu && parentMenu->showPopup_)
  91. ShowPopup(true);
  92. }
  93. }
  94. else
  95. {
  96. // Hide child menu popup when its parent is no longer being hovered
  97. if (sibling && sibling != this)
  98. sibling->ShowPopup(false);
  99. }
  100. }
  101. void Menu::OnShowPopup()
  102. {
  103. }
  104. bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile)
  105. {
  106. // Apply the style first, but only for non-internal elements
  107. if (!internal_ && styleFile)
  108. {
  109. // Use style override if defined, otherwise type name
  110. String styleName = source.GetAttribute("style");
  111. if (styleName.Empty())
  112. styleName = GetTypeName();
  113. SetStyle(styleFile, styleName);
  114. }
  115. // Then load rest of the attributes from the source
  116. if (!Serializable::LoadXML(source))
  117. return false;
  118. unsigned nextInternalChild = 0;
  119. // Load child elements. Internal elements are not to be created as they already exist
  120. XMLElement childElem = source.GetChild("element");
  121. while (childElem)
  122. {
  123. bool internalElem = childElem.GetBool("internal");
  124. bool popupElem = childElem.GetBool("popup");
  125. String typeName = childElem.GetAttribute("type");
  126. if (typeName.Empty())
  127. typeName = "UIElement";
  128. UIElement* child = 0;
  129. if (!internalElem)
  130. {
  131. if (!popupElem)
  132. child = CreateChild(typeName);
  133. else
  134. {
  135. // Do not add the popup element as a child even temporarily, as that can break layouts
  136. SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(typeName));
  137. if (!popup)
  138. LOGERROR("Could not create popup element type " + ShortStringHash(typeName).ToString());
  139. else
  140. {
  141. child = popup;
  142. SetPopup(popup);
  143. }
  144. }
  145. }
  146. else
  147. {
  148. // An internal popup element should already exist
  149. if (popupElem)
  150. child = popup_;
  151. else
  152. {
  153. for (unsigned i = nextInternalChild; i < children_.Size(); ++i)
  154. {
  155. if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName)
  156. {
  157. child = children_[i];
  158. nextInternalChild = i + 1;
  159. break;
  160. }
  161. }
  162. if (!child)
  163. LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName());
  164. }
  165. }
  166. if (child)
  167. {
  168. if (!child->LoadXML(childElem, styleFile))
  169. return false;
  170. }
  171. childElem = childElem.GetNext("element");
  172. }
  173. ApplyAttributes();
  174. return true;
  175. }
  176. bool Menu::SaveXML(XMLElement& dest)
  177. {
  178. // Write type and internal flag
  179. if (!dest.SetString("type", GetTypeName()))
  180. return false;
  181. if (internal_)
  182. {
  183. if (!dest.SetBool("internal", internal_))
  184. return false;
  185. }
  186. // Write attributes
  187. if (!Serializable::SaveXML(dest))
  188. return false;
  189. // Write child elements
  190. for (unsigned i = 0; i < children_.Size(); ++i)
  191. {
  192. UIElement* element = children_[i];
  193. XMLElement childElem = dest.CreateChild("element");
  194. if (!element->SaveXML(childElem))
  195. return false;
  196. }
  197. // Save the popup element as a "virtual" child element
  198. if (popup_)
  199. {
  200. XMLElement childElem = dest.CreateChild("element");
  201. childElem.SetBool("popup", true);
  202. if (!popup_->SaveXML(childElem))
  203. return false;
  204. }
  205. return true;
  206. }
  207. void Menu::SetPopup(UIElement* popup)
  208. {
  209. if (popup == this)
  210. return;
  211. if (popup_ && !popup)
  212. ShowPopup(false);
  213. popup_ = popup;
  214. // Detach from current parent (if any) to only show when it is time
  215. if (popup_)
  216. popup_->Remove();
  217. }
  218. void Menu::SetPopupOffset(const IntVector2& offset)
  219. {
  220. popupOffset_ = offset;
  221. }
  222. void Menu::SetPopupOffset(int x, int y)
  223. {
  224. popupOffset_ = IntVector2(x, y);
  225. }
  226. void Menu::ShowPopup(bool enable)
  227. {
  228. if (!popup_)
  229. return;
  230. if (enable)
  231. {
  232. // Find the UI root element for showing the popup
  233. UIElement* root = GetRoot();
  234. if (!root)
  235. return;
  236. OnShowPopup();
  237. if (popup_->GetParent() != root)
  238. root->AddChild(popup_);
  239. popup_->SetPosition(GetScreenPosition() + popupOffset_);
  240. popup_->SetVisible(true);
  241. popup_->SetVar(VAR_ORIGIN, (void*)this);
  242. popup_->BringToFront();
  243. }
  244. else
  245. {
  246. // If the popup has child menus, hide their popups as well
  247. PODVector<UIElement*> children;
  248. popup_->GetChildren(children, true);
  249. for (PODVector<UIElement*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  250. {
  251. Menu* menu = dynamic_cast<Menu*>(*i);
  252. if (menu)
  253. menu->ShowPopup(false);
  254. }
  255. popup_->SetVar(VAR_ORIGIN, Variant::EMPTY);
  256. popup_->SetVisible(false);
  257. popup_->Remove();
  258. }
  259. SetVar(VAR_SHOW_POPUP, enable);
  260. showPopup_ = enable;
  261. selected_ = enable;
  262. }
  263. void Menu::SetAccelerator(int key, int qualifiers)
  264. {
  265. acceleratorKey_ = key;
  266. acceleratorQualifiers_ = qualifiers;
  267. if (key)
  268. SubscribeToEvent(E_KEYDOWN, HANDLER(Menu, HandleKeyDown));
  269. else
  270. UnsubscribeFromEvent(E_KEYDOWN);
  271. }
  272. void Menu::HandlePressedReleased(StringHash eventType, VariantMap& eventData)
  273. {
  274. // If this menu shows a sublevel popup, react to button press. Else react to release
  275. if (eventType == E_PRESSED)
  276. {
  277. if (!popup_)
  278. return;
  279. }
  280. if (eventType == E_RELEASED)
  281. {
  282. if (popup_)
  283. return;
  284. }
  285. // Manual handling of the popup, so switch off the auto popup flag
  286. autoPopup_ = false;
  287. // Toggle popup visibility if exists
  288. ShowPopup(!showPopup_);
  289. // Send event on each click if no popup, or whenever the popup is opened
  290. if (!popup_ || showPopup_)
  291. {
  292. using namespace MenuSelected;
  293. VariantMap newEventData;
  294. newEventData[P_ELEMENT] = (void*)this;
  295. SendEvent(E_MENUSELECTED, newEventData);
  296. }
  297. }
  298. void Menu::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  299. {
  300. if (!showPopup_)
  301. return;
  302. using namespace FocusChanged;
  303. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  304. UIElement* root = GetRoot();
  305. // If another element was focused due to the menu button being clicked, do not hide the popup
  306. if (eventType == E_FOCUSCHANGED && static_cast<UIElement*>(eventData[P_CLICKEDELEMENT].GetPtr()))
  307. return;
  308. // If clicked emptiness or defocused, hide the popup
  309. if (!element)
  310. {
  311. ShowPopup(false);
  312. return;
  313. }
  314. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  315. // In that case, do not hide
  316. while (element)
  317. {
  318. if (element == this || element == popup_)
  319. return;
  320. if (element->GetParent() == root)
  321. element = static_cast<UIElement*>(element->GetVar(VAR_ORIGIN).GetPtr());
  322. else
  323. element = element->GetParent();
  324. }
  325. ShowPopup(false);
  326. }
  327. void Menu::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  328. {
  329. if (!enabled_)
  330. return;
  331. using namespace KeyDown;
  332. // Activate if accelerator key pressed
  333. if (eventData[P_KEY].GetInt() == acceleratorKey_ && (acceleratorQualifiers_ == QUAL_ANY || eventData[P_QUALIFIERS].GetInt() ==
  334. acceleratorQualifiers_) && eventData[P_REPEAT].GetBool() == false)
  335. {
  336. // Ignore if UI has modal element
  337. UI* ui = GetSubsystem<UI>();
  338. if (ui->GetModalElement())
  339. return;
  340. HandlePressedReleased(eventType, eventData);
  341. }
  342. }
  343. }