CPreGame.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 class 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(){int type=1;bool selectable=false;bool selected=false;int state=0;bool 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(){int type=2;int fun=NULL;bool 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<T>(Pos,Fun,Imgs,Sel,gr),key(My){ourPoinGroup=gr;};
  90. void select(bool on=true) {(*this).Button<T>::select(on);ourPoinGroup->setYour(this);
  91. #ifndef __amigaos4__
  92. CPG->printRating();
  93. #else
  94. #warning not compile here
  95. #endif
  96. }
  97. };
  98. template <class T> class CPoinGroup :public CGroup<T>
  99. {
  100. public:
  101. int * gdzie; //where (po polsku, bo by by³o s³owo kluczowe :/)
  102. void setYour(IntSelBut<T> * your){*gdzie=your->key;};
  103. };
  104. template <class T> class CGroup
  105. {
  106. public:
  107. Button<T> * selected;
  108. int type; // 1=sinsel
  109. CGroup():selected(NULL),type(0){};
  110. };
  111. class PreGameTab
  112. {
  113. public:
  114. bool showed;
  115. virtual void init()=0;
  116. virtual void show()=0;
  117. virtual void hide()=0;
  118. PreGameTab();
  119. };
  120. class RanSel : public PreGameTab
  121. {
  122. Button<> horcpl[9], horcte[9], conpl[9], conte[8], water[4], monster[4], //last is random
  123. size[4], twoLevel, showRand;
  124. CGroup<> *Ghorcpl, *Ghorcte, *Gconpl, *Gconte, *Gwater, *Gmonster, *Gsize;
  125. };
  126. class Options : public PreGameTab
  127. {
  128. bool inited;
  129. struct OptionSwitch:public HighButton
  130. {
  131. void hover(bool on=true){};
  132. void select(bool on=true){};
  133. OptionSwitch( SDL_Rect Pos, CDefHandler* Imgs, bool Left, int Which)
  134. :HighButton(Pos,Imgs,false,7),left(Left),which(Which)
  135. {selectable=false;highlightable=false;}
  136. void press(bool down=true);
  137. bool left;
  138. int playerID;
  139. int serialID;
  140. int which; //-1=castle;0=hero;1=bonus
  141. };
  142. struct PlayerFlag:public HighButton
  143. {
  144. int color;
  145. PlayerFlag(SDL_Rect Pos, CDefHandler* Imgs, int Color)
  146. :HighButton(Pos,Imgs,false,7),color(Color)
  147. {selectable=false;highlightable=true;}
  148. void hover(bool on=true);
  149. void press(bool down=true);
  150. void select(bool on=true){};
  151. };
  152. struct PlayerOptions
  153. {
  154. PlayerOptions(int serial, int player);
  155. Ecolor color;
  156. PlayerFlag flag;
  157. //SDL_Surface * bg;
  158. OptionSwitch Cleft, Cright, Hleft, Hright, Bleft, Bright;
  159. int nr;
  160. };
  161. public:
  162. std::set<int> usedHeroes;
  163. Slider<> * turnLength;
  164. SDL_Surface * bg,
  165. * rHero, * rCastle, * nHero, * nCastle;
  166. std::vector<SDL_Surface*> bgs;
  167. std::vector<CDefHandler*> flags;
  168. CDefHandler //* castles, * heroes, * bonus,
  169. * left, * right,
  170. * bonuses;
  171. std::vector<PlayerOptions*> poptions;
  172. void show();
  173. void hide();
  174. void init();
  175. void showIcon (int what, int nr, bool abs); //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  176. bool canUseThisHero(int ID);
  177. int nextAllowedHero(int min, int max, int incl, int dir); //incl 0 - wlacznie; incl 1 - wylacznie; min-max - zakres szukania
  178. Options(){inited=showed=false;};
  179. ~Options();
  180. };
  181. class MapSel : public PreGameTab
  182. {
  183. public:
  184. ESortBy sortBy;
  185. SDL_Surface * bg;
  186. int selected; //selected map
  187. CDefHandler * Dtypes, * Dvic;
  188. CDefHandler *Dsizes, * Dloss,
  189. * sFlags;
  190. std::vector<Mapa*> scenList;
  191. std::vector<SDL_Surface*> scenImgs;
  192. //int current;
  193. std::vector<CMapInfo> ourMaps;
  194. IntBut<> small, medium, large, xlarge, all;
  195. SetrButton<> nrplayer, mapsize, type, name, viccon, loscon;
  196. Slider<> *slid, *descslid;
  197. int sizeFilter;
  198. int whichWL(int nr);
  199. int countWL();
  200. void show();
  201. void hide();
  202. void init();
  203. std::string gdiff(std::string ss);
  204. void printMaps(int from,int to=18, int at=0, bool abs=false);
  205. void select(int which, bool updateMapsList=true);
  206. void moveByOne(bool up);
  207. void printSelectedInfo();
  208. void printFlags();
  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