3e86bfc8bea61b4885b4e884d7b87d98987827ad
[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/globals.hpp"
22 #include "video/drawing_context.hpp"
23 #include "video/renderer.hpp"
24 #include "video/sdl/sdl_renderer.hpp"
25
26 MouseCursor* MouseCursor::current_ = 0;
27
28 MouseCursor::MouseCursor(std::string cursor_file) : 
29   mouse_pos(),
30   mid_x(0), 
31   mid_y(0),
32   state_before_click(),
33   cur_state(),
34   cursor()
35 {
36   cursor = Surface::create(cursor_file);
37
38   cur_state = MC_NORMAL;
39 }
40
41 MouseCursor::~MouseCursor()
42 {
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
56 MouseCursor::set_pos(const Vector& pos)
57 {
58   mouse_pos = pos;
59 }
60
61 void MouseCursor::set_mid(int x, int y)
62 {
63   mid_x = x;
64   mid_y = y;
65 }
66
67 void MouseCursor::draw(DrawingContext& context)
68 {
69   if(cur_state == MC_HIDE)
70     return;
71
72   // Not using coordinates from mouse, as they are in the wrong
73   // coordinate system, see:
74   // https://bugzilla.libsdl.org/show_bug.cgi?id=2442
75   Uint8 ispressed = SDL_GetMouseState(NULL, NULL);
76
77   int x = int(mouse_pos.x);
78   int y = int(mouse_pos.y);
79
80   int w = (int) cursor->get_width();
81   int h = (int) (cursor->get_height() / MC_STATES_NB);
82   if(ispressed & SDL_BUTTON(1) || ispressed & SDL_BUTTON(2)) {
83     if(cur_state != MC_CLICK) {
84       state_before_click = cur_state;
85       cur_state = MC_CLICK;
86     }
87   } else {
88     if(cur_state == MC_CLICK)
89       cur_state = state_before_click;
90   }
91
92   context.draw_surface_part(cursor, Vector(0, h*cur_state),
93                             Vector(w, h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
94 }
95
96 /* EOF */