ExperimentalGear/scripts/graphics/display.lua

63 lines
2.2 KiB
Lua

local Display = {
design = {width = 1080, height = 1920},
screen = {width = nil, height = nil},
view = {width = nil, height = nil},
ratio = {landscapeUW = 21 / 9, landscapeWide = 16 / 9, landscapeStd = 4 / 3, portrait = 9 / 16},
}
function Display.transformToScreenSpace()
gfx.Translate((Display.screen.width - Display.view.width) / 2, 0);
gfx.Scale(Display.view.width / Display.design.width, Display.view.height / Display.design.height);
gfx.Scissor(0, 0, Display.design.width, Display.design.height);
end
function Display.updateResolution(ratio)
if not ratio then ratio = Display.ratio.portrait end
local screenWidth, screenHeight = game.GetResolution()
if screenWidth ~= Display.screen.width or screenHeight ~= Display.screen.height then
Display.screen.width, Display.screen.height = screenWidth, screenHeight
Display.view.width, Display.view.height = ratio * Display.screen.height, Display.screen.height
end
end
---Convert screenspace coordinates to viewspace coordinates
---@param screenX number
---@param screenY number
---@param offsetX? number Viewport offset from the left side (defaults to the portrait viewport offset)
---@param offsetY? number Viewport offset from the top side (defaults to 0)
---@return number, number
function Display.toViewSpace(screenX, screenY, offsetX, offsetY)
offsetX = offsetX or (Display.screen.width - Display.view.width) / 2
offsetY = offsetY or 0
local viewX, viewY, scaleX, scaleY
scaleX = Display.design.width / Display.view.width
scaleY = Display.design.height / Display.view.height
viewX = (screenX - offsetX) * scaleX
viewY = (screenY - offsetY) * scaleY
return viewX, viewY
end
---Set's up scaled transforms based on the current resolution.
---@param x number
---@param y number
---@param rotation number
---@return number, boolean # The scale applied to the transform and the current landscape state
function Display.setUpTransforms(x, y, rotation)
local scale = Display.screen.width / Display.view.width;
local isLandscape = Display.view.width > Display.view.height;
gfx.ResetTransform();
gfx.Translate(x, y);
gfx.Rotate(rotation);
gfx.Scale(scale, scale);
return scale, isLandscape;
end
return Display