ObjectLists.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "../GameEngine.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;
  28. std::shared_ptr<CIntObject> item = createObject(index);
  29. if(!item)
  30. item = std::make_shared<CIntObject>();
  31. addChild(item.get());
  32. if (isActive())
  33. item->activate();
  34. return item;
  35. }
  36. CTabbedInt::CTabbedInt(CreateFunc create, Point position, size_t ActiveID)
  37. : CObjectList(create),
  38. activeTab(nullptr),
  39. activeID(ActiveID)
  40. {
  41. pos += position;
  42. reset();
  43. }
  44. void CTabbedInt::setActive(size_t which)
  45. {
  46. if(which != activeID)
  47. {
  48. activeID = which;
  49. reset();
  50. }
  51. }
  52. size_t CTabbedInt::getActive() const
  53. {
  54. return activeID;
  55. }
  56. void CTabbedInt::reset()
  57. {
  58. deleteItem(activeTab);
  59. activeTab = createItem(activeID);
  60. activeTab->moveTo(pos.topLeft());
  61. if(isActive())
  62. redraw();
  63. }
  64. std::shared_ptr<CIntObject> CTabbedInt::getItem()
  65. {
  66. return activeTab;
  67. }
  68. CListBox::CListBox(CreateFunc create, Point Pos, Point ItemOffset, size_t VisibleSize,
  69. size_t TotalSize, size_t InitialPos, int Slider, Rect SliderPos)
  70. : CObjectList(create),
  71. first(InitialPos),
  72. totalSize(TotalSize),
  73. itemOffset(ItemOffset)
  74. {
  75. pos += Pos;
  76. items.resize(VisibleSize, nullptr);
  77. if(Slider & 1)
  78. {
  79. OBJECT_CONSTRUCTION;
  80. slider = std::make_shared<CSlider>(
  81. SliderPos.topLeft(),
  82. SliderPos.w,
  83. std::bind(&CListBox::moveToPos, this, _1),
  84. (int)VisibleSize,
  85. (int)TotalSize,
  86. (int)InitialPos,
  87. Slider & 2 ? Orientation::HORIZONTAL : Orientation::VERTICAL,
  88. Slider & 4 ? CSlider::BLUE : CSlider::BROWN
  89. );
  90. slider->setPanningStep(itemOffset.x + itemOffset.y);
  91. }
  92. reset();
  93. }
  94. // Used to move active items after changing list position
  95. void CListBox::updatePositions()
  96. {
  97. Point itemPos = pos.topLeft();
  98. for (auto & elem : items)
  99. {
  100. (elem)->moveTo(itemPos);
  101. itemPos += itemOffset;
  102. }
  103. if(slider)
  104. {
  105. slider->scrollTo((int)first);
  106. moveChildForeground(slider.get());
  107. }
  108. parent->setRedrawParent(true);
  109. parent->redraw();
  110. }
  111. void CListBox::reset()
  112. {
  113. // hack to ensure that all items will be recreated with new address
  114. // save current item list so all shared_ptr's will be destroyed only on scope exit and not inside loop below
  115. // see comment in EventDispatcher::handleLeftButtonClick for details on why this hack is needed
  116. auto itemsCopy = items;
  117. size_t current = first;
  118. for (auto & elem : items)
  119. {
  120. deleteItem(elem);
  121. elem = createItem(current++);
  122. }
  123. updatePositions();
  124. }
  125. void CListBox::resize(size_t newSize)
  126. {
  127. if (totalSize == newSize)
  128. return;
  129. totalSize = newSize;
  130. if (slider)
  131. slider->setAmount((int)totalSize);
  132. reset();
  133. }
  134. size_t CListBox::size()
  135. {
  136. return totalSize;
  137. }
  138. std::shared_ptr<CIntObject> CListBox::getItem(size_t which)
  139. {
  140. if(which < first || which > first + items.size() || which > totalSize)
  141. return std::shared_ptr<CIntObject>();
  142. size_t i=first;
  143. for (auto iter = items.begin(); iter != items.end(); iter++, i++)
  144. if( i == which)
  145. return *iter;
  146. return std::shared_ptr<CIntObject>();
  147. }
  148. std::shared_ptr<CSlider> CListBox::getSlider()
  149. {
  150. return slider;
  151. }
  152. size_t CListBox::getIndexOf(std::shared_ptr<CIntObject> item)
  153. {
  154. size_t i=first;
  155. for(auto iter = items.begin(); iter != items.end(); iter++, i++)
  156. if(*iter == item)
  157. return i;
  158. return size_t(-1);
  159. }
  160. void CListBox::scrollTo(size_t which)
  161. {
  162. //scroll up
  163. if(first > which)
  164. moveToPos(which);
  165. //scroll down
  166. else if (first + items.size() <= which && which < totalSize)
  167. moveToPos(which - items.size() + 1);
  168. }
  169. void CListBox::moveToPos(size_t which)
  170. {
  171. //Calculate new position
  172. size_t maxPossible;
  173. if(totalSize > items.size())
  174. maxPossible = totalSize - items.size();
  175. else
  176. maxPossible = 0;
  177. size_t newPos = std::min(which, maxPossible);
  178. //If move distance is 1 (most of calls from Slider) - use faster shifts instead of resetting all items
  179. if(first - newPos == 1)
  180. {
  181. moveToPrev();
  182. }
  183. else if(newPos - first == 1)
  184. {
  185. moveToNext();
  186. }
  187. else if(newPos != first)
  188. {
  189. first = newPos;
  190. reset();
  191. }
  192. }
  193. void CListBox::moveToNext()
  194. {
  195. //Remove front item and insert new one to end
  196. if(first + items.size() < totalSize)
  197. {
  198. first++;
  199. deleteItem(items.front());
  200. items.pop_front();
  201. items.push_back(createItem(first+items.size()));
  202. updatePositions();
  203. }
  204. }
  205. void CListBox::moveToPrev()
  206. {
  207. //Remove last item and insert new one at start
  208. if(first)
  209. {
  210. first--;
  211. deleteItem(items.back());
  212. items.pop_back();
  213. items.push_front(createItem(first));
  214. updatePositions();
  215. }
  216. }
  217. size_t CListBox::getPos()
  218. {
  219. return first;
  220. }
  221. const std::list<std::shared_ptr<CIntObject>> & CListBox::getItems()
  222. {
  223. return items;
  224. }
  225. CListBoxWithCallback::CListBoxWithCallback(CListBoxWithCallback::MovedPosCallback callback, CreateFunc create, Point pos, Point itemOffset,
  226. size_t visibleSize, size_t totalSize, size_t initialPos, int slider, Rect sliderPos)
  227. : CListBox(create, pos, itemOffset, visibleSize, totalSize, initialPos, slider, sliderPos)
  228. {
  229. movedPosCallback = callback;
  230. }
  231. void CListBoxWithCallback::scrollTo(size_t pos)
  232. {
  233. CListBox::scrollTo(pos);
  234. if(movedPosCallback)
  235. movedPosCallback(getPos());
  236. }
  237. void CListBoxWithCallback::moveToPos(size_t pos)
  238. {
  239. CListBox::moveToPos(pos);
  240. if(movedPosCallback)
  241. movedPosCallback(getPos());
  242. }
  243. void CListBoxWithCallback::moveToNext()
  244. {
  245. CListBox::moveToNext();
  246. if(movedPosCallback)
  247. movedPosCallback(getPos());
  248. }
  249. void CListBoxWithCallback::moveToPrev()
  250. {
  251. CListBox::moveToPrev();
  252. if(movedPosCallback)
  253. movedPosCallback(getPos());
  254. }