InterfaceObjectConfigurable.cpp 25 KB

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