Menu.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 "Window.h"
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. extern ShortStringHash VAR_ORIGIN;
  34. const ShortStringHash VAR_SHOW_POPUP("ShowPopup");
  35. OBJECTTYPESTATIC(Menu);
  36. Menu::Menu(Context* context) :
  37. Button(context),
  38. popupOffset_(IntVector2::ZERO),
  39. showPopup_(false),
  40. acceleratorKey_(0),
  41. acceleratorQualifiers_(0),
  42. autoPopup_(true)
  43. {
  44. SubscribeToEvent(this, E_PRESSED, HANDLER(Menu, HandlePressedReleased));
  45. SubscribeToEvent(this, E_RELEASED, HANDLER(Menu, HandlePressedReleased));
  46. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(Menu, HandleFocusChanged));
  47. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(Menu, HandleFocusChanged));
  48. }
  49. Menu::~Menu()
  50. {
  51. if (popup_ && showPopup_)
  52. ShowPopup(false);
  53. }
  54. void Menu::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<Menu>();
  57. COPY_BASE_ATTRIBUTES(Menu, Button);
  58. REF_ACCESSOR_ATTRIBUTE(Menu, VAR_INTVECTOR2, "Popup Offset", GetPopupOffset, SetPopupOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  59. }
  60. void Menu::Update(float timeStep)
  61. {
  62. if (popup_ && showPopup_)
  63. {
  64. const Vector<SharedPtr<UIElement> >& children = popup_->GetChildren();
  65. for (unsigned i = 0; i < children.Size(); ++i)
  66. {
  67. Menu* menu = dynamic_cast<Menu*>(children[i].Get());
  68. if (menu && !menu->autoPopup_ && !menu->IsHovering())
  69. menu->autoPopup_ = true;
  70. }
  71. }
  72. }
  73. void Menu::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  74. {
  75. Button::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  76. Menu* sibling = static_cast<Menu*>(parent_->GetChild(VAR_SHOW_POPUP, true));
  77. if (popup_ && !showPopup_)
  78. {
  79. // Check if popup is shown by one of the siblings
  80. if (sibling)
  81. {
  82. // "Move" the popup from sibling menu to this menu
  83. sibling->ShowPopup(false);
  84. ShowPopup(true);
  85. return;
  86. }
  87. if (autoPopup_)
  88. {
  89. // Show popup when parent menu has its popup shown
  90. Menu* parentMenu = static_cast<Menu*>(parent_->GetVar(VAR_ORIGIN).GetPtr());
  91. if (parentMenu && parentMenu->showPopup_)
  92. ShowPopup(true);
  93. }
  94. }
  95. else
  96. {
  97. // Hide child menu popup when its parent is no longer being hovered
  98. if (sibling && sibling != this)
  99. sibling->ShowPopup(false);
  100. }
  101. }
  102. void Menu::OnShowPopup()
  103. {
  104. }
  105. bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile, bool setInstanceDefault)
  106. {
  107. // Get style override if defined
  108. String styleName = source.GetAttribute("style");
  109. // Apply the style first, if the style file is available
  110. if (styleFile)
  111. {
  112. // If not defined, use type name
  113. if (styleName.Empty())
  114. styleName = GetTypeName();
  115. SetStyle(styleFile, styleName);
  116. }
  117. // The 'style' attribute value in the style file cannot be equals to original's applied style to prevent infinite loop
  118. else if (!styleName.Empty() && styleName != appliedStyle_)
  119. {
  120. // Attempt to use the default style file
  121. styleFile = GetDefaultStyle();
  122. if (styleFile)
  123. {
  124. // Remember the original applied style
  125. String appliedStyle(appliedStyle_);
  126. SetStyle(styleFile, styleName);
  127. appliedStyle_ = appliedStyle;
  128. }
  129. }
  130. // Then load rest of the attributes from the source
  131. if (!Serializable::LoadXML(source, setInstanceDefault))
  132. return false;
  133. unsigned nextInternalChild = 0;
  134. // Load child elements. Internal elements are not to be created as they already exist
  135. XMLElement childElem = source.GetChild("element");
  136. while (childElem)
  137. {
  138. bool internalElem = childElem.GetBool("internal");
  139. bool popupElem = childElem.GetBool("popup");
  140. String typeName = childElem.GetAttribute("type");
  141. if (typeName.Empty())
  142. typeName = "UIElement";
  143. unsigned index = childElem.HasAttribute("index") ? childElem.GetUInt("index") : M_MAX_UNSIGNED;
  144. UIElement* child = 0;
  145. if (!internalElem)
  146. {
  147. if (!popupElem)
  148. child = CreateChild(typeName, String::EMPTY, index);
  149. else
  150. {
  151. // Do not add the popup element as a child even temporarily, as that can break layouts
  152. SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(typeName));
  153. if (!popup)
  154. LOGERROR("Could not create popup element type " + typeName);
  155. else
  156. {
  157. child = popup;
  158. SetPopup(popup);
  159. }
  160. }
  161. }
  162. else
  163. {
  164. // An internal popup element should already exist
  165. if (popupElem)
  166. child = popup_;
  167. else
  168. {
  169. for (unsigned i = nextInternalChild; i < children_.Size(); ++i)
  170. {
  171. if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName)
  172. {
  173. child = children_[i];
  174. nextInternalChild = i + 1;
  175. break;
  176. }
  177. }
  178. if (!child)
  179. LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName());
  180. }
  181. }
  182. if (child)
  183. {
  184. if (!styleFile)
  185. styleFile = GetDefaultStyle();
  186. // As popup is not a child element in itself, the parental chain to acquire the default style file is broken for popup's child elements
  187. // To recover from this, popup needs to have the default style set in its own instance so the popup's child elements can find it later
  188. if (popupElem)
  189. child->SetDefaultStyle(styleFile);
  190. if (!child->LoadXML(childElem, styleFile, setInstanceDefault))
  191. return false;
  192. }
  193. childElem = childElem.GetNext("element");
  194. }
  195. ApplyAttributes();
  196. return true;
  197. }
  198. bool Menu::SaveXML(XMLElement& dest) const
  199. {
  200. if (!Button::SaveXML(dest))
  201. return false;
  202. // Save the popup element as a "virtual" child element
  203. if (popup_)
  204. {
  205. XMLElement childElem = dest.CreateChild("element");
  206. childElem.SetBool("popup", true);
  207. if (!popup_->SaveXML(childElem))
  208. return false;
  209. // Filter popup implicit attributes
  210. if (!FilterPopupImplicitAttributes(childElem))
  211. {
  212. LOGERROR("Could not remove popup implicit attributes");
  213. return false;
  214. }
  215. }
  216. return true;
  217. }
  218. void Menu::SetPopup(UIElement* popup)
  219. {
  220. if (popup == this)
  221. return;
  222. // Currently only allow popup 'window'
  223. if (popup->GetType() != Window::GetTypeStatic())
  224. {
  225. LOGERROR("Could not set popup element of type " + popup->GetTypeName() + ", only support popup window for now");
  226. return;
  227. }
  228. if (popup_ && !popup)
  229. ShowPopup(false);
  230. popup_ = popup;
  231. // Detach from current parent (if any) to only show when it is time
  232. if (popup_)
  233. popup_->Remove();
  234. }
  235. void Menu::SetPopupOffset(const IntVector2& offset)
  236. {
  237. popupOffset_ = offset;
  238. }
  239. void Menu::SetPopupOffset(int x, int y)
  240. {
  241. popupOffset_ = IntVector2(x, y);
  242. }
  243. void Menu::ShowPopup(bool enable)
  244. {
  245. if (!popup_)
  246. return;
  247. if (enable)
  248. {
  249. OnShowPopup();
  250. popup_->SetVar(VAR_ORIGIN, (void*)this);
  251. static_cast<Window*>(popup_.Get())->SetModal(true);
  252. popup_->SetPosition(GetScreenPosition() + popupOffset_);
  253. popup_->SetVisible(true);
  254. popup_->BringToFront();
  255. }
  256. else
  257. {
  258. // If the popup has child menus, hide their popups as well
  259. PODVector<UIElement*> children;
  260. popup_->GetChildren(children, true);
  261. for (PODVector<UIElement*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  262. {
  263. Menu* menu = dynamic_cast<Menu*>(*i);
  264. if (menu)
  265. menu->ShowPopup(false);
  266. }
  267. static_cast<Window*>(popup_.Get())->SetModal(false);
  268. const_cast<VariantMap&>(popup_->GetVars()).Erase(VAR_ORIGIN);
  269. popup_->SetVisible(false);
  270. popup_->Remove();
  271. }
  272. SetVar(VAR_SHOW_POPUP, enable);
  273. showPopup_ = enable;
  274. selected_ = enable;
  275. }
  276. void Menu::SetAccelerator(int key, int qualifiers)
  277. {
  278. acceleratorKey_ = key;
  279. acceleratorQualifiers_ = qualifiers;
  280. if (key)
  281. SubscribeToEvent(E_KEYDOWN, HANDLER(Menu, HandleKeyDown));
  282. else
  283. UnsubscribeFromEvent(E_KEYDOWN);
  284. }
  285. bool Menu::FilterPopupImplicitAttributes(XMLElement& dest) const
  286. {
  287. if (!RemoveChildXML(dest, "Position"))
  288. return false;
  289. if (!RemoveChildXML(dest, "Is Visible"))
  290. return false;
  291. return true;
  292. }
  293. void Menu::HandlePressedReleased(StringHash eventType, VariantMap& eventData)
  294. {
  295. // If this menu shows a sublevel popup, react to button press. Else react to release
  296. if (eventType == E_PRESSED)
  297. {
  298. if (!popup_)
  299. return;
  300. }
  301. if (eventType == E_RELEASED)
  302. {
  303. if (popup_)
  304. return;
  305. }
  306. // Manual handling of the popup, so switch off the auto popup flag
  307. autoPopup_ = false;
  308. // Toggle popup visibility if exists
  309. ShowPopup(!showPopup_);
  310. // Send event on each click if no popup, or whenever the popup is opened
  311. if (!popup_ || showPopup_)
  312. {
  313. using namespace MenuSelected;
  314. VariantMap newEventData;
  315. newEventData[P_ELEMENT] = (void*)this;
  316. SendEvent(E_MENUSELECTED, newEventData);
  317. }
  318. }
  319. void Menu::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  320. {
  321. if (!showPopup_)
  322. return;
  323. using namespace FocusChanged;
  324. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  325. UIElement* root = GetRoot();
  326. // If another element was focused due to the menu button being clicked, do not hide the popup
  327. if (eventType == E_FOCUSCHANGED && static_cast<UIElement*>(eventData[P_CLICKEDELEMENT].GetPtr()))
  328. return;
  329. // If clicked emptiness or defocused, hide the popup
  330. if (!element)
  331. {
  332. ShowPopup(false);
  333. return;
  334. }
  335. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  336. // In that case, do not hide
  337. while (element)
  338. {
  339. if (element == this || element == popup_)
  340. return;
  341. if (element->GetParent() == root)
  342. element = static_cast<UIElement*>(element->GetVar(VAR_ORIGIN).GetPtr());
  343. else
  344. element = element->GetParent();
  345. }
  346. ShowPopup(false);
  347. }
  348. void Menu::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  349. {
  350. if (!enabled_)
  351. return;
  352. using namespace KeyDown;
  353. // Activate if accelerator key pressed
  354. if (eventData[P_KEY].GetInt() == acceleratorKey_ && (acceleratorQualifiers_ == QUAL_ANY || eventData[P_QUALIFIERS].GetInt() ==
  355. acceleratorQualifiers_) && eventData[P_REPEAT].GetBool() == false)
  356. {
  357. // Ignore if UI has modal element
  358. UI* ui = GetSubsystem<UI>();
  359. if (ui->HasModalElement())
  360. return;
  361. HandlePressedReleased(eventType, eventData);
  362. }
  363. }
  364. }