Menu.cpp 13 KB

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