From 5fb76d3bc486b2ca90c88d290111315177442cde Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Thu, 11 Mar 2021 07:48:32 -0800 Subject: [PATCH] [Mouse] Allow customizing screen to slab transforms Fix #20. Add function arg to customize screen to slab coordinates. Improve interoperability with screen resizing libraries by allowing users to specify a function to transform screen coordinates into game coordinates. For example, with Ulydev/push you could do: local function get_push_mouse(x,y) local new_x,new_y = push:toGame(x,y) if new_x and new_y then return new_x,new_y end return x,y end local game_width, game_height, window_width, window_height = 800, 600, 1920, 1080 push:setupScreen(game_width, game_height, window_width, window_height) Slab.Initialize(get_push_mouse, args) close #80 --- Internal/Input/Mouse.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Internal/Input/Mouse.lua b/Internal/Input/Mouse.lua index 367fa17..3183ac3 100644 --- a/Internal/Input/Mouse.lua +++ b/Internal/Input/Mouse.lua @@ -55,7 +55,12 @@ local Events = {} -- For more information, refer to the SetCustomCursor/ClearCustomCursor functions. local CustomCursors = {} +local function TransformPoint(X,Y) + return X,Y +end + local function OnMouseMoved(X, Y, DX, DY, IsTouch) + X, Y = TransformPoint(X, Y) State.X = X State.Y = Y State.AsyncDeltaX = State.AsyncDeltaX + DX @@ -78,6 +83,7 @@ local function PushEvent(Type, X, Y, Button, IsTouch, Presses) end local function OnMousePressed(X, Y, Button, IsTouch, Presses) + X, Y = TransformPoint(X, Y) PushEvent(Common.Event.Pressed, X, Y, Button, IsTouch, Presses) if MousePressedFn ~= nil then @@ -86,6 +92,7 @@ local function OnMousePressed(X, Y, Button, IsTouch, Presses) end local function OnMouseReleased(X, Y, Button, IsTouch, Presses) + X, Y = TransformPoint(X, Y) PushEvent(Common.Event.Released, X, Y, Button, IsTouch, Presses) if MouseReleasedFn ~= nil then @@ -110,7 +117,9 @@ local function ProcessEvents() Events = {} end -function Mouse.Initialize(Args) +function Mouse.Initialize(Args, TransformPointToSlab) + TransformPoint = TransformPointToSlab or TransformPoint + MouseMovedFn = love.handlers['mousemoved'] MousePressedFn = love.handlers['mousepressed'] MouseReleasedFn = love.handlers['mousereleased']