InterfaceObjectConfigurable.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * InterfaceBuilder.cpp, 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. #include "StdInc.h"
  11. #include "InterfaceObjectConfigurable.h"
  12. #include "../CGameInfo.h"
  13. #include "../gui/CAnimation.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../widgets/CComponent.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/MiscWidgets.h"
  18. #include "../widgets/ObjectLists.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../windows/GUIClasses.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config):
  24. CIntObject()
  25. {
  26. init(config);
  27. }
  28. InterfaceObjectConfigurable::InterfaceObjectConfigurable():
  29. CIntObject()
  30. {
  31. }
  32. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  33. {
  34. callbacks[callbackName] = callback;
  35. }
  36. void InterfaceObjectConfigurable::init(const JsonNode &config)
  37. {
  38. OBJ_CONSTRUCTION;
  39. int unnamedObjectId = 0;
  40. const std::string unnamedObjectPrefix = "__widget_";
  41. for(const auto & item : config["items"].Vector())
  42. {
  43. std::string name = item["name"].isNull()
  44. ? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
  45. : item["name"].String();
  46. widgets[name] = buildWidget(item);
  47. }
  48. }
  49. std::string InterfaceObjectConfigurable::buildText(const JsonNode & config) const
  50. {
  51. if(config.isNull())
  52. return "";
  53. if(config.isNumber())
  54. {
  55. return CGI->generaltexth->allTexts[config.Integer()];
  56. }
  57. return config.String();
  58. }
  59. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(const JsonNode & config)
  60. {
  61. assert(!config.isNull());
  62. auto type = config["type"].String();
  63. int x = 0, y = 0;
  64. if(!config["position"].isNull())
  65. {
  66. x = config["position"]["x"].Integer();
  67. y = config["position"]["y"].Integer();
  68. }
  69. std::string image;
  70. std::string text = buildText(config["text"]);
  71. auto alignment = EAlignment::CENTER;
  72. auto color = Colors::DEFAULT_KEY_COLOR;
  73. auto font = EFonts::FONT_TIMES;
  74. if(!config["image"].isNull())
  75. image = config["image"].String();
  76. if(!config["alignment"].isNull())
  77. {
  78. if(config["alignment"].String() == "left")
  79. alignment = EAlignment::TOPLEFT;
  80. if(config["alignment"].String() == "center")
  81. alignment = EAlignment::CENTER;
  82. if(config["alignment"].String() == "right")
  83. alignment = EAlignment::BOTTOMRIGHT;
  84. }
  85. if(!config["color"].isNull())
  86. {
  87. if(config["color"].String() == "yellow")
  88. color = Colors::YELLOW;
  89. if(config["color"].String() == "white")
  90. color = Colors::WHITE;
  91. if(config["color"].String() == "gold")
  92. color = Colors::METALLIC_GOLD;
  93. if(config["color"].String() == "green")
  94. color = Colors::GREEN;
  95. if(config["color"].String() == "orange")
  96. color = Colors::ORANGE;
  97. if(config["color"].String() == "bright-yellow")
  98. color = Colors::BRIGHT_YELLOW;
  99. }
  100. if(!config["font"].isNull())
  101. {
  102. if(config["font"].String() == "big")
  103. font = EFonts::FONT_BIG;
  104. if(config["font"].String() == "medium")
  105. font = EFonts::FONT_MEDIUM;
  106. if(config["font"].String() == "small")
  107. font = EFonts::FONT_SMALL;
  108. if(config["font"].String() == "tiny")
  109. font = EFonts::FONT_TINY;
  110. }
  111. if(type == "picture")
  112. {
  113. return std::make_shared<CPicture>(image, x, y);
  114. }
  115. if(type == "label")
  116. {
  117. return std::make_shared<CLabel>(x, y, font, alignment, color, text);
  118. }
  119. if(type == "toggleGroup")
  120. {
  121. auto group = std::make_shared<CToggleGroup>(0);
  122. group->pos.x += x;
  123. group->pos.y += y;
  124. if(!config["items"].isNull())
  125. {
  126. SObjectConstruction obj__i(group.get());
  127. int itemIdx = -1;
  128. for(const auto & item : config["items"].Vector())
  129. {
  130. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  131. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  132. }
  133. }
  134. if(!config["selected"].isNull())
  135. group->setSelected(config["selected"].Integer());
  136. if(!config["callback"].isNull())
  137. group->addCallback(callbacks[config["callback"].String()]);
  138. return group;
  139. }
  140. if(type == "toggleButton")
  141. {
  142. std::pair<std::string, std::string> zelp;
  143. if(!config["zelp"].isNull())
  144. zelp = CGI->generaltexth->zelp[config["zelp"].Integer()];
  145. auto button = std::make_shared<CToggleButton>(Point(x, y), image, zelp);
  146. if(!config["selected"].isNull())
  147. button->setSelected(config["selected"].Bool());
  148. if(!config["imageOrder"].isNull())
  149. {
  150. auto imgOrder = config["imageOrder"].Vector();
  151. assert(imgOrder.size() >= 4);
  152. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  153. }
  154. if(!config["callback"].isNull())
  155. button->addCallback(callbacks[config["callback"].String()]);
  156. return button;
  157. }
  158. if(type == "button")
  159. {
  160. std::pair<std::string, std::string> zelp;
  161. if(!config["zelp"].isNull())
  162. zelp = CGI->generaltexth->zelp[config["zelp"].Integer()];
  163. auto button = std::make_shared<CButton>(Point(x, y), image, zelp);
  164. if(!config["items"].isNull())
  165. {
  166. for(const auto & item : config["items"].Vector())
  167. {
  168. button->addOverlay(buildWidget(item));
  169. }
  170. }
  171. if(!config["callback"].isNull())
  172. button->addCallback(std::bind(callbacks[config["callback"].String()], 0));
  173. return button;
  174. }
  175. if(type == "labelGroup")
  176. {
  177. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  178. if(!config["items"].isNull())
  179. {
  180. for(const auto & item : config["items"].Vector())
  181. {
  182. if(!item["position"].isNull())
  183. {
  184. x = item["position"]["x"].Integer();
  185. y = item["position"]["y"].Integer();
  186. }
  187. if(!item["text"].isNull())
  188. text = buildText(item["text"]);
  189. group->add(x, y, text);
  190. }
  191. }
  192. return group;
  193. }
  194. return std::shared_ptr<CIntObject>(nullptr);
  195. }