emu_window.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cmath>
  6. #include "common/assert.h"
  7. #include "core/core.h"
  8. #include "core/frontend/emu_window.h"
  9. #include "core/frontend/key_map.h"
  10. #include "video_core/video_core.h"
  11. void EmuWindow::ButtonPressed(Service::HID::PadState pad) {
  12. pad_state.hex |= pad.hex;
  13. }
  14. void EmuWindow::ButtonReleased(Service::HID::PadState pad) {
  15. pad_state.hex &= ~pad.hex;
  16. }
  17. void EmuWindow::CirclePadUpdated(float x, float y) {
  18. constexpr int MAX_CIRCLEPAD_POS = 0x9C; // Max value for a circle pad position
  19. // Make sure the coordinates are in the unit circle,
  20. // otherwise normalize it.
  21. float r = x * x + y * y;
  22. if (r > 1) {
  23. r = std::sqrt(r);
  24. x /= r;
  25. y /= r;
  26. }
  27. circle_pad_x = static_cast<s16>(x * MAX_CIRCLEPAD_POS);
  28. circle_pad_y = static_cast<s16>(y * MAX_CIRCLEPAD_POS);
  29. }
  30. /**
  31. * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout
  32. * @param layout FramebufferLayout object describing the framebuffer size and screen positions
  33. * @param framebuffer_x Framebuffer x-coordinate to check
  34. * @param framebuffer_y Framebuffer y-coordinate to check
  35. * @return True if the coordinates are within the touchpad, otherwise false
  36. */
  37. static bool IsWithinTouchscreen(const Layout::FramebufferLayout& layout, unsigned framebuffer_x,
  38. unsigned framebuffer_y) {
  39. return (
  40. framebuffer_y >= layout.bottom_screen.top && framebuffer_y < layout.bottom_screen.bottom &&
  41. framebuffer_x >= layout.bottom_screen.left && framebuffer_x < layout.bottom_screen.right);
  42. }
  43. std::tuple<unsigned, unsigned> EmuWindow::ClipToTouchScreen(unsigned new_x, unsigned new_y) {
  44. new_x = std::max(new_x, framebuffer_layout.bottom_screen.left);
  45. new_x = std::min(new_x, framebuffer_layout.bottom_screen.right - 1);
  46. new_y = std::max(new_y, framebuffer_layout.bottom_screen.top);
  47. new_y = std::min(new_y, framebuffer_layout.bottom_screen.bottom - 1);
  48. return std::make_tuple(new_x, new_y);
  49. }
  50. void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y) {
  51. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y))
  52. return;
  53. touch_x = VideoCore::kScreenBottomWidth *
  54. (framebuffer_x - framebuffer_layout.bottom_screen.left) /
  55. (framebuffer_layout.bottom_screen.right - framebuffer_layout.bottom_screen.left);
  56. touch_y = VideoCore::kScreenBottomHeight *
  57. (framebuffer_y - framebuffer_layout.bottom_screen.top) /
  58. (framebuffer_layout.bottom_screen.bottom - framebuffer_layout.bottom_screen.top);
  59. touch_pressed = true;
  60. }
  61. void EmuWindow::TouchReleased() {
  62. touch_pressed = false;
  63. touch_x = 0;
  64. touch_y = 0;
  65. }
  66. void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y) {
  67. if (!touch_pressed)
  68. return;
  69. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y))
  70. std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y);
  71. TouchPressed(framebuffer_x, framebuffer_y);
  72. }
  73. void EmuWindow::AccelerometerChanged(float x, float y, float z) {
  74. constexpr float coef = 512;
  75. std::lock_guard<std::mutex> lock(accel_mutex);
  76. // TODO(wwylele): do a time stretch as it in GyroscopeChanged
  77. // The time stretch formula should be like
  78. // stretched_vector = (raw_vector - gravity) * stretch_ratio + gravity
  79. accel_x = static_cast<s16>(x * coef);
  80. accel_y = static_cast<s16>(y * coef);
  81. accel_z = static_cast<s16>(z * coef);
  82. }
  83. void EmuWindow::GyroscopeChanged(float x, float y, float z) {
  84. constexpr float FULL_FPS = 60;
  85. float coef = GetGyroscopeRawToDpsCoefficient();
  86. float stretch = Core::System::GetInstance().perf_stats.GetLastFrameTimeScale();
  87. std::lock_guard<std::mutex> lock(gyro_mutex);
  88. gyro_x = static_cast<s16>(x * coef * stretch);
  89. gyro_y = static_cast<s16>(y * coef * stretch);
  90. gyro_z = static_cast<s16>(z * coef * stretch);
  91. }
  92. void EmuWindow::UpdateCurrentFramebufferLayout(unsigned width, unsigned height) {
  93. Layout::FramebufferLayout layout;
  94. switch (Settings::values.layout_option) {
  95. case Settings::LayoutOption::SingleScreen:
  96. layout = Layout::SingleFrameLayout(width, height, Settings::values.swap_screen);
  97. break;
  98. case Settings::LayoutOption::LargeScreen:
  99. layout = Layout::LargeFrameLayout(width, height, Settings::values.swap_screen);
  100. break;
  101. case Settings::LayoutOption::Default:
  102. default:
  103. layout = Layout::DefaultFrameLayout(width, height, Settings::values.swap_screen);
  104. break;
  105. }
  106. NotifyFramebufferLayoutChanged(layout);
  107. }