Menu.cpp 13 KB

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