InterfaceObjectConfigurable.cpp 12 KB

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