2
0

InterfaceBuilder.cpp 5.5 KB

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