emu_window.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "emu_window.h"
  5. #include "video_core/video_core.h"
  6. void EmuWindow::KeyPressed(KeyMap::HostDeviceKey key) {
  7. pad_state.hex |= KeyMap::GetPadKey(key).hex;
  8. }
  9. void EmuWindow::KeyReleased(KeyMap::HostDeviceKey key) {
  10. pad_state.hex &= ~KeyMap::GetPadKey(key).hex;
  11. }
  12. /**
  13. * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout
  14. * @param layout FramebufferLayout object describing the framebuffer size and screen positions
  15. * @param framebuffer_x Framebuffer x-coordinate to check
  16. * @param framebuffer_y Framebuffer y-coordinate to check
  17. * @return True if the coordinates are within the touchpad, otherwise false
  18. */
  19. static bool IsWithinTouchscreen(const EmuWindow::FramebufferLayout& layout, unsigned framebuffer_x,
  20. unsigned framebuffer_y) {
  21. return (framebuffer_y >= layout.bottom_screen.top &&
  22. framebuffer_y < layout.bottom_screen.bottom &&
  23. framebuffer_x >= layout.bottom_screen.left &&
  24. framebuffer_x < layout.bottom_screen.right);
  25. }
  26. std::tuple<unsigned,unsigned> EmuWindow::ClipToTouchScreen(unsigned new_x, unsigned new_y) {
  27. new_x = std::max(new_x, framebuffer_layout.bottom_screen.left);
  28. new_x = std::min(new_x, framebuffer_layout.bottom_screen.right-1);
  29. new_y = std::max(new_y, framebuffer_layout.bottom_screen.top);
  30. new_y = std::min(new_y, framebuffer_layout.bottom_screen.bottom-1);
  31. return std::make_tuple(new_x, new_y);
  32. }
  33. void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y) {
  34. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y))
  35. return;
  36. touch_x = VideoCore::kScreenBottomWidth * (framebuffer_x - framebuffer_layout.bottom_screen.left) /
  37. (framebuffer_layout.bottom_screen.right - framebuffer_layout.bottom_screen.left);
  38. touch_y = VideoCore::kScreenBottomHeight * (framebuffer_y - framebuffer_layout.bottom_screen.top) /
  39. (framebuffer_layout.bottom_screen.bottom - framebuffer_layout.bottom_screen.top);
  40. touch_pressed = true;
  41. pad_state.touch = 1;
  42. }
  43. void EmuWindow::TouchReleased() {
  44. touch_pressed = false;
  45. touch_x = 0;
  46. touch_y = 0;
  47. pad_state.touch = 0;
  48. }
  49. void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y) {
  50. if (!touch_pressed)
  51. return;
  52. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y))
  53. std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y);
  54. TouchPressed(framebuffer_x, framebuffer_y);
  55. }
  56. EmuWindow::FramebufferLayout EmuWindow::FramebufferLayout::DefaultScreenLayout(unsigned width, unsigned height) {
  57. ASSERT(width > 0);
  58. ASSERT(height > 0);
  59. EmuWindow::FramebufferLayout res = { width, height, {}, {} };
  60. float window_aspect_ratio = static_cast<float>(height) / width;
  61. float emulation_aspect_ratio = static_cast<float>(VideoCore::kScreenTopHeight * 2) /
  62. VideoCore::kScreenTopWidth;
  63. if (window_aspect_ratio > emulation_aspect_ratio) {
  64. // Window is narrower than the emulation content => apply borders to the top and bottom
  65. int viewport_height = static_cast<int>(std::round(emulation_aspect_ratio * width));
  66. res.top_screen.left = 0;
  67. res.top_screen.right = res.top_screen.left + width;
  68. res.top_screen.top = (height - viewport_height) / 2;
  69. res.top_screen.bottom = res.top_screen.top + viewport_height / 2;
  70. int bottom_width = static_cast<int>((static_cast<float>(VideoCore::kScreenBottomWidth) /
  71. VideoCore::kScreenTopWidth) * (res.top_screen.right - res.top_screen.left));
  72. int bottom_border = ((res.top_screen.right - res.top_screen.left) - bottom_width) / 2;
  73. res.bottom_screen.left = bottom_border;
  74. res.bottom_screen.right = res.bottom_screen.left + bottom_width;
  75. res.bottom_screen.top = res.top_screen.bottom;
  76. res.bottom_screen.bottom = res.bottom_screen.top + viewport_height / 2;
  77. } else {
  78. // Otherwise, apply borders to the left and right sides of the window.
  79. int viewport_width = static_cast<int>(std::round(height / emulation_aspect_ratio));
  80. res.top_screen.left = (width - viewport_width) / 2;
  81. res.top_screen.right = res.top_screen.left + viewport_width;
  82. res.top_screen.top = 0;
  83. res.top_screen.bottom = res.top_screen.top + height / 2;
  84. int bottom_width = static_cast<int>((static_cast<float>(VideoCore::kScreenBottomWidth) /
  85. VideoCore::kScreenTopWidth) * (res.top_screen.right - res.top_screen.left));
  86. int bottom_border = ((res.top_screen.right - res.top_screen.left) - bottom_width) / 2;
  87. res.bottom_screen.left = res.top_screen.left + bottom_border;
  88. res.bottom_screen.right = res.bottom_screen.left + bottom_width;
  89. res.bottom_screen.top = res.top_screen.bottom;
  90. res.bottom_screen.bottom = res.bottom_screen.top + height / 2;
  91. }
  92. return res;
  93. }