InterfaceObjectConfigurable.cpp 22 KB

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