InterfaceObjectConfigurable.cpp 24 KB

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