DropDownList.cpp 6.6 KB

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