local ScreenScaler = {} function ScreenScaler:hideBorders() local r, g, b, a = love.graphics.getColor() love.graphics.setColor(love.graphics.getBackgroundColor()) local w, h = love.graphics.getDimensions() if self.offset_x ~= 0 then love.graphics.rectangle("fill", 0, 0, self.offset_x, h) love.graphics.rectangle("fill", w-self.offset_x, 0, self.offset_x, h) end if self.offset_y ~= 0 then love.graphics.rectangle("fill", 0, 0, w, self.offset_y) love.graphics.rectangle("fill", 0, h-self.offset_y, w, self.offset_y) end love.graphics.setColor(r, g, b, a) end function ScreenScaler:getPosition(x, y) return (x - self.offset_x) / self.scale, (y - self.offset_y) / self.scale end function ScreenScaler:isInBounds(x, y) if self.scale then local w, h = love.graphics.getDimensions() return x >= self.offset_x and x < w-self.offset_x and y >= self.offset_y and y < h-self.offset_y else return true end end function ScreenScaler:getMousePosition() if self.scale then return self:getPosition(love.mouse.getPosition()) else return love.mouse.getPosition() end end function ScreenScaler:getDimensions() if self.current_width and self.current_height then return self.current_width, self.current_height else return love.graphics.getDimensions() end end function ScreenScaler:overrideScaling(width, height) local sw, sh = love.graphics.getDimensions() self.scale = math.min(sw / width, sh / height) self.offset_x = (sw - width * self.scale)/2 self.offset_y = (sh - height * self.scale)/2 self.current_width = width self.current_height = height end function ScreenScaler:start(p1, p2) local width, height if type(p1) == "number" and type(p2) == "number" then width, height = p1, p2 self.canvas = nil elseif p1:typeOf("Canvas") then self.canvas = p1 width, height = self.canvas:getDimensions() else return end local sw, sh = love.graphics.getDimensions() self.current_width = width self.current_height = height self.scale = math.min(sw / width, sh / height) self.offset_x = (sw - width * self.scale)/2 self.offset_y = (sh - height * self.scale)/2 self.active = true love.graphics.push() if self.canvas then love.graphics.setCanvas(self.canvas) love.graphics.clear() else love.graphics.translate(self.offset_x, self.offset_y) love.graphics.scale(self.scale) end end function ScreenScaler:finish() love.graphics.pop() if self.canvas then love.graphics.setCanvas() love.graphics.setColor(1, 1, 1) love.graphics.draw(self.canvas, self.offset_x, self.offset_y, 0, self.scale) else self:hideBorders() end self.canvas = nil self.active = false end return ScreenScaler