CComponent.h 4.3 KB

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