DropDownList.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "DropDownList.h"
  26. #include "ListView.h"
  27. #include "UIEvents.h"
  28. #include "Window.h"
  29. #include "DebugNew.h"
  30. OBJECTTYPESTATIC(DropDownList);
  31. DropDownList::DropDownList(Context* context) :
  32. Menu(context),
  33. resizePopup_(false)
  34. {
  35. Window* window = new Window(context_);
  36. SetPopup(window);
  37. listView_ = new ListView(context_);
  38. listView_->SetScrollBarsVisible(false, false);
  39. listView_->SetFocusMode(FM_NOTFOCUSABLE);
  40. popup_->SetLayout(LM_VERTICAL);
  41. popup_->AddChild(listView_);
  42. placeholder_ = new UIElement(context_);
  43. AddChild(placeholder_);
  44. SubscribeToEvent(listView_, E_ITEMSELECTED, HANDLER(DropDownList, HandleItemSelected));
  45. }
  46. DropDownList::~DropDownList()
  47. {
  48. }
  49. void DropDownList::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<DropDownList>();
  52. }
  53. void DropDownList::SetStyle(const XMLElement& element)
  54. {
  55. Menu::SetStyle(element);
  56. XMLElement listElem = element.GetChild("listview");
  57. if (listElem)
  58. listView_->SetStyle(listElem);
  59. XMLElement popupElem = element.GetChild("popup");
  60. if (popupElem)
  61. popup_->SetStyle(popupElem);
  62. XMLElement placeholderElem = element.GetChild("placeholder");
  63. if (placeholderElem)
  64. placeholder_->SetStyle(placeholderElem);
  65. UIElement* root = GetRoot();
  66. XMLElement itemElem = element.GetChild("popupitem");
  67. if (root)
  68. {
  69. while (itemElem)
  70. {
  71. if (itemElem.HasAttribute("name"))
  72. AddItem(root->GetChild(itemElem.GetAttribute("name"), true));
  73. itemElem = itemElem.GetNext("popupitem");
  74. }
  75. }
  76. if (element.HasChild("selection"))
  77. SetSelection(element.GetChild("selection").GetInt("value"));
  78. if (element.HasChild("resizepopup"))
  79. SetResizePopup(element.GetChild("resizepopup").GetBool("enable"));
  80. }
  81. void DropDownList::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  82. {
  83. Button::GetBatches(batches, quads, currentScissor);
  84. if (!placeholder_->IsVisible())
  85. return;
  86. UIElement* selectedItem = GetSelectedItem();
  87. if (selectedItem)
  88. {
  89. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  90. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  91. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  92. IntVector2 offset = targetPos - originalPos;
  93. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  94. bool hover = selectedItem->IsHovering();
  95. selectedItem->SetHovering(false);
  96. selectedItem->GetBatchesWithOffset(offset, batches, quads, currentScissor);
  97. selectedItem->SetHovering(hover);
  98. }
  99. }
  100. void DropDownList::OnShowPopup()
  101. {
  102. // Resize the popup to match the size of the list content, and optionally match the button width
  103. UIElement* content = listView_->GetContentElement();
  104. content->UpdateLayout();
  105. const IntVector2& contentSize = content->GetSize();
  106. const IntRect& border = popup_->GetLayoutBorder();
  107. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_, contentSize.y_ + border.top_ +
  108. border.bottom_);
  109. // Check if popup fits below the button. If not, show above instead
  110. bool showAbove = false;
  111. UIElement* root = GetRoot();
  112. if (root)
  113. {
  114. const IntVector2& screenPos = GetScreenPosition();
  115. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  116. showAbove = true;
  117. }
  118. if (!showAbove)
  119. SetPopupOffset(0, GetHeight());
  120. else
  121. SetPopupOffset(0, -popup_->GetHeight());
  122. }
  123. void DropDownList::AddItem(UIElement* item)
  124. {
  125. InsertItem(listView_->GetNumItems(), item);
  126. }
  127. void DropDownList::InsertItem(unsigned index, UIElement* item)
  128. {
  129. listView_->InsertItem(index, item);
  130. // If there was no selection, set to the first
  131. if (listView_->GetSelection() == M_MAX_UNSIGNED)
  132. listView_->SetSelection(0);
  133. }
  134. void DropDownList::RemoveItem(UIElement* item)
  135. {
  136. listView_->RemoveItem(item);
  137. }
  138. void DropDownList::RemoveItem(unsigned index)
  139. {
  140. listView_->RemoveItem(index);
  141. }
  142. void DropDownList::RemoveAllItems()
  143. {
  144. listView_->RemoveAllItems();
  145. }
  146. void DropDownList::SetSelection(unsigned index)
  147. {
  148. listView_->SetSelection(index);
  149. }
  150. void DropDownList::SetResizePopup(bool enable)
  151. {
  152. resizePopup_ = enable;
  153. }
  154. unsigned DropDownList::GetNumItems() const
  155. {
  156. return listView_->GetNumItems();
  157. }
  158. UIElement* DropDownList::GetItem(unsigned index) const
  159. {
  160. return listView_->GetItem(index);
  161. }
  162. PODVector<UIElement*> DropDownList::GetItems() const
  163. {
  164. PODVector<UIElement*> items;
  165. listView_->GetContentElement()->GetChildren(items);
  166. return items;
  167. }
  168. unsigned DropDownList::GetSelection() const
  169. {
  170. return listView_->GetSelection();
  171. }
  172. UIElement* DropDownList::GetSelectedItem() const
  173. {
  174. return listView_->GetSelectedItem();
  175. }
  176. void DropDownList::HandleItemSelected(StringHash eventType, VariantMap& eventData)
  177. {
  178. // Resize the selection placeholder to match the selected item
  179. UIElement* selectedItem = GetSelectedItem();
  180. if (selectedItem)
  181. placeholder_->SetSize(selectedItem->GetSize());
  182. // Close the popup as the selection was made
  183. if (GetShowPopup())
  184. ShowPopup(false);
  185. // Send the event forward
  186. using namespace ItemSelected;
  187. VariantMap newEventData;
  188. newEventData[P_ELEMENT] = (void*)this;
  189. newEventData[P_SELECTION] = GetSelection();
  190. SendEvent(E_ITEMSELECTED, newEventData);
  191. }