Menu.cpp 12 KB

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