Menu.cpp 11 KB

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