ComboBox.cpp 4.0 KB

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