CPreGame.h 8.2 KB

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