f03dd1c20177a69490f84fd915d7e1bf712cf103
[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(const std::string& cursor_file,
29                          const std::string& cursor_click_file,
30                          const std::string& cursor_link_file) :
31   m_mid_x(0), 
32   m_mid_y(0),
33   m_state(MC_NORMAL),
34   m_cursor()
35 {
36   m_cursor.push_back(Surface::create(cursor_file));
37   m_cursor.push_back(Surface::create(cursor_click_file));
38   m_cursor.push_back(Surface::create(cursor_link_file));
39 }
40
41 MouseCursor::~MouseCursor()
42 {
43 }
44
45 void MouseCursor::set_state(MouseCursorState nstate)
46 {
47   m_state = nstate;
48 }
49
50 void MouseCursor::set_mid(int x, int y)
51 {
52   m_mid_x = x;
53   m_mid_y = y;
54 }
55
56 void MouseCursor::draw(DrawingContext& context)
57 {
58   if (m_state != MC_HIDE)
59   {
60     int x;
61     int y;
62     Uint8 ispressed = SDL_GetMouseState(&x, &y);
63
64     Vector mouse_pos = Renderer::instance()->to_logical(x, y);
65
66     x = int(mouse_pos.x);
67     y = int(mouse_pos.y);
68
69     int tmp_state = m_state;
70     if (ispressed & SDL_BUTTON(1) || ispressed & SDL_BUTTON(2))
71     {
72       tmp_state = MC_CLICK;
73     }
74
75     context.draw_surface(m_cursor[static_cast<int>(tmp_state)],
76                          Vector(x - m_mid_x, y - m_mid_y),
77                          LAYER_GUI + 100);
78   }
79 }
80
81 /* EOF */