emu_window.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include <mutex>
  6. #include "core/frontend/emu_window.h"
  7. #include "core/frontend/input.h"
  8. #include "core/settings.h"
  9. class EmuWindow::TouchState : public Input::Factory<Input::TouchDevice>,
  10. public std::enable_shared_from_this<TouchState> {
  11. public:
  12. std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage&) override {
  13. return std::make_unique<Device>(shared_from_this());
  14. }
  15. std::mutex mutex;
  16. bool touch_pressed = false; ///< True if touchpad area is currently pressed, otherwise false
  17. float touch_x = 0.0f; ///< Touchpad X-position
  18. float touch_y = 0.0f; ///< Touchpad Y-position
  19. private:
  20. class Device : public Input::TouchDevice {
  21. public:
  22. explicit Device(std::weak_ptr<TouchState>&& touch_state) : touch_state(touch_state) {}
  23. std::tuple<float, float, bool> GetStatus() const override {
  24. if (auto state = touch_state.lock()) {
  25. std::lock_guard<std::mutex> guard(state->mutex);
  26. return std::make_tuple(state->touch_x, state->touch_y, state->touch_pressed);
  27. }
  28. return std::make_tuple(0.0f, 0.0f, false);
  29. }
  30. private:
  31. std::weak_ptr<TouchState> touch_state;
  32. };
  33. };
  34. EmuWindow::EmuWindow() {
  35. // TODO: Find a better place to set this.
  36. config.min_client_area_size = std::make_pair(400u, 480u);
  37. active_config = config;
  38. touch_state = std::make_shared<TouchState>();
  39. Input::RegisterFactory<Input::TouchDevice>("emu_window", touch_state);
  40. }
  41. EmuWindow::~EmuWindow() {
  42. Input::UnregisterFactory<Input::TouchDevice>("emu_window");
  43. }
  44. /**
  45. * Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout
  46. * @param layout FramebufferLayout object describing the framebuffer size and screen positions
  47. * @param framebuffer_x Framebuffer x-coordinate to check
  48. * @param framebuffer_y Framebuffer y-coordinate to check
  49. * @return True if the coordinates are within the touchpad, otherwise false
  50. */
  51. static bool IsWithinTouchscreen(const Layout::FramebufferLayout& layout, unsigned framebuffer_x,
  52. unsigned framebuffer_y) {
  53. return (
  54. framebuffer_y >= layout.bottom_screen.top && framebuffer_y < layout.bottom_screen.bottom &&
  55. framebuffer_x >= layout.bottom_screen.left && framebuffer_x < layout.bottom_screen.right);
  56. }
  57. std::tuple<unsigned, unsigned> EmuWindow::ClipToTouchScreen(unsigned new_x, unsigned new_y) {
  58. new_x = std::max(new_x, framebuffer_layout.bottom_screen.left);
  59. new_x = std::min(new_x, framebuffer_layout.bottom_screen.right - 1);
  60. new_y = std::max(new_y, framebuffer_layout.bottom_screen.top);
  61. new_y = std::min(new_y, framebuffer_layout.bottom_screen.bottom - 1);
  62. return std::make_tuple(new_x, new_y);
  63. }
  64. void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y) {
  65. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y))
  66. return;
  67. std::lock_guard<std::mutex> guard(touch_state->mutex);
  68. touch_state->touch_x =
  69. static_cast<float>(framebuffer_x - framebuffer_layout.bottom_screen.left) /
  70. (framebuffer_layout.bottom_screen.right - framebuffer_layout.bottom_screen.left);
  71. touch_state->touch_y =
  72. static_cast<float>(framebuffer_y - framebuffer_layout.bottom_screen.top) /
  73. (framebuffer_layout.bottom_screen.bottom - framebuffer_layout.bottom_screen.top);
  74. touch_state->touch_pressed = true;
  75. }
  76. void EmuWindow::TouchReleased() {
  77. std::lock_guard<std::mutex> guard(touch_state->mutex);
  78. touch_state->touch_pressed = false;
  79. touch_state->touch_x = 0;
  80. touch_state->touch_y = 0;
  81. }
  82. void EmuWindow::TouchMoved(unsigned framebuffer_x, unsigned framebuffer_y) {
  83. if (!touch_state->touch_pressed)
  84. return;
  85. if (!IsWithinTouchscreen(framebuffer_layout, framebuffer_x, framebuffer_y))
  86. std::tie(framebuffer_x, framebuffer_y) = ClipToTouchScreen(framebuffer_x, framebuffer_y);
  87. TouchPressed(framebuffer_x, framebuffer_y);
  88. }
  89. void EmuWindow::UpdateCurrentFramebufferLayout(unsigned width, unsigned height) {
  90. Layout::FramebufferLayout layout;
  91. if (Settings::values.custom_layout == true) {
  92. layout = Layout::CustomFrameLayout(width, height);
  93. } else {
  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::SideScreen:
  102. layout = Layout::SideFrameLayout(width, height, Settings::values.swap_screen);
  103. break;
  104. case Settings::LayoutOption::Default:
  105. default:
  106. layout = Layout::DefaultFrameLayout(width, height, Settings::values.swap_screen);
  107. break;
  108. }
  109. }
  110. NotifyFramebufferLayoutChanged(layout);
  111. }