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