DropDownList.cpp 11 KB

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