InterfaceObjectConfigurable.cpp 26 KB

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