/
mousecursor.cpp
89 lines (72 loc) · 2.08 KB
/
mousecursor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "gui/mousecursor.hpp"
#include <SDL.h>
#include "supertux/globals.hpp"
#include "video/drawing_context.hpp"
#include "video/renderer.hpp"
#include "video/sdl/sdl_renderer.hpp"
MouseCursor* MouseCursor::current_ = 0;
MouseCursor::MouseCursor(std::string cursor_file) :
mid_x(0),
mid_y(0),
state_before_click(),
cur_state(),
cursor()
{
cursor = Surface::create(cursor_file);
cur_state = MC_NORMAL;
}
MouseCursor::~MouseCursor()
{
}
int MouseCursor::state()
{
return cur_state;
}
void MouseCursor::set_state(int nstate)
{
cur_state = nstate;
}
void MouseCursor::set_mid(int x, int y)
{
mid_x = x;
mid_y = y;
}
void MouseCursor::draw(DrawingContext& context)
{
if(cur_state == MC_HIDE)
return;
int x,y,w,h;
Uint8 ispressed = SDL_GetMouseState(&x,&y);
Vector mouse_pos = Renderer::instance()->to_logical(x, y);
x = int(mouse_pos.x);
y = int(mouse_pos.y);
w = (int) cursor->get_width();
h = (int) (cursor->get_height() / MC_STATES_NB);
if(ispressed &SDL_BUTTON(1) || ispressed &SDL_BUTTON(2)) {
if(cur_state != MC_CLICK) {
state_before_click = cur_state;
cur_state = MC_CLICK;
}
} else {
if(cur_state == MC_CLICK)
cur_state = state_before_click;
}
context.draw_surface_part(cursor, Vector(0, h*cur_state),
Vector(w, h), Vector(x-mid_x, y-mid_y), LAYER_GUI+100);
}
/* EOF */