Skip to content
Permalink
Browse files
Merge pull request #8581 from jordan-woyak/ciface-ar-aware
InputCommon: Make "Cursor" inputs aware of the rendered aspect ratio.
  • Loading branch information
Tilka committed Jan 25, 2020
2 parents 9827aa7 + b92f648 commit b0e0404
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 21 deletions.
@@ -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'
// hotplug thread), or when devices are refreshed
// Returns a handle for later removing the callback.
@@ -10,6 +10,7 @@
#include <memory>
#include <mutex>

#include "Common/Matrix.h"
#include "Common/WindowSystemInfo.h"
#include "InputCommon/ControllerInterface/Device.h"

@@ -52,6 +53,14 @@ class ControllerInterface : public ciface::Core::DeviceContainer
bool IsInit() const { return m_is_init; }
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);
void UnregisterDevicesChangedCallback(const HotplugCallbackHandle& handle);
void InvokeDevicesChangedCallbacks() const;
@@ -62,6 +71,7 @@ class ControllerInterface : public ciface::Core::DeviceContainer
std::atomic<bool> m_is_init;
std::atomic<bool> m_is_populating_devices{false};
WindowSystemInfo m_wsi;
std::atomic<float> m_aspect_ratio_adjustment = 1;
};

extern ControllerInterface g_controller_interface;
@@ -125,9 +125,11 @@ void KeyboardMouse::UpdateCursorInput()
const auto win_width = rect.right - rect.left;
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.
m_state_in.cursor.x = ControlState(point.x) / win_width * 2 - 1;
m_state_in.cursor.y = ControlState(point.y) / win_height * 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) * window_scale.y;
}

void KeyboardMouse::UpdateInput()
@@ -6,6 +6,7 @@

#include <windows.h>

#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/DInput/DInput8.h"
#include "InputCommon/ControllerInterface/Device.h"

@@ -20,10 +21,7 @@ class KeyboardMouse : public Core::Device
{
BYTE keyboard[256];
DIMOUSESTATE2 mouse;
struct
{
ControlState x, y;
} cursor;
Common::TVec2<ControlState> cursor;
};

class Key : public Input
@@ -6,6 +6,7 @@

#include <QuartzCore/QuartzCore.h>

#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/Device.h"

namespace ciface::Quartz
@@ -64,10 +65,7 @@ class KeyboardAndMouse : public Core::Device
std::string GetSource() const override;

private:
struct
{
float x, y;
} m_cursor;
Common::Vec2 m_cursor;

uint32_t m_windowid;
};
@@ -9,6 +9,8 @@
#include <Carbon/Carbon.h>
#include <Cocoa/Cocoa.h>

#include "InputCommon/ControllerInterface/ControllerInterface.h"

namespace ciface::Quartz
{
std::string KeycodeToName(const CGKeyCode keycode)
@@ -177,10 +179,12 @@
CGPoint loc = CGEventGetLocation(event);
CFRelease(event);

const auto window_scale = g_controller_interface.GetWindowInputScale();

loc.x -= bounds.origin.x;
loc.y -= bounds.origin.y;
m_cursor.x = loc.x / bounds.size.width * 2 - 1.0;
m_cursor.y = loc.y / bounds.size.height * 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) * window_scale.y;
}

std::string KeyboardAndMouse::GetName() const
@@ -212,9 +212,11 @@ void KeyboardMouse::UpdateCursor()
XWindowAttributes 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
m_state.cursor.x = win_x / (float)win_attribs.width * 2 - 1;
m_state.cursor.y = win_y / (float)win_attribs.height * 2 - 1;
m_state.cursor.x = (win_x / win_attribs.width * 2 - 1) * window_scale.x;
m_state.cursor.y = (win_y / win_attribs.height * 2 - 1) * window_scale.y;
}

void KeyboardMouse::UpdateInput()
@@ -12,6 +12,7 @@ extern "C" {
#include <X11/keysym.h>
}

#include "Common/Matrix.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

namespace ciface::XInput2
@@ -25,10 +26,8 @@ class KeyboardMouse : public Core::Device
{
char keyboard[32];
unsigned int buttons;
struct
{
float x, y;
} cursor, axis;
Common::Vec2 cursor;
Common::Vec2 axis;
};

class Key : public Input
@@ -50,6 +50,8 @@
#include "Core/Host.h"
#include "Core/Movie.h"

#include "InputCommon/ControllerInterface/ControllerInterface.h"

#include "VideoCommon/AbstractFramebuffer.h"
#include "VideoCommon/AbstractStagingTexture.h"
#include "VideoCommon/AbstractTexture.h"
@@ -119,6 +121,9 @@ bool Renderer::Initialize()

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
// can require additional graphics sub-systems so it needs to be done first
ShutdownFrameDumping();
@@ -775,10 +780,15 @@ void Renderer::UpdateDrawRectangle()
g_Config.fAspectRatioHackH = 1;
}

float draw_width, draw_height, crop_width, crop_height;

// 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;

// crop the picture to a standard aspect ratio

0 comments on commit b0e0404

Please sign in to comment.