CComponent.h 3.4 KB

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