-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
AnimatedHighlightGroup.lua
188 lines (140 loc) · 5.55 KB
/
AnimatedHighlightGroup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
--[=[
@client
@class AnimatedHighlightGroup
]=]
local require = require(script.Parent.loader).load(script)
local BaseObject = require("BaseObject")
local AnimatedHighlightStack = require("AnimatedHighlightStack")
local Maid = require("Maid")
local EnumUtils = require("EnumUtils")
local AnimatedHighlightModel = require("AnimatedHighlightModel")
local Rx = require("Rx")
local AnimatedHighlightGroup = setmetatable({}, BaseObject)
AnimatedHighlightGroup.ClassName = "AnimatedHighlightGroup"
AnimatedHighlightGroup.__index = AnimatedHighlightGroup
function AnimatedHighlightGroup.new()
local self = setmetatable(BaseObject.new(), AnimatedHighlightGroup)
self._defaultValues = self._maid:Add(AnimatedHighlightModel.new())
self._defaultValues:SetHighlightDepthMode(Enum.HighlightDepthMode.AlwaysOnTop)
self._defaultValues:SetFillColor(Color3.new(1, 1, 1))
self._defaultValues:SetOutlineColor(Color3.new(1, 1, 1))
self._defaultValues:SetTransparencySpeed(40)
self._defaultValues:SetColorSpeed(40)
self._defaultValues:SetOutlineTransparency(0)
self._defaultValues:SetFillTransparency(0.5)
self._defaultValues:SetSpeed(20)
self._highlightStacks = {}
return self
end
--[=[
Sets the depth mode. Either can be:
* Enum.HighlightDepthMode.AlwaysOnTop
* Enum.HighlightDepthMode.Occluded
@param depthMode Enum.HighlightDepthMode
]=]
function AnimatedHighlightGroup:SetDefaultHighlightDepthMode(depthMode)
assert(EnumUtils.isOfType(Enum.HighlightDepthMode, depthMode))
self._defaultValues:SetHighlightDepthMode(depthMode)
end
function AnimatedHighlightGroup:SetDefaultFillTransparency(transparency)
assert(type(transparency) == "number", "Bad transparency")
self._defaultValues:SetFillTransparency(transparency)
end
function AnimatedHighlightGroup:SetDefaultOutlineTransparency(outlineTransparency)
assert(type(outlineTransparency) == "number", "Bad outlineTransparency")
self._defaultValues:SetOutlineTransparency(outlineTransparency)
end
function AnimatedHighlightGroup:SetDefaultFillColor(color)
assert(typeof(color) == "Color3", "Bad color")
self._defaultValues:SetFillColor(color)
end
function AnimatedHighlightGroup:GetDefaultFillColor()
return self._defaultValues:GetFillColor()
end
function AnimatedHighlightGroup:SetDefaultOutlineColor(color)
assert(typeof(color) == "Color3", "Bad color")
self._defaultValues:SetOutlineColor(color)
end
function AnimatedHighlightGroup:SetDefaultTransparencySpeed(speed)
assert(type(speed) == "number", "Bad speed")
self._defaultValues:SetTransparencySpeed(speed)
end
function AnimatedHighlightGroup:SetDefaultSpeed(speed)
assert(type(speed) == "number", "Bad speed")
self._defaultValues:SetSpeed(speed)
end
function AnimatedHighlightGroup:GetDefaultOutlineColor()
return self._defaultValues:GetOutlineColor()
end
--[=[
Returns an AnimatedHighlightModel which can be used to adjust the values
@param adornee Instance
@param observeScore number
@return AnimatedHighlightModel
]=]
function AnimatedHighlightGroup:Highlight(adornee, observeScore)
observeScore = observeScore or Rx.of(0)
if type(observeScore) == "number" then
observeScore = Rx.of(observeScore)
end
assert(typeof(adornee) == "Instance", "Bad adornee")
assert(observeScore, "Bad observeScore")
return self:_getOrCreateHighlightStackHandle(adornee, observeScore)
end
function AnimatedHighlightGroup:_setDefaultValues(highlight, doNotAnimate)
highlight:SetHighlightDepthMode(self._defaultValues.HighlightDepthMode.Value)
highlight:SetTransparencySpeed(self._defaultValues.TransparencySpeed.Value)
highlight:SetSpeed(self._defaultValues.Speed.Value)
highlight:SetFillTransparency(self._defaultValues.FillTransparency.Value, doNotAnimate)
highlight:SetOutlineTransparency(self._defaultValues.OutlineTransparency.Value, doNotAnimate)
highlight:SetFillColor(self._defaultValues.FillColor.Value)
highlight:SetOutlineColor(self._defaultValues.OutlineColor.Value)
end
function AnimatedHighlightGroup:_getOrCreateHighlightStackHandle(adornee, observeScore)
assert(observeScore, "Bad observeScore")
local foundHighlightStack = self._highlightStacks[adornee]
if foundHighlightStack then
return foundHighlightStack:GetHandle(observeScore)
end
local maid = Maid.new()
local highlightStack = AnimatedHighlightStack.new(adornee, self._defaultValues)
maid:GiveTask(highlightStack)
maid:GiveTask(highlightStack.Destroying:Connect(function()
self:_removeHighlightStack(highlightStack)
end))
local handle = highlightStack:GetHandle(observeScore)
maid:GiveTask(highlightStack.Done:Connect(function()
self:_removeHighlightStack(highlightStack)
end))
self._highlightStacks[adornee] = highlightStack
maid:GiveTask(function()
if self._highlightStacks[adornee] == highlightStack then
self._highlightStacks[adornee] = nil
end
end)
self._maid[highlightStack] = maid
return handle
end
function AnimatedHighlightGroup:HighlightWithTransferredProperties(fromAdornee, toAdornee, observeScore)
assert(typeof(fromAdornee) == "Instance", "Bad fromAdornee")
assert(typeof(toAdornee) == "Instance", "Bad toAdornee")
local source = self._highlightStacks[fromAdornee]
if not source then
return self:Highlight(toAdornee, observeScore)
end
local handle = self:Highlight(toAdornee, observeScore)
local target = self._highlightStacks[toAdornee]
if target then
target:SetPropertiesFrom(source)
else
warn("Failed to find stack")
end
return handle
end
function AnimatedHighlightGroup:_removeHighlightStack(highlightStack)
self._maid[highlightStack] = nil
end
function AnimatedHighlightGroup:_findHighlightAdornee(adornee)
return self._highlightStacks[adornee]
end
return AnimatedHighlightGroup