CConfigHandler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #pragma once
  2. #include "../lib/JsonNode.h"
  3. /*
  4. * CConfighandler.h, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. class Settings;
  13. class SettingsListener;
  14. /// Main storage of game settings
  15. class DLL_LINKAGE SettingsStorage
  16. {
  17. //Helper struct to access specific node either via chain of operator[] or with one operator() (vector)
  18. template<typename Accessor>
  19. struct DLL_LINKAGE NodeAccessor
  20. {
  21. SettingsStorage & parent;
  22. std::vector<std::string> path;
  23. NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path);
  24. NodeAccessor<Accessor> operator [] (std::string nextNode) const;
  25. NodeAccessor<Accessor> operator () (std::vector<std::string> _path);
  26. operator Accessor() const;
  27. };
  28. std::set<SettingsListener*> listeners;
  29. JsonNode config;
  30. JsonNode & getNode(std::vector<std::string> path);
  31. // Calls all required listeners
  32. void invalidateNode(const std::vector<std::string> &changedPath);
  33. Settings get(std::vector<std::string> path);
  34. public:
  35. // Initialize config structure
  36. SettingsStorage();
  37. void init();
  38. // Get write access to config node at path
  39. const NodeAccessor<Settings> write;
  40. // Get access to listener at path
  41. const NodeAccessor<SettingsListener> listen;
  42. //Read access, see JsonNode::operator[]
  43. const JsonNode& operator [](std::string value);
  44. friend class SettingsListener;
  45. friend class Settings;
  46. };
  47. /// Class for listening changes in specific part of configuration (e.g. change of music volume)
  48. class DLL_LINKAGE SettingsListener
  49. {
  50. SettingsStorage &parent;
  51. // Path to this node
  52. std::vector<std::string> path;
  53. // Callback
  54. std::function<void(const JsonNode&)> callback;
  55. SettingsListener(SettingsStorage &_parent, const std::vector<std::string> &_path);
  56. // Executes callback if changedpath begins with path
  57. void nodeInvalidated(const std::vector<std::string> & changedPath);
  58. public:
  59. SettingsListener(const SettingsListener &sl);
  60. ~SettingsListener();
  61. // assign callback function
  62. void operator()(std::function<void(const JsonNode&)> _callback);
  63. friend class SettingsStorage;
  64. };
  65. /// System options, provides write access to config tree with auto-saving on change
  66. class DLL_LINKAGE Settings
  67. {
  68. SettingsStorage &parent;
  69. //path to this node
  70. std::vector<std::string> path;
  71. JsonNode &node;
  72. JsonNode copy;
  73. //Get access to node pointed by path
  74. Settings(SettingsStorage &_parent, const std::vector<std::string> &_path);
  75. public:
  76. //Saves config if it was modified
  77. ~Settings();
  78. //Returns node selected during construction
  79. JsonNode* operator ->();
  80. const JsonNode* operator ->() const;
  81. //Helper, replaces JsonNode::operator[]
  82. JsonNode& operator [](std::string value);
  83. const JsonNode& operator [](std::string value) const;
  84. friend class SettingsStorage;
  85. };
  86. namespace config
  87. {
  88. struct DLL_LINKAGE ButtonInfo
  89. {
  90. std::string defName;
  91. std::vector<std::string> additionalDefs;
  92. int x, y; //position on the screen
  93. bool playerColoured; //if true button will be colored to main player's color (works properly only for appropriate 8bpp graphics)
  94. };
  95. /// Struct which holds data about position of several GUI elements at the adventure map screen
  96. struct DLL_LINKAGE AdventureMapConfig
  97. {
  98. //minimap properties
  99. int minimapX, minimapY, minimapW, minimapH;
  100. //statusbar
  101. int statusbarX, statusbarY; //pos
  102. std::string statusbarG; //graphic name
  103. //resdatabar
  104. int resdatabarX, resdatabarY, resDist, resDateDist, resOffsetX, resOffsetY; //pos
  105. std::string resdatabarG; //graphic name
  106. //infobox
  107. int infoboxX, infoboxY;
  108. //advmap
  109. int advmapX, advmapY, advmapW, advmapH;
  110. bool smoothMove;
  111. bool puzzleSepia;
  112. //general properties
  113. std::string mainGraphic;
  114. //buttons
  115. ButtonInfo kingOverview, underground, questlog, sleepWake, moveHero, spellbook, advOptions,
  116. sysOptions, nextHero, endTurn;
  117. //hero list
  118. int hlistX, hlistY, hlistSize;
  119. std::string hlistMB, hlistMN, hlistAU, hlistAD;
  120. //town list
  121. int tlistX, tlistY, tlistSize;
  122. std::string tlistAU, tlistAD;
  123. //gems
  124. int gemX[4], gemY[4];
  125. std::vector<std::string> gemG;
  126. //in-game console
  127. int inputLineLength, outputLineLength;
  128. //kingdom overview
  129. int overviewPics, overviewSize; //pic count in def and count of visible slots
  130. std::string overviewBg; //background name
  131. };
  132. struct DLL_LINKAGE GUIOptions
  133. {
  134. AdventureMapConfig ac;
  135. };
  136. /// Handles adventure map screen settings
  137. class DLL_LINKAGE CConfigHandler
  138. {
  139. GUIOptions *current; // pointer to current gui options
  140. public:
  141. typedef std::map<std::pair<int,int>, GUIOptions > GuiOptionsMap;
  142. GuiOptionsMap guiOptions;
  143. void init();
  144. CConfigHandler(void); //c-tor
  145. ~CConfigHandler(void); //d-tor
  146. GUIOptions *go() { return current; };
  147. void SetResolution(int x, int y) {
  148. current = &guiOptions[std::pair<int,int>(x, y)];
  149. }
  150. };
  151. }
  152. extern DLL_LINKAGE SettingsStorage settings;
  153. extern DLL_LINKAGE config::CConfigHandler conf;