controller_hud.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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::START] = Rectf::from_center(pos + Vector(16.0f, 24.0f), Sizef(16, 8));
  39. m_controls[Control::ESCAPE] = Rectf::from_center(pos + Vector(-16.0f, 24.0f), Sizef(16, 8));
  40. }
  41. void
  42. ControllerHUD::draw(DrawingContext& context)
  43. {
  44. Canvas& canvas = context.color();
  45. Controller& controller = InputManager::current()->get_controller();
  46. for(const auto& control: m_controls)
  47. {
  48. if (controller.pressed(control.first)) {
  49. canvas.draw_filled_rect(control.second, Color::WHITE, LAYER_HUD);
  50. } else if (controller.released(control.first)) {
  51. canvas.draw_filled_rect(control.second, Color::BLACK, LAYER_HUD);
  52. } else if (controller.hold(control.first)) {
  53. canvas.draw_filled_rect(control.second, Color::RED, LAYER_HUD);
  54. } else {
  55. canvas.draw_filled_rect(control.second, Color(0.5f, 0.5f, 0.5f), LAYER_HUD);
  56. }
  57. }
  58. }
  59. /* EOF */