2
0

InterfaceObjectConfigurable.cpp 23 KB

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