Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / gui / mousecursor.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program 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 //  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "gui/mousecursor.hpp"
18
19 #include <SDL.h>
20
21 #include "supertux/main.hpp"
22 #include "video/drawing_context.hpp"
23
24 MouseCursor* MouseCursor::current_ = 0;
25 extern SDL_Surface* g_screen;
26
27 MouseCursor::MouseCursor(std::string cursor_file) : mid_x(0), mid_y(0)
28 {
29   cursor = new Surface(cursor_file);
30
31   cur_state = MC_NORMAL;
32 }
33
34 MouseCursor::~MouseCursor()
35 {
36   delete cursor;
37 }
38
39 int MouseCursor::state()
40 {
41   return cur_state;
42 }
43
44 void MouseCursor::set_state(int nstate)
45 {
46   cur_state = nstate;
47 }
48
49 void MouseCursor::set_mid(int x, int y)
50 {
51   mid_x = x;
52   mid_y = y;
53 }
54
55 void MouseCursor::draw(DrawingContext& context)
56 {
57   if(cur_state == MC_HIDE)
58     return;
59
60   int x,y,w,h;
61   Uint8 ispressed = SDL_GetMouseState(&x,&y);
62
63   x = int(x * float(SCREEN_WIDTH)/g_screen->w);
64   y = int(y * float(SCREEN_HEIGHT)/g_screen->h);
65
66   w = (int) cursor->get_width();
67   h = (int) (cursor->get_height() / MC_STATES_NB);
68   if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) {
69     if(cur_state != MC_CLICK) {
70       state_before_click = cur_state;
71       cur_state = MC_CLICK;
72     }
73   } else {
74     if(cur_state == MC_CLICK)
75       cur_state = state_before_click;
76   }
77
78   context.draw_surface_part(cursor, Vector(0, h*cur_state),
79                             Vector(w, h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
80 }
81
82 /* EOF */