CPreGame.h 8.2 KB

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