Merge pull request #8581 from jordan-woyak/ciface-ar-aware

InputCommon: Make "Cursor" inputs aware of the rendered aspect ratio.
This commit is contained in:
Tilka 2020-01-25 20:28:01 +00:00 committed by GitHub
commit b0e040431a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 59 additions and 21 deletions

View File

@ -257,6 +257,21 @@ void ControllerInterface::UpdateInput()
} }
} }
void ControllerInterface::SetAspectRatioAdjustment(float value)
{
m_aspect_ratio_adjustment = value;
}
Common::Vec2 ControllerInterface::GetWindowInputScale() const
{
const auto ar = m_aspect_ratio_adjustment.load();
if (ar > 1)
return {1.f, ar};
else
return {1 / ar, 1.f};
}
// Register a callback to be called when a device is added or removed (as from the input backends' // Register a callback to be called when a device is added or removed (as from the input backends'
// hotplug thread), or when devices are refreshed // hotplug thread), or when devices are refreshed
// Returns a handle for later removing the callback. // Returns a handle for later removing the callback.

View File

@ -10,6 +10,7 @@
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include "Common/Matrix.h"
#include "Common/WindowSystemInfo.h" #include "Common/WindowSystemInfo.h"
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"
@ -52,6 +53,14 @@ public:
bool IsInit() const { return m_is_init; } bool IsInit() const { return m_is_init; }
void UpdateInput(); void UpdateInput();
// Set adjustment from the full render window aspect-ratio to the drawn aspect-ratio.
// Used to fit mouse cursor inputs to the relevant region of the render window.
void SetAspectRatioAdjustment(float);
// Calculated from the aspect-ratio adjustment.
// Inputs based on window coordinates should be multiplied by this.
Common::Vec2 GetWindowInputScale() const;
HotplugCallbackHandle RegisterDevicesChangedCallback(std::function<void(void)> callback); HotplugCallbackHandle RegisterDevicesChangedCallback(std::function<void(void)> callback);
void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle); void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle);
void InvokeDevicesChangedCallbacks() const; void InvokeDevicesChangedCallbacks() const;
@ -62,6 +71,7 @@ private:
std::atomic<bool> m_is_init; std::atomic<bool> m_is_init;
std::atomic<bool> m_is_populating_devices{false}; std::atomic<bool> m_is_populating_devices{false};
WindowSystemInfo m_wsi; WindowSystemInfo m_wsi;
std::atomic<float> m_aspect_ratio_adjustment = 1;
}; };
extern ControllerInterface g_controller_interface; extern ControllerInterface g_controller_interface;

View File

@ -125,9 +125,11 @@ void KeyboardMouse::UpdateCursorInput()
const auto win_width = rect.right - rect.left; const auto win_width = rect.right - rect.left;
const auto win_height = rect.bottom - rect.top; const auto win_height = rect.bottom - rect.top;
const auto window_scale = g_controller_interface.GetWindowInputScale();
// Convert the cursor position to a range from -1 to 1. // Convert the cursor position to a range from -1 to 1.
m_state_in.cursor.x = ControlState(point.x) / win_width * 2 - 1; m_state_in.cursor.x = (ControlState(point.x) / win_width * 2 - 1) * window_scale.x;
m_state_in.cursor.y = ControlState(point.y) / win_height * 2 - 1; m_state_in.cursor.y = (ControlState(point.y) / win_height * 2 - 1) * window_scale.y;
} }
void KeyboardMouse::UpdateInput() void KeyboardMouse::UpdateInput()

View File

@ -6,6 +6,7 @@
#include <windows.h> #include <windows.h>
#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/DInput/DInput8.h" #include "InputCommon/ControllerInterface/DInput/DInput8.h"
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"
@ -20,10 +21,7 @@ private:
{ {
BYTE keyboard[256]; BYTE keyboard[256];
DIMOUSESTATE2 mouse; DIMOUSESTATE2 mouse;
struct Common::TVec2<ControlState> cursor;
{
ControlState x, y;
} cursor;
}; };
class Key : public Input class Key : public Input

View File

@ -6,6 +6,7 @@
#include <QuartzCore/QuartzCore.h> #include <QuartzCore/QuartzCore.h>
#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"
namespace ciface::Quartz namespace ciface::Quartz
@ -64,10 +65,7 @@ public:
std::string GetSource() const override; std::string GetSource() const override;
private: private:
struct Common::Vec2 m_cursor;
{
float x, y;
} m_cursor;
uint32_t m_windowid; uint32_t m_windowid;
}; };

View File

