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