InterfaceObjectConfigurable.cpp 24 KB

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