InterfaceObjectConfigurable.cpp 26 KB

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