CComponent.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include "../../lib/NetPacksBase.h"
  3. #include "CIntObjectClasses.h"
  4. /*
  5. * CComponent.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. /// common popup window component
  14. class CComponent : public virtual CIntObject
  15. {
  16. public:
  17. enum Etype
  18. {
  19. primskill, secskill, resource, creature, artifact, experience, spell, morale, luck, building, hero, flag, typeInvalid
  20. };
  21. //NOTE: not all types have exact these sizes or have less than 4 of them. In such cases closest one will be used
  22. enum ESize
  23. {
  24. tiny, // ~22-24px
  25. small, // ~30px
  26. medium,// ~42px
  27. large, // ~82px
  28. sizeInvalid
  29. };
  30. private:
  31. size_t getIndex();
  32. const std::vector<std::string> getFileName();
  33. void setSurface(std::string defName, int imgPos);
  34. std::string getSubtitleInternal();
  35. void init(Etype Type, int Subtype, int Val, ESize imageSize);
  36. public:
  37. CAnimImage *image; //our image
  38. Etype compType; //component type
  39. ESize size; //component size.
  40. int subtype; //type-dependant subtype. See getSomething methods for details
  41. int val; // value \ strength \ amount of component. See getSomething methods for details
  42. bool perDay; // add "per day" text to subtitle
  43. std::string getDescription();
  44. std::string getSubtitle();
  45. CComponent(Etype Type, int Subtype, int Val = 0, ESize imageSize=large);//c-tor
  46. CComponent(const Component &c); //c-tor
  47. void clickRight(tribool down, bool previousState); //call-in
  48. };
  49. /// component that can be selected or deselected
  50. class CSelectableComponent : public CComponent, public CKeyShortcut
  51. {
  52. void init();
  53. public:
  54. bool selected; //if true, this component is selected
  55. std::function<void()> onSelect; //function called on selection change
  56. void showAll(SDL_Surface * to);
  57. void select(bool on);
  58. void clickLeft(tribool down, bool previousState); //call-in
  59. CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize=large, std::function<void()> OnSelect = nullptr); //c-tor
  60. CSelectableComponent(const Component &c, std::function<void()> OnSelect = nullptr); //c-tor
  61. };
  62. /// box with multiple components (up to 8?)
  63. /// will take ownership on components and delete them afterwards
  64. class CComponentBox : public CIntObject
  65. {
  66. std::vector<CComponent *> components;
  67. CSelectableComponent * selected;
  68. std::function<void(int newID)> onSelect;
  69. void selectionChanged(CSelectableComponent * newSelection);
  70. //get position of "or" text between these comps
  71. //it will place "or" equidistant to both images
  72. Point getOrTextPos(CComponent *left, CComponent * right);
  73. //get distance between these copmonents
  74. int getDistance(CComponent *left, CComponent * right);
  75. void placeComponents(bool selectable);
  76. public:
  77. /// return index of selected item
  78. int selectedIndex();
  79. /// constructor for quite common 1-components popups
  80. /// if position width or height are 0 then it will be determined automatically
  81. CComponentBox(CComponent * components, Rect position);
  82. /// constructor for non-selectable components
  83. CComponentBox(std::vector<CComponent *> components, Rect position);
  84. /// constructor for selectable components
  85. /// will also create "or" labels between components
  86. /// onSelect - optional function that will be called every time on selection change
  87. CComponentBox(std::vector<CSelectableComponent *> components, Rect position, std::function<void(int newID)> onSelect = nullptr);
  88. };
  89. /// Can interact on left and right mouse clicks
  90. class LRClickableAreaWTextComp: public LRClickableAreaWText
  91. {
  92. public:
  93. int baseType;
  94. int bonusValue, type;
  95. virtual void clickLeft(tribool down, bool previousState);
  96. virtual void clickRight(tribool down, bool previousState);
  97. LRClickableAreaWTextComp(const Rect &Pos = Rect(0,0,0,0), int BaseType = -1);
  98. CComponent * createComponent() const;
  99. };