ComboBox.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. void constructItems();
  31. bool receiveEvent(const Point & position, int eventType) const override;
  32. void clickPressed(const Point & cursorPosition) override;
  33. void setItem(const void *);
  34. private:
  35. std::shared_ptr<DropDown::Item> buildItem(const JsonNode & config);
  36. void sliderMove(int slidPos);
  37. void updateListItems();
  38. ComboBox & comboBox;
  39. std::vector<std::shared_ptr<Item>> items;
  40. std::vector<const void *> curItems;
  41. };
  42. friend class DropDown;
  43. void setItem(const void *);
  44. public:
  45. ComboBox(Point position, const std::string & defName, const std::pair<std::string, std::string> & help, const JsonNode & dropDownDescriptor, EShortcut key = {}, bool playerColoredButton = false);
  46. //define this callback to fill input vector with data for the combo box
  47. std::function<void(std::vector<const void *> &)> onConstructItems;
  48. //callback is called when item is selected and its value can be used
  49. std::function<void(const void *)> onSetItem;
  50. //return text value from item data
  51. std::function<std::string(int, const void *)> getItemText;
  52. };