CComponent.h 3.4 KB

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