InterfaceObjectConfigurable.cpp 25 KB

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