InterfaceObjectConfigurable.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  262. }
  263. }
  264. if(!config["selected"].isNull())
  265. group->setSelected(config["selected"].Integer());
  266. if(!config["callback"].isNull())
  267. group->addCallback(callbacks.at(config["callback"].String()));
  268. return group;
  269. }
  270. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  271. {
  272. logGlobal->debug("Building widget CToggleButton");
  273. auto position = readPosition(config["position"]);
  274. auto image = config["image"].String();
  275. auto help = readHintText(config["help"]);
  276. auto button = std::make_shared<CToggleButton>(position, image, help);
  277. if(!config["items"].isNull())
  278. {
  279. for(const auto & item : config["items"].Vector())
  280. {
  281. button->addOverlay(buildWidget(item));
  282. }
  283. }
  284. if(!config["selected"].isNull())
  285. button->setSelected(config["selected"].Bool());
  286. if(!config["imageOrder"].isNull())
  287. {
  288. auto imgOrder = config["imageOrder"].Vector();
  289. assert(imgOrder.size() >= 4);
  290. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  291. }
  292. loadToggleButtonCallback(button, config["callback"]);
  293. return button;
  294. }
  295. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  296. {
  297. logGlobal->debug("Building widget CButton");
  298. auto position = readPosition(config["position"]);
  299. auto image = config["image"].String();
  300. auto help = readHintText(config["help"]);
  301. auto button = std::make_shared<CButton>(position, image, help);
  302. if(!config["items"].isNull())
  303. {
  304. for(const auto & item : config["items"].Vector())
  305. {
  306. button->addOverlay(buildWidget(item));
  307. }
  308. }
  309. if(!config["imageOrder"].isNull())
  310. {
  311. auto imgOrder = config["imageOrder"].Vector();
  312. assert(imgOrder.size() >= 4);
  313. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  314. }
  315. loadButtonBorderColor(button, config["borderColor"]);
  316. loadButtonCallback(button, config["callback"]);
  317. loadButtonHotkey(button, config["hotkey"]);
  318. return button;
  319. }
  320. void InterfaceObjectConfigurable::loadButtonBorderColor(std::shared_ptr<CButton> button, const JsonNode & config) const
  321. {
  322. if (config.isNull())
  323. return;
  324. auto color = readColor(config);
  325. button->setBorderColor(color);
  326. }
  327. void InterfaceObjectConfigurable::loadToggleButtonCallback(std::shared_ptr<CToggleButton> button, const JsonNode & config) const
  328. {
  329. if(config.isNull())
  330. return;
  331. std::string callbackName = config.String();
  332. if (callbacks.count(callbackName) > 0)
  333. button->addCallback(callbacks.at(callbackName));
  334. else
  335. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  336. }
  337. void InterfaceObjectConfigurable::loadButtonCallback(std::shared_ptr<CButton> button, const JsonNode & config) const
  338. {
  339. if(config.isNull())
  340. return;
  341. std::string callbackName = config.String();
  342. if (callbacks.count(callbackName) > 0)
  343. button->addCallback(std::bind(callbacks.at(callbackName), 0));
  344. else
  345. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  346. }
  347. void InterfaceObjectConfigurable::loadButtonHotkey(std::shared_ptr<CButton> button, const JsonNode & config) const
  348. {
  349. if(config.isNull())
  350. return;
  351. if(config.getType() != JsonNode::JsonType::DATA_STRING)
  352. {
  353. logGlobal->error("Invalid shortcut format - string expected!");
  354. return;
  355. }
  356. button->assignedKey = readHotkey(config);
  357. auto target = shortcuts.find(button->assignedKey);
  358. if (target == shortcuts.end())
  359. return;
  360. button->addCallback(target->second.callback);
  361. target->second.assignedToButton = true;
  362. }
  363. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  364. {
  365. logGlobal->debug("Building widget CLabelGroup");
  366. auto font = readFont(config["font"]);
  367. auto alignment = readTextAlignment(config["alignment"]);
  368. auto color = readColor(config["color"]);
  369. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  370. if(!config["items"].isNull())
  371. {
  372. for(const auto & item : config["items"].Vector())
  373. {
  374. auto position = readPosition(item["position"]);
  375. auto text = readText(item["text"]);
  376. group->add(position.x, position.y, text);
  377. }
  378. }
  379. return group;
  380. }
  381. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  382. {
  383. logGlobal->debug("Building widget CSlider");
  384. auto position = readPosition(config["position"]);
  385. int length = config["size"].Integer();
  386. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  387. auto itemsVisible = config["itemsVisible"].Integer();
  388. auto itemsTotal = config["itemsTotal"].Integer();
  389. auto value = config["selected"].Integer();
  390. bool horizontal = config["orientation"].String() == "horizontal";
  391. const auto & result =
  392. std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  393. if (!config["scrollBounds"].isNull())
  394. {
  395. Rect bounds = readRect(config["scrollBounds"]);
  396. result->setScrollBounds(bounds);
  397. }
  398. return result;
  399. }
  400. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  401. {
  402. logGlobal->debug("Building widget CAnimImage");
  403. auto position = readPosition(config["position"]);
  404. auto image = config["image"].String();
  405. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  406. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  407. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  408. }
  409. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  410. {
  411. logGlobal->debug("Building widget CFilledTexture");
  412. auto image = config["image"].String();
  413. auto rect = readRect(config["rect"]);
  414. return std::make_shared<CFilledTexture>(image, rect);
  415. }
  416. /// Small helper class that provides ownership for shared_ptr's of child elements
  417. class InterfaceLayoutWidget : public CIntObject
  418. {
  419. public:
  420. std::vector<std::shared_ptr<CIntObject>> ownedChildren;
  421. };
  422. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildLayout(const JsonNode & config)
  423. {
  424. logGlobal->debug("Building widget Layout");
  425. bool vertical = config["vertical"].Bool();
  426. bool horizontal = config["horizontal"].Bool();
  427. bool dynamic = config["dynamic"].Bool();
  428. int distance = config["distance"].Integer();
  429. std::string customType = config["customType"].String();
  430. auto position = readPosition(config["position"]);
  431. auto result = std::make_shared<InterfaceLayoutWidget>();
  432. result->moveBy(position);
  433. Point layoutPosition;
  434. for(auto item : config["items"].Vector())
  435. {
  436. if (item["type"].String().empty())
  437. item["type"].String() = customType;
  438. auto widget = buildWidget(item);
  439. addWidget(item["name"].String(), widget);
  440. result->ownedChildren.push_back(widget);
  441. result->addChild(widget.get(), false);
  442. widget->moveBy(position + layoutPosition);
  443. if (dynamic && vertical)
  444. layoutPosition.y += widget->pos.h;
  445. if (dynamic && horizontal)
  446. layoutPosition.x += widget->pos.w;
  447. if (vertical)
  448. layoutPosition.y += distance;
  449. if (horizontal)
  450. layoutPosition.x += distance;
  451. }
  452. return result;
  453. }
  454. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  455. {
  456. logGlobal->debug("Building widget CShowableAnim");
  457. auto position = readPosition(config["position"]);
  458. auto image = config["image"].String();
  459. ui8 flags = 0;
  460. if(!config["repeat"].Bool())
  461. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  462. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  463. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  464. if(!config["alpha"].isNull())
  465. anim->setAlpha(config["alpha"].Integer());
  466. if(!config["callback"].isNull())
  467. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  468. if(!config["frames"].isNull())
  469. {
  470. auto b = config["frames"]["start"].Integer();
  471. auto e = config["frames"]["end"].Integer();
  472. anim->set(group, b, e);
  473. }
  474. return anim;
  475. }
  476. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  477. {
  478. assert(!config.isNull());
  479. logGlobal->debug("Building widget from config");
  480. //overrides from variables
  481. for(auto & item : config["overrides"].Struct())
  482. {
  483. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  484. config[item.first] = variables[item.second.String()];
  485. }
  486. auto type = config["type"].String();
  487. auto buildIterator = builders.find(type);
  488. if(buildIterator != builders.end())
  489. return (buildIterator->second)(config);
  490. logGlobal->error("Builder with type %s is not registered", type);
  491. return nullptr;
  492. }
  493. void InterfaceObjectConfigurable::setShortcutBlocked(EShortcut shortcut, bool isBlocked)
  494. {
  495. auto target = shortcuts.find(shortcut);
  496. if (target == shortcuts.end())
  497. return;
  498. target->second.blocked = isBlocked;
  499. for (auto & entry : widgets)
  500. {
  501. auto button = std::dynamic_pointer_cast<CButton>(entry.second);
  502. if (button && button->assignedKey == shortcut)
  503. button->block(isBlocked);
  504. }
  505. }
  506. void InterfaceObjectConfigurable::addShortcut(EShortcut shortcut, std::function<void()> callback)
  507. {
  508. assert(shortcuts.count(shortcut) == 0);
  509. shortcuts[shortcut].callback = callback;
  510. }
  511. void InterfaceObjectConfigurable::keyPressed(EShortcut key)
  512. {
  513. auto target = shortcuts.find(key);
  514. if (target == shortcuts.end())
  515. return;
  516. if (target->second.assignedToButton)
  517. return; // will be handled by button instance
  518. if (target->second.blocked)
  519. return;
  520. target->second.callback();
  521. }