controller_hud.cpp 3.0 KB

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