InterfaceObjectConfigurable.cpp 18 KB

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