InterfaceObjectConfigurable.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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/Buttons.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../widgets/Slider.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../../lib//constants/StringConstants.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. }
  51. void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
  52. {
  53. builders[type] = f;
  54. }
  55. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  56. {
  57. callbacks[callbackName] = callback;
  58. }
  59. void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
  60. {
  61. auto iter = widgets.find(name);
  62. if(iter != widgets.end())
  63. widgets.erase(iter);
  64. }
  65. void InterfaceObjectConfigurable::loadCustomBuilders(const JsonNode & config)
  66. {
  67. for(auto & item : config.Struct())
  68. {
  69. std::string typeName = item.first;
  70. JsonNode baseConfig = item.second;
  71. auto const & functor = [this, baseConfig](const JsonNode & widgetConfig) -> std::shared_ptr<CIntObject>
  72. {
  73. JsonNode actualConfig = widgetConfig;
  74. JsonUtils::mergeCopy(actualConfig, baseConfig);
  75. return this->buildWidget(actualConfig);
  76. };
  77. registerBuilder(typeName, functor);
  78. }
  79. }
  80. void InterfaceObjectConfigurable::build(const JsonNode &config)
  81. {
  82. OBJ_CONSTRUCTION;
  83. logGlobal->debug("Building configurable interface object");
  84. auto * items = &config;
  85. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  86. {
  87. if (!config["library"].isNull())
  88. {
  89. const JsonNode library(ResourceID(config["library"].String()));
  90. loadCustomBuilders(library);
  91. }
  92. loadCustomBuilders(config["customTypes"]);
  93. for(auto & item : config["variables"].Struct())
  94. {
  95. logGlobal->debug("Read variable named %s", item.first);
  96. variables[item.first] = item.second;
  97. }
  98. items = &config["items"];
  99. }
  100. for(const auto & item : items->Vector())
  101. addWidget(item["name"].String(), buildWidget(item));
  102. }
  103. void InterfaceObjectConfigurable::addConditional(const std::string & name, bool active)
  104. {
  105. conditionals[name] = active;
  106. }
  107. void InterfaceObjectConfigurable::addWidget(const std::string & namePreferred, std::shared_ptr<CIntObject> widget)
  108. {
  109. static const std::string unnamedObjectPrefix = "__widget_";
  110. std::string nameActual;
  111. if (widgets.count(namePreferred) == 0)
  112. nameActual = namePreferred;
  113. else
  114. logGlobal->error("Duplicated widget name: '%s'", namePreferred);
  115. if (nameActual.empty())
  116. nameActual = unnamedObjectPrefix + std::to_string(unnamedObjectId++);
  117. logGlobal->debug("Building widget with name %s", nameActual);
  118. widgets[nameActual] = widget;
  119. }
  120. std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
  121. {
  122. if(config.isNull())
  123. return "";
  124. std::string s = config.String();
  125. logGlobal->debug("Reading text from translations by key: %s", s);
  126. return CGI->generaltexth->translate(s);
  127. }
  128. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  129. {
  130. Point p;
  131. logGlobal->debug("Reading point");
  132. p.x = config["x"].Integer();
  133. p.y = config["y"].Integer();
  134. return p;
  135. }
  136. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  137. {
  138. Rect p;
  139. logGlobal->debug("Reading rect");
  140. p.x = config["x"].Integer();
  141. p.y = config["y"].Integer();
  142. p.w = config["w"].Integer();
  143. p.h = config["h"].Integer();
  144. return p;
  145. }
  146. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  147. {
  148. logGlobal->debug("Reading text alignment");
  149. if(!config.isNull())
  150. {
  151. if(config.String() == "center")
  152. return ETextAlignment::CENTER;
  153. if(config.String() == "left")
  154. return ETextAlignment::TOPLEFT;
  155. if(config.String() == "right")
  156. return ETextAlignment::BOTTOMRIGHT;
  157. }
  158. logGlobal->debug("Uknown text alignment attribute");
  159. return ETextAlignment::CENTER;
  160. }
  161. ColorRGBA InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  162. {
  163. logGlobal->debug("Reading color");
  164. if(!config.isNull())
  165. {
  166. if(config.isString())
  167. {
  168. if(config.String() == "yellow")
  169. return Colors::YELLOW;
  170. if(config.String() == "white")
  171. return Colors::WHITE;
  172. if(config.String() == "gold")
  173. return Colors::METALLIC_GOLD;
  174. if(config.String() == "green")
  175. return Colors::GREEN;
  176. if(config.String() == "orange")
  177. return Colors::ORANGE;
  178. if(config.String() == "bright-yellow")
  179. return Colors::BRIGHT_YELLOW;
  180. }
  181. if(config.isVector())
  182. {
  183. const auto & asVector = config.Vector();
  184. if(asVector.size() == 4)
  185. return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer(), asVector[3].Integer());
  186. if(asVector.size() == 3)
  187. return ColorRGBA(asVector[0].Integer(), asVector[1].Integer(), asVector[2].Integer());
  188. }
  189. }
  190. logGlobal->debug("Uknown color attribute");
  191. return Colors::DEFAULT_KEY_COLOR;
  192. }
  193. PlayerColor InterfaceObjectConfigurable::readPlayerColor(const JsonNode & config) const
  194. {
  195. logGlobal->debug("Reading PlayerColor");
  196. if(!config.isNull() && config.isString())
  197. return PlayerColor(vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, config.String()));
  198. logGlobal->debug("Unknown PlayerColor attribute");
  199. return PlayerColor::CANNOT_DETERMINE;
  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.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.count(callbackName) > 0)
  374. button->addCallback(callbacks.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.count(callbackName) > 0)
  384. button->addCallback(std::bind(callbacks.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.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. auto playerColor = readPlayerColor(config["color"]);
  458. if(playerColor.isValidPlayer())
  459. {
  460. auto result = std::make_shared<FilledTexturePlayerColored>(image, rect);
  461. result->playerColored(playerColor);
  462. }
  463. return std::make_shared<CFilledTexture>(image, rect);
  464. }
  465. /// Small helper class that provides ownership for shared_ptr's of child elements
  466. class InterfaceLayoutWidget : public CIntObject
  467. {
  468. public:
  469. std::vector<std::shared_ptr<CIntObject>> ownedChildren;
  470. InterfaceLayoutWidget();
  471. };
  472. InterfaceLayoutWidget::InterfaceLayoutWidget()
  473. :CIntObject()
  474. {
  475. setRedrawParent(true);
  476. }
  477. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildLayout(const JsonNode & config)
  478. {
  479. logGlobal->debug("Building widget Layout");
  480. bool vertical = config["vertical"].Bool();
  481. bool horizontal = config["horizontal"].Bool();
  482. bool dynamic = config["dynamic"].Bool();
  483. int distance = config["distance"].Integer();
  484. std::string customType = config["customType"].String();
  485. auto position = readPosition(config["position"]);
  486. auto result = std::make_shared<InterfaceLayoutWidget>();
  487. result->moveBy(position);
  488. Point layoutPosition;
  489. for(auto item : config["items"].Vector())
  490. {
  491. if (item["type"].String().empty())
  492. item["type"].String() = customType;
  493. if (!item["created"].isNull())
  494. {
  495. std::string name = item["created"].String();
  496. if (conditionals.count(name) != 0)
  497. {
  498. if (!conditionals.at(name))
  499. continue;
  500. }
  501. else
  502. {
  503. logMod->warn("Unknown condition %s in widget!", name);
  504. }
  505. }
  506. auto widget = buildWidget(item);
  507. addWidget(item["name"].String(), widget);
  508. result->ownedChildren.push_back(widget);
  509. result->addChild(widget.get(), false);
  510. widget->moveBy(position + layoutPosition);
  511. if (dynamic && vertical)
  512. layoutPosition.y += widget->pos.h;
  513. if (dynamic && horizontal)
  514. layoutPosition.x += widget->pos.w;
  515. if (vertical)
  516. layoutPosition.y += distance;
  517. if (horizontal)
  518. layoutPosition.x += distance;
  519. }
  520. return result;
  521. }
  522. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  523. {
  524. logGlobal->debug("Building widget CShowableAnim");
  525. auto position = readPosition(config["position"]);
  526. auto image = config["image"].String();
  527. ui8 flags = 0;
  528. if(!config["repeat"].Bool())
  529. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  530. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  531. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  532. if(!config["alpha"].isNull())
  533. anim->setAlpha(config["alpha"].Integer());
  534. if(!config["callback"].isNull())
  535. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  536. if(!config["frames"].isNull())
  537. {
  538. auto b = config["frames"]["start"].Integer();
  539. auto e = config["frames"]["end"].Integer();
  540. anim->set(group, b, e);
  541. }
  542. return anim;
  543. }
  544. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  545. {
  546. assert(!config.isNull());
  547. logGlobal->debug("Building widget from config");
  548. //overrides from variables
  549. for(auto & item : config["overrides"].Struct())
  550. {
  551. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  552. config[item.first] = variables[item.second.String()];
  553. }
  554. auto type = config["type"].String();
  555. auto buildIterator = builders.find(type);
  556. if(buildIterator != builders.end())
  557. return (buildIterator->second)(config);
  558. logGlobal->error("Builder with type %s is not registered", type);
  559. return nullptr;
  560. }
  561. void InterfaceObjectConfigurable::setShortcutBlocked(EShortcut shortcut, bool isBlocked)
  562. {
  563. auto target = shortcuts.find(shortcut);
  564. if (target == shortcuts.end())
  565. return;
  566. target->second.blocked = isBlocked;
  567. for (auto & entry : widgets)
  568. {
  569. auto button = std::dynamic_pointer_cast<CButton>(entry.second);
  570. if (button && button->assignedKey == shortcut)
  571. button->block(isBlocked);
  572. }
  573. }
  574. void InterfaceObjectConfigurable::addShortcut(EShortcut shortcut, std::function<void()> callback)
  575. {
  576. assert(shortcuts.count(shortcut) == 0);
  577. shortcuts[shortcut].callback = callback;
  578. }
  579. void InterfaceObjectConfigurable::keyPressed(EShortcut key)
  580. {
  581. auto target = shortcuts.find(key);
  582. if (target == shortcuts.end())
  583. return;
  584. if (target->second.assignedToButton)
  585. return; // will be handled by button instance
  586. if (target->second.blocked)
  587. return;
  588. target->second.callback();
  589. }