DropDownList.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Input/InputEvents.h"
  6. #include "../IO/Log.h"
  7. #include "../UI/DropDownList.h"
  8. #include "../UI/ListView.h"
  9. #include "../UI/Text.h"
  10. #include "../UI/UI.h"
  11. #include "../UI/UIEvents.h"
  12. #include "../UI/Window.h"
  13. #include "../DebugNew.h"
  14. namespace Urho3D
  15. {
  16. extern const char* UI_CATEGORY;
  17. DropDownList::DropDownList(Context* context) :
  18. Menu(context),
  19. resizePopup_(false),
  20. selectionAttr_(0)
  21. {
  22. focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
  23. auto* window = new Window(context_);
  24. window->SetInternal(true);
  25. SetPopup(window);
  26. listView_ = new ListView(context_);
  27. listView_->SetInternal(true);
  28. listView_->SetScrollBarsVisible(false, false);
  29. popup_->SetLayout(LM_VERTICAL);
  30. popup_->AddChild(listView_);
  31. placeholder_ = CreateChild<UIElement>("DDL_Placeholder");
  32. placeholder_->SetInternal(true);
  33. auto* text = placeholder_->CreateChild<Text>("DDL_Placeholder_Text");
  34. text->SetInternal(true);
  35. text->SetVisible(false);
  36. SubscribeToEvent(listView_, E_ITEMCLICKED, URHO3D_HANDLER(DropDownList, HandleItemClicked));
  37. SubscribeToEvent(listView_, E_UNHANDLEDKEY, URHO3D_HANDLER(DropDownList, HandleListViewKey));
  38. SubscribeToEvent(listView_, E_SELECTIONCHANGED, URHO3D_HANDLER(DropDownList, HandleSelectionChanged));
  39. }
  40. DropDownList::~DropDownList() = default;
  41. void DropDownList::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<DropDownList>(UI_CATEGORY);
  44. URHO3D_COPY_BASE_ATTRIBUTES(Menu);
  45. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE_DEFOCUSABLE);
  46. URHO3D_ACCESSOR_ATTRIBUTE("Selection", GetSelection, SetSelectionAttr, 0, AM_FILE);
  47. URHO3D_ACCESSOR_ATTRIBUTE("Resize Popup", GetResizePopup, SetResizePopup, false, AM_FILE);
  48. }
  49. void DropDownList::ApplyAttributes()
  50. {
  51. // Reapply selection after possible items have been loaded
  52. SetSelection(selectionAttr_);
  53. }
  54. void DropDownList::GetBatches(Vector<UIBatch>& batches, Vector<float>& vertexData, const IntRect& currentScissor)
  55. {
  56. Menu::GetBatches(batches, vertexData, currentScissor);
  57. if (!placeholder_->IsVisible())
  58. return;
  59. UIElement* selectedItem = GetSelectedItem();
  60. if (selectedItem)
  61. {
  62. // Can not easily copy the selected item. However, it can be re-rendered on the placeholder's position
  63. const IntVector2& targetPos = placeholder_->GetScreenPosition();
  64. const IntVector2& originalPos = selectedItem->GetScreenPosition();
  65. IntVector2 offset = targetPos - originalPos;
  66. // GetBatches() usually resets the hover flag. Therefore get its value and then reset it for the real rendering
  67. // Render the selected item without its selection color, so temporarily reset the item's selected attribute
  68. bool hover = selectedItem->IsHovering();
  69. selectedItem->SetSelected(false);
  70. selectedItem->SetHovering(false);
  71. selectedItem->GetBatchesWithOffset(offset, batches, vertexData, currentScissor);
  72. selectedItem->SetSelected(true);
  73. selectedItem->SetHovering(hover);
  74. }
  75. }
  76. void DropDownList::OnShowPopup()
  77. {
  78. // Resize the popup to match the size of the list content, and optionally match the button width
  79. UIElement* content = listView_->GetContentElement();
  80. content->UpdateLayout();
  81. const IntVector2& contentSize = content->GetSize();
  82. const IntRect& border = popup_->GetLayoutBorder();
  83. popup_->SetSize(resizePopup_ ? GetWidth() : contentSize.x_ + border.left_ + border.right_,
  84. contentSize.y_ + border.top_ + border.bottom_);
  85. // Check if popup fits below the button. If not, show above instead
  86. bool showAbove = false;
  87. UIElement* root = GetRoot();
  88. if (root)
  89. {
  90. const IntVector2& screenPos = GetScreenPosition();
  91. if (screenPos.y_ + GetHeight() + popup_->GetHeight() > root->GetHeight() && screenPos.y_ - popup_->GetHeight() >= 0)
  92. showAbove = true;
  93. }
  94. SetPopupOffset(0, showAbove ? -popup_->GetHeight() : GetHeight());
  95. // Focus the ListView to allow making the selection with keys
  96. GetSubsystem<UI>()->SetFocusElement(listView_);
  97. }
  98. void DropDownList::OnHidePopup()
  99. {
  100. // When the popup is hidden, propagate the selection
  101. using namespace ItemSelected;
  102. VariantMap& eventData = GetEventDataMap();
  103. eventData[P_ELEMENT] = this;
  104. eventData[P_SELECTION] = GetSelection();
  105. SendEvent(E_ITEMSELECTED, eventData);
  106. }
  107. void DropDownList::OnSetEditable()
  108. {
  109. listView_->SetEditable(editable_);
  110. }
  111. void DropDownList::AddItem(UIElement* item)
  112. {
  113. InsertItem(ENDPOS, item);
  114. }
  115. void DropDownList::InsertItem(i32 index, UIElement* item)
  116. {
  117. assert(index >= 0 || index == ENDPOS);
  118. listView_->InsertItem(index, item);
  119. // If there was no selection, set to the first
  120. if (GetSelection() == NINDEX)
  121. SetSelection(0);
  122. }
  123. void DropDownList::RemoveItem(UIElement* item)
  124. {
  125. listView_->RemoveItem(item);
  126. }
  127. void DropDownList::RemoveItem(i32 index)
  128. {
  129. assert(index >= 0);
  130. listView_->RemoveItem(index);
  131. }
  132. void DropDownList::RemoveAllItems()
  133. {
  134. listView_->RemoveAllItems();
  135. }
  136. void DropDownList::SetSelection(i32 index)
  137. {
  138. assert(index >= 0);
  139. listView_->SetSelection(index);
  140. }
  141. void DropDownList::SetPlaceholderText(const String& text)
  142. {
  143. placeholder_->GetChildStaticCast<Text>(0)->SetText(text);
  144. }
  145. void DropDownList::SetResizePopup(bool enable)
  146. {
  147. resizePopup_ = enable;
  148. }
  149. i32 DropDownList::GetNumItems() const
  150. {
  151. return listView_->GetNumItems();
  152. }
  153. UIElement* DropDownList::GetItem(i32 index) const
  154. {
  155. assert(index >= 0);
  156. return listView_->GetItem(index);
  157. }
  158. Vector<UIElement*> DropDownList::GetItems() const
  159. {
  160. return listView_->GetItems();
  161. }
  162. i32 DropDownList::GetSelection() const
  163. {
  164. return listView_->GetSelection();
  165. }
  166. UIElement* DropDownList::GetSelectedItem() const
  167. {
  168. return listView_->GetSelectedItem();
  169. }
  170. const String& DropDownList::GetPlaceholderText() const
  171. {
  172. return placeholder_->GetChildStaticCast<Text>(0)->GetText();
  173. }
  174. void DropDownList::SetSelectionAttr(i32 index)
  175. {
  176. assert(index >= 0);
  177. selectionAttr_ = index;
  178. // We may not have the list items yet. Apply the index again in ApplyAttributes().
  179. SetSelection(index);
  180. }
  181. bool DropDownList::FilterImplicitAttributes(XMLElement& dest) const
  182. {
  183. if (!Menu::FilterImplicitAttributes(dest))
  184. return false;
  185. if (!RemoveChildXML(dest, "Popup Offset"))
  186. return false;
  187. XMLElement childElem = dest.GetChild("element");
  188. if (!childElem)
  189. return false;
  190. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder"))
  191. return false;
  192. if (!RemoveChildXML(childElem, "Size"))
  193. return false;
  194. childElem = childElem.GetChild("element");
  195. if (!childElem)
  196. return false;
  197. if (!RemoveChildXML(childElem, "Name", "DDL_Placeholder_Text"))
  198. return false;
  199. if (!RemoveChildXML(childElem, "Is Visible"))
  200. return false;
  201. return true;
  202. }
  203. bool DropDownList::FilterPopupImplicitAttributes(XMLElement& dest) const
  204. {
  205. if (!Menu::FilterPopupImplicitAttributes(dest))
  206. return false;
  207. // Window popup
  208. if (dest.GetAttribute("style").Empty() && !dest.SetAttribute("style", "none"))
  209. return false;
  210. if (!RemoveChildXML(dest, "Layout Mode", "Vertical"))
  211. return false;
  212. if (!RemoveChildXML(dest, "Size"))
  213. return false;
  214. // ListView
  215. XMLElement childElem = dest.GetChild("element");
  216. if (!childElem)
  217. return false;
  218. if (!listView_->FilterAttributes(childElem))
  219. return false;
  220. if (childElem.GetAttribute("style").Empty() && !childElem.SetAttribute("style", "none"))
  221. return false;
  222. if (!RemoveChildXML(childElem, "Focus Mode", "NotFocusable"))
  223. return false;
  224. if (!RemoveChildXML(childElem, "Auto Show/Hide Scrollbars", "false"))
  225. return false;
  226. // Horizontal scroll bar
  227. XMLElement hScrollElem = childElem.GetChild("element");
  228. // Vertical scroll bar
  229. XMLElement vScrollElem = hScrollElem.GetNext("element");
  230. // Scroll panel
  231. XMLElement panelElem = vScrollElem.GetNext("element");
  232. if (hScrollElem && !hScrollElem.GetParent().RemoveChild(hScrollElem))
  233. return false;
  234. if (vScrollElem && !vScrollElem.GetParent().RemoveChild(vScrollElem))
  235. return false;
  236. if (panelElem)
  237. {
  238. if (panelElem.GetAttribute("style").Empty() && !panelElem.SetAttribute("style", "none"))
  239. return false;
  240. // Item container
  241. XMLElement containerElem = panelElem.GetChild("element");
  242. if (containerElem)
  243. {
  244. if (containerElem.GetAttribute("style").Empty() && !containerElem.SetAttribute("style", "none"))
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. void DropDownList::HandleItemClicked(StringHash eventType, VariantMap& eventData)
  251. {
  252. // Resize the selection placeholder to match the selected item
  253. UIElement* selectedItem = GetSelectedItem();
  254. if (selectedItem)
  255. placeholder_->SetSize(selectedItem->GetSize());
  256. // Close and defocus the popup. This will actually send the selection forward
  257. if (listView_->HasFocus())
  258. GetSubsystem<UI>()->SetFocusElement(focusMode_ < FM_FOCUSABLE ? nullptr : this);
  259. ShowPopup(false);
  260. }
  261. void DropDownList::HandleListViewKey(StringHash eventType, VariantMap& eventData)
  262. {
  263. using namespace UnhandledKey;
  264. // If enter pressed in the list view, close and propagate selection
  265. int key = eventData[P_KEY].GetI32();
  266. if (key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER)
  267. HandleItemClicked(eventType, eventData);
  268. }
  269. void DropDownList::HandleSelectionChanged(StringHash eventType, VariantMap& eventData)
  270. {
  271. // Display the place holder text when there is no selection, however, the place holder text is only visible when the place holder itself is set to visible
  272. placeholder_->GetChild(0)->SetVisible(GetSelection() == NINDEX);
  273. }
  274. }