Added a function to draw text on center of screen for comodity.
[supertux.git] / lib / gui / mousecursor.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include "../video/drawing_context.h"
21 #include "../gui/mousecursor.h"
22
23 using namespace SuperTux;
24
25 MouseCursor* MouseCursor::current_ = 0;
26
27 MouseCursor::MouseCursor(std::string cursor_file, int frames) : mid_x(0), mid_y(0)
28 {
29   cursor = new Surface(cursor_file, true);
30   
31   cur_state = MC_NORMAL;
32   cur_frame = 0;
33   tot_frames = frames;
34
35   timer.init(false);
36   timer.start(MC_FRAME_PERIOD);
37   
38   SDL_ShowCursor(SDL_DISABLE);
39 }
40
41 MouseCursor::~MouseCursor()
42 {
43   delete cursor;
44
45   SDL_ShowCursor(SDL_ENABLE);
46 }
47
48 int MouseCursor::state()
49 {
50   return cur_state;
51 }
52
53 void MouseCursor::set_state(int nstate)
54 {
55   cur_state = nstate;
56 }
57
58 void MouseCursor::set_mid(int x, int y)
59 {
60   mid_x = x;
61   mid_y = y;
62 }
63
64 void MouseCursor::draw(DrawingContext& context)
65 {
66   if(cur_state == MC_HIDE)
67     return;
68
69   int x,y,w,h;
70   Uint8 ispressed = SDL_GetMouseState(&x,&y);
71   w = cursor->w / tot_frames;
72   h = cursor->h / MC_STATES_NB;
73   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2))
74     {
75       if(cur_state != MC_CLICK)
76         {
77           state_before_click = cur_state;
78           cur_state = MC_CLICK;
79         }
80     }
81   else
82     {
83       if(cur_state == MC_CLICK)
84         cur_state = state_before_click;
85     }
86
87   if(timer.get_left() < 0 && tot_frames > 1)
88     {
89       cur_frame++;
90       if(cur_frame++ >= tot_frames)
91         cur_frame = 0;
92
93       timer.start(MC_FRAME_PERIOD);
94     }
95
96   context.draw_surface_part(cursor, Vector(w*cur_frame, h*cur_state), Vector(w,
97         h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
98 }