ComboBox.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * ComboBox.h, 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. #pragma once
  11. #include "../gui/InterfaceObjectConfigurable.h"
  12. #include "Buttons.h"
  13. class ComboBox : public CButton
  14. {
  15. class DropDown : public InterfaceObjectConfigurable
  16. {
  17. struct Item : public InterfaceObjectConfigurable
  18. {
  19. DropDown & dropDown;
  20. const void * item = nullptr;
  21. Item(const JsonNode &, ComboBox::DropDown &, Point position);
  22. void updateItem(int index, const void * item = nullptr);
  23. void hover(bool on) override;
  24. void clickPressed(const Point & cursorPosition) override;
  25. void clickReleased(const Point & cursorPosition) override;
  26. };
  27. friend struct Item;
  28. public:
  29. DropDown(const JsonNode &, ComboBox &);
  30. bool receiveEvent(const Point & position, int eventType) const override;
  31. void clickPressed(const Point & cursorPosition) override;
  32. void setItem(const void *);
  33. private:
  34. std::shared_ptr<DropDown::Item> buildItem(const JsonNode & config);
  35. void sliderMove(int slidPos);
  36. void updateListItems();
  37. ComboBox & comboBox;
  38. std::vector<std::shared_ptr<Item>> items;
  39. std::vector<const void *> curItems;
  40. };
  41. friend class DropDown;
  42. void setItem(const void *);
  43. public:
  44. ComboBox(Point position, const std::string & defName, const std::pair<std::string, std::string> & help, const JsonNode & dropDownDescriptor, EShortcut key = {}, bool playerColoredButton = false);
  45. //define this callback to fill input vector with data for the combo box
  46. std::function<void(std::vector<const void *> &)> onConstructItems;
  47. //callback is called when item is selected and its value can be used
  48. std::function<void(const void *)> onSetItem;
  49. //return text value from item data
  50. std::function<std::string(int, const void *)> getItemText;
  51. void setItem(int id);
  52. };