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 "sprite/sprite.hpp" |
22 | #include "video/drawing_context.hpp" |
23 | #include "video/renderer.hpp" |
24 | #include "video/surface.hpp" |
25 | #include "video/video_system.hpp" |
26 | #include "video/viewport.hpp" |
27 | |
28 | MouseCursor* MouseCursor::current_ = nullptr; |
29 | |
30 | MouseCursor::MouseCursor(SpritePtr sprite) : |
31 | m_state(MouseCursorState::NORMAL), |
32 | m_applied_state(MouseCursorState::HIDE), |
33 | m_sprite(std::move(sprite)), |
34 | m_icon() |
35 | { |
36 | } |
37 | |
38 | void |
39 | MouseCursor::set_state(MouseCursorState state) |
40 | { |
41 | m_state = state; |
42 | } |
43 | |
44 | void |
45 | MouseCursor::set_icon(SurfacePtr icon) |
46 | { |
47 | m_icon = std::move(icon); |
48 | } |
49 | |
50 | void |
51 | MouseCursor::apply_state(MouseCursorState state) |
52 | { |
53 | if (m_applied_state != state) |
54 | { |
55 | m_applied_state = state; |
56 | |
57 | switch(state) |
58 | { |
59 | case MouseCursorState::NORMAL: |
60 | m_sprite->set_action("normal" ); |
61 | break; |
62 | |
63 | case MouseCursorState::CLICK: |
64 | m_sprite->set_action("click" ); |
65 | break; |
66 | |
67 | case MouseCursorState::LINK: |
68 | m_sprite->set_action("link" ); |
69 | break; |
70 | |
71 | case MouseCursorState::HIDE: |
72 | break; |
73 | } |
74 | } |
75 | } |
76 | |
77 | void |
78 | MouseCursor::draw(DrawingContext& context) |
79 | { |
80 | if (m_state != MouseCursorState::HIDE) |
81 | { |
82 | int x, y; |
83 | Uint32 ispressed = SDL_GetMouseState(&x, &y); |
84 | |
85 | if (ispressed & SDL_BUTTON(1) || ispressed & SDL_BUTTON(2)) |
86 | { |
87 | apply_state(MouseCursorState::CLICK); |
88 | } |
89 | else |
90 | { |
91 | apply_state(m_state); |
92 | } |
93 | |
94 | Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(x, y); |
95 | |
96 | m_sprite->draw(context.color(), mouse_pos, LAYER_GUI + 100); |
97 | |
98 | if (m_icon) { |
99 | context.color().draw_surface(m_icon, |
100 | Vector(mouse_pos.x, |
101 | mouse_pos.y - static_cast<float>(m_icon->get_height())), |
102 | LAYER_GUI + 100); |
103 | } |
104 | } |
105 | } |
106 | |
107 | /* EOF */ |
108 | |