ComboBox.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * ComboBox.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 "ComboBox.h"
  12. #include "Slider.h"
  13. #include "Images.h"
  14. #include "TextControls.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/WindowHandler.h"
  17. ComboBox::DropDown::Item::Item(const JsonNode & config, ComboBox::DropDown & _dropDown, Point position)
  18. : InterfaceObjectConfigurable(LCLICK | HOVER, position),
  19. dropDown(_dropDown)
  20. {
  21. build(config);
  22. if(auto w = widget<CPicture>("hoverImage"))
  23. {
  24. pos.w = w->pos.w;
  25. pos.h = w->pos.h;
  26. }
  27. setRedrawParent(true);
  28. }
  29. void ComboBox::DropDown::Item::updateItem(int idx, const void * _item)
  30. {
  31. if(auto w = widget<CLabel>("labelName"))
  32. {
  33. item = _item;
  34. if(dropDown.comboBox.getItemText)
  35. w->setText(dropDown.comboBox.getItemText(idx, item));
  36. }
  37. }
  38. void ComboBox::DropDown::Item::hover(bool on)
  39. {
  40. auto h = widget<CPicture>("hoverImage");
  41. auto w = widget<CLabel>("labelName");
  42. if(h && w)
  43. {
  44. if(w->getText().empty())
  45. h->visible = false;
  46. else
  47. h->visible = on;
  48. }
  49. redraw();
  50. }
  51. void ComboBox::DropDown::Item::clickPressed(const Point & cursorPosition)
  52. {
  53. if(isHovered())
  54. dropDown.setItem(item);
  55. }
  56. void ComboBox::DropDown::Item::clickReleased(const Point & cursorPosition)
  57. {
  58. dropDown.clickPressed(cursorPosition);
  59. dropDown.clickReleased(cursorPosition);
  60. }
  61. ComboBox::DropDown::DropDown(const JsonNode & config, ComboBox & _comboBox):
  62. InterfaceObjectConfigurable(LCLICK | HOVER),
  63. comboBox(_comboBox)
  64. {
  65. REGISTER_BUILDER("item", &ComboBox::DropDown::buildItem);
  66. if(comboBox.onConstructItems)
  67. comboBox.onConstructItems(curItems);
  68. addCallback("sliderMove", std::bind(&ComboBox::DropDown::sliderMove, this, std::placeholders::_1));
  69. pos = comboBox.pos;
  70. build(config);
  71. if(auto w = widget<CSlider>("slider"))
  72. {
  73. w->setAmount(curItems.size());
  74. }
  75. //FIXME: this should be done by InterfaceObjectConfigurable, but might have side-effects
  76. pos = children.front()->pos;
  77. for (auto const & child : children)
  78. pos = pos.include(child->pos);
  79. updateListItems();
  80. }
  81. std::shared_ptr<ComboBox::DropDown::Item> ComboBox::DropDown::buildItem(const JsonNode & config)
  82. {
  83. auto position = readPosition(config["position"]);
  84. items.push_back(std::make_shared<Item>(config, *this, position));
  85. return items.back();
  86. }
  87. void ComboBox::DropDown::sliderMove(int slidPos)
  88. {
  89. auto w = widget<CSlider>("slider");
  90. if(!w)
  91. return; // ignore spurious call when slider is being created
  92. updateListItems();
  93. redraw();
  94. }
  95. bool ComboBox::DropDown::receiveEvent(const Point & position, int eventType) const
  96. {
  97. if (eventType == LCLICK)
  98. return true; // we want drop box to close when clicking outside drop box borders
  99. return CIntObject::receiveEvent(position, eventType);
  100. }
  101. void ComboBox::DropDown::clickPressed(const Point & cursorPosition)
  102. {
  103. if (!pos.isInside(cursorPosition))
  104. {
  105. assert(GH.windows().isTopWindow(this));
  106. GH.windows().popWindows(1);
  107. }
  108. }
  109. void ComboBox::DropDown::updateListItems()
  110. {
  111. if(auto w = widget<CSlider>("slider"))
  112. {
  113. int elemIdx = w->getValue();
  114. for(auto item : items)
  115. {
  116. if(elemIdx < curItems.size())
  117. {
  118. item->updateItem(elemIdx, curItems[elemIdx]);
  119. elemIdx++;
  120. }
  121. else
  122. {
  123. item->updateItem(elemIdx);
  124. }
  125. }
  126. }
  127. }
  128. void ComboBox::DropDown::setItem(const void * item)
  129. {
  130. comboBox.setItem(item);
  131. assert(GH.windows().isTopWindow(this));
  132. GH.windows().popWindows(1);
  133. }
  134. ComboBox::ComboBox(Point position, const std::string & defName, const std::pair<std::string, std::string> & help, const JsonNode & dropDownDescriptor, EShortcut key, bool playerColoredButton):
  135. CButton(position, defName, help, 0, key, playerColoredButton)
  136. {
  137. addCallback([&, dropDownDescriptor]()
  138. {
  139. GH.windows().createAndPushWindow<ComboBox::DropDown>(dropDownDescriptor, *this);
  140. });
  141. }
  142. void ComboBox::setItem(const void * item)
  143. {
  144. if(auto w = std::dynamic_pointer_cast<CLabel>(overlay); getItemText)
  145. addTextOverlay(getItemText(0, item), w->font, w->color);
  146. if(onSetItem)
  147. onSetItem(item);
  148. }