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