InterfaceObjectConfigurable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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, int used, Point offset):
  24. InterfaceObjectConfigurable(used, offset)
  25. {
  26. init(config);
  27. }
  28. InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
  29. CIntObject(used, offset)
  30. {
  31. REGISTER_BUILDER("picture", &InterfaceObjectConfigurable::buildPicture);
  32. REGISTER_BUILDER("image", &InterfaceObjectConfigurable::buildImage);
  33. REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture);
  34. REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation);
  35. REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel);
  36. REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup);
  37. REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton);
  38. REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
  39. REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
  40. REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
  41. }
  42. void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
  43. {
  44. builders[type] = f;
  45. }
  46. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  47. {
  48. callbacks[callbackName] = callback;
  49. }
  50. void InterfaceObjectConfigurable::init(const JsonNode &config)
  51. {
  52. OBJ_CONSTRUCTION;
  53. logGlobal->debug("Building configurable interface object");
  54. for(auto & item : config["variables"].Struct())
  55. {
  56. logGlobal->debug("Read variable named %s", item.first);
  57. variables[item.first] = item.second;
  58. }
  59. int unnamedObjectId = 0;
  60. const std::string unnamedObjectPrefix = "__widget_";
  61. for(const auto & item : config["items"].Vector())
  62. {
  63. std::string name = item["name"].isNull()
  64. ? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
  65. : item["name"].String();
  66. logGlobal->debug("Building widget with name %s", name);
  67. widgets[name] = buildWidget(item);
  68. }
  69. }
  70. std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
  71. {
  72. if(config.isNull())
  73. return "";
  74. if(config.isNumber())
  75. {
  76. logGlobal->debug("Reading text from generaltext handler id:%d", config.Integer());
  77. return CGI->generaltexth->allTexts[config.Integer()];
  78. }
  79. const std::string delimiter = "/";
  80. std::string s = config.String();
  81. logGlobal->debug("Reading text from translations by key: %s", s);
  82. JsonNode translated = CGI->generaltexth->localizedTexts;
  83. for(size_t p = s.find(delimiter); p != std::string::npos; p = s.find(delimiter))
  84. {
  85. translated = translated[s.substr(0, p)];
  86. s.erase(0, p + delimiter.length());
  87. }
  88. if(s == config.String())
  89. {
  90. logGlobal->warn("Reading non-translated text: %s", s);
  91. return s;
  92. }
  93. return translated[s].String();
  94. }
  95. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  96. {
  97. Point p;
  98. logGlobal->debug("Reading point");
  99. p.x = config["x"].Integer();
  100. p.y = config["y"].Integer();
  101. return p;
  102. }
  103. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  104. {
  105. Rect p;
  106. logGlobal->debug("Reading rect");
  107. p.x = config["x"].Integer();
  108. p.y = config["y"].Integer();
  109. p.w = config["w"].Integer();
  110. p.h = config["h"].Integer();
  111. return p;
  112. }
  113. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  114. {
  115. logGlobal->debug("Reading text alignment");
  116. if(!config.isNull())
  117. {
  118. if(config.String() == "center")
  119. return ETextAlignment::CENTER;
  120. if(config.String() == "left")
  121. return ETextAlignment::TOPLEFT;
  122. if(config.String() == "right")
  123. return ETextAlignment::BOTTOMRIGHT;
  124. }
  125. logGlobal->debug("Uknown text alignment attribute");
  126. return ETextAlignment::CENTER;
  127. }
  128. SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  129. {
  130. logGlobal->debug("Reading color");
  131. if(!config.isNull())
  132. {
  133. if(config.String() == "yellow")
  134. return Colors::YELLOW;
  135. if(config.String() == "white")
  136. return Colors::WHITE;
  137. if(config.String() == "gold")
  138. return Colors::METALLIC_GOLD;
  139. if(config.String() == "green")
  140. return Colors::GREEN;
  141. if(config.String() == "orange")
  142. return Colors::ORANGE;
  143. if(config.String() == "bright-yellow")
  144. return Colors::BRIGHT_YELLOW;
  145. }
  146. logGlobal->debug("Uknown color attribute");
  147. return Colors::DEFAULT_KEY_COLOR;
  148. }
  149. EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
  150. {
  151. logGlobal->debug("Reading font");
  152. if(!config.isNull())
  153. {
  154. if(config.String() == "big")
  155. return EFonts::FONT_BIG;
  156. if(config.String() == "medium")
  157. return EFonts::FONT_MEDIUM;
  158. if(config.String() == "small")
  159. return EFonts::FONT_SMALL;
  160. if(config.String() == "tiny")
  161. return EFonts::FONT_TINY;
  162. }
  163. logGlobal->debug("Uknown font attribute");
  164. return EFonts::FONT_TIMES;
  165. }
  166. std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
  167. {
  168. logGlobal->debug("Reading hint text");
  169. std::pair<std::string, std::string> result;
  170. if(!config.isNull())
  171. {
  172. if(config.isNumber())
  173. {
  174. logGlobal->debug("Reading hint text (zelp) from generaltext handler id:%d", config.Integer());
  175. return CGI->generaltexth->zelp[config.Integer()];
  176. }
  177. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  178. {
  179. result.first = readText(config["hover"]);
  180. result.second = readText(config["help"]);
  181. return result;
  182. }
  183. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  184. {
  185. logGlobal->debug("Reading non-translated hint: %s", config.String());
  186. result.first = result.second = config.String();
  187. }
  188. }
  189. return result;
  190. }
  191. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  192. {
  193. logGlobal->debug("Building widget CPicture");
  194. auto image = config["image"].String();
  195. auto position = readPosition(config["position"]);
  196. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  197. if(!config["visible"].isNull())
  198. pic->visible = config["visible"].Bool();
  199. return pic;
  200. }
  201. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  202. {
  203. logGlobal->debug("Building widget CLabel");
  204. auto font = readFont(config["font"]);
  205. auto alignment = readTextAlignment(config["alignment"]);
  206. auto color = readColor(config["color"]);
  207. auto text = readText(config["text"]);
  208. auto position = readPosition(config["position"]);
  209. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  210. }
  211. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  212. {
  213. logGlobal->debug("Building widget CToggleGroup");
  214. auto position = readPosition(config["position"]);
  215. auto group = std::make_shared<CToggleGroup>(0);
  216. group->pos += position;
  217. if(!config["items"].isNull())
  218. {
  219. OBJ_CONSTRUCTION_TARGETED(group.get());
  220. int itemIdx = -1;
  221. for(const auto & item : config["items"].Vector())
  222. {
  223. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  224. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  225. }
  226. }
  227. if(!config["selected"].isNull())
  228. group->setSelected(config["selected"].Integer());
  229. if(!config["callback"].isNull())
  230. group->addCallback(callbacks.at(config["callback"].String()));
  231. return group;
  232. }
  233. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  234. {
  235. logGlobal->debug("Building widget CToggleButton");
  236. auto position = readPosition(config["position"]);
  237. auto image = config["image"].String();
  238. auto zelp = readHintText(config["zelp"]);
  239. auto button = std::make_shared<CToggleButton>(position, image, zelp);
  240. if(!config["selected"].isNull())
  241. button->setSelected(config["selected"].Bool());
  242. if(!config["imageOrder"].isNull())
  243. {
  244. auto imgOrder = config["imageOrder"].Vector();
  245. assert(imgOrder.size() >= 4);
  246. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  247. }
  248. if(!config["callback"].isNull())
  249. button->addCallback(callbacks.at(config["callback"].String()));
  250. return button;
  251. }
  252. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  253. {
  254. logGlobal->debug("Building widget CButton");
  255. auto position = readPosition(config["position"]);
  256. auto image = config["image"].String();
  257. auto zelp = readHintText(config["zelp"]);
  258. auto button = std::make_shared<CButton>(position, image, zelp);
  259. if(!config["items"].isNull())
  260. {
  261. for(const auto & item : config["items"].Vector())
  262. {
  263. button->addOverlay(buildWidget(item));
  264. }
  265. }
  266. if(!config["callback"].isNull())
  267. button->addCallback(std::bind(callbacks.at(config["callback"].String()), 0));
  268. return button;
  269. }
  270. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  271. {
  272. logGlobal->debug("Building widget CLabelGroup");
  273. auto font = readFont(config["font"]);
  274. auto alignment = readTextAlignment(config["alignment"]);
  275. auto color = readColor(config["color"]);
  276. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  277. if(!config["items"].isNull())
  278. {
  279. for(const auto & item : config["items"].Vector())
  280. {
  281. auto position = readPosition(item["position"]);
  282. auto text = readText(item["text"]);
  283. group->add(position.x, position.y, text);
  284. }
  285. }
  286. return group;
  287. }
  288. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  289. {
  290. logGlobal->debug("Building widget CSlider");
  291. auto position = readPosition(config["position"]);
  292. int length = config["size"].Integer();
  293. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  294. auto itemsVisible = config["itemsVisible"].Integer();
  295. auto itemsTotal = config["itemsTotal"].Integer();
  296. auto value = config["selected"].Integer();
  297. bool horizontal = config["orientation"].String() == "horizontal";
  298. return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  299. }
  300. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  301. {
  302. logGlobal->debug("Building widget CAnimImage");
  303. auto position = readPosition(config["position"]);
  304. auto image = config["image"].String();
  305. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  306. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  307. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  308. }
  309. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  310. {
  311. logGlobal->debug("Building widget CFilledTexture");
  312. auto image = config["image"].String();
  313. auto rect = readRect(config["rect"]);
  314. return std::make_shared<CFilledTexture>(image, rect);
  315. }
  316. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  317. {
  318. logGlobal->debug("Building widget CShowableAnim");
  319. auto position = readPosition(config["position"]);
  320. auto image = config["image"].String();
  321. ui8 flags = 0;
  322. if(!config["repeat"].Bool())
  323. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  324. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  325. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  326. if(!config["alpha"].isNull())
  327. anim->setAlpha(config["alpha"].Integer());
  328. if(!config["callback"].isNull())
  329. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  330. if(!config["frames"].isNull())
  331. {
  332. auto b = config["frames"]["start"].Integer();
  333. auto e = config["frames"]["end"].Integer();
  334. anim->set(group, b, e);
  335. }
  336. return anim;
  337. }
  338. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  339. {
  340. assert(!config.isNull());
  341. logGlobal->debug("Building widget from config");
  342. //overrides from variables
  343. for(auto & item : config["overrides"].Struct())
  344. {
  345. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  346. config[item.first] = variables[item.second.String()];
  347. }
  348. auto type = config["type"].String();
  349. auto buildIterator = builders.find(type);
  350. if(buildIterator != builders.end())
  351. return (buildIterator->second)(config);
  352. logGlobal->error("Builder with type %s is not registered", type);
  353. return nullptr;
  354. }