InterfaceObjectConfigurable.cpp 16 KB

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