InterfaceObjectConfigurable.cpp 14 KB

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