InterfaceBuilder.cpp 5.5 KB

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