InterfaceObjectConfigurable.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 "../widgets/CComponent.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/MiscWidgets.h"
  18. #include "../widgets/ObjectLists.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../windows/GUIClasses.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../../lib/CGeneralTextHandler.h"
  23. static std::map<std::string, int> KeycodeMap{
  24. {"up", SDLK_UP},
  25. {"down", SDLK_DOWN},
  26. {"left", SDLK_LEFT},
  27. {"right", SDLK_RIGHT},
  28. {"space", SDLK_SPACE},
  29. {"escape", SDLK_ESCAPE},
  30. {"backspace", SDLK_BACKSPACE},
  31. {"enter", SDLK_RETURN}
  32. };
  33. InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
  34. InterfaceObjectConfigurable(used, offset)
  35. {
  36. build(config);
  37. }
  38. InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
  39. CIntObject(used, offset)
  40. {
  41. REGISTER_BUILDER("picture", &InterfaceObjectConfigurable::buildPicture);
  42. REGISTER_BUILDER("image", &InterfaceObjectConfigurable::buildImage);
  43. REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture);
  44. REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation);
  45. REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel);
  46. REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup);
  47. REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton);
  48. REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
  49. REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
  50. REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
  51. }
  52. void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
  53. {
  54. builders[type] = f;
  55. }
  56. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  57. {
  58. callbacks[callbackName] = callback;
  59. }
  60. void InterfaceObjectConfigurable::deleteWidget(const std::string & name)
  61. {
  62. auto iter = widgets.find(name);
  63. if(iter != widgets.end())
  64. widgets.erase(iter);
  65. }
  66. void InterfaceObjectConfigurable::build(const JsonNode &config)
  67. {
  68. OBJ_CONSTRUCTION;
  69. logGlobal->debug("Building configurable interface object");
  70. auto * items = &config;
  71. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  72. {
  73. for(auto & item : config["variables"].Struct())
  74. {
  75. logGlobal->debug("Read variable named %s", item.first);
  76. variables[item.first] = item.second;
  77. }
  78. items = &config["items"];
  79. }
  80. const std::string unnamedObjectPrefix = "__widget_";
  81. for(const auto & item : items->Vector())
  82. {
  83. std::string name = item["name"].isNull()
  84. ? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
  85. : item["name"].String();
  86. logGlobal->debug("Building widget with name %s", name);
  87. widgets[name] = buildWidget(item);
  88. }
  89. }
  90. std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
  91. {
  92. if(config.isNull())
  93. return "";
  94. std::string s = config.String();
  95. logGlobal->debug("Reading text from translations by key: %s", s);
  96. return CGI->generaltexth->translate(s);
  97. }
  98. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  99. {
  100. Point p;
  101. logGlobal->debug("Reading point");
  102. p.x = config["x"].Integer();
  103. p.y = config["y"].Integer();
  104. return p;
  105. }
  106. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  107. {
  108. Rect p;
  109. logGlobal->debug("Reading rect");
  110. p.x = config["x"].Integer();
  111. p.y = config["y"].Integer();
  112. p.w = config["w"].Integer();
  113. p.h = config["h"].Integer();
  114. return p;
  115. }
  116. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  117. {
  118. logGlobal->debug("Reading text alignment");
  119. if(!config.isNull())
  120. {
  121. if(config.String() == "center")
  122. return ETextAlignment::CENTER;
  123. if(config.String() == "left")
  124. return ETextAlignment::TOPLEFT;
  125. if(config.String() == "right")
  126. return ETextAlignment::BOTTOMRIGHT;
  127. }
  128. logGlobal->debug("Uknown text alignment attribute");
  129. return ETextAlignment::CENTER;
  130. }
  131. SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  132. {
  133. logGlobal->debug("Reading color");
  134. if(!config.isNull())
  135. {
  136. if(config.String() == "yellow")
  137. return Colors::YELLOW;
  138. if(config.String() == "white")
  139. return Colors::WHITE;
  140. if(config.String() == "gold")
  141. return Colors::METALLIC_GOLD;
  142. if(config.String() == "green")
  143. return Colors::GREEN;
  144. if(config.String() == "orange")
  145. return Colors::ORANGE;
  146. if(config.String() == "bright-yellow")
  147. return Colors::BRIGHT_YELLOW;
  148. }
  149. logGlobal->debug("Uknown color attribute");
  150. return Colors::DEFAULT_KEY_COLOR;
  151. }
  152. EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
  153. {
  154. logGlobal->debug("Reading font");
  155. if(!config.isNull())
  156. {
  157. if(config.String() == "big")
  158. return EFonts::FONT_BIG;
  159. if(config.String() == "medium")
  160. return EFonts::FONT_MEDIUM;
  161. if(config.String() == "small")
  162. return EFonts::FONT_SMALL;
  163. if(config.String() == "tiny")
  164. return EFonts::FONT_TINY;
  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. int InterfaceObjectConfigurable::readKeycode(const JsonNode & config) const
  191. {
  192. logGlobal->debug("Reading keycode");
  193. if(config.getType() == JsonNode::JsonType::DATA_INTEGER)
  194. return config.Integer();
  195. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  196. {
  197. auto s = config.String();
  198. if(s.size() == 1) //keyboard symbol
  199. return s[0];
  200. if (KeycodeMap.count(s))
  201. return KeycodeMap[s];
  202. logGlobal->error("Invalid keycode '%s' in interface configuration!", config.String());
  203. return SDLK_UNKNOWN;
  204. }
  205. logGlobal->error("Invalid keycode format in interface configuration! Expected string or integer!", config.String());
  206. return SDLK_UNKNOWN;
  207. }
  208. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  209. {
  210. logGlobal->debug("Building widget CPicture");
  211. auto image = config["image"].String();
  212. auto position = readPosition(config["position"]);
  213. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  214. if(!config["visible"].isNull())
  215. pic->visible = config["visible"].Bool();
  216. if ( config["playerColored"].Bool() && LOCPLINT)
  217. pic->colorize(LOCPLINT->playerID);
  218. return pic;
  219. }
  220. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  221. {
  222. logGlobal->debug("Building widget CLabel");
  223. auto font = readFont(config["font"]);
  224. auto alignment = readTextAlignment(config["alignment"]);
  225. auto color = readColor(config["color"]);
  226. auto text = readText(config["text"]);
  227. auto position = readPosition(config["position"]);
  228. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  229. }
  230. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  231. {
  232. logGlobal->debug("Building widget CToggleGroup");
  233. auto position = readPosition(config["position"]);
  234. auto group = std::make_shared<CToggleGroup>(0);
  235. group->pos += position;
  236. if(!config["items"].isNull())
  237. {
  238. OBJ_CONSTRUCTION_TARGETED(group.get());
  239. int itemIdx = -1;
  240. for(const auto & item : config["items"].Vector())
  241. {
  242. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  243. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  244. }
  245. }
  246. if(!config["selected"].isNull())
  247. group->setSelected(config["selected"].Integer());
  248. if(!config["callback"].isNull())
  249. group->addCallback(callbacks.at(config["callback"].String()));
  250. return group;
  251. }
  252. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  253. {
  254. logGlobal->debug("Building widget CToggleButton");
  255. auto position = readPosition(config["position"]);
  256. auto image = config["image"].String();
  257. auto help = readHintText(config["help"]);
  258. auto button = std::make_shared<CToggleButton>(position, image, help);
  259. if(!config["items"].isNull())
  260. {
  261. for(const auto & item : config["items"].Vector())
  262. {
  263. button->addOverlay(buildWidget(item));
  264. }
  265. }
  266. if(!config["selected"].isNull())
  267. button->setSelected(config["selected"].Bool());
  268. if(!config["imageOrder"].isNull())
  269. {
  270. auto imgOrder = config["imageOrder"].Vector();
  271. assert(imgOrder.size() >= 4);
  272. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  273. }
  274. if(!config["callback"].isNull())
  275. {
  276. std::string callbackName = config["callback"].String();
  277. if (callbacks.count(callbackName))
  278. button->addCallback(callbacks.at(callbackName));
  279. else
  280. logGlobal->error("Invalid callback '%s' in widget", callbackName );
  281. }
  282. return button;
  283. }
  284. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  285. {
  286. logGlobal->debug("Building widget CButton");
  287. auto position = readPosition(config["position"]);
  288. auto image = config["image"].String();
  289. auto help = readHintText(config["help"]);
  290. auto button = std::make_shared<CButton>(position, image, help);
  291. if(!config["items"].isNull())
  292. {
  293. for(const auto & item : config["items"].Vector())
  294. {
  295. button->addOverlay(buildWidget(item));
  296. }
  297. }
  298. if(!config["imageOrder"].isNull())
  299. {
  300. auto imgOrder = config["imageOrder"].Vector();
  301. assert(imgOrder.size() >= 4);
  302. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  303. }
  304. if(!config["callback"].isNull())
  305. button->addCallback(std::bind(callbacks.at(config["callback"].String()), 0));
  306. if(!config["hotkey"].isNull())
  307. {
  308. if(config["hotkey"].getType() == JsonNode::JsonType::DATA_VECTOR)
  309. {
  310. for(auto k : config["hotkey"].Vector())
  311. button->assignedKeys.insert(readKeycode(k));
  312. }
  313. else
  314. button->assignedKeys.insert(readKeycode(config["hotkey"]));
  315. }
  316. return button;
  317. }
  318. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  319. {
  320. logGlobal->debug("Building widget CLabelGroup");
  321. auto font = readFont(config["font"]);
  322. auto alignment = readTextAlignment(config["alignment"]);
  323. auto color = readColor(config["color"]);
  324. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  325. if(!config["items"].isNull())
  326. {
  327. for(const auto & item : config["items"].Vector())
  328. {
  329. auto position = readPosition(item["position"]);
  330. auto text = readText(item["text"]);
  331. group->add(position.x, position.y, text);
  332. }
  333. }
  334. return group;
  335. }
  336. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  337. {
  338. logGlobal->debug("Building widget CSlider");
  339. auto position = readPosition(config["position"]);
  340. int length = config["size"].Integer();
  341. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  342. auto itemsVisible = config["itemsVisible"].Integer();
  343. auto itemsTotal = config["itemsTotal"].Integer();
  344. auto value = config["selected"].Integer();
  345. bool horizontal = config["orientation"].String() == "horizontal";
  346. auto const & result = std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  347. if (!config["scrollBounds"].isNull())
  348. {
  349. Rect bounds = readRect(config["scrollBounds"]);
  350. result->setScrollBounds(bounds);
  351. }
  352. return result;
  353. }
  354. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  355. {
  356. logGlobal->debug("Building widget CAnimImage");
  357. auto position = readPosition(config["position"]);
  358. auto image = config["image"].String();
  359. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  360. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  361. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  362. }
  363. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  364. {
  365. logGlobal->debug("Building widget CFilledTexture");
  366. auto image = config["image"].String();
  367. auto rect = readRect(config["rect"]);
  368. return std::make_shared<CFilledTexture>(image, rect);
  369. }
  370. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  371. {
  372. logGlobal->debug("Building widget CShowableAnim");
  373. auto position = readPosition(config["position"]);
  374. auto image = config["image"].String();
  375. ui8 flags = 0;
  376. if(!config["repeat"].Bool())
  377. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  378. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  379. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  380. if(!config["alpha"].isNull())
  381. anim->setAlpha(config["alpha"].Integer());
  382. if(!config["callback"].isNull())
  383. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  384. if(!config["frames"].isNull())
  385. {
  386. auto b = config["frames"]["start"].Integer();
  387. auto e = config["frames"]["end"].Integer();
  388. anim->set(group, b, e);
  389. }
  390. return anim;
  391. }
  392. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  393. {
  394. assert(!config.isNull());
  395. logGlobal->debug("Building widget from config");
  396. //overrides from variables
  397. for(auto & item : config["overrides"].Struct())
  398. {
  399. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  400. config[item.first] = variables[item.second.String()];
  401. }
  402. auto type = config["type"].String();
  403. auto buildIterator = builders.find(type);
  404. if(buildIterator != builders.end())
  405. return (buildIterator->second)(config);
  406. logGlobal->error("Builder with type %s is not registered", type);
  407. return nullptr;
  408. }