UIListView.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicEditor
  4. #include "AtomicEditor.h"
  5. #include <Atomic/UI/TBUI.h>
  6. #include <Atomic/IO/Log.h>
  7. #include "UIListView.h"
  8. using namespace tb;
  9. namespace AtomicEditor
  10. {
  11. ListViewItem* ListViewItem::AddChild(const char *text, const char* icon, const TBID &id)
  12. {
  13. ListViewItem* child = new ListViewItem(text, id, icon, source_);
  14. child->parent_ = this;
  15. child->depth_ = depth_ + 1;
  16. source_->AddItem(child);
  17. children_.Push(child);
  18. return child;
  19. }
  20. ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source,
  21. TBSelectItemViewer *sourceviewer, int index)
  22. : source_(source)
  23. , sourceviewer_(sourceviewer)
  24. , index_(index)
  25. , item_(item)
  26. {
  27. SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  28. SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP);
  29. SetPaintOverflowFadeout(false);
  30. item_->widget_ = this;
  31. for (int i = 0; i < item->depth_; i++)
  32. {
  33. LayoutParams lp;
  34. lp.SetWidth(6);
  35. lp.SetHeight(4);
  36. TBWidget* spacer = new TBWidget();
  37. spacer->SetLayoutParams(lp);
  38. GetContentRoot()->AddChild(spacer);
  39. }
  40. if (item->icon_.Length())
  41. {
  42. TBSkinImage* skinImage = new TBSkinImage(TBIDC(item->icon_.CString()));
  43. skinImage->SetIgnoreInput(true);
  44. GetContentRoot()->AddChild(skinImage);
  45. }
  46. TBTextField* tfield = new TBTextField();
  47. tfield->SetIgnoreInput(true);
  48. tfield->SetSkinBg(TBIDC("Folder"));
  49. tfield->SetText(item->str);
  50. SetSkinBg(TBIDC("TBSelectItem"));
  51. GetContentRoot()->AddChild(tfield);
  52. SetID(item->id);
  53. }
  54. bool ListViewItemWidget::OnEvent(const TBWidgetEvent &ev)
  55. {
  56. // get clicks this way, not sure we want to
  57. if (ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == item_->id)
  58. {
  59. item_->SetExpanded(!item_->GetExpanded());
  60. source_->list_->InvalidateList();
  61. // want to bubble
  62. return false;
  63. }
  64. return TBLayout::OnEvent(ev);
  65. }
  66. bool ListViewItemSource::Filter(int index, const char *filter)
  67. {
  68. ListViewItem* item = GetItem(index);
  69. if (!item->parent_)
  70. return true;
  71. if (item->parent_->GetExpanded())
  72. return true;
  73. return false;
  74. }
  75. TBWidget *ListViewItemSource::CreateItemWidget(int index, TBSelectItemViewer *viewer)
  76. {
  77. ListViewItem* item = GetItem(index);
  78. if (TBLayout *layout = new ListViewItemWidget(item, this, viewer, index))
  79. return layout;
  80. return nullptr;
  81. }
  82. ListView::ListView(Context* context, const TBID& id) :
  83. AEWidget(context),
  84. rootList_(0),
  85. source_(0)
  86. {
  87. rootList_ = new TBSelectList();
  88. rootList_->SetID(id);
  89. //rootList_->GetScrollContainer()->SetScrollMode(SCROLL_MODE_X_AUTO_Y_AUTO);
  90. // dummy filter so filter is called
  91. rootList_->SetFilter(" ");
  92. delegate_->SetGravity(WIDGET_GRAVITY_ALL);
  93. rootList_->SetGravity(WIDGET_GRAVITY_ALL);
  94. source_ = new ListViewItemSource(rootList_);
  95. rootList_->SetSource(source_);
  96. delegate_->AddChild(rootList_);
  97. }
  98. ListView::~ListView()
  99. {
  100. }
  101. void ListView::SelectItemByID(const TBID &id)
  102. {
  103. for (int i = 0; i < source_->GetNumItems(); i++)
  104. {
  105. ListViewItem* item = source_->GetItem(i);
  106. if (id == item->id)
  107. {
  108. item->SetExpanded(true);
  109. rootList_->SetValue(i);
  110. rootList_->InvalidateList();
  111. return;
  112. }
  113. }
  114. }
  115. bool ListView::OnEvent(const TBWidgetEvent &ev)
  116. {
  117. return false;
  118. }
  119. }