2
0

Menu.cpp 12 KB

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