InterfaceObjectConfigurable.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. InterfaceObjectConfigurable::InterfaceObjectConfigurable(const JsonNode & config, int used, Point offset):
  24. CIntObject(used, offset)
  25. {
  26. init(config);
  27. }
  28. InterfaceObjectConfigurable::InterfaceObjectConfigurable(int used, Point offset):
  29. CIntObject(used, offset)
  30. {
  31. }
  32. void InterfaceObjectConfigurable::addCallback(const std::string & callbackName, std::function<void(int)> callback)
  33. {
  34. callbacks[callbackName] = callback;
  35. }
  36. void InterfaceObjectConfigurable::init(const JsonNode &config)
  37. {
  38. OBJ_CONSTRUCTION;
  39. for(auto & item : config["variables"].Struct())
  40. {
  41. variables[item.first] = item.second;
  42. }
  43. int unnamedObjectId = 0;
  44. const std::string unnamedObjectPrefix = "__widget_";
  45. for(const auto & item : config["items"].Vector())
  46. {
  47. std::string name = item["name"].isNull()
  48. ? unnamedObjectPrefix + std::to_string(unnamedObjectId++)
  49. : item["name"].String();
  50. widgets[name] = buildWidget(item);
  51. }
  52. }
  53. std::string InterfaceObjectConfigurable::readText(const JsonNode & config) const
  54. {
  55. if(config.isNull())
  56. return "";
  57. if(config.isNumber())
  58. {
  59. return CGI->generaltexth->allTexts[config.Integer()];
  60. }
  61. const std::string delimiter = "/";
  62. std::string s = config.String();
  63. JsonNode translated = CGI->generaltexth->localizedTexts;
  64. for(size_t p = s.find(delimiter); p != std::string::npos; p = s.find(delimiter))
  65. {
  66. translated = translated[s.substr(0, p)];
  67. s.erase(0, p + delimiter.length());
  68. }
  69. if(s == config.String())
  70. return s;
  71. return translated[s].String();
  72. }
  73. Point InterfaceObjectConfigurable::readPosition(const JsonNode & config) const
  74. {
  75. Point p;
  76. p.x = config["x"].Integer();
  77. p.y = config["y"].Integer();
  78. return p;
  79. }
  80. Rect InterfaceObjectConfigurable::readRect(const JsonNode & config) const
  81. {
  82. Rect p;
  83. p.x = config["x"].Integer();
  84. p.y = config["y"].Integer();
  85. p.w = config["w"].Integer();
  86. p.h = config["h"].Integer();
  87. return p;
  88. }
  89. ETextAlignment InterfaceObjectConfigurable::readTextAlignment(const JsonNode & config) const
  90. {
  91. if(!config.isNull())
  92. {
  93. if(config.String() == "center")
  94. return ETextAlignment::CENTER;
  95. if(config.String() == "left")
  96. return ETextAlignment::TOPLEFT;
  97. if(config.String() == "right")
  98. return ETextAlignment::BOTTOMRIGHT;
  99. }
  100. return ETextAlignment::CENTER;
  101. }
  102. SDL_Color InterfaceObjectConfigurable::readColor(const JsonNode & config) const
  103. {
  104. if(!config.isNull())
  105. {
  106. if(config.String() == "yellow")
  107. return Colors::YELLOW;
  108. if(config.String() == "white")
  109. return Colors::WHITE;
  110. if(config.String() == "gold")
  111. return Colors::METALLIC_GOLD;
  112. if(config.String() == "green")
  113. return Colors::GREEN;
  114. if(config.String() == "orange")
  115. return Colors::ORANGE;
  116. if(config.String() == "bright-yellow")
  117. return Colors::BRIGHT_YELLOW;
  118. }
  119. return Colors::DEFAULT_KEY_COLOR;
  120. }
  121. EFonts InterfaceObjectConfigurable::readFont(const JsonNode & config) const
  122. {
  123. if(!config.isNull())
  124. {
  125. if(config.String() == "big")
  126. return EFonts::FONT_BIG;
  127. if(config.String() == "medium")
  128. return EFonts::FONT_MEDIUM;
  129. if(config.String() == "small")
  130. return EFonts::FONT_SMALL;
  131. if(config.String() == "tiny")
  132. return EFonts::FONT_TINY;
  133. }
  134. return EFonts::FONT_TIMES;
  135. }
  136. std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(const JsonNode & config) const
  137. {
  138. std::pair<std::string, std::string> result;
  139. if(!config.isNull())
  140. {
  141. if(config.isNumber())
  142. return CGI->generaltexth->zelp[config.Integer()];
  143. if(config.getType() == JsonNode::JsonType::DATA_STRUCT)
  144. {
  145. result.first = readText(config["hover"]);
  146. result.second = readText(config["help"]);
  147. return result;
  148. }
  149. if(config.getType() == JsonNode::JsonType::DATA_STRING)
  150. {
  151. result.first = result.second = config.String();
  152. }
  153. }
  154. return result;
  155. }
  156. std::shared_ptr<CPicture> InterfaceObjectConfigurable::buildPicture(const JsonNode & config) const
  157. {
  158. auto image = config["image"].String();
  159. auto position = readPosition(config["position"]);
  160. auto pic = std::make_shared<CPicture>(image, position.x, position.y);
  161. if(!config["visible"].isNull())
  162. pic->visible = config["visible"].Bool();
  163. return pic;
  164. }
  165. std::shared_ptr<CLabel> InterfaceObjectConfigurable::buildLabel(const JsonNode & config) const
  166. {
  167. auto font = readFont(config["font"]);
  168. auto alignment = readTextAlignment(config["alignment"]);
  169. auto color = readColor(config["color"]);
  170. auto text = readText(config["text"]);
  171. auto position = readPosition(config["position"]);
  172. return std::make_shared<CLabel>(position.x, position.y, font, alignment, color, text);
  173. }
  174. std::shared_ptr<CToggleGroup> InterfaceObjectConfigurable::buildToggleGroup(const JsonNode & config) const
  175. {
  176. auto position = readPosition(config["position"]);
  177. auto group = std::make_shared<CToggleGroup>(0);
  178. group->pos += position;
  179. if(!config["items"].isNull())
  180. {
  181. SObjectConstruction obj__i(group.get());
  182. int itemIdx = -1;
  183. for(const auto & item : config["items"].Vector())
  184. {
  185. itemIdx = item["index"].isNull() ? itemIdx + 1 : item["index"].Integer();
  186. group->addToggle(itemIdx, std::dynamic_pointer_cast<CToggleBase>(buildWidget(item)));
  187. }
  188. }
  189. if(!config["selected"].isNull())
  190. group->setSelected(config["selected"].Integer());
  191. if(!config["callback"].isNull())
  192. group->addCallback(callbacks.at(config["callback"].String()));
  193. return group;
  194. }
  195. std::shared_ptr<CToggleButton> InterfaceObjectConfigurable::buildToggleButton(const JsonNode & config) const
  196. {
  197. auto position = readPosition(config["position"]);
  198. auto image = config["image"].String();
  199. auto zelp = readHintText(config["zelp"]);
  200. auto button = std::make_shared<CToggleButton>(position, image, zelp);
  201. if(!config["selected"].isNull())
  202. button->setSelected(config["selected"].Bool());
  203. if(!config["imageOrder"].isNull())
  204. {
  205. auto imgOrder = config["imageOrder"].Vector();
  206. assert(imgOrder.size() >= 4);
  207. button->setImageOrder(imgOrder[0].Integer(), imgOrder[1].Integer(), imgOrder[2].Integer(), imgOrder[3].Integer());
  208. }
  209. if(!config["callback"].isNull())
  210. button->addCallback(callbacks.at(config["callback"].String()));
  211. return button;
  212. }
  213. std::shared_ptr<CButton> InterfaceObjectConfigurable::buildButton(const JsonNode & config) const
  214. {
  215. auto position = readPosition(config["position"]);
  216. auto image = config["image"].String();
  217. auto zelp = readHintText(config["zelp"]);
  218. auto button = std::make_shared<CButton>(position, image, zelp);
  219. if(!config["items"].isNull())
  220. {
  221. for(const auto & item : config["items"].Vector())
  222. {
  223. button->addOverlay(buildWidget(item));
  224. }
  225. }
  226. if(!config["callback"].isNull())
  227. button->addCallback(std::bind(callbacks.at(config["callback"].String()), 0));
  228. return button;
  229. }
  230. std::shared_ptr<CLabelGroup> InterfaceObjectConfigurable::buildLabelGroup(const JsonNode & config) const
  231. {
  232. auto font = readFont(config["font"]);
  233. auto alignment = readTextAlignment(config["alignment"]);
  234. auto color = readColor(config["color"]);
  235. auto group = std::make_shared<CLabelGroup>(font, alignment, color);
  236. if(!config["items"].isNull())
  237. {
  238. for(const auto & item : config["items"].Vector())
  239. {
  240. auto position = readPosition(item["position"]);
  241. auto text = readText(item["text"]);
  242. group->add(position.x, position.y, text);
  243. }
  244. }
  245. return group;
  246. }
  247. std::shared_ptr<CSlider> InterfaceObjectConfigurable::buildSlider(const JsonNode & config) const
  248. {
  249. auto position = readPosition(config["position"]);
  250. int length = config["size"].Integer();
  251. auto style = config["style"].String() == "brown" ? CSlider::BROWN : CSlider::BLUE;
  252. auto itemsVisible = config["itemsVisible"].Integer();
  253. auto itemsTotal = config["itemsTotal"].Integer();
  254. auto value = config["selected"].Integer();
  255. bool horizontal = config["orientation"].String() == "horizontal";
  256. return std::make_shared<CSlider>(position, length, callbacks.at(config["callback"].String()), itemsVisible, itemsTotal, value, horizontal, style);
  257. }
  258. std::shared_ptr<CAnimImage> InterfaceObjectConfigurable::buildImage(const JsonNode & config) const
  259. {
  260. auto position = readPosition(config["position"]);
  261. auto image = config["image"].String();
  262. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  263. int frame = config["frame"].isNull() ? 0 : config["frame"].Integer();
  264. return std::make_shared<CAnimImage>(image, frame, group, position.x, position.y);
  265. }
  266. std::shared_ptr<CFilledTexture> InterfaceObjectConfigurable::buildTexture(const JsonNode & config) const
  267. {
  268. auto image = config["image"].String();
  269. auto rect = readRect(config["rect"]);
  270. return std::make_shared<CFilledTexture>(image, rect);
  271. }
  272. std::shared_ptr<CShowableAnim> InterfaceObjectConfigurable::buildAnimation(const JsonNode & config) const
  273. {
  274. auto position = readPosition(config["position"]);
  275. auto image = config["image"].String();
  276. ui8 flags = 0;
  277. if(!config["repeat"].Bool())
  278. flags |= CShowableAnim::EFlags::PLAY_ONCE;
  279. int group = config["group"].isNull() ? 0 : config["group"].Integer();
  280. auto anim = std::make_shared<CShowableAnim>(position.x, position.y, image, flags, 4, group);
  281. if(!config["alpha"].isNull())
  282. anim->setAlpha(config["alpha"].Integer());
  283. if(!config["callback"].isNull())
  284. anim->callback = std::bind(callbacks.at(config["callback"].String()), 0);
  285. if(!config["frames"].isNull())
  286. {
  287. auto b = config["frames"]["start"].Integer();
  288. auto e = config["frames"]["end"].Integer();
  289. anim->set(group, b, e);
  290. }
  291. return anim;
  292. }
  293. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildWidget(JsonNode config) const
  294. {
  295. assert(!config.isNull());
  296. //overrides from variables
  297. for(auto & item : config["overrides"].Struct())
  298. {
  299. config[item.first] = variables[item.second.String()];
  300. }
  301. auto type = config["type"].String();
  302. if(type == "picture")
  303. {
  304. return buildPicture(config);
  305. }
  306. if(type == "image")
  307. {
  308. return buildImage(config);
  309. }
  310. if(type == "texture")
  311. {
  312. return buildTexture(config);
  313. }
  314. if(type == "animation")
  315. {
  316. return buildAnimation(config);
  317. }
  318. if(type == "label")
  319. {
  320. return buildLabel(config);
  321. }
  322. if(type == "toggleGroup")
  323. {
  324. return buildToggleGroup(config);
  325. }
  326. if(type == "toggleButton")
  327. {
  328. return buildToggleButton(config);
  329. }
  330. if(type == "button")
  331. {
  332. return buildButton(config);
  333. }
  334. if(type == "labelGroup")
  335. {
  336. return buildLabelGroup(config);
  337. }
  338. if(type == "slider")
  339. {
  340. return buildSlider(config);
  341. }
  342. if(type == "custom")
  343. {
  344. return const_cast<InterfaceObjectConfigurable*>(this)->buildCustomWidget(config);
  345. }
  346. return std::shared_ptr<CIntObject>(nullptr);
  347. }
  348. std::shared_ptr<CIntObject> InterfaceObjectConfigurable::buildCustomWidget(const JsonNode & config)
  349. {
  350. return nullptr;
  351. }