2
0

InterfaceObjectConfigurable.cpp 26 KB

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