CComponent.h 4.7 KB

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