@ -9,6 +9,8 @@
#include <Carbon/Carbon.h> #include <Carbon/Carbon.h>
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#include "InputCommon/ControllerInterface/ControllerInterface.h"
namespace ciface::Quartz namespace ciface::Quartz
{ {
std::string KeycodeToName(const CGKeyCode keycode) std::string KeycodeToName(const CGKeyCode keycode)
@ -177,10 +179,12 @@ void KeyboardAndMouse::UpdateInput()
CGPoint loc = CGEventGetLocation(event); CGPoint loc = CGEventGetLocation(event);
CFRelease(event); CFRelease(event);
const auto window_scale = g_controller_interface.GetWindowInputScale();
loc.x -= bounds.origin.x; loc.x -= bounds.origin.x;
loc.y -= bounds.origin.y; loc.y -= bounds.origin.y;
m_cursor.x = loc.x / bounds.size.width * 2 - 1.0; m_cursor.x = (loc.x / bounds.size.width * 2 - 1.0) * window_scale.x;
m_cursor.y = loc.y / bounds.size.height * 2 - 1.0; m_cursor.y = (loc.y / bounds.size.height * 2 - 1.0) * window_scale.y;
} }
std::string KeyboardAndMouse::GetName() const std::string KeyboardAndMouse::GetName() const

View File

@ -212,9 +212,11 @@ void KeyboardMouse::UpdateCursor()
XWindowAttributes win_attribs; XWindowAttributes win_attribs;
XGetWindowAttributes(m_display, m_window, &win_attribs); XGetWindowAttributes(m_display, m_window, &win_attribs);
const auto window_scale = g_controller_interface.GetWindowInputScale();
// the mouse position as a range from -1 to 1 // the mouse position as a range from -1 to 1
m_state.cursor.x = win_x / (float)win_attribs.width * 2 - 1; m_state.cursor.x = (win_x / win_attribs.width * 2 - 1) * window_scale.x;
m_state.cursor.y = win_y / (float)win_attribs.height * 2 - 1; m_state.cursor.y = (win_y / win_attribs.height * 2 - 1) * window_scale.y;
} }
void KeyboardMouse::UpdateInput() void KeyboardMouse::UpdateInput()

View File

@ -12,6 +12,7 @@ extern "C" {
#include <X11/keysym.h> #include <X11/keysym.h>
} }
#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h" #include "InputCommon/ControllerInterface/ControllerInterface.h"
namespace ciface::XInput2 namespace ciface::XInput2
@ -25,10 +26,8 @@ private:
{ {
char keyboard[32]; char keyboard[32];
unsigned int buttons; unsigned int buttons;
struct Common::Vec2 cursor;
{ Common::Vec2 axis;
float x, y;
} cursor, axis;
}; };
class Key : public Input class Key : public Input

View File

@ -50,6 +50,8 @@
#include "Core/Host.h" #include "Core/Host.h"
#include "Core/Movie.h" #include "Core/Movie.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "VideoCommon/AbstractFramebuffer.h" #include "VideoCommon/AbstractFramebuffer.h"
#include "VideoCommon/AbstractStagingTexture.h" #include "VideoCommon/AbstractStagingTexture.h"
#include "VideoCommon/AbstractTexture.h" #include "VideoCommon/AbstractTexture.h"
@ -119,6 +121,9 @@ bool Renderer::Initialize()
void Renderer::Shutdown() void Renderer::Shutdown()
{ {
// Disable ControllerInterface's aspect ratio adjustments so mapping dialog behaves normally.
g_controller_interface.SetAspectRatioAdjustment(1);
// First stop any framedumping, which might need to dump the last xfb frame. This process // First stop any framedumping, which might need to dump the last xfb frame. This process
// can require additional graphics sub-systems so it needs to be done first // can require additional graphics sub-systems so it needs to be done first
ShutdownFrameDumping(); ShutdownFrameDumping();
@ -775,10 +780,15 @@ void Renderer::UpdateDrawRectangle()
g_Config.fAspectRatioHackH = 1; g_Config.fAspectRatioHackH = 1;
} }
float draw_width, draw_height, crop_width, crop_height;
// get the picture aspect ratio // get the picture aspect ratio
draw_width = crop_width = CalculateDrawAspectRatio(); const float draw_aspect_ratio = CalculateDrawAspectRatio();
// Make ControllerInterface aware of the render window region actually being used
// to adjust mouse cursor inputs.
g_controller_interface.SetAspectRatioAdjustment(draw_aspect_ratio / (win_width / win_height));
float draw_width, draw_height, crop_width, crop_height;
draw_width = crop_width = draw_aspect_ratio;
draw_height = crop_height = 1; draw_height = crop_height = 1;
// crop the picture to a standard aspect ratio // crop the picture to a standard aspect ratio