Menu.cpp 13 KB

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