InterfaceObjectConfigurable.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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::deleteWidget(const std::string & name)
  59. {
  60. auto iter = widgets.find(name);
  61. if(iter != widgets.end())
  62. widgets.erase(iter);
  63. }
  64. void InterfaceObjectConfigurable::build(const JsonNode &config)
  65. {
  66. OBJ_CONSTRUCTION;
  67. logGlobal->debug("Building configurable interface object");
  68. auto * items = &config;
  69. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  70. {
  71. for(auto & item : config["variables"].Struct())
  72. {
  73. logGlobal->debug("Read variable named %s", item.first);
  74. variables[item.first] = item.second;
  75. }
  76. items = &config["items"];
  77. }
  78. const std::string unnamedObjectPrefix = "__widget_";
  79. for(const auto & item : items->Vector())
  80. {
  81. std::string name = item["name"].isNull()
  82. ? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
  83. : item["name"].String();
  84. logGlobal->debug("Building widget with name %s", name);
  85. widgets[name] = buildWidget(item);
  86. }
  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. }
  164. logGlobal->debug("Uknown font attribute");
  165. return EFonts::FONT_TIMES;
  166. }
  167. std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
  168. {
  169. logGlobal->debug("Reading hint text");
  170. std::pair<std::string, std::string> result;
  171. if(!config.isNull())
  172. {
  173. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  174. {
  175. result.first = readText(config["hover"]);
  176. result.second = readText(config["help"]);
  177. return result;
  178. }
  179. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  180. {
  181. logGlobal->debug("Reading hint text (help) from generaltext handler:%sd", config.String());
  182. result.first = CGI->generaltexth->translate( config.String(), "hover");
  183. result.second = CGI->generaltexth->translate( config.String(), "help");
  184. }
  185. }
  186. return result;
  187. }
  188. int InterfaceObjectConfigurable::readKeycode(const JsonNode & config) const
  189. {
  190. logGlobal->debug("Reading keycode");
  191. if(config.getType() == JsonNode::JsonType::DATA_INTEGER)
  192. return config.Integer();
  193. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  194. {
  195. auto s = config.String();
  196. if(s.size() == 1) //keyboard symbol
  197. return s[0];
  198. return KeycodeMap[s];
  199. }
  200. return 0;
  201. }
  202. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  203. {
  204. logGlobal->debug("Building widget CPicture");
  205. auto image = config["image"].String();
  206. auto position = readPosition(config["position"]);
  207. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  208. if(!config["visible"].isNull())
  209. pic->visible = config["visible"].Bool();
  210. return pic;
  211. }
  212. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  213. {
  214. logGlobal->debug("Building widget CLabel");
  215. auto font = readFont(config["font"]);
  216. auto alignment = readTextAlignment(config["alignment"]);
  217. auto color = readColor(config["color"]);
  218. auto text = readText(config["text"]);
  219. auto position = readPosition(config["position"]);
  220. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  221. }
  222. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  223. {
  224. logGlobal->debug("Building widget CToggleGroup");
  225. auto position = readPosition(config["position"]);
  226. auto group = std::make_shared<CToggleGroup>(0);
  227. group->pos += position;
  228. if(!config["items"].isNull())
  229. {
  230. OBJ_CONSTRUCTION_TARGETED(group.get());
  231. int itemIdx = -1;
  232. for(const auto & item : config["items"].Vector())
  233. {
  234. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  235. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  236. }
  237. }
  238. if(!config["selected"].isNull())
  239. group->setSelected(config["selected"].Integer());
  240. if(!config["callback"].isNull())
  241. group->addCallback(callbacks.at(config["callback"].String()));
  242. return group;
  243. }
  244. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  245. {
  246. logGlobal->debug("Building widget CToggleButton");
  247. auto position = readPosition(config["position"]);
  248. auto image = config["image"].String();
  249. auto help = readHintText(config["help"]);
  250. auto button = std::make_shared<CToggleButton>(position, image, help);
  251. if(!config["selected"].isNull())
  252. button->setSelected(config["selected"].Bool());
  253. if(!config["imageOrder"].isNull())
  254. {
  255. auto imgOrder = config["imageOrder"].Vector();
  256. assert(imgOrder.size() >= 4);
  257. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  258. }
  259. if(!config["callback"].isNull())
  260. button->addCallback(callbacks.at(config["callback"].String()));
  261. return button;
  262. }
  263. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  264. {
  265. logGlobal->debug("Building widget CButton");
  266. auto position = readPosition(config["position"]);
  267. auto image = config["image"].String();
  268. auto help = readHintText(config["help"]);
  269. auto button = std::make_shared<CButton>(position, image, help);
  270. if(!config["items"].isNull())
  271. {
  272. for(const auto & item : config["items"].Vector())
  273. {
  274. button->addOverlay(buildWidget(item));
  275. }
  276. }
  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(std::bind(callbacks.at(config["callback"].String()), 0));
  285. if(!config["hotkey"].isNull())
  286. {
  287. if(config["hotkey"].getType() == JsonNode::JsonType::DATA_VECTOR)
  288. {
  289. for(auto k : config["hotkey"].Vector())
  290. button->assignedKeys.insert(readKeycode(k));
  291. }
  292. else
  293. button->assignedKeys.insert(readKeycode(config["hotkey"]));
  294. }
  295. return button;
  296. }
  297. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  298. {
  299. logGlobal->debug("Building widget CLabelGroup");
  300. auto font = readFont(config["font"]);
  301. auto alignment = readTextAlignment(config["alignment"]);
  302. auto color = readColor(config["color"]);
  303. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  304. if(!config["items"].isNull())
  305. {
  306. for(const auto & item : config["items"].Vector())
  307. {
  308. auto position = readPosition(item["position"]);
  309. auto text = readText(item["text"]);
  310. group->add(position.x, position.y, text);
  311. }
  312. }
  313. return group;
  314. }
  315. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  316. {
  317. logGlobal->debug("Building widget CSlider");
  318. auto position = readPosition(config["position"]);
  319. int length = config["size"].Integer();
  320. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  321. auto itemsVisible = config["itemsVisible"].Integer();
  322. auto itemsTotal = config["itemsTotal"].Integer();
  323. auto value = config["selected"].Integer();
  324. bool horizontal = config["orientation"].String() == "horizontal";
  325. return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  326. }
  327. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  328. {
  329. logGlobal->debug("Building widget CAnimImage");
  330. auto position = readPosition(config["position"]);
  331. auto image = config["image"].String();
  332. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  333. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  334. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  335. }
  336. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  337. {
  338. logGlobal->debug("Building widget CFilledTexture");
  339. auto image = config["image"].String();
  340. auto rect = readRect(config["rect"]);
  341. return std::make_shared<CFilledTexture>(image, rect);
  342. }
  343. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  344. {
  345. logGlobal->debug("Building widget CShowableAnim");
  346. auto position = readPosition(config["position"]);
  347. auto image = config["image"].String();
  348. ui8 flags = 0;
  349. if(!config["repeat"].Bool())
  350. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  351. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  352. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  353. if(!config["alpha"].isNull())
  354. anim->setAlpha(config["alpha"].Integer());
  355. if(!config["callback"].isNull())
  356. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  357. if(!config["frames"].isNull())
  358. {
  359. auto b = config["frames"]["start"].Integer();
  360. auto e = config["frames"]["end"].Integer();
  361. anim->set(group, b, e);
  362. }
  363. return anim;
  364. }
  365. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  366. {
  367. assert(!config.isNull());
  368. logGlobal->debug("Building widget from config");
  369. //overrides from variables
  370. for(auto & item : config["overrides"].Struct())
  371. {
  372. logGlobal->debug("Config attribute %s was overriden by variable %s", item.first, item.second.String());
  373. config[item.first] = variables[item.second.String()];
  374. }
  375. auto type = config["type"].String();
  376. auto buildIterator = builders.find(type);
  377. if(buildIterator != builders.end())
  378. return (buildIterator->second)(config);
  379. logGlobal->error("Builder with type %s is not registered", type);
  380. return nullptr;
  381. }