InterfaceObjectConfigurable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 "../widgets/CComponent.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/MiscWidgets.h"
  20. #include "../widgets/ObjectLists.h"
  21. #include "../widgets/TextControls.h"
  22. #include "../windows/GUIClasses.h"
  23. #include "../windows/InfoWindows.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/filesystem/ResourceID.h"
  26. InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
  27. InterfaceObjectConfigurable(used, offset)
  28. {
  29. build(config);
  30. }
  31. InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
  32. CIntObject(used, offset)
  33. {
  34. REGISTER_BUILDER("picture", &InterfaceObjectConfigurable::buildPicture);
  35. REGISTER_BUILDER("image", &InterfaceObjectConfigurable::buildImage);
  36. REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture);
  37. REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation);
  38. REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel);
  39. REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup);
  40. REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton);
  41. REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
  42. REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
  43. REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
  44. REGISTER_BUILDER("layout", &InterfaceObjectConfigurable::buildLayout);
  45. }
  46. void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
  47. {
  48. builders[type] = f;
  49. }
  50. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  51. {
  52. callbacks[callbackName] = callback;
  53. }
  54. void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
  55. {
  56. auto iter = widgets.find(name);
  57. if(iter != widgets.end())
  58. widgets.erase(iter);
  59. }
  60. void InterfaceObjectConfigurable::loadCustomBuilders(const JsonNode & config)
  61. {
  62. for(auto & item : config.Struct())
  63. {
  64. std::string typeName = item.first;
  65. JsonNode baseConfig = item.second;
  66. auto const & functor = [this, baseConfig](const JsonNode & widgetConfig) -> std::shared_ptr<CIntObject>
  67. {
  68. JsonNode actualConfig = widgetConfig;
  69. JsonUtils::mergeCopy(actualConfig, baseConfig);
  70. return this->buildWidget(actualConfig);
  71. };
  72. registerBuilder(typeName, functor);
  73. }
  74. }
  75. void InterfaceObjectConfigurable::build(const JsonNode &config)
  76. {
  77. OBJ_CONSTRUCTION;
  78. logGlobal->debug("Building configurable interface object");
  79. auto * items = &config;
  80. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  81. {
  82. if (!config["library"].isNull())
  83. {
  84. const JsonNode library(ResourceID(config["library"].String()));
  85. loadCustomBuilders(library);
  86. }
  87. loadCustomBuilders(config["customTypes"]);
  88. for(auto & item : config["variables"].Struct())
  89. {
  90. logGlobal->debug("Read variable named %s", item.first);
  91. variables[item.first] = item.second;
  92. }
  93. items = &config["items"];
  94. }
  95. for(const auto & item : items->Vector())
  96. addWidget(item["name"].String(), buildWidget(item));
  97. }
  98. void InterfaceObjectConfigurable::addWidget(const std::string & namePreferred, std::shared_ptr<CIntObject> widget)
  99. {
  100. static const std::string unnamedObjectPrefix = "__widget_";
  101. std::string nameActual;
  102. if (widgets.count(namePreferred) == 0)
  103. nameActual = namePreferred;
  104. else
  105. logGlobal->error("Duplicated widget name: '%s'", namePreferred);
  106. if (nameActual.empty())
  107. nameActual = unnamedObjectPrefix + std::to_string(unnamedObjectId++);
  108. logGlobal->debug("Building widget with name %s", nameActual);
  109. widgets[nameActual] = widget;
  110. }
  111. std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
  112. {
  113. if(config.isNull())
  114. return "";
  115. std::string s = config.String();
  116. logGlobal->debug("Reading text from translations by key: %s", s);
  117. return CGI->generaltexth->translate(s);
  118. }
  119. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  120. {
  121. Point p;
  122. logGlobal->debug("Reading point");
  123. p.x = config["x"].Integer();
  124. p.y = config["y"].Integer();
  125. return p;
  126. }
  127. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  128. {
  129. Rect p;
  130. logGlobal->debug("Reading rect");
  131. p.x = config["x"].Integer();
  132. p.y = config["y"].Integer();
  133. p.w = config["w"].Integer();
  134. p.h = config["h"].Integer();
  135. return p;
  136. }
  137. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  138. {
  139. logGlobal->debug("Reading text alignment");
  140. if(!config.isNull())
  141. {
  142. if(config.String() == "center")
  143. return ETextAlignment::CENTER;
  144. if(config.String() == "left")
  145. return ETextAlignment::TOPLEFT;
  146. if(config.String() == "right")
  147. return ETextAlignment::BOTTOMRIGHT;
  148. }
  149. logGlobal->debug("Uknown text alignment attribute");
  150. return ETextAlignment::CENTER;
  151. }
  152. SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  153. {
  154. logGlobal->debug("Reading color");
  155. if(!config.isNull())
  156. {
  157. if(config.String() == "yellow")
  158. return Colors::YELLOW;
  159. if(config.String() == "white")
  160. return Colors::WHITE;
  161. if(config.String() == "gold")
  162. return Colors::METALLIC_GOLD;
  163. if(config.String() == "green")
  164. return Colors::GREEN;
  165. if(config.String() == "orange")
  166. return Colors::ORANGE;
  167. if(config.String() == "bright-yellow")
  168. return Colors::BRIGHT_YELLOW;
  169. }
  170. logGlobal->debug("Uknown color attribute");
  171. return Colors::DEFAULT_KEY_COLOR;
  172. }
  173. EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
  174. {
  175. logGlobal->debug("Reading font");
  176. if(!config.isNull())
  177. {
  178. if(config.String() == "big")
  179. return EFonts::FONT_BIG;
  180. if(config.String() == "medium")
  181. return EFonts::FONT_MEDIUM;
  182. if(config.String() == "small")
  183. return EFonts::FONT_SMALL;
  184. if(config.String() == "tiny")
  185. return EFonts::FONT_TINY;
  186. if(config.String() == "calisto")
  187. return EFonts::FONT_CALLI;
  188. }
  189. logGlobal->debug("Uknown font attribute");
  190. return EFonts::FONT_TIMES;
  191. }
  192. std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
  193. {
  194. logGlobal->debug("Reading hint text");
  195. std::pair<std::string, std::string> result;
  196. if(!config.isNull())
  197. {
  198. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  199. {
  200. result.first = readText(config["hover"]);
  201. result.second = readText(config["help"]);
  202. return result;
  203. }
  204. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  205. {
  206. logGlobal->debug("Reading hint text (help) from generaltext handler:%sd", config.String());
  207. result.first = CGI->generaltexth->translate( config.String(), "hover");
  208. result.second = CGI->generaltexth->translate( config.String(), "help");
  209. }
  210. }
  211. return result;
  212. }
  213. EShortcut InterfaceObjectConfigurable::readHotkey(const JsonNode & config) const
  214. {
  215. logGlobal->debug("Reading hotkey");
  216. if(config.getType() != JsonNode::JsonType::DATA_STRING)
  217. {
  218. logGlobal->error("Invalid hotket format in interface configuration! Expected string!", config.String());
  219. return EShortcut::NONE;
  220. }
  221. EShortcut result = GH.shortcutsHandler().findShortcut(config.String());
  222. if (result == EShortcut::NONE)
  223. logGlobal->error("Invalid hotkey '%s' in interface configuration!", config.String());
  224. return result;;
  225. }
  226. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  227. {
  228. logGlobal->debug("Building widget CPicture");
  229. auto image = config["image"].String();
  230. auto position = readPosition(config["position"]);
  231. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  232. if(!config["visible"].isNull())
  233. pic->visible = config["visible"].Bool();
  234. if ( config["playerColored"].Bool() && LOCPLINT)
  235. pic->colorize(LOCPLINT->playerID);
  236. return pic;
  237. }
  238. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  239. {
  240. logGlobal->debug("Building widget CLabel");
  241. auto font = readFont(config["font"]);
  242. auto alignment = readTextAlignment(config["alignment"]);
  243. auto color = readColor(config["color"]);
  244. auto text = readText(config["text"]);
  245. auto position = readPosition(config["position"]);
  246. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  247. }
  248. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  249. {
  250. logGlobal->debug("Building widget CToggleGroup");
  251. auto position = readPosition(config["position"]);
  252. auto group = std::make_shared<CToggleGroup>(0);
  253. group->pos += position;
  254. if(!config["items"].isNull())
  255. {
  256. OBJ_CONSTRUCTION_TARGETED(group.get());
  257. int itemIdx = -1;
  258. for(const auto & item : config["items"].Vector())
  259. {
  260. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  261. auto newToggle = std::dynamic_pointer_cast<CToggleButton>(buildWidget(item));
  262. group->addToggle(itemIdx, newToggle);
  263. }
  264. }
  265. if(!config["selected"].isNull())
  266. group->setSelected(config["selected"].Integer());
  267. if(!config["callback"].isNull())
  268. group->addCallback(callbacks.at(config["callback"].String()));
  269. return group;
  270. }
  271. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  272. {
  273. logGlobal->debug("Building widget CToggleButton");
  274. auto position = readPosition(config["position"]);
  275. auto image = config["image"].String();
  276. auto help = readHintText(config["help"]);
  277. auto button = std::make_shared<CToggleButton>(position, image, help);
  278. if(!config["items"].isNull())
  279. {
  280. for(const auto & item : config["items"].Vector())
  281. {
  282. button->addOverlay(buildWidget(item));
  283. }
  284. }
  285. if(!config["selected"].isNull())
  286. button->setSelected(config["selected"].Bool());
  287. if(!config["imageOrder"].isNull())
  288. {
  289. auto imgOrder = config["imageOrder"].Vector();
  290. assert(imgOrder.size() >= 4);
  291. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  292. }
  293. loadToggleButtonCallback(button, config["callback"]);
  294. return button;
  295. }
  296. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  297. {
  298. logGlobal->debug("Building widget CButton");
  299. auto position = readPosition(config["position"]);
  300. auto image = config["image"].String();
  301. auto help = readHintText(config["help"]);
  302. auto button = std::make_shared<CButton>(position, image, help);
  303. if(!config["items"].isNull())
  304. {
  305. for(const auto & item : config["items"].Vector())
  306. {
  307. button->addOverlay(buildWidget(item));
  308. }
  309. }
  310. if(!config["imageOrder"].isNull())
  311. {
  312. auto imgOrder = config["imageOrder"].Vector();
  313. assert(imgOrder.size() >= 4);
  314. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  315. }
  316. loadButtonBorderColor(button, config["borderColor"]);
  317. loadButtonCallback(button, config["callback"]);
  318. loadButtonHotkey(button, config["hotkey"]);
  319. return button;
  320. }
  321. void InterfaceObjectConfigurable::loadButtonBorderColor(std::shared_ptr<CButton> button, const JsonNode & config) const
  322. {
  323. if (config.isNull())
  324. return;
  325. auto color = readColor(config);
  326. button->setBorderColor(color);
  327. }
  328. void InterfaceObjectConfigurable::loadToggleButtonCallback(std::shared_ptr<CToggleButton> button, const JsonNode & config) const
  329. {
  330. if(config.isNull())
  331. return;
  332. std::string callbackName = config.String();
  333. if (callbacks.count(callbackName) > 0)
  334. button->addCallback(callbacks.at(callbackName));
  335. else
  336. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  337. }
  338. void InterfaceObjectConfigurable::loadButtonCallback(std::shared_ptr<CButton> button, const JsonNode & config) const
  339. {
  340. if(config.isNull())
  341. return;
  342. std::string callbackName = config.String();
  343. if (callbacks.count(callbackName) > 0)
  344. button->addCallback(std::bind(callbacks.at(callbackName), 0));
  345. else
  346. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  347. }
  348. void InterfaceObjectConfigurable::loadButtonHotkey(std::shared_ptr<CButton> button, const JsonNode & config) const
  349. {
  350. if(config.isNull())
  351. return;
  352. if(config.getType() != JsonNode::JsonType::DATA_STRING)
  353. {
  354. logGlobal->error("Invalid shortcut format - string expected!");
  355. return;
  356. }
  357. button->assignedKey = readHotkey(config);
  358. auto target = shortcuts.find(button->assignedKey);
  359. if (target == shortcuts.end())
  360. return;
  361. button->addCallback(target->second.callback);
  362. target->second.assignedToButton = true;
  363. }
  364. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  365. {
  366. logGlobal->debug("Building widget CLabelGroup");
  367. auto font = readFont(config["font"]);
  368. auto alignment = readTextAlignment(config["alignment"]);
  369. auto color = readColor(config["color"]);
  370. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  371. if(!config["items"].isNull())
  372. {
  373. for(const auto & item : config["items"].Vector())
  374. {
  375. auto position = readPosition(item["position"]);
  376. auto text = readText(item["text"]);
  377. group->add(position.x, position.y, text);
  378. }
  379. }
  380. return group;
  381. }
  382. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  383. {
  384. logGlobal->debug("Building widget CSlider");
  385. auto position = readPosition(config["position"]);
  386. int length = config["size"].Integer();
  387. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  388. auto itemsVisible = config["itemsVisible"].Integer();
  389. auto itemsTotal = config["itemsTotal"].Integer();
  390. auto value = config["selected"].Integer();
  391. bool horizontal = config["orientation"].String() == "horizontal";
  392. const auto & result =
  393. std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  394. if (!config["scrollBounds"].isNull())
  395. {
  396. Rect bounds = readRect(config["scrollBounds"]);
  397. result->setScrollBounds(bounds);
  398. }
  399. return result;
  400. }
  401. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  402. {
  403. logGlobal->debug("Building widget CAnimImage");
  404. auto position = readPosition(config["position"]);
  405. auto image = config["image"].String();
  406. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  407. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  408. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  409. }
  410. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  411. {
  412. logGlobal->debug("Building widget CFilledTexture");
  413. auto image = config["image"].String();
  414. auto rect = readRect(config["rect"]);
  415. return std::make_shared<CFilledTexture>(image, rect);
  416. }
  417. /// Small helper class that provides ownership for shared_ptr's of child elements
  418. class InterfaceLayoutWidget : public CIntObject
  419. {
  420. public:
  421. std::vector<std::shared_ptr<CIntObject>> ownedChildren;
  422. };
  423. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildLayout(const JsonNode & config)
  424. {
  425. logGlobal->debug("Building widget Layout");
  426. bool vertical = config["vertical"].Bool();
  427. bool horizontal = config["horizontal"].Bool();
  428. bool dynamic = config["dynamic"].Bool();
  429. int distance = config["distance"].Integer();
  430. std::string customType = config["customType"].String();
  431. auto position = readPosition(config["position"]);
  432. auto result = std::make_shared<InterfaceLayoutWidget>();
  433. result->moveBy(position);
  434. Point layoutPosition;
  435. for(auto item : config["items"].Vector())
  436. {
  437. if (item["type"].String().empty())
  438. item["type"].String() = customType;
  439. auto widget = buildWidget(item);
  440. addWidget(item["name"].String(), widget);
  441. result->ownedChildren.push_back(widget);
  442. result->addChild(widget.get(), false);
  443. widget->moveBy(position + layoutPosition);
  444. if (dynamic && vertical)
  445. layoutPosition.y += widget->pos.h;
  446. if (dynamic && horizontal)
  447. layoutPosition.x += widget->pos.w;
  448. if (vertical)
  449. layoutPosition.y += distance;
  450. if (horizontal)
  451. layoutPosition.x += distance;
  452. }
  453. return result;
  454. }
  455. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  456. {
  457. logGlobal->debug("Building widget CShowableAnim");
  458. auto position = readPosition(config["position"]);
  459. auto image = config["image"].String();
  460. ui8 flags = 0;
  461. if(!config["repeat"].Bool())
  462. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  463. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  464. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  465. if(!config["alpha"].isNull())
  466. anim->setAlpha(config["alpha"].Integer());
  467. if(!config["callback"].isNull())
  468. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  469. if(!config["frames"].isNull())
  470. {
  471. auto b = config["frames"]["start"].Integer();
  472. auto e = config["frames"]["end"].Integer();
  473. anim->set(group, b, e);
  474. }
  475. return anim;
  476. }
  477. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  478. {
  479. assert(!config.isNull());
  480. logGlobal->debug("Building widget from config");
  481. //overrides from variables
  482. for(auto & item : config["overrides"].Struct())
  483. {
  484. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  485. config[item.first] = variables[item.second.String()];
  486. }
  487. auto type = config["type"].String();
  488. auto buildIterator = builders.find(type);
  489. if(buildIterator != builders.end())
  490. return (buildIterator->second)(config);
  491. logGlobal->error("Builder with type %s is not registered", type);
  492. return nullptr;
  493. }
  494. void InterfaceObjectConfigurable::setShortcutBlocked(EShortcut shortcut, bool isBlocked)
  495. {
  496. auto target = shortcuts.find(shortcut);
  497. if (target == shortcuts.end())
  498. return;
  499. target->second.blocked = isBlocked;
  500. for (auto & entry : widgets)
  501. {
  502. auto button = std::dynamic_pointer_cast<CButton>(entry.second);
  503. if (button && button->assignedKey == shortcut)
  504. button->block(isBlocked);
  505. }
  506. }
  507. void InterfaceObjectConfigurable::addShortcut(EShortcut shortcut, std::function<void()> callback)
  508. {
  509. assert(shortcuts.count(shortcut) == 0);
  510. shortcuts[shortcut].callback = callback;
  511. }
  512. void InterfaceObjectConfigurable::keyPressed(EShortcut key)
  513. {
  514. auto target = shortcuts.find(key);
  515. if (target == shortcuts.end())
  516. return;
  517. if (target->second.assignedToButton)
  518. return; // will be handled by button instance
  519. if (target->second.blocked)
  520. return;
  521. target->second.callback();
  522. }