ObjectLists.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * ObjectLists.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ObjectLists.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "Slider.h"
  14. CObjectList::CObjectList(CreateFunc create)
  15. : createObject(create)
  16. {
  17. }
  18. void CObjectList::deleteItem(std::shared_ptr<CIntObject> item)
  19. {
  20. if(!item)
  21. return;
  22. item->deactivate();
  23. removeChild(item.get());
  24. }
  25. std::shared_ptr<CIntObject> CObjectList::createItem(size_t index)
  26. {
  27. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  28. std::shared_ptr<CIntObject> item = createObject(index);
  29. if(!item)
  30. item = std::make_shared<CIntObject>();
  31. item->recActions = defActions;
  32. addChild(item.get());
  33. if (isActive())
  34. item->activate();
  35. return item;
  36. }
  37. CTabbedInt::CTabbedInt(CreateFunc create, Point position, size_t ActiveID)
  38. : CObjectList(create),
  39. activeTab(nullptr),
  40. activeID(ActiveID)
  41. {
  42. defActions &= ~DISPOSE;
  43. pos += position;
  44. reset();
  45. }
  46. void CTabbedInt::setActive(size_t which)
  47. {
  48. if(which != activeID)
  49. {
  50. activeID = which;
  51. reset();
  52. }
  53. }
  54. size_t CTabbedInt::getActive() const
  55. {
  56. return activeID;
  57. }
  58. void CTabbedInt::reset()
  59. {
  60. deleteItem(activeTab);
  61. activeTab = createItem(activeID);
  62. activeTab->moveTo(pos.topLeft());
  63. if(isActive())
  64. redraw();
  65. }
  66. std::shared_ptr<CIntObject> CTabbedInt::getItem()
  67. {
  68. return activeTab;
  69. }
  70. CListBox::CListBox(CreateFunc create, Point Pos, Point ItemOffset, size_t VisibleSize,
  71. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos)
  72. : CObjectList(create),
  73. first(InitialPos),
  74. totalSize(TotalSize),
  75. itemOffset(ItemOffset)
  76. {
  77. pos += Pos;
  78. items.resize(VisibleSize, nullptr);
  79. if(Slider & 1)
  80. {
  81. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  82. slider = std::make_shared<CSlider>(
  83. SliderPos.topLeft(),
  84. SliderPos.w,
  85. std::bind(&CListBox::moveToPos, this, _1),
  86. (int)VisibleSize,
  87. (int)TotalSize,
  88. (int)InitialPos,
  89. Slider & 2 ? Orientation::HORIZONTAL : Orientation::VERTICAL,
  90. Slider & 4 ? CSlider::BLUE : CSlider::BROWN
  91. );
  92. slider->setPanningStep(itemOffset.x + itemOffset.y);
  93. }
  94. reset();
  95. }
  96. // Used to move active items after changing list position
  97. void CListBox::updatePositions()
  98. {
  99. Point itemPos = pos.topLeft();
  100. for (auto & elem : items)
  101. {
  102. (elem)->moveTo(itemPos);
  103. itemPos += itemOffset;
  104. }
  105. if (isActive())
  106. {
  107. redraw();
  108. if (slider)
  109. slider->scrollTo((int)first);
  110. }
  111. }
  112. void CListBox::reset()
  113. {
  114. size_t current = first;
  115. for (auto & elem : items)
  116. {
  117. deleteItem(elem);
  118. elem = createItem(current++);
  119. }
  120. updatePositions();
  121. }
  122. void CListBox::resize(size_t newSize)
  123. {
  124. if (totalSize == newSize)
  125. return;
  126. totalSize = newSize;
  127. if (slider)
  128. slider->setAmount((int)totalSize);
  129. reset();
  130. }
  131. size_t CListBox::size()
  132. {
  133. return totalSize;
  134. }
  135. std::shared_ptr<CIntObject> CListBox::getItem(size_t which)
  136. {
  137. if(which < first || which > first + items.size() || which > totalSize)
  138. return std::shared_ptr<CIntObject>();
  139. size_t i=first;
  140. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  141. if( i == which)
  142. return *iter;
  143. return std::shared_ptr<CIntObject>();
  144. }
  145. size_t CListBox::getIndexOf(std::shared_ptr<CIntObject> item)
  146. {
  147. size_t i=first;
  148. for(auto iter = items.begin(); iter != items.end(); iter++, i++)
  149. if(*iter == item)
  150. return i;
  151. return size_t(-1);
  152. }
  153. void CListBox::scrollTo(size_t which)
  154. {
  155. //scroll up
  156. if(first > which)
  157. moveToPos(which);
  158. //scroll down
  159. else if (first + items.size() <= which && which < totalSize)
  160. moveToPos(which - items.size() + 1);
  161. }
  162. void CListBox::moveToPos(size_t which)
  163. {
  164. //Calculate new position
  165. size_t maxPossible;
  166. if(totalSize > items.size())
  167. maxPossible = totalSize - items.size();
  168. else
  169. maxPossible = 0;
  170. size_t newPos = std::min(which, maxPossible);
  171. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  172. if(first - newPos == 1)
  173. {
  174. moveToPrev();
  175. }
  176. else if(newPos - first == 1)
  177. {
  178. moveToNext();
  179. }
  180. else if(newPos != first)
  181. {
  182. first = newPos;
  183. reset();
  184. }
  185. }
  186. void CListBox::moveToNext()
  187. {
  188. //Remove front item and insert new one to end
  189. if(first + items.size() < totalSize)
  190. {
  191. first++;
  192. deleteItem(items.front());
  193. items.pop_front();
  194. items.push_back(createItem(first+items.size()));
  195. updatePositions();
  196. }
  197. }
  198. void CListBox::moveToPrev()
  199. {
  200. //Remove last item and insert new one at start
  201. if(first)
  202. {
  203. first--;
  204. deleteItem(items.back());
  205. items.pop_back();
  206. items.push_front(createItem(first));
  207. updatePositions();
  208. }
  209. }
  210. size_t CListBox::getPos()
  211. {
  212. return first;
  213. }
  214. const std::list<std::shared_ptr<CIntObject>> & CListBox::getItems()
  215. {
  216. return items;
  217. }
  218. CListBoxWithCallback::CListBoxWithCallback(CListBoxWithCallback::MovedPosCallback callback, CreateFunc create, Point pos, Point itemOffset,
  219. size_t visibleSize, size_t totalSize, size_t initialPos, int slider, Rect sliderPos)
  220. : CListBox(create, pos, itemOffset, visibleSize, totalSize, initialPos, slider, sliderPos)
  221. {
  222. movedPosCallback = callback;
  223. }
  224. void CListBoxWithCallback::scrollTo(size_t pos)
  225. {
  226. CListBox::scrollTo(pos);
  227. if(movedPosCallback)
  228. movedPosCallback(getPos());
  229. }
  230. void CListBoxWithCallback::moveToPos(size_t pos)
  231. {
  232. CListBox::moveToPos(pos);
  233. if(movedPosCallback)
  234. movedPosCallback(getPos());
  235. }
  236. void CListBoxWithCallback::moveToNext()
  237. {
  238. CListBox::moveToNext();
  239. if(movedPosCallback)
  240. movedPosCallback(getPos());
  241. }
  242. void CListBoxWithCallback::moveToPrev()
  243. {
  244. CListBox::moveToPrev();
  245. if(movedPosCallback)
  246. movedPosCallback(getPos());
  247. }