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 <math.h>
19 #include "defs.h"
20 #include "sprite.h"
21 #include "scrollbar.h"
22 #include "pointer.h"
23 #include "ticks.h"
24 
25 static int sbCur;
26 
27 static int dirCntDwn;
28 
29 static spriteType* scrollBarGfx[4];
30 
scrollBarInit()31 void scrollBarInit()
32 {
33   sbCur=0;
34   dirCntDwn=0;
35   scrollBarGfx[0] = cutSprite(loadImg(DATADIR"data/menu/scrollbar.png"),0,0,19,186);
36   scrollBarGfx[1] = cutSprite(scrollBarGfx[0]->img,0,186,17,3);
37   scrollBarGfx[2] = cutSprite(scrollBarGfx[0]->img,0,186+3,11,8);
38   scrollBarGfx[3] = cutSprite(scrollBarGfx[0]->img,0,186+3+8,11,8);
39 }
40 
41 //Returns current position if a change happened, or -1 if nothing happened.
42 //size is the number of items in the list, current is the currently selected item.
scrollBar(SDL_Surface * screen,int x,int y,float size,int current,int chdir)43 int scrollBar(SDL_Surface* screen, int x, int y, float size, int current, int chdir)
44 {
45   SDL_Rect r;
46   int retVal=-1;
47 
48   if( chdir < 0 )
49   {
50     dirCntDwn = -120;
51   } else if( chdir >  0 )
52   {
53     dirCntDwn = 120;
54   }
55 
56   drawSprite(screen, scrollBarGfx[0], x, y);
57 
58   //Draw indicator
59   drawSprite(screen, scrollBarGfx[1], x+1,y+18+round(147.0/size*(float)current) );
60 
61   //Check for mouse hover over buttons
62   r.x = x;
63   r.w = r.x+18;
64   r.y = y;
65   r.h = r.y+18;
66   if( (isPointerInBox(&r) && current > 0) || dirCntDwn < 0)
67   {
68     drawSprite(screen, scrollBarGfx[2], r.x+4, r.y+6 );
69     if(isPointerClicked())
70     {
71       retVal=current-1;
72     }
73   }
74   //Check for mouse hover over buttons
75   r.y = y+167;
76   r.h = r.y+18;
77   if( (isPointerInBox(&r) && current < size) || dirCntDwn > 0 )
78   {
79     drawSprite(screen, scrollBarGfx[3], r.x+4, r.y+5 );
80     if(isPointerClicked())
81     {
82       retVal=current+1;
83     }
84   }
85 
86   r.y = y+16;
87   r.h = r.y+152;
88   if( isPointerInBox(&r) && getInpPointerState()->isDown )
89   {
90     retVal=(int)round( size/150.0 * (float)(getInpPointerState()->vpY-(r.y)));
91   }
92 
93   if(dirCntDwn < 0 )
94   {
95     dirCntDwn += getTicks();
96     if( dirCntDwn > 0 )
97     {
98       dirCntDwn=0;
99     }
100   } else if(dirCntDwn > 0)
101   {
102     dirCntDwn -= getTicks();
103     if(dirCntDwn < 0 )
104     {
105       dirCntDwn=0;
106     }
107   }
108 
109   return(retVal);
110 }
111