902f86772db738556669d50867e65ce48bc0f900
[supertux.git] / src / gui / mousecursor.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.hpp"
23 #include "gui/mousecursor.hpp"
24 #include "main.hpp"
25
26 MouseCursor* MouseCursor::current_ = 0;
27 extern SDL_Surface* screen;
28
29 MouseCursor::MouseCursor(std::string cursor_file) : mid_x(0), mid_y(0)
30 {
31   cursor = new Surface(cursor_file);
32   
33   cur_state = MC_NORMAL;
34
35   SDL_ShowCursor(SDL_DISABLE);
36 }
37
38 MouseCursor::~MouseCursor()
39 {
40   delete cursor;
41
42   SDL_ShowCursor(SDL_ENABLE);
43 }
44
45 int MouseCursor::state()
46 {
47   return cur_state;
48 }
49
50 void MouseCursor::set_state(int nstate)
51 {
52   cur_state = nstate;
53 }
54
55 void MouseCursor::set_mid(int x, int y)
56 {
57   mid_x = x;
58   mid_y = y;
59 }
60
61 void MouseCursor::draw(DrawingContext& context)
62 {
63   if(cur_state == MC_HIDE)
64     return;
65
66   int x,y,w,h;
67   Uint8 ispressed = SDL_GetMouseState(&x,&y);
68
69   x = int(x * float(SCREEN_WIDTH)/screen->w);
70   y = int(y * float(SCREEN_HEIGHT)/screen->h);
71
72   w = (int) cursor->get_width();
73   h = (int) (cursor->get_height() / MC_STATES_NB);
74   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) {
75     if(cur_state != MC_CLICK) {
76       state_before_click = cur_state;
77       cur_state = MC_CLICK;
78     }
79   } else {
80     if(cur_state == MC_CLICK)
81       cur_state = state_before_click;
82   }
83
84   context.draw_surface_part(cursor, Vector(0, h*cur_state),
85           Vector(w, h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
86 }