ComboBox.h 2.0 KB

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