/
UICornerUtils.lua
79 lines (64 loc) · 1.63 KB
/
UICornerUtils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
--[=[
@class UICornerUtils
]=]
local UICornerUtils = {}
--[=[
Creates a new UI corner
@param scale number
@param parent Instance
@return UICorner
]=]
function UICornerUtils.fromScale(scale, parent)
local uiCorner = Instance.new("UICorner")
uiCorner.CornerRadius = UDim.new(scale, 0)
uiCorner.Parent = parent
return uiCorner
end
--[=[
Creates a new UI corner from offset
@param offset number
@param parent Instance
@return UICorner
]=]
function UICornerUtils.fromOffset(offset, parent)
local uiCorner = Instance.new("UICorner")
uiCorner.CornerRadius = UDim.new(0, offset)
uiCorner.Parent = parent
return uiCorner
end
-- framePosition is top left corner
-- returns position, relativePosition, normal
function UICornerUtils.clampPositionToFrame(framePosition, frameSize, radius, point)
assert(radius >= 0, "Bad radius")
assert(point, "Bad point")
local px, py = point.x, point.y
local fpx, fpy = framePosition.x, framePosition.y
local fsx, fsy = frameSize.x, frameSize.y
local minx = fpx + radius
local maxx = fpx + fsx - radius
local miny = fpy + radius
local maxy = fpy + fsy - radius
-- relative position to inner box
local rpx, rpy
if minx < maxx then
rpx = math.clamp(px, minx, maxx)
else
rpx = minx
end
if miny < maxy then
rpy = math.clamp(py, miny, maxy)
else
rpy = miny
end
local position = Vector2.new(rpx, rpy)
-- project in direction of offset
local direction = point - position
if direction.magnitude == 0 then
-- Shouldn't happen!
return nil, nil
end
local normal = direction.unit
local outsidePosition = position + normal*radius
return outsidePosition, normal
end
return UICornerUtils