InterfaceObjectConfigurable.cpp 13 KB

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