1 | // SuperTux |
2 | // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com> |
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 "editor/button_widget.hpp" |
18 | |
19 | #include "video/viewport.hpp" |
20 | #include "video/video_system.hpp" |
21 | |
22 | ButtonWidget::ButtonWidget(SpritePtr sprite, const Vector& pos, |
23 | std::function<void()> sig_click) : |
24 | m_sprite(std::move(sprite)), |
25 | m_rect(pos, Sizef(static_cast<float>(m_sprite->get_width()), |
26 | static_cast<float>(m_sprite->get_width()))), |
27 | m_grab(false), |
28 | m_hover(false), |
29 | m_sig_click(std::move(sig_click)) |
30 | { |
31 | } |
32 | |
33 | void |
34 | ButtonWidget::draw(DrawingContext& context) |
35 | { |
36 | context.color().draw_filled_rect(m_rect, Color(0.0f, 0.0f, 0.0f, 0.6f), 4.0f, |
37 | LAYER_GUI-5); |
38 | |
39 | if (m_sprite) { |
40 | m_sprite->draw(context.color(), m_rect.p1(), LAYER_GUI-5); |
41 | } |
42 | |
43 | if (m_grab) { |
44 | context.color().draw_filled_rect(m_rect, Color(0.9f, 0.9f, 1.0f, 0.9f), 4.0f, |
45 | LAYER_GUI-5); |
46 | } else if (m_hover) { |
47 | context.color().draw_filled_rect(m_rect, Color(0.9f, 0.9f, 1.0f, 0.6f), 4.0f, |
48 | LAYER_GUI-5); |
49 | } |
50 | } |
51 | |
52 | void |
53 | ButtonWidget::update(float dt_sec) |
54 | { |
55 | } |
56 | |
57 | void |
58 | ButtonWidget::setup() |
59 | { |
60 | } |
61 | |
62 | void |
63 | ButtonWidget::resize() |
64 | { |
65 | } |
66 | |
67 | bool |
68 | ButtonWidget::on_mouse_button_up(const SDL_MouseButtonEvent& button) |
69 | { |
70 | if (button.button != SDL_BUTTON_LEFT) return false; |
71 | |
72 | Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y); |
73 | |
74 | if (m_grab) { |
75 | if (m_rect.contains(mouse_pos)) { |
76 | if (m_sig_click) { |
77 | m_sig_click(); |
78 | } |
79 | } |
80 | m_grab = false; |
81 | return true; |
82 | } else { |
83 | m_hover = false; |
84 | return false; |
85 | } |
86 | } |
87 | |
88 | bool |
89 | ButtonWidget::on_mouse_button_down(const SDL_MouseButtonEvent& button) |
90 | { |
91 | if (button.button != SDL_BUTTON_LEFT) return false; |
92 | |
93 | Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y); |
94 | |
95 | if (m_rect.contains(mouse_pos)) { |
96 | m_hover = true; |
97 | m_grab = true; |
98 | return true; |
99 | } else { |
100 | m_hover = false; |
101 | return false; |
102 | } |
103 | } |
104 | |
105 | bool |
106 | ButtonWidget::on_mouse_motion(const SDL_MouseMotionEvent& motion) |
107 | { |
108 | Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y); |
109 | |
110 | if (m_grab) { |
111 | m_hover = m_rect.contains(mouse_pos); |
112 | return true; |
113 | } else if (m_rect.contains(mouse_pos)) { |
114 | m_hover = true; |
115 | return false; |
116 | } else { |
117 | m_hover = false; |
118 | return false; |
119 | } |
120 | } |
121 | |
122 | /* EOF */ |
123 | |