InterfaceObjectConfigurable.cpp 17 KB

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