InterfaceObjectConfigurable.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "../gui/CAnimation.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. {"enter", SDLK_RETURN}
  30. };
  31. InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
  32. InterfaceObjectConfigurable(used, offset)
  33. {
  34. build(config);
  35. }
  36. InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
  37. CIntObject(used, offset)
  38. {
  39. REGISTER_BUILDER("picture", &InterfaceObjectConfigurable::buildPicture);
  40. REGISTER_BUILDER("image", &InterfaceObjectConfigurable::buildImage);
  41. REGISTER_BUILDER("texture", &InterfaceObjectConfigurable::buildTexture);
  42. REGISTER_BUILDER("animation", &InterfaceObjectConfigurable::buildAnimation);
  43. REGISTER_BUILDER("label", &InterfaceObjectConfigurable::buildLabel);
  44. REGISTER_BUILDER("toggleGroup", &InterfaceObjectConfigurable::buildToggleGroup);
  45. REGISTER_BUILDER("toggleButton", &InterfaceObjectConfigurable::buildToggleButton);
  46. REGISTER_BUILDER("button", &InterfaceObjectConfigurable::buildButton);
  47. REGISTER_BUILDER("labelGroup", &InterfaceObjectConfigurable::buildLabelGroup);
  48. REGISTER_BUILDER("slider", &InterfaceObjectConfigurable::buildSlider);
  49. }
  50. void InterfaceObjectConfigurable::registerBuilder(const std::string & type, BuilderFunction f)
  51. {
  52. builders[type] = f;
  53. }
  54. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  55. {
  56. callbacks[callbackName] = callback;
  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. if(config.isNumber())
  87. {
  88. logGlobal->debug("Reading text from generaltext handler id:%d", config.Integer());
  89. return CGI->generaltexth->allTexts[config.Integer()];
  90. }
  91. const std::string delimiter = "/";
  92. std::string s = config.String();
  93. logGlobal->debug("Reading text from translations by key: %s", s);
  94. JsonNode translated = CGI->generaltexth->localizedTexts;
  95. for(size_t p = s.find(delimiter); p != std::string::npos; p = s.find(delimiter))
  96. {
  97. translated = translated[s.substr(0, p)];
  98. s.erase(0, p + delimiter.length());
  99. }
  100. if(s == config.String())
  101. {
  102. logGlobal->warn("Reading non-translated text: %s", s);
  103. return s;
  104. }
  105. return translated[s].String();
  106. }
  107. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  108. {
  109. Point p;
  110. logGlobal->debug("Reading point");
  111. p.x = config["x"].Integer();
  112. p.y = config["y"].Integer();
  113. return p;
  114. }
  115. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  116. {
  117. Rect p;
  118. logGlobal->debug("Reading rect");
  119. p.x = config["x"].Integer();
  120. p.y = config["y"].Integer();
  121. p.w = config["w"].Integer();
  122. p.h = config["h"].Integer();
  123. return p;
  124. }
  125. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  126. {
  127. logGlobal->debug("Reading text alignment");
  128. if(!config.isNull())
  129. {
  130. if(config.String() == "center")
  131. return ETextAlignment::CENTER;
  132. if(config.String() == "left")
  133. return ETextAlignment::TOPLEFT;
  134. if(config.String() == "right")
  135. return ETextAlignment::BOTTOMRIGHT;
  136. }
  137. logGlobal->debug("Uknown text alignment attribute");
  138. return ETextAlignment::CENTER;
  139. }
  140. SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  141. {
  142. logGlobal->debug("Reading color");
  143. if(!config.isNull())
  144. {
  145. if(config.String() == "yellow")
  146. return Colors::YELLOW;
  147. if(config.String() == "white")
  148. return Colors::WHITE;
  149. if(config.String() == "gold")
  150. return Colors::METALLIC_GOLD;
  151. if(config.String() == "green")
  152. return Colors::GREEN;
  153. if(config.String() == "orange")
  154. return Colors::ORANGE;
  155. if(config.String() == "bright-yellow")
  156. return Colors::BRIGHT_YELLOW;
  157. }
  158. logGlobal->debug("Uknown color attribute");
  159. return Colors::DEFAULT_KEY_COLOR;
  160. }
  161. EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
  162. {
  163. logGlobal->debug("Reading font");
  164. if(!config.isNull())
  165. {
  166. if(config.String() == "big")
  167. return EFonts::FONT_BIG;
  168. if(config.String() == "medium")
  169. return EFonts::FONT_MEDIUM;
  170. if(config.String() == "small")
  171. return EFonts::FONT_SMALL;
  172. if(config.String() == "tiny")
  173. return EFonts::FONT_TINY;
  174. }
  175. logGlobal->debug("Uknown font attribute");
  176. return EFonts::FONT_TIMES;
  177. }
  178. std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
  179. {
  180. logGlobal->debug("Reading hint text");
  181. std::pair<std::string, std::string> result;
  182. if(!config.isNull())
  183. {
  184. if(config.isNumber())
  185. {
  186. logGlobal->debug("Reading hint text (zelp) from generaltext handler id:%d", config.Integer());
  187. return CGI->generaltexth->zelp[config.Integer()];
  188. }
  189. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  190. {
  191. result.first = readText(config["hover"]);
  192. result.second = readText(config["help"]);
  193. return result;
  194. }
  195. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  196. {
  197. logGlobal->debug("Reading non-translated hint: %s", config.String());
  198. result.first = result.second = config.String();
  199. }
  200. }
  201. return result;
  202. }
  203. int InterfaceObjectConfigurable::readKeycode(const JsonNode & config) const
  204. {
  205. logGlobal->debug("Reading keycode");
  206. if(config.getType() == JsonNode::JsonType::DATA_INTEGER)
  207. return config.Integer();
  208. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  209. {
  210. auto s = config.String();
  211. if(s.size() == 1) //keyboard symbol
  212. return s[0];
  213. return KeycodeMap[s];
  214. }
  215. return 0;
  216. }
  217. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  218. {
  219. logGlobal->debug("Building widget CPicture");
  220. auto image = config["image"].String();
  221. auto position = readPosition(config["position"]);
  222. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  223. if(!config["visible"].isNull())
  224. pic->visible = config["visible"].Bool();
  225. return pic;
  226. }
  227. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  228. {
  229. logGlobal->debug("Building widget CLabel");
  230. auto font = readFont(config["font"]);
  231. auto alignment = readTextAlignment(config["alignment"]);
  232. auto color = readColor(config["color"]);
  233. auto text = readText(config["text"]);
  234. auto position = readPosition(config["position"]);
  235. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  236. }
  237. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  238. {
  239. logGlobal->debug("Building widget CToggleGroup");
  240. auto position = readPosition(config["position"]);
  241. auto group = std::make_shared<CToggleGroup>(0);
  242. group->pos += position;
  243. if(!config["items"].isNull())
  244. {
  245. OBJ_CONSTRUCTION_TARGETED(group.get());
  246. int itemIdx = -1;
  247. for(const auto & item : config["items"].Vector())
  248. {
  249. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  250. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  251. }
  252. }
  253. if(!config["selected"].isNull())
  254. group->setSelected(config["selected"].Integer());
  255. if(!config["callback"].isNull())
  256. group->addCallback(callbacks.at(config["callback"].String()));
  257. return group;
  258. }
  259. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  260. {
  261. logGlobal->debug("Building widget CToggleButton");
  262. auto position = readPosition(config["position"]);
  263. auto image = config["image"].String();
  264. auto zelp = readHintText(config["zelp"]);
  265. auto button = std::make_shared<CToggleButton>(position, image, zelp);
  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. button->addCallback(callbacks.at(config["callback"].String()));
  276. return button;
  277. }
  278. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  279. {
  280. logGlobal->debug("Building widget CButton");
  281. auto position = readPosition(config["position"]);
  282. auto image = config["image"].String();
  283. auto zelp = readHintText(config["zelp"]);
  284. auto button = std::make_shared<CButton>(position, image, zelp);
  285. if(!config["items"].isNull())
  286. {
  287. for(const auto & item : config["items"].Vector())
  288. {
  289. button->addOverlay(buildWidget(item));
  290. }
  291. }
  292. if(!config["imageOrder"].isNull())
  293. {
  294. auto imgOrder = config["imageOrder"].Vector();
  295. assert(imgOrder.size() >= 4);
  296. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  297. }
  298. if(!config["callback"].isNull())
  299. button->addCallback(std::bind(callbacks.at(config["callback"].String()), 0));
  300. if(!config["hotkey"].isNull())
  301. {
  302. if(config["hotkey"].getType() == JsonNode::JsonType::DATA_VECTOR)
  303. {
  304. for(auto k : config["hotkey"].Vector())
  305. button->assignedKeys.insert(readKeycode(k));
  306. }
  307. else
  308. button->assignedKeys.insert(readKeycode(config["hotkey"]));
  309. }
  310. return button;
  311. }
  312. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  313. {
  314. logGlobal->debug("Building widget CLabelGroup");
  315. auto font = readFont(config["font"]);
  316. auto alignment = readTextAlignment(config["alignment"]);
  317. auto color = readColor(config["color"]);
  318. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  319. if(!config["items"].isNull())
  320. {
  321. for(const auto & item : config["items"].Vector())
  322. {
  323. auto position = readPosition(item["position"]);
  324. auto text = readText(item["text"]);
  325. group->add(position.x, position.y, text);
  326. }
  327. }
  328. return group;
  329. }
  330. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  331. {
  332. logGlobal->debug("Building widget CSlider");
  333. auto position = readPosition(config["position"]);
  334. int length = config["size"].Integer();
  335. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  336. auto itemsVisible = config["itemsVisible"].Integer();
  337. auto itemsTotal = config["itemsTotal"].Integer();
  338. auto value = config["selected"].Integer();
  339. bool horizontal = config["orientation"].String() == "horizontal";
  340. return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  341. }
  342. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  343. {
  344. logGlobal->debug("Building widget CAnimImage");
  345. auto position = readPosition(config["position"]);
  346. auto image = config["image"].String();
  347. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  348. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  349. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  350. }
  351. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  352. {
  353. logGlobal->debug("Building widget CFilledTexture");
  354. auto image = config["image"].String();
  355. auto rect = readRect(config["rect"]);
  356. return std::make_shared<CFilledTexture>(image, rect);
  357. }
  358. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  359. {
  360. logGlobal->debug("Building widget CShowableAnim");
  361. auto position = readPosition(config["position"]);
  362. auto image = config["image"].String();
  363. ui8 flags = 0;
  364. if(!config["repeat"].Bool())
  365. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  366. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  367. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  368. if(!config["alpha"].isNull())
  369. anim->setAlpha(config["alpha"].Integer());
  370. if(!config["callback"].isNull())
  371. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  372. if(!config["frames"].isNull())
  373. {
  374. auto b = config["frames"]["start"].Integer();
  375. auto e = config["frames"]["end"].Integer();
  376. anim->set(group, b, e);
  377. }
  378. return anim;
  379. }
  380. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  381. {
  382. assert(!config.isNull());
  383. logGlobal->debug("Building widget from config");
  384. //overrides from variables
  385. for(auto & item : config["overrides"].Struct())
  386. {
  387. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  388. config[item.first] = variables[item.second.String()];
  389. }
  390. auto type = config["type"].String();
  391. auto buildIterator = builders.find(type);
  392. if(buildIterator != builders.end())
  393. return (buildIterator->second)(config);
  394. logGlobal->error("Builder with type %s is not registered", type);
  395. return nullptr;
  396. }