Menu.cpp 10.0 KB

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