CPreGame.h 8.3 KB

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