1 /************************************************************************
2  * This file is part of Wizznic.                                        *
3  * Copyright 2009-2015 Jimmy Christensen <dusted@dusted.dk>             *
4  * Wizznic is free software: you can redistribute it and/or modify      *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation, either version 3 of the License, or    *
7  * (at your option) any later version.                                  *
8  *                                                                      *
9  * Wizznic is distributed in the hope that it will be useful,           *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
12  * GNU General Public License for more details.                         *
13  *                                                                      *
14  * You should have received a copy of the GNU General Public License    *
15  * along with Wizznic.  If not, see <http://www.gnu.org/licenses/>.     *
16  ************************************************************************/
17 
18 #include "skipleveldialog.h"
19 #include "input.h"
20 #include "sprite.h"
21 #include "input.h"
22 #include "defs.h"
23 #include "pack.h"
24 #include "player.h"
25 #include "settings.h"
26 
27 static SDL_Surface* skipDialogSurface=NULL;
28 
29 static SDL_Surface* restartHighlightSurface=NULL;
30 static SDL_Surface* skipHighlightSurface=NULL;
31 static aniType* restartHighlightAni=NULL;
32 static aniType* skipHighlightAni=NULL;
33 
34 static int lastPackNum=1;
35 static int selected=0;
36 static int btnClicked=0;
37 
38 static SDL_Rect restartBoxBtn;
39 static SDL_Rect skipBoxBtn;
40 
loadSkipLevelDialog()41 void loadSkipLevelDialog()
42 {
43   //Do we need to load the dialog?
44   if( lastPackNum == -1 || lastPackNum != packState()->selected )
45   {
46     lastPackNum = packState()->selected;
47     //Free existing resources if they are loaded
48     if( skipDialogSurface ) SDL_FreeSurface(skipDialogSurface);
49     if( restartHighlightSurface ) SDL_FreeSurface(restartHighlightSurface);
50     if( skipHighlightSurface ) SDL_FreeSurface(skipHighlightSurface);
51     if( restartHighlightAni ) freeAni(restartHighlightAni);
52     if( skipHighlightAni ) freeAni(skipHighlightAni);
53 
54     //Load new images
55     skipDialogSurface = loadImg( packGetFile(".","skipleveldialog.png") );
56     restartHighlightSurface = loadImg( packGetFile(".", "skipleveldialog-hl0.png") );
57     skipHighlightSurface = loadImg( packGetFile(".", "skipleveldialog-hl1.png") );
58 
59     //Make animations
60     restartHighlightAni = mkAni(restartHighlightSurface, 48,44,150);
61     skipHighlightAni = mkAni(skipHighlightSurface, 48,44,150);
62 
63     //Define positions used for checking pointer against the buttons
64     restartBoxBtn.x = HSCREENW-45;
65     restartBoxBtn.y = HSCREENH-21;
66     restartBoxBtn.w = HSCREENW;
67     restartBoxBtn.h = HSCREENH+20;
68 
69     skipBoxBtn.x = HSCREENW;
70     skipBoxBtn.y = HSCREENH-21;
71     skipBoxBtn.w = HSCREENW+46;
72     skipBoxBtn.h = HSCREENH+20;
73 
74     btnClicked=0;
75 
76   }
77 }
78 
skipLevelDialog(SDL_Surface * screen)79 int skipLevelDialog(SDL_Surface* screen)
80 {
81   loadSkipLevelDialog();
82 
83   if( getButton(C_LEFT) || getButton(C_RIGHT) )
84   {
85     resetBtn(C_LEFT);
86     resetBtn(C_RIGHT);
87     if( selected==0 )
88     {
89       selected=1;
90     } else {
91       selected=0;
92     }
93   }
94 
95   if(getInpPointerState()->timeSinceMoved<POINTER_SHOW_TIMEOUT)
96   {
97     if( isPointerInBox(&skipBoxBtn))
98     {
99       selected=1;
100       if(isBoxClicked( &skipBoxBtn))
101       {
102         btnClicked=1;
103       }
104     } else if( isPointerInBox( &restartBoxBtn))
105     {
106       selected=0;
107       if(isBoxClicked( &restartBoxBtn))
108       {
109         btnClicked=1;
110       }
111     }
112   }
113 
114   if( getButton(C_BTNB) || btnClicked )
115   {
116     resetBtn(C_BTNB);
117     resetMouseBtn();
118     btnClicked = 0;
119     player()->timeouts=0;
120     return(selected);
121   }
122 
123   SDL_BlitSurface( skipDialogSurface, 0, screen, &(setting()->bgPos) );
124 
125   if(selected==0)
126   {
127     playAni(restartHighlightAni);
128     drawAni(screen, restartHighlightAni, HSCREENW-48,HSCREENH-22);
129   } else {
130     playAni(skipHighlightAni);
131     drawAni(screen, skipHighlightAni, HSCREENW,HSCREENH-22);
132   }
133 
134   //Caller only reacts to 0 or 1. We use 2 to indicate that no selection has been made yet.
135   return(2);
136 }
137