InterfaceObjectConfigurable.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 "../CPlayerInterface.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/ShortcutHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../render/Graphics.h"
  18. #include "../render/IFont.h"
  19. #include "../widgets/CComponent.h"
  20. #include "../widgets/ComboBox.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/MiscWidgets.h"
  23. #include "../widgets/ObjectLists.h"
  24. #include "../widgets/Slider.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../windows/GUIClasses.h"
  27. #include "../windows/InfoWindows.h"
  28. #include "../../lib//constants/StringConstants.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/filesystem/ResourceID.h"
  31. InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
  32. InterfaceObjectConfigurable(used, offset)
  33. {
  34. build(config);
  35. }
  36. InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
  37. CIntObject(used, offset)
  38. {
  39. REGISTER_BUILDER("picture", &InterfaceObjectConfigurable::buildPicture);
  40. REGISTER_BUILDER("image", &InterfaceObjectConfigurable::buildImage);
  41. REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture);
  42. REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation);
  43. REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel);
  44. REGISTER_BUILDER("multiLineLabel", &InterfaceObjectConfigurable::buildMultiLineLabel);
  45. REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup);
  46. REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton);
  47. REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
  48. REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
  49. REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
  50. REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout);
  51. REGISTER_BUILDER("comboBox", &InterfaceObjectConfigurable::buildComboBox);
  52. REGISTER_BUILDER("textInput", &InterfaceObjectConfigurable::buildTextInput);
  53. }
  54. void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
  55. {
  56. builders[type] = f;
  57. }
  58. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  59. {
  60. callbacks_int[callbackName] = callback;
  61. }
  62. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(std::string)> callback)
  63. {
  64. callbacks_string[callbackName] = callback;
  65. }
  66. void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
  67. {
  68. auto iter = widgets.find(name);
  69. if(iter != widgets.end())
  70. widgets.erase(iter);
  71. }
  72. void InterfaceObjectConfigurable::loadCustomBuilders(const JsonNode & config)
  73. {
  74. for(auto & item : config.Struct())
  75. {
  76. std::string typeName = item.first;
  77. JsonNode baseConfig = item.second;
  78. auto const & functor = [this, baseConfig](const JsonNode & widgetConfig) -> std::shared_ptr<CIntObject>
  79. {
  80. JsonNode actualConfig = widgetConfig;
  81. JsonUtils::mergeCopy(actualConfig, baseConfig);
  82. return this->buildWidget(actualConfig);
  83. };
  84. registerBuilder(typeName, functor);
  85. }
  86. }
  87. void InterfaceObjectConfigurable::build(const JsonNode &config)
  88. {
  89. OBJ_CONSTRUCTION;
  90. logGlobal->debug("Building configurable interface object");
  91. auto * items = &config;
  92. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  93. {
  94. if (!config["library"].isNull())
  95. {
  96. const JsonNode library(ResourceID(config["library"].String()));
  97. loadCustomBuilders(library);
  98. }
  99. loadCustomBuilders(config["customTypes"]);
  100. for(auto & item : config["variables"].Struct())
  101. {
  102. logGlobal->debug("Read variable named %s", item.first);
  103. variables[item.first] = item.second;
  104. }
  105. items = &config["items"];
  106. }
  107. for(const auto & item : items->Vector())
  108. addWidget(item["name"].String(), buildWidget(item));
  109. }
  110. void InterfaceObjectConfigurable::addConditional(const std::string & name, bool active)
  111. {
  112. conditionals[name] = active;
  113. }
  114. void InterfaceObjectConfigurable::addWidget(const std::string & namePreferred, std::shared_ptr<CIntObject> widget)
  115. {
  116. static const std::string unnamedObjectPrefix = "__widget_";
  117. std::string nameActual;
  118. if (widgets.count(namePreferred) == 0)
  119. nameActual = namePreferred;
  120. else
  121. logGlobal->error("Duplicated widget name: '%s'", namePreferred);
  122. if (nameActual.empty())
  123. nameActual = unnamedObjectPrefix + std::to_string(unnamedObjectId++);
  124. logGlobal->debug("Building widget with name %s", nameActual);
  125. widgets[nameActual] = widget;
  126. }
  127. std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
  128. {
  129. if(config.isNull())
  130. return "";
  131. std::string s = config.String();
  132. if(s.empty())
  133. return s;
  134. logGlobal->debug("Reading text from translations by key: %s", s);
  135. return CGI->generaltexth->translate(s);
  136. }
  137. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  138. {
  139. Point p;
  140. logGlobal->debug("Reading point");
  141. p.x = config["x"].Integer();
  142. p.y = config["y"].Integer();
  143. return p;
  144. }
  145. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  146. {
  147. Rect p;
  148. logGlobal->debug("Reading rect");
  149. p.x = config["x"].Integer();
  150. p.y = config["y"].Integer();
  151. p.w = config["w"].Integer();
  152. p.h = config["h"].Integer();
  153. return p;
  154. }
  155. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  156. {
  157. logGlobal->debug("Reading text alignment");
  158. if(!config.isNull())
  159. {
  160. if(config.String() == "center")
  161. return ETextAlignment::CENTER;
  162. if(config.String() == "left")
  163. return ETextAlignment::TOPLEFT;
  164. if(config.String() == "right")
  165. return ETextAlignment::BOTTOMRIGHT;
  166. }
  167. logGlobal->debug("Uknown text alignment attribute");
  168. return ETextAlignment::CENTER;
  169. }
  170. ColorRGBA InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  171. {
  172. logGlobal->debug("Reading color");
  173. if(!config.isNull())
  174. {
  175. if(config.isString())
  176. {
  177. if(config.String() == "yellow")
  178. return Colors::YELLOW;
  179. if(config.String() == "white")
  180. return Colors::WHITE;
  181. if(config.String() == "gold")
  182. return Colors::METALLIC_GOLD;
  183. if(config.String() == "green")
  184. return Colors::GREEN;
  185. if(config.String() == "orange")
  186. return Colors::ORANGE;
  187. if(config.String() == "bright-yellow")
  188. return Colors::BRIGHT_YELLOW;
  189. }
  190. if(config.isVector())
  191. {
  192. const auto & asVector = config.Vector();
  193. if(asVector.size() == 4)
  194. return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer(), asVector[3].Integer());
  195. if(asVector.size() == 3)
  196. return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer());
  197. }
  198. }
  199. logGlobal->debug("Uknown color attribute");
  200. return Colors::DEFAULT_KEY_COLOR;
  201. }
  202. PlayerColor InterfaceObjectConfigurable::readPlayerColor(const JsonNode & config) const
  203. {
  204. logGlobal->debug("Reading PlayerColor");
  205. if(!config.isNull() && config.isString())
  206. return PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, config.String()));
  207. logGlobal->debug("Unknown PlayerColor attribute");
  208. return PlayerColor::CANNOT_DETERMINE;
  209. }
  210. EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
  211. {
  212. logGlobal->debug("Reading font");
  213. if(!config.isNull())
  214. {
  215. if(config.String() == "big")
  216. return EFonts::FONT_BIG;
  217. if(config.String() == "medium")
  218. return EFonts::FONT_MEDIUM;
  219. if(config.String() == "small")
  220. return EFonts::FONT_SMALL;
  221. if(config.String() == "tiny")
  222. return EFonts::FONT_TINY;
  223. if(config.String() == "calisto")
  224. return EFonts::FONT_CALLI;
  225. }
  226. logGlobal->debug("Uknown font attribute");
  227. return EFonts::FONT_TIMES;
  228. }
  229. std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
  230. {
  231. logGlobal->debug("Reading hint text");
  232. std::pair<std::string, std::string> result;
  233. if(!config.isNull())
  234. {
  235. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  236. {
  237. result.first = readText(config["hover"]);
  238. result.second = readText(config["help"]);
  239. return result;
  240. }
  241. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  242. {
  243. logGlobal->debug("Reading hint text (help) from generaltext handler:%sd", config.String());
  244. result.first = CGI->generaltexth->translate( config.String(), "hover");
  245. result.second = CGI->generaltexth->translate( config.String(), "help");
  246. }
  247. }
  248. return result;
  249. }
  250. EShortcut InterfaceObjectConfigurable::readHotkey(const JsonNode & config) const
  251. {
  252. logGlobal->debug("Reading hotkey");
  253. if(config.getType() != JsonNode::JsonType::DATA_STRING)
  254. {
  255. logGlobal->error("Invalid hotket format in interface configuration! Expected string!", config.String());
  256. return EShortcut::NONE;
  257. }
  258. EShortcut result = GH.shortcuts().findShortcut(config.String());
  259. if (result == EShortcut::NONE)
  260. logGlobal->error("Invalid hotkey '%s' in interface configuration!", config.String());
  261. return result;;
  262. }
  263. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  264. {
  265. logGlobal->debug("Building widget CPicture");
  266. auto image = config["image"].String();
  267. auto position = readPosition(config["position"]);
  268. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  269. if(!config["visible"].isNull())
  270. pic->visible = config["visible"].Bool();
  271. if ( config["playerColored"].Bool() && LOCPLINT)
  272. pic->colorize(LOCPLINT->playerID);
  273. return pic;
  274. }
  275. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  276. {
  277. logGlobal->debug("Building widget CLabel");
  278. auto font = readFont(config["font"]);
  279. auto alignment = readTextAlignment(config["alignment"]);
  280. auto color = readColor(config["color"]);
  281. auto text = readText(config["text"]);
  282. auto position = readPosition(config["position"]);
  283. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  284. }
  285. std::shared_ptr<CMultiLineLabel> InterfaceObjectConfigurable::buildMultiLineLabel(const JsonNode & config) const
  286. {
  287. logGlobal->debug("Building widget CMultiLineLabel");
  288. auto font = readFont(config["font"]);
  289. auto alignment = readTextAlignment(config["alignment"]);
  290. auto color = readColor(config["color"]);
  291. auto text = readText(config["text"]);
  292. Rect rect = readRect(config["rect"]);
  293. if(!config["adoptHeight"].isNull() && config["adoptHeight"].Bool())
  294. rect.h = graphics->fonts[font]->getLineHeight() * 2;
  295. return std::make_shared<CMultiLineLabel>(rect, font, alignment, color, text);
  296. }
  297. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  298. {
  299. logGlobal->debug("Building widget CToggleGroup");
  300. auto position = readPosition(config["position"]);
  301. auto group = std::make_shared<CToggleGroup>(0);
  302. group->pos += position;
  303. if(!config["items"].isNull())
  304. {
  305. OBJ_CONSTRUCTION_TARGETED(group.get());
  306. int itemIdx = -1;
  307. for(const auto & item : config["items"].Vector())
  308. {
  309. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  310. auto newToggle = std::dynamic_pointer_cast<CToggleButton>(buildWidget(item));
  311. group->addToggle(itemIdx, newToggle);
  312. }
  313. }
  314. if(!config["selected"].isNull())
  315. group->setSelected(config["selected"].Integer());
  316. if(!config["callback"].isNull())
  317. group->addCallback(callbacks_int.at(config["callback"].String()));
  318. return group;
  319. }
  320. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  321. {
  322. logGlobal->debug("Building widget CToggleButton");
  323. auto position = readPosition(config["position"]);
  324. auto image = config["image"].String();
  325. auto help = readHintText(config["help"]);
  326. auto button = std::make_shared<CToggleButton>(position, image, help);
  327. if(!config["items"].isNull())
  328. {
  329. for(const auto & item : config["items"].Vector())
  330. {
  331. button->addOverlay(buildWidget(item));
  332. }
  333. }
  334. if(!config["selected"].isNull())
  335. button->setSelected(config["selected"].Bool());
  336. if(!config["imageOrder"].isNull())
  337. {
  338. auto imgOrder = config["imageOrder"].Vector();
  339. assert(imgOrder.size() >= 4);
  340. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  341. }
  342. loadToggleButtonCallback(button, config["callback"]);
  343. return button;
  344. }
  345. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  346. {
  347. logGlobal->debug("Building widget CButton");
  348. auto position = readPosition(config["position"]);
  349. auto image = config["image"].String();
  350. auto help = readHintText(config["help"]);
  351. auto button = std::make_shared<CButton>(position, image, help);
  352. if(!config["items"].isNull())
  353. {
  354. for(const auto & item : config["items"].Vector())
  355. {
  356. button->addOverlay(buildWidget(item));
  357. }
  358. }
  359. if(!config["imageOrder"].isNull())
  360. {
  361. auto imgOrder = config["imageOrder"].Vector();
  362. assert(imgOrder.size() >= 4);
  363. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  364. }
  365. loadButtonBorderColor(button, config["borderColor"]);
  366. loadButtonCallback(button, config["callback"]);
  367. loadButtonHotkey(button, config["hotkey"]);
  368. return button;
  369. }
  370. void InterfaceObjectConfigurable::loadButtonBorderColor(std::shared_ptr<CButton> button, const JsonNode & config) const
  371. {
  372. if (config.isNull())
  373. return;
  374. auto color = readColor(config);
  375. button->setBorderColor(color);
  376. }
  377. void InterfaceObjectConfigurable::loadToggleButtonCallback(std::shared_ptr<CToggleButton> button, const JsonNode & config) const
  378. {
  379. if(config.isNull())
  380. return;
  381. std::string callbackName = config.String();
  382. if (callbacks_int.count(callbackName) > 0)
  383. button->addCallback(callbacks_int.at(callbackName));
  384. else
  385. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  386. }
  387. void InterfaceObjectConfigurable::loadButtonCallback(std::shared_ptr<CButton> button, const JsonNode & config) const
  388. {
  389. if(config.isNull())
  390. return;
  391. std::string callbackName = config.String();
  392. if (callbacks_int.count(callbackName) > 0)
  393. button->addCallback(std::bind(callbacks_int.at(callbackName), 0));
  394. else
  395. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  396. }
  397. void InterfaceObjectConfigurable::loadButtonHotkey(std::shared_ptr<CButton> button, const JsonNode & config) const
  398. {
  399. if(config.isNull())
  400. return;
  401. if(config.getType() != JsonNode::JsonType::DATA_STRING)
  402. {
  403. logGlobal->error("Invalid shortcut format - string expected!");
  404. return;
  405. }
  406. button->assignedKey = readHotkey(config);
  407. auto target = shortcuts.find(button->assignedKey);
  408. if (target == shortcuts.end())
  409. return;
  410. button->addCallback(target->second.callback);
  411. target->second.assignedToButton = true;
  412. }
  413. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  414. {
  415. logGlobal->debug("Building widget CLabelGroup");
  416. auto font = readFont(config["font"]);
  417. auto alignment = readTextAlignment(config["alignment"]);
  418. auto color = readColor(config["color"]);
  419. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  420. if(!config["items"].isNull())
  421. {
  422. for(const auto & item : config["items"].Vector())
  423. {
  424. auto position = readPosition(item["position"]);
  425. auto text = readText(item["text"]);
  426. group->add(position.x, position.y, text);
  427. }
  428. }
  429. return group;
  430. }
  431. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  432. {
  433. logGlobal->debug("Building widget CSlider");
  434. auto position = readPosition(config["position"]);
  435. int length = config["size"].Integer();
  436. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  437. auto itemsVisible = config["itemsVisible"].Integer();
  438. auto itemsTotal = config["itemsTotal"].Integer();
  439. auto value = config["selected"].Integer();
  440. bool horizontal = config["orientation"].String() == "horizontal";
  441. const auto & result =
  442. std::make_shared<CSlider>(position, length, callbacks_int.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal ? Orientation::HORIZONTAL : Orientation::VERTICAL, style);
  443. if(!config["scrollBounds"].isNull())
  444. {
  445. Rect bounds = readRect(config["scrollBounds"]);
  446. result->setScrollBounds(bounds);
  447. }
  448. if(!config["panningStep"].isNull())
  449. result->setPanningStep(config["panningStep"].Integer());
  450. return result;
  451. }
  452. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  453. {
  454. logGlobal->debug("Building widget CAnimImage");
  455. auto position = readPosition(config["position"]);
  456. auto image = config["image"].String();
  457. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  458. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  459. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  460. }
  461. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  462. {
  463. logGlobal->debug("Building widget CFilledTexture");
  464. auto image = config["image"].String();
  465. auto rect = readRect(config["rect"]);
  466. auto playerColor = readPlayerColor(config["color"]);
  467. if(playerColor.isValidPlayer())
  468. {
  469. auto result = std::make_shared<FilledTexturePlayerColored>(image, rect);
  470. result->playerColored(playerColor);
  471. }
  472. return std::make_shared<CFilledTexture>(image, rect);
  473. }
  474. std::shared_ptr<ComboBox> InterfaceObjectConfigurable::buildComboBox(const JsonNode & config)
  475. {
  476. logGlobal->debug("Building widget ComboBox");
  477. auto position = readPosition(config["position"]);
  478. auto image = config["image"].String();
  479. auto help = readHintText(config["help"]);
  480. auto result = std::make_shared<ComboBox>(position, image, help, config["dropDown"]);
  481. if(!config["items"].isNull())
  482. {
  483. for(const auto & item : config["items"].Vector())
  484. {
  485. result->addOverlay(buildWidget(item));
  486. }
  487. }
  488. if(!config["imageOrder"].isNull())
  489. {
  490. auto imgOrder = config["imageOrder"].Vector();
  491. assert(imgOrder.size() >= 4);
  492. result->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  493. }
  494. loadButtonBorderColor(result, config["borderColor"]);
  495. loadButtonHotkey(result, config["hotkey"]);
  496. return result;
  497. }
  498. std::shared_ptr<CTextInput> InterfaceObjectConfigurable::buildTextInput(const JsonNode & config) const
  499. {
  500. logGlobal->debug("Building widget CTextInput");
  501. auto rect = readRect(config["rect"]);
  502. auto offset = readPosition(config["backgroundOffset"]);
  503. auto bgName = config["background"].String();
  504. auto result = std::make_shared<CTextInput>(rect, offset, bgName, 0);
  505. if(!config["alignment"].isNull())
  506. result->alignment = readTextAlignment(config["alignment"]);
  507. if(!config["font"].isNull())
  508. result->font = readFont(config["font"]);
  509. if(!config["color"].isNull())
  510. result->setColor(readColor(config["color"]));
  511. if(!config["text"].isNull() && config["text"].isString())
  512. result->setText(config["text"].String()); //for input field raw string is taken
  513. if(!config["callback"].isNull())
  514. result->cb += callbacks_string.at(config["callback"].String());
  515. if(!config["help"].isNull())
  516. result->setHelpText(readText(config["help"]));
  517. return result;
  518. }
  519. /// Small helper class that provides ownership for shared_ptr's of child elements
  520. class InterfaceLayoutWidget : public CIntObject
  521. {
  522. public:
  523. std::vector<std::shared_ptr<CIntObject>> ownedChildren;
  524. InterfaceLayoutWidget();
  525. };
  526. InterfaceLayoutWidget::InterfaceLayoutWidget()
  527. :CIntObject()
  528. {
  529. setRedrawParent(true);
  530. }
  531. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildLayout(const JsonNode & config)
  532. {
  533. logGlobal->debug("Building widget Layout");
  534. bool vertical = config["vertical"].Bool();
  535. bool horizontal = config["horizontal"].Bool();
  536. bool dynamic = config["dynamic"].Bool();
  537. int distance = config["distance"].Integer();
  538. std::string customType = config["customType"].String();
  539. auto position = readPosition(config["position"]);
  540. auto result = std::make_shared<InterfaceLayoutWidget>();
  541. result->moveBy(position);
  542. Point layoutPosition;
  543. for(auto item : config["items"].Vector())
  544. {
  545. if (item["type"].String().empty())
  546. item["type"].String() = customType;
  547. if (!item["created"].isNull())
  548. {
  549. std::string name = item["created"].String();
  550. if (conditionals.count(name) != 0)
  551. {
  552. if (!conditionals.at(name))
  553. continue;
  554. }
  555. else
  556. {
  557. logMod->warn("Unknown condition %s in widget!", name);
  558. }
  559. }
  560. auto widget = buildWidget(item);
  561. addWidget(item["name"].String(), widget);
  562. result->ownedChildren.push_back(widget);
  563. result->addChild(widget.get(), false);
  564. widget->moveBy(position + layoutPosition);
  565. if (dynamic && vertical)
  566. layoutPosition.y += widget->pos.h;
  567. if (dynamic && horizontal)
  568. layoutPosition.x += widget->pos.w;
  569. if (vertical)
  570. layoutPosition.y += distance;
  571. if (horizontal)
  572. layoutPosition.x += distance;
  573. }
  574. return result;
  575. }
  576. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  577. {
  578. logGlobal->debug("Building widget CShowableAnim");
  579. auto position = readPosition(config["position"]);
  580. auto image = config["image"].String();
  581. ui8 flags = 0;
  582. if(!config["repeat"].Bool())
  583. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  584. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  585. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  586. if(!config["alpha"].isNull())
  587. anim->setAlpha(config["alpha"].Integer());
  588. if(!config["callback"].isNull())
  589. anim->callback = std::bind(callbacks_int.at(config["callback"].String()), 0);
  590. if(!config["frames"].isNull())
  591. {
  592. auto b = config["frames"]["start"].Integer();
  593. auto e = config["frames"]["end"].Integer();
  594. anim->set(group, b, e);
  595. }
  596. return anim;
  597. }
  598. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  599. {
  600. assert(!config.isNull());
  601. logGlobal->debug("Building widget from config");
  602. //overrides from variables
  603. for(auto & item : config["overrides"].Struct())
  604. {
  605. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  606. config[item.first] = variables[item.second.String()];
  607. }
  608. auto type = config["type"].String();
  609. auto buildIterator = builders.find(type);
  610. if(buildIterator != builders.end())
  611. return (buildIterator->second)(config);
  612. logGlobal->error("Builder with type %s is not registered", type);
  613. return nullptr;
  614. }
  615. void InterfaceObjectConfigurable::setShortcutBlocked(EShortcut shortcut, bool isBlocked)
  616. {
  617. auto target = shortcuts.find(shortcut);
  618. if (target == shortcuts.end())
  619. return;
  620. target->second.blocked = isBlocked;
  621. for (auto & entry : widgets)
  622. {
  623. auto button = std::dynamic_pointer_cast<CButton>(entry.second);
  624. if (button && button->assignedKey == shortcut)
  625. button->block(isBlocked);
  626. }
  627. }
  628. void InterfaceObjectConfigurable::addShortcut(EShortcut shortcut, std::function<void()> callback)
  629. {
  630. assert(shortcuts.count(shortcut) == 0);
  631. shortcuts[shortcut].callback = callback;
  632. }
  633. void InterfaceObjectConfigurable::keyPressed(EShortcut key)
  634. {
  635. auto target = shortcuts.find(key);
  636. if (target == shortcuts.end())
  637. return;
  638. if (target->second.assignedToButton)
  639. return; // will be handled by button instance
  640. if (target->second.blocked)
  641. return;
  642. target->second.callback();
  643. }