New Norwegian Translation
[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 <config.h>
21
22 #include "video/drawing_context.h"
23 #include "gui/mousecursor.h"
24
25 using namespace SuperTux;
26
27 MouseCursor* MouseCursor::current_ = 0;
28
29 MouseCursor::MouseCursor(std::string cursor_file, int frames) : mid_x(0), mid_y(0)
30 {
31   cursor = new Surface(cursor_file, true);
32   
33   cur_state = MC_NORMAL;
34   cur_frame = 0;
35   tot_frames = frames;
36
37   timer.init(false);
38   timer.start(MC_FRAME_PERIOD);
39   
40   SDL_ShowCursor(SDL_DISABLE);
41 }
42
43 MouseCursor::~MouseCursor()
44 {
45   delete cursor;
46
47   SDL_ShowCursor(SDL_ENABLE);
48 }
49
50 int MouseCursor::state()
51 {
52   return cur_state;
53 }
54
55 void MouseCursor::set_state(int nstate)
56 {
57   cur_state = nstate;
58 }
59
60 void MouseCursor::set_mid(int x, int y)
61 {
62   mid_x = x;
63   mid_y = y;
64 }
65
66 void MouseCursor::draw(DrawingContext& context)
67 {
68   if(cur_state == MC_HIDE)
69     return;
70
71   int x,y,w,h;
72   Uint8 ispressed = SDL_GetMouseState(&x,&y);
73   w = cursor->w / tot_frames;
74   h = cursor->h / MC_STATES_NB;
75   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2))
76     {
77       if(cur_state != MC_CLICK)
78         {
79           state_before_click = cur_state;
80           cur_state = MC_CLICK;
81         }
82     }
83   else
84     {
85       if(cur_state == MC_CLICK)
86         cur_state = state_before_click;
87     }
88
89   if(timer.get_left() < 0 && tot_frames > 1)
90     {
91       cur_frame++;
92       if(cur_frame++ >= tot_frames)
93         cur_frame = 0;
94
95       timer.start(MC_FRAME_PERIOD);
96     }
97
98   context.draw_surface_part(cursor, Vector(w*cur_frame, h*cur_state), Vector(w,
99         h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
100 }