CPreGame.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #ifndef __CPREGAME_H__
  2. #define __CPREGAME_H__
  3. #include "global.h"
  4. #include <set>
  5. #include <SDL.h>
  6. #include "StartInfo.h"
  7. #include "CMessage.h"
  8. #include "map.h"
  9. #include "hch/CMusicHandler.h"
  10. #include <boost/function.hpp>
  11. #include <boost/bind.hpp>
  12. #include <cstdlib>
  13. /*
  14. * CPreGame.h, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. class CPreGame;
  23. class CDefHandler;
  24. extern CPreGame * CPG;
  25. typedef void(CPreGame::*ttt)();
  26. class CGroup;
  27. class CPoinGroup ;
  28. class IntSelBut;
  29. struct HighButton
  30. {
  31. int ID;
  32. int type;
  33. SDL_Rect pos;
  34. CDefHandler* imgs;
  35. int state;
  36. bool freeimgs;
  37. HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel=false, int id=-1);
  38. HighButton();
  39. virtual ~HighButton();
  40. bool selectable, selected;
  41. bool highlightable, highlighted;
  42. virtual void show();
  43. virtual void press(bool down=true);
  44. virtual void hover(bool on=true)=0;
  45. virtual void select(bool on=true)=0;
  46. };
  47. struct Button: public HighButton
  48. {
  49. CGroup *ourGroup;
  50. Button( SDL_Rect Pos, boost::function<void()> Fun,CDefHandler* Imgs, bool Sel=false, CGroup* gr=NULL, int id=-1);
  51. Button();
  52. boost::function<void()> fun;
  53. virtual void hover(bool on=true);
  54. virtual void select(bool on=true);
  55. };
  56. struct SetrButton: public Button
  57. {
  58. int key, * poin;
  59. virtual void press(bool down=true);
  60. SetrButton();
  61. };
  62. class Slider
  63. {
  64. public:
  65. bool vertical; // false means horizontal
  66. SDL_Rect pos; // position
  67. Button *up, *down, //or left/right
  68. *slider;
  69. int positionsAmnt, capacity;// capacity - amount of positions dispplayed at once
  70. int whereAreWe; // first displayed thing
  71. bool moving;
  72. boost::function<void(int)> fun;
  73. void clickDown(int x, int y, bool bzgl=true);
  74. void clickUp(int x, int y, bool bzgl=true);
  75. void mMove(int x, int y, bool bzgl=true);
  76. void moveUp();
  77. void moveDown();
  78. void deactivate();
  79. void activate();
  80. Slider(int x, int y, int h, int amnt, int cap, bool ver);
  81. ~Slider();
  82. void updateSlid();
  83. void handleIt(SDL_Event sev);
  84. };
  85. struct IntBut: public Button
  86. {
  87. public:
  88. int key;
  89. int * what;
  90. IntBut();
  91. void set();
  92. };
  93. class CGroup
  94. {
  95. public:
  96. Button * selected;
  97. int type; // 1=sinsel
  98. CGroup():selected(NULL),type(0){};
  99. };
  100. class CPoinGroup :public CGroup
  101. {
  102. public:
  103. int * gdzie; //where (po polsku, bo by by�o s�owo kluczowe :/)
  104. void setYour(IntSelBut * your);
  105. };
  106. struct IntSelBut: public Button
  107. {
  108. public:
  109. CPoinGroup * ourPoinGroup;
  110. int key;
  111. IntSelBut(){};
  112. IntSelBut( SDL_Rect Pos, boost::function<void()> Fun,CDefHandler* Imgs, bool Sel=false, CPoinGroup* gr=NULL, int My=-1);
  113. void select(bool on=true);
  114. };
  115. class PreGameTab
  116. {
  117. public:
  118. bool showed;
  119. virtual void init()=0;
  120. virtual void show()=0;
  121. virtual void hide()=0;
  122. PreGameTab();
  123. };
  124. class RanSel : public PreGameTab
  125. {
  126. Button horcpl[9], horcte[9], conpl[9], conte[8], water[4], monster[4], //last is random
  127. size[4], twoLevel, showRand;
  128. CGroup *Ghorcpl, *Ghorcte, *Gconpl, *Gconte, *Gwater, *Gmonster, *Gsize;
  129. };
  130. class Options : public PreGameTab
  131. {
  132. bool inited;
  133. struct OptionSwitch:public HighButton
  134. {
  135. void hover(bool on=true){};
  136. void select(bool on=true){};
  137. OptionSwitch( SDL_Rect Pos, CDefHandler* Imgs, bool Left, int Which)
  138. :HighButton(Pos,Imgs,false,7),left(Left),which(Which)
  139. {selectable=false;highlightable=false;}
  140. void press(bool down=true);
  141. bool left;
  142. int playerID;
  143. int serialID;
  144. int which; //-1=castle;0=hero;1=bonus
  145. };
  146. struct PlayerFlag:public HighButton
  147. {
  148. int color;
  149. PlayerFlag(SDL_Rect Pos, CDefHandler* Imgs, int Color)
  150. :HighButton(Pos,Imgs,false,7),color(Color)
  151. {selectable=false;highlightable=true;}
  152. void hover(bool on=true);
  153. void press(bool down=true);
  154. void select(bool on=true){};
  155. };
  156. struct PlayerOptions
  157. {
  158. PlayerOptions(int serial, int player);
  159. Ecolor color;
  160. PlayerFlag flag;
  161. //SDL_Surface * bg;
  162. OptionSwitch Cleft, Cright, Hleft, Hright, Bleft, Bright;
  163. int nr;
  164. };
  165. public:
  166. std::set<int> usedHeroes;
  167. Slider * turnLength;
  168. SDL_Surface * bg,
  169. * rHero, * rCastle, * nHero, * nCastle;
  170. std::vector<SDL_Surface*> bgs;
  171. std::vector<CDefHandler*> flags;
  172. CDefHandler //* castles, * heroes, * bonus,
  173. * left, * right,
  174. * bonuses;
  175. std::vector<PlayerOptions*> poptions;
  176. void show();
  177. void hide();
  178. void init();
  179. void showIcon (int what, int nr, bool abs); //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  180. bool canUseThisHero(int ID);
  181. int nextAllowedHero(int min, int max, int incl, int dir); //incl 0 - wlacznie; incl 1 - wylacznie; min-max - zakres szukania
  182. Options(){inited=showed=false;};
  183. ~Options();
  184. };
  185. class MapSel : public PreGameTab
  186. {
  187. public:
  188. ESortBy sortBy;
  189. SDL_Surface * bg;
  190. int selected; //selected map
  191. CDefHandler * Dtypes, * Dvic;
  192. CDefHandler *Dsizes, * Dloss,
  193. * sFlags;
  194. std::vector<SDL_Surface*> scenImgs;
  195. //int current;
  196. std::vector<CMapInfo*> ourMaps;
  197. std::vector<CMapInfo*> ourGames;
  198. IntBut small, medium, large, xlarge, all;
  199. SetrButton nrplayer, mapsize, type, name, viccon, loscon;
  200. Slider *slid, *descslid;
  201. int sizeFilter;
  202. int whichWL(int nr);
  203. int countWL();
  204. void show();
  205. void hide();
  206. void init();
  207. std::string gdiff(std::string ss);
  208. void printMaps(int from,int to=18, int at=0, bool abs=false);
  209. void select(int which, bool updateMapsList=true, bool forceSettingsUpdate=false);
  210. void moveByOne(bool up);
  211. void printSelectedInfo();
  212. void printFlags();
  213. void processMaps(const std::vector<std::string> &pliczkiTemp, int start, int threads);
  214. void processGames(const std::vector<std::string> &pliczkiTemp);
  215. CMapInfo &selectedMap();
  216. std::vector<CMapInfo*> &curVector();
  217. MapSel();
  218. ~MapSel();
  219. };
  220. class ScenSel
  221. {
  222. public:
  223. CPoinGroup * difficulty;
  224. bool listShowed;
  225. //RanSel ransel;
  226. MapSel mapsel;
  227. SDL_Surface * background, *savenameStrip, *scenInf, *scenList, *randMap, *options ;
  228. Button bScens, bOptions, bRandom, bBegin, bLoad, bBack;
  229. IntSelBut bEasy, bNormal, bHard, bExpert, bImpossible;
  230. Button * pressed;
  231. std::vector<Mapa> maps;
  232. int selectedDiff;
  233. void initRanSel();
  234. void showRanSel();
  235. void hideRanSel();
  236. void genScenList();
  237. ScenSel();
  238. ~ScenSel();
  239. } ;
  240. class CPreGame
  241. {
  242. public:
  243. std::string playerName;
  244. int playerColor;
  245. HighButton * highlighted;
  246. PreGameTab* currentTab;
  247. StartInfo ret;
  248. bool run;
  249. bool first; //hasn't we showed the scensel
  250. int fromnewgame; //1 - new game; 0 - load game; 2 - save game
  251. std::vector<Slider *> interested;
  252. CMusicHandler * mush;
  253. std::vector<HighButton *> btns;
  254. SDL_Rect * currentMessage;
  255. SDL_Surface * behindCurMes;
  256. CDefHandler *ok, *cancel;
  257. enum EState { //where are we?
  258. mainMenu, newGame, loadGame, ScenarioList
  259. } state;
  260. struct menuItems {
  261. menuItems();
  262. ~menuItems();
  263. SDL_Surface * background, *bgAd;
  264. CDefHandler *newGame, *loadGame, *highScores,*credits, *quit;
  265. SDL_Rect lNewGame, lLoadGame, lHighScores, lCredits, lQuit;
  266. ttt fNewGame, fLoadGame, fHighScores, fCredits, fQuit;
  267. int highlighted;//0=none; 1=new game; 2=load game; 3=high score; 4=credits; 5=quit
  268. } * ourMainMenu, * ourNewMenu, * ourLoadMenu;
  269. ScenSel *ourScenSel;
  270. Options * ourOptions;
  271. std::string map; //selected map
  272. CPreGame(); //c-tor
  273. ~CPreGame();//d-tor
  274. std::string buttonText(int which);
  275. menuItems * currentItems();
  276. void(CPreGame::*handleOther)(SDL_Event&);
  277. void scenHandleEv(SDL_Event& sEvent);
  278. void begin(){run=false;ret.difficulty=ourScenSel->selectedDiff;};
  279. void quitAskBox();
  280. void quit(){exit(EXIT_SUCCESS);};
  281. void initScenSel();
  282. void showScenSel();
  283. void showScenList();
  284. void initOptions();
  285. void showOptions();
  286. void initNewMenu();
  287. void initLoadMenu();
  288. void showNewMenu();
  289. void showLoadMenu();
  290. void showMainMenu();
  291. StartInfo runLoop(); // runs mainloop of PreGame
  292. void initMainMenu(); //loads components for main menu
  293. void highlightButton(int which, int on); //highlights one from 5 main menu buttons
  294. void showCenBox (std::string data); //
  295. void showAskBox (std::string data, void(*f1)(),void(*f2)());
  296. void hideBox ();
  297. void printRating();
  298. void printMapsFrom(int from);
  299. void setTurnLength(int on);
  300. void sortMaps();
  301. };
  302. #endif // __CPREGAME_H__