Last active
April 12, 2020 19:31
-
-
Save EngineerSmith/7d65992c7acc4748ae1c614724658a83 to your computer and use it in GitHub Desktop.
Script used to reposition GUI elements when window resizes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local floor = math.floor | |
| local point = { | |
| Center = 0, | |
| North = 1, | |
| NorthWest = 2, | |
| West = 3, | |
| SouthWest = 4, | |
| South = 5, | |
| SouthEast = 6, | |
| East = 7, | |
| NorthEast = 8 | |
| } | |
| local anchor = {} | |
| anchor.__index = Anchor | |
| -- MVP anchor.new("Center", 0,0) | |
| function anchor.new(anchorPoint, x, y, width, height, padding) | |
| local self = {} | |
| setmetatable(self, anchor) | |
| if width then | |
| if type(width) == "number" then | |
| width = {min=width,max=width} | |
| else | |
| width.min = width.min or 0 | |
| width.max = width.max or 0 | |
| end | |
| else | |
| width = {min=0,max=0} | |
| end | |
| if height then | |
| if type(height) == "number" then | |
| height = {min=height,max=height} | |
| else | |
| height.min = height.min or 0 | |
| height.max = height.max or 0 | |
| end | |
| else | |
| height = {min=0,max=0} | |
| end | |
| padding = padding or {} | |
| padding.top = padding.top or 0 | |
| padding.bottom = padding.bottom or 0 | |
| padding.left = padding.left or 0 | |
| padding.right = padding.right or 0 | |
| self.anchorPoint = point[anchorPoint or error("Anchor requires AnchorPoint")] or error("Invalid AnchorPoint") | |
| self.anchorPointStr = anchorPoint | |
| self.x = x or error("Anchor requires X") | |
| self.y = y or error("Anchor requires Y") | |
| self.width = width | |
| self.height = height | |
| self.padding = padding | |
| return self | |
| end | |
| local function CalculatePosition(anchorPoint, x, y, w, h, padding, windowWidth, windowHeight) | |
| local centerX, centerY = floor(windowWidth / 2) - floor(w / 2), floor(windowHeight / 2) - floor(h / 2) | |
| x = x + (padding.right - padding.left) | |
| y = y + (padding.bottom - padding.top) | |
| if anchorPoint == point.Center then | |
| return centerX + x, centerY + y | |
| elseif anchorPoint == point.North then | |
| return centerX + x, y | |
| elseif anchorPoint == point.NorthWest then | |
| return x, y | |
| elseif anchorPoint == point.West then | |
| return x, centerY + y | |
| elseif anchorPoint == point.SouthWest then | |
| return x, windowHeight - y | |
| elseif anchorPoint == point.South then | |
| return centerX + x, windowHeight - y | |
| elseif anchorPoint == point.SouthEast then | |
| return windowWidth - x, windowHeight - y | |
| elseif anchorPoint == point.East then | |
| return windowWidth - x, centerY + y | |
| elseif anchorPoint == point.NorthEast then | |
| return windowWidth - x, y | |
| end | |
| error("Unknown anchor point") | |
| end | |
| local function CalculateLength(min, max, padding, windowLength) | |
| if max + padding <= windowLength then | |
| return max | |
| elseif min + padding <= windowLength then | |
| return windowLength - padding | |
| else | |
| return min | |
| end | |
| end | |
| function anchor:getRect(windowWidth, windowHeight) | |
| local w = CalculateLength(self.width.min, self.width.max, self.padding.left + self.padding.right, windowWidth) | |
| local h = CalculateLength(self.height.min, self.height.max, self.padding.top + self.padding.bottom, windowHeight) | |
| local x, y = CalculatePosition(self.anchorPoint, self.x, self.y, w, h, self.padding, windowWidth, windowHeight) | |
| return x, y, w, h | |
| end | |
| return setmetatable(anchor, {__call=function(_, ...) return anchor.new(...) end}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment