InterfaceObjectConfigurable.cpp 22 KB

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