InterfaceBuilder.cpp 5.3 KB

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