Menu.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "InputEvents.h"
  26. #include "Log.h"
  27. #include "Menu.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. REF_ACCESSOR_ATTRIBUTE(Menu, VAR_INTVECTOR2, "Popup Offset", GetPopupOffset, SetPopupOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  55. COPY_BASE_ATTRIBUTES(Menu, Button);
  56. }
  57. void Menu::OnShowPopup()
  58. {
  59. }
  60. bool Menu::LoadXML(const XMLElement& source, XMLFile* styleFile)
  61. {
  62. // Apply the style first, but only for non-internal elements
  63. if (!internal_ && styleFile)
  64. {
  65. // Use style override if defined, otherwise type name
  66. String styleName = source.GetAttribute("style");
  67. if (styleName.Empty())
  68. styleName = GetTypeName();
  69. SetStyle(styleFile, styleName);
  70. }
  71. // Then load rest of the attributes from the source
  72. if (!Serializable::LoadXML(source))
  73. return false;
  74. unsigned nextInternalChild = 0;
  75. // Load child elements. Internal elements are not to be created as they already exist
  76. XMLElement childElem = source.GetChild("element");
  77. while (childElem)
  78. {
  79. bool internalElem = childElem.GetBool("internal");
  80. bool popupElem = childElem.GetBool("popup");
  81. String typeName = childElem.GetAttribute("type");
  82. if (typeName.Empty())
  83. typeName = "UIElement";
  84. UIElement* child = 0;
  85. if (!internalElem)
  86. {
  87. if (!popupElem)
  88. child = CreateChild(ShortStringHash(typeName));
  89. else
  90. {
  91. // Do not add the popup element as a child even temporarily, as that can break layouts
  92. SharedPtr<UIElement> popup = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(typeName)));
  93. if (!popup)
  94. LOGERROR("Could not create popup element type " + ShortStringHash(typeName).ToString());
  95. else
  96. {
  97. child = popup;
  98. SetPopup(popup);
  99. }
  100. }
  101. }
  102. else
  103. {
  104. // An internal popup element should already exist
  105. if (popupElem)
  106. child = popup_;
  107. else
  108. {
  109. for (unsigned i = nextInternalChild; i < children_.Size(); ++i)
  110. {
  111. if (children_[i]->IsInternal() && children_[i]->GetTypeName() == typeName)
  112. {
  113. child = children_[i];
  114. nextInternalChild = i + 1;
  115. break;
  116. }
  117. }
  118. if (!child)
  119. LOGWARNING("Could not find matching internal child element of type " + typeName + " in " + GetTypeName());
  120. }
  121. }
  122. if (child)
  123. {
  124. if (!child->LoadXML(childElem, styleFile))
  125. return false;
  126. }
  127. childElem = childElem.GetNext("element");
  128. }
  129. ApplyAttributes();
  130. return true;
  131. }
  132. bool Menu::SaveXML(XMLElement& dest)
  133. {
  134. // Write type and internal flag
  135. if (!dest.SetString("type", GetTypeName()))
  136. return false;
  137. if (internal_)
  138. {
  139. if (!dest.SetBool("internal", internal_))
  140. return false;
  141. }
  142. // Write attributes
  143. if (!Serializable::SaveXML(dest))
  144. return false;
  145. // Write child elements
  146. for (unsigned i = 0; i < children_.Size(); ++i)
  147. {
  148. UIElement* element = children_[i];
  149. XMLElement childElem = dest.CreateChild("element");
  150. if (!element->SaveXML(childElem))
  151. return false;
  152. }
  153. // Save the popup element as a "virtual" child element
  154. if (popup_)
  155. {
  156. XMLElement childElem = dest.CreateChild("element");
  157. childElem.SetBool("popup", true);
  158. if (!popup_->SaveXML(childElem))
  159. return false;
  160. }
  161. return true;
  162. }
  163. void Menu::SetPopup(UIElement* popup)
  164. {
  165. if (popup == this)
  166. return;
  167. if (popup_ && !popup)
  168. ShowPopup(false);
  169. popup_ = popup;
  170. // Detach from current parent (if any) to only show when it is time
  171. if (popup_)
  172. popup_->Remove();
  173. }
  174. void Menu::SetPopupOffset(const IntVector2& offset)
  175. {
  176. popupOffset_ = offset;
  177. }
  178. void Menu::SetPopupOffset(int x, int y)
  179. {
  180. popupOffset_ = IntVector2(x, y);
  181. }
  182. void Menu::ShowPopup(bool enable)
  183. {
  184. if (!popup_)
  185. return;
  186. if (enable)
  187. {
  188. // Find the UI root element for showing the popup
  189. UIElement* root = GetRoot();
  190. if (!root)
  191. return;
  192. OnShowPopup();
  193. if (popup_->GetParent() != root)
  194. root->AddChild(popup_);
  195. popup_->SetPosition(GetScreenPosition() + popupOffset_);
  196. popup_->SetVisible(true);
  197. popup_->SetVar(originHash, (void*)this);
  198. popup_->BringToFront();
  199. }
  200. else
  201. {
  202. // If the popup has child menus, hide their popups as well
  203. PODVector<UIElement*> children;
  204. popup_->GetChildren(children, true);
  205. for (PODVector<UIElement*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  206. {
  207. Menu* menu = dynamic_cast<Menu*>(*i);
  208. if (menu)
  209. menu->ShowPopup(false);
  210. }
  211. popup_->SetVar(originHash, Variant());
  212. popup_->SetVisible(false);
  213. popup_->Remove();
  214. }
  215. showPopup_ = enable;
  216. selected_ = enable;
  217. }
  218. void Menu::SetAccelerator(int key, int qualifiers)
  219. {
  220. acceleratorKey_ = key;
  221. acceleratorQualifiers_ = qualifiers;
  222. if (key)
  223. SubscribeToEvent(E_KEYDOWN, HANDLER(Menu, HandleKeyDown));
  224. else
  225. UnsubscribeFromEvent(E_KEYDOWN);
  226. }
  227. void Menu::HandlePressedReleased(StringHash eventType, VariantMap& eventData)
  228. {
  229. // If this menu shows a sublevel popup, react to button press. Else react to release
  230. if (eventType == E_PRESSED)
  231. {
  232. if (!popup_)
  233. return;
  234. }
  235. if (eventType == E_RELEASED)
  236. {
  237. if (popup_)
  238. return;
  239. }
  240. // Toggle popup visibility if exists
  241. ShowPopup(!showPopup_);
  242. // Send event on each click if no popup, or whenever the popup is opened
  243. if (!popup_ || showPopup_)
  244. {
  245. using namespace MenuSelected;
  246. VariantMap newEventData;
  247. newEventData[P_ELEMENT] = (void*)this;
  248. SendEvent(E_MENUSELECTED, newEventData);
  249. }
  250. }
  251. void Menu::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
  252. {
  253. if (!showPopup_)
  254. return;
  255. using namespace FocusChanged;
  256. UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
  257. UIElement* root = GetRoot();
  258. // If another element was focused due to the menu button being clicked, do not hide the popup
  259. if (eventType == E_FOCUSCHANGED && static_cast<UIElement*>(eventData[P_CLICKEDELEMENT].GetPtr()))
  260. return;
  261. // If clicked emptiness or defocused, hide the popup
  262. if (!element)
  263. {
  264. ShowPopup(false);
  265. return;
  266. }
  267. // Otherwise see if the clicked element has either the menu item or the popup in its parent chain.
  268. // In that case, do not hide
  269. while (element)
  270. {
  271. if (element == this || element == popup_)
  272. return;
  273. if (element->GetParent() == root)
  274. element = static_cast<UIElement*>(element->GetVar(originHash).GetPtr());
  275. else
  276. element = element->GetParent();
  277. }
  278. ShowPopup(false);
  279. }
  280. void Menu::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  281. {
  282. if (!active_)
  283. return;
  284. using namespace KeyDown;
  285. // Activate if accelerator key pressed
  286. if (eventData[P_KEY].GetInt() == acceleratorKey_ && (acceleratorQualifiers_ == QUAL_ANY || eventData[P_QUALIFIERS].GetInt() ==
  287. acceleratorQualifiers_) && eventData[P_REPEAT].GetBool() == false)
  288. HandlePressedReleased(eventType, eventData);
  289. }
  290. }