Menu.cpp 13 KB

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