CComponent.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * CComponent.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/CIntObject.h"
  12. #include "../render/EFont.h"
  13. #include "../../lib/filesystem/ResourcePath.h"
  14. #include "../../lib/networkPacks/Component.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. struct Component;
  17. VCMI_LIB_NAMESPACE_END
  18. class CAnimImage;
  19. class CLabel;
  20. /// common popup window component
  21. class CComponent : public virtual CIntObject
  22. {
  23. public:
  24. //NOTE: not all types have exact these sizes or have less than 4 of them. In such cases closest one will be used
  25. enum ESize
  26. {
  27. tiny, // ~22-24px
  28. small, // ~30px
  29. medium,// ~42px
  30. large, // ~82px
  31. sizeInvalid
  32. };
  33. private:
  34. std::vector<std::shared_ptr<CLabel>> lines;
  35. size_t getIndex() const;
  36. std::vector<AnimationPath> getFileName() const;
  37. void setSurface(const AnimationPath & defName, int imgPos);
  38. void init(ComponentType Type, ComponentSubType Subtype, std::optional<int32_t> Val, ESize imageSize, EFonts font, const std::string & ValText);
  39. public:
  40. std::shared_ptr<CAnimImage> image;
  41. Component data;
  42. std::string customSubtitle;
  43. ESize size; //component size.
  44. EFonts font; //Font size of label
  45. bool newLine; //Line break after component
  46. std::string getDescription() const;
  47. std::string getSubtitle() const;
  48. CComponent(ComponentType Type, ComponentSubType Subtype, std::optional<int32_t> Val = std::nullopt, ESize imageSize=large, EFonts font = FONT_SMALL);
  49. CComponent(ComponentType Type, ComponentSubType Subtype, const std::string & Val, ESize imageSize=large, EFonts font = FONT_SMALL);
  50. CComponent(const Component &c, ESize imageSize=large, EFonts font = FONT_SMALL);
  51. void showPopupWindow(const Point & cursorPosition) override; //call-in
  52. };
  53. /// component that can be selected or deselected
  54. class CSelectableComponent : public CComponent, public CKeyShortcut
  55. {
  56. void init();
  57. public:
  58. bool selected; //if true, this component is selected
  59. std::function<void()> onSelect; //function called on selection change
  60. std::function<void()> onChoose; //function called on doubleclick
  61. void showAll(Canvas & to) override;
  62. void select(bool on);
  63. void clickPressed(const Point & cursorPosition) override; //call-in
  64. void clickDouble(const Point & cursorPosition) override; //call-in
  65. CSelectableComponent(ComponentType Type, ComponentSubType Sub, int Val, ESize imageSize=large, std::function<void()> OnSelect = nullptr);
  66. CSelectableComponent(const Component & c, std::function<void()> OnSelect = nullptr);
  67. };
  68. /// box with multiple components (up to 8?)
  69. /// will take ownership on components and delete them afterwards
  70. class CComponentBox : public CIntObject
  71. {
  72. std::vector<std::shared_ptr<CComponent>> components;
  73. std::vector<std::shared_ptr<CLabel>> orLabels;
  74. std::shared_ptr<CSelectableComponent> selected;
  75. std::function<void(int newID)> onSelect;
  76. static constexpr int defaultBetweenImagesMin = 42;
  77. static constexpr int defaultBetweenSubtitlesMin = 10;
  78. static constexpr int defaultBetweenRows = 22;
  79. static constexpr int defaultComponentsInRow = 4;
  80. int betweenImagesMin;
  81. int betweenSubtitlesMin;
  82. int betweenRows;
  83. int componentsInRow;
  84. void selectionChanged(std::shared_ptr<CSelectableComponent> newSelection);
  85. //get position of "or" text between these comps
  86. //it will place "or" equidistant to both images
  87. Point getOrTextPos(CComponent *left, CComponent * right);
  88. //get distance between these copmonents
  89. int getDistance(CComponent *left, CComponent * right);
  90. void placeComponents(bool selectable);
  91. public:
  92. /// return index of selected item
  93. int selectedIndex();
  94. /// constructors for non-selectable components
  95. CComponentBox(std::vector<std::shared_ptr<CComponent>> components, Rect position);
  96. CComponentBox(std::vector<std::shared_ptr<CComponent>> components, Rect position, int betweenImagesMin, int betweenSubtitlesMin, int betweenRows, int componentsInRow);
  97. /// constructor for selectable components
  98. /// will also create "or" labels between components
  99. /// onSelect - optional function that will be called every time on selection change
  100. CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> components, Rect position, std::function<void(int newID)> onSelect = nullptr);
  101. CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> components, Rect position, std::function<void(int newID)> onSelect, int betweenImagesMin, int betweenSubtitlesMin, int betweenRows, int componentsInRow);
  102. };