CComponent.h 3.5 KB

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