InterfaceObjectConfigurable.cpp 15 KB

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