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 "supertux/controller_hud.hpp" |
18 | |
19 | #include "control/input_manager.hpp" |
20 | #include "math/vector.hpp" |
21 | #include "video/drawing_context.hpp" |
22 | |
23 | ControllerHUD::ControllerHUD() : |
24 | m_controls() |
25 | { |
26 | const Sizef btn_size(16, 16); |
27 | const Vector pos(128, 64); |
28 | |
29 | const Vector dpad_pos = pos + Vector(-64.0f, 0.0f); |
30 | m_controls[Control::LEFT] = Rectf::from_center(dpad_pos + Vector(-16.0f, 0.0f), btn_size); |
31 | m_controls[Control::RIGHT] = Rectf::from_center(dpad_pos + Vector(16.0f, 0.0f), btn_size); |
32 | m_controls[Control::UP] = Rectf::from_center(dpad_pos + Vector(0.0f, -16.0f), btn_size); |
33 | m_controls[Control::DOWN] = Rectf::from_center(dpad_pos + Vector(0.0f, 16.0f), btn_size); |
34 | |
35 | const Vector peek_pos = pos + Vector(0.0f, -24.0f); |
36 | m_controls[Control::PEEK_LEFT] = Rectf::from_center(peek_pos + Vector(-16.0f, 0.0f), btn_size); |
37 | m_controls[Control::PEEK_RIGHT] = Rectf::from_center(peek_pos + Vector(16.0f, 0.0f), btn_size); |
38 | m_controls[Control::PEEK_UP] = Rectf::from_center(peek_pos + Vector(0.0f, -16.0f), btn_size); |
39 | m_controls[Control::PEEK_DOWN] = Rectf::from_center(peek_pos + Vector(0.0f, 16.0f), btn_size); |
40 | |
41 | const Vector btn_pos = pos + Vector(64.0f, -8.0f); |
42 | m_controls[Control::ACTION] = Rectf::from_center(btn_pos + Vector(-20.0f, 0.0f), btn_size); |
43 | m_controls[Control::JUMP] = Rectf::from_center(btn_pos + Vector(0.0f, 20.0f), btn_size); |
44 | |
45 | m_controls[Control::START] = Rectf::from_center(pos + Vector(16.0f, 24.0f), Sizef(16, 8)); |
46 | m_controls[Control::ESCAPE] = Rectf::from_center(pos + Vector(-16.0f, 24.0f), Sizef(16, 8)); |
47 | } |
48 | |
49 | void |
50 | ControllerHUD::draw(DrawingContext& context) |
51 | { |
52 | Canvas& canvas = context.color(); |
53 | Controller& controller = InputManager::current()->get_controller(); |
54 | |
55 | for(const auto& control: m_controls) |
56 | { |
57 | if (controller.pressed(control.first)) { |
58 | canvas.draw_filled_rect(control.second, Color::WHITE, LAYER_HUD); |
59 | } else if (controller.released(control.first)) { |
60 | canvas.draw_filled_rect(control.second, Color::BLACK, LAYER_HUD); |
61 | } else if (controller.hold(control.first)) { |
62 | canvas.draw_filled_rect(control.second, Color::RED, LAYER_HUD); |
63 | } else { |
64 | canvas.draw_filled_rect(control.second, Color(0.5f, 0.5f, 0.5f), LAYER_HUD); |
65 | } |
66 | } |
67 | } |
68 | |
69 | /* EOF */ |
70 | |