Menu.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. static const ShortStringHash originHash("Origin");
  33. OBJECTTYPESTATIC(Menu);
  34. Menu::Menu(Context* context) :
  35. Button(context),
  36. popupOffset_(IntVector2::ZERO),
  37. showPopup_(false),
  38. acceleratorKey_(0),
  39. acceleratorQualifiers_(0)
  40. {
  41. SubscribeToEvent(this, E_PRESSED, HANDLER(Menu, HandlePressedReleased));
  42. SubscribeToEvent(this, E_RELEASED, HANDLER(Menu, HandlePressedReleased));
  43. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(Menu, HandleFocusChanged));
  44. SubscribeToEvent(E_FOCUSCHANGED, HANDLER(Menu, HandleFocusChanged));
  45. }
  46. Menu::~Menu()
  47. {
  48. if (popup_)
  49. ShowPopup(false);
  50. }
  51. void Menu::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<Menu>();
  54. COPY_BASE_ATTRIBUTES(Menu, Button);
  55. REF_ACCESSOR_ATTRIBUTE(Menu, VAR_INTVECTOR2, "Popup Offset", GetPopupOffset, SetPopupOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  56. }
  57. void Menu::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  58. {
  59. Button::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  60. if (popup_ && !showPopup_)
  61. {
  62. // Check if popup is shown by one of the siblings
  63. Vector<SharedPtr<UIElement> > children = GetParent()->GetChildren();
  64. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin(); i != children.End(); ++i)
  65. {
  66. if ((*i) != this && (*i)->GetType() == Menu::GetTypeStatic())
  67. {
  68. Menu* sibling = static_cast<Menu*>(i->Get());
  69. if (sibling->showPopup_)
  70. {
  71. // "Move" the popup from sibling menu to this menu
  72. sibling->ShowPopup(false);
  73. ShowPopup(true);
  74. return;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. void Menu::OnShowPopup()
  81. {
  82. }
  83. bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile)
  84. {
  85. // Apply the style first, but only for non-internal elements
  86. if (!internal_ && styleFile)
  87. {
  88. // Use style override if defined, otherwise type name
  89. String styleName = source.GetAttribute("style");
  90. if (styleName.Empty())
  91. styleName = GetTypeName();
  92. SetStyle(styleFile, styleName);
  93. }
  94. // Then load rest of the attributes from the source
  95. if (!Serializable::LoadXML(source))
  96. return false;
  97. unsigned nextInternalChild = 0;
  98. // Load child elements. Internal elements are not to be created as they already exist
  99. XMLElement childElem = source.GetChild("element");
  100. while (childElem)
  101. {
  102. bool internalElem = childElem.GetBool("internal");
  103. bool popupElem = childElem.GetBool("popup");
  104. String typeName = childElem.GetAttribute("type");
  105. if (typeName.Empty())
  106. typeName = "UIElement";
  107. UIElement* child = 0;
  108. if (!internalElem)
  109. {
  110. if (!popupElem)
  111. child = CreateChild(typeName);
  112. else
  113. {
  114. // Do not add the popup element as a child even temporarily, as that can break layouts
  115. SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(typeName));
  116. if (!popup)
  117. LOGERROR("Could not create popup element type " + ShortStringHash(typeName).ToString());
  118. else
  119. {
  120. child = popup;
  121. SetPopup(popup);
  122. }
  123. }
  124. }
  125. else
  126. {
  127. // An internal popup element should already exist
  128. if (popupElem)
  129. child = popup_;
  130. else
  131. {
  132. for (unsigned i = nextInternalChild; i < children_.Size(); ++i)
  133. {
  134. if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName)
  135. {
  136. child = children_[i];
  137. nextInternalChild = i + 1;
  138. break;
  139. }
  140. }
  141. if (!child)
  142. LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName());
  143. }
  144. }
  145. if (child)
  146. {
  147. if (!child->LoadXML(childElem, styleFile))
  148. return false;
  149. }
  150. childElem = childElem.GetNext("element");
  151. }
  152. ApplyAttributes();
  153. return true;
  154. }
  155. bool Menu::SaveXML(XMLElement& dest)
  156. {
  157. // Write type and internal flag
  158. if (!dest.SetString("type", GetTypeName()))
  159. return false;
  160. if (internal_)
  161. {
  162. if (!dest.SetBool("internal", internal_))
  163. return false;
  164. }
  165. // Write attributes
  166. if (!Serializable::SaveXML(dest))
  167. return false;
  168. // Write child elements
  169. for (unsigned i = 0; i < children_.Size(); ++i)
  170. {
  171. UIElement* element = children_[i];
  172. XMLElement childElem = dest.CreateChild("element");
  173. if (!element->SaveXML(childElem))
  174. return false;
  175. }
  176. // Save the popup element as a "virtual" child element
  177. if (popup_)
  178. {
  179. XMLElement childElem = dest.CreateChild("element");
  180. childElem.SetBool("popup", true);
  181. if (!popup_->SaveXML(childElem))
  182. return false;
  183. }
  184. return true;
  185. }
  186. void Menu::SetPopup(UIElement* popup)
  187. {
  188. if (popup == this)
  189. return;
  190. if (popup_ && !popup)
  191. ShowPopup(false);
  192. popup_ = popup;
  193. // Detach from current parent (if any) to only show when it is time
  194. if (popup_)
  195. popup_->Remove();
  196. }
  197. void Menu::SetPopupOffset(const IntVector2& offset)
  198. {
  199. popupOffset_ = offset;
  200. }
  201. void Menu::SetPopupOffset(int x, int y)
  202. {
  203. popupOffset_ = IntVector2(x, y);
  204. }
  205. void Menu::ShowPopup(bool enable)
  206. {
  207. if (!popup_)
  208. return;
  209. if (enable)
  210. {
  211. // Find the UI root element for showing the popup
  212. UIElement* root = GetRoot();
  213. if (!root)
  214. return;
  215. OnShowPopup();
  216. if (popup_->GetParent() != root)
  217. root->AddChild(popup_);
  218. popup_->SetPosition(GetScreenPosition() + popupOffset_);
  219. popup_->SetVisible(true);
  220. popup_->SetVar(originHash, (void*)this);
  221. popup_->BringToFront();
  222. }
  223. else
  224. {
  225. // If the popup has child menus, hide their popups as well
  226. PODVector<UIElement*> children;
  227. popup_->GetChildren(children, true);
  228. for (PODVector<UIElement*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  229. {
  230. Menu* menu = dynamic_cast<Menu*>(*i);
  231. if (menu)
  232. menu->ShowPopup(false);
  233. }
  234. popup_->SetVar(originHash, Variant::EMPTY);
  235. popup_->SetVisible(false);
  236. popup_->Remove();
  237. }
  238. showPopup_ = enable;
  239. selected_ = enable;
  240. }
  241. void Menu::SetAccelerator(int key, int qualifiers)
  242. {
  243. acceleratorKey_ = key;
  244. acceleratorQualifiers_ = qualifiers;
  245. if (key)
  246. SubscribeToEvent(E_KEYDOWN, HANDLER(Menu, HandleKeyDown));
  247. else
  248. UnsubscribeFromEvent(E_KEYDOWN);
  249. }
  250. void Menu::HandlePressedReleased(StringHash eventType, VariantMap& eventData)
  251. {
  252. // If this menu shows a sublevel popup, react to button press. Else react to release
  253. if (eventType == E_PRESSED)
  254. {
  255. if (!popup_)
  256. return;
  257. }
  258. if (eventType == E_RELEASED)
  259. {
  260. if (popup_)
  261. return;
  262. }
  263. // Toggle popup visibility if exists
  264. ShowPopup(!showPopup_);
  265. // Send event on each click if no popup, or whenever the popup is opened
  266. if (!popup_ || showPopup_)
  267. {
  268. using namespace MenuSelected;
  269. VariantMap newEventData;
  270. newEventData[P_ELEMENT] = (void*)this;
  271. SendEvent(E_MENUSELECTED, newEventData);
  272. }
  273. }
  274. void Menu::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  275. {
  276. if (!showPopup_)
  277. return;
  278. using namespace FocusChanged;
  279. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  280. UIElement* root = GetRoot();
  281. // If another element was focused due to the menu button being clicked, do not hide the popup
  282. if (eventType == E_FOCUSCHANGED && static_cast<UIElement*>(eventData[P_CLICKEDELEMENT].GetPtr()))
  283. return;
  284. // If clicked emptiness or defocused, hide the popup
  285. if (!element)
  286. {
  287. ShowPopup(false);
  288. return;
  289. }
  290. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  291. // In that case, do not hide
  292. while (element)
  293. {
  294. if (element == this || element == popup_)
  295. return;
  296. if (element->GetParent() == root)
  297. element = static_cast<UIElement*>(element->GetVar(originHash).GetPtr());
  298. else
  299. element = element->GetParent();
  300. }
  301. ShowPopup(false);
  302. }
  303. void Menu::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  304. {
  305. if (!enabled_)
  306. return;
  307. using namespace KeyDown;
  308. // Activate if accelerator key pressed
  309. if (eventData[P_KEY].GetInt() == acceleratorKey_ && (acceleratorQualifiers_ == QUAL_ANY || eventData[P_QUALIFIERS].GetInt() ==
  310. acceleratorQualifiers_) && eventData[P_REPEAT].GetBool() == false)
  311. {
  312. // Ignore if UI has modal element
  313. UI* ui = GetSubsystem<UI>();
  314. if (ui->GetModalElement())
  315. return;
  316. HandlePressedReleased(eventType, eventData);
  317. }
  318. }
  319. }