|
| 1 | +local TweenService = game:GetService("TweenService") |
| 2 | +local Selection = game:GetService("Selection") |
| 3 | + |
| 4 | +local selectColor = Color3.fromRGB(214, 129, 0) |
| 5 | +local hoverColor = Color3.fromRGB(255, 184, 0) |
| 6 | +local selectionColors = { selectColor, hoverColor } |
| 7 | + |
| 8 | +local HighlightSelectionClass = {} |
| 9 | +HighlightSelectionClass.__index = HighlightSelectionClass |
| 10 | + |
| 11 | +-- Create a new highlight and set it to the selection. |
| 12 | +function HighlightSelectionClass.setup(selection: Instance?) |
| 13 | + local self = {} |
| 14 | + setmetatable(self, HighlightSelectionClass) |
| 15 | + |
| 16 | + local highlight = Instance.new("Highlight") |
| 17 | + highlight.Name = "HighlightSelection" |
| 18 | + highlight.FillTransparency = 1 |
| 19 | + highlight.OutlineColor = selectionColors[1] |
| 20 | + -- highlight.Adornee = selection |
| 21 | + highlight.Parent = script |
| 22 | + self._highlight = highlight |
| 23 | + |
| 24 | + self._selection = selection |
| 25 | + self._hover = false |
| 26 | + |
| 27 | + self:SetHoverOver(true) |
| 28 | + self:SetEnabled(true) |
| 29 | + |
| 30 | + self:_SetupSelectionHandling() |
| 31 | + |
| 32 | + return self |
| 33 | +end |
| 34 | + |
| 35 | +-- Detects if the selection is selected from Roblox Studio and changes the highlight color. |
| 36 | +function HighlightSelectionClass:_SetupSelectionHandling() |
| 37 | + Selection.SelectionChanged:Connect(function() |
| 38 | + local selected = Selection:Get()[1] |
| 39 | + |
| 40 | + if selected == self._selection then |
| 41 | + self:SetHoverOver(true) |
| 42 | + else |
| 43 | + self:SetHoverOver(false) |
| 44 | + end |
| 45 | + end) |
| 46 | +end |
| 47 | + |
| 48 | +-- Tweens the highlight color like in Roblox Studio depending on hover boolean. |
| 49 | +function HighlightSelectionClass:SetHoverOver(hover: boolean) |
| 50 | + self._hover = hover |
| 51 | + local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, math.huge, true) |
| 52 | + |
| 53 | + if self._hover == true then |
| 54 | + local tween = TweenService:Create(self._highlight, tweenInfo, { OutlineColor = selectionColors[2] }) |
| 55 | + tween:Play() |
| 56 | + else |
| 57 | + self._highlight.OutlineColor = selectionColors[2] |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +-- Changes Enabled property of the Highlight based on the disabled boolean. |
| 62 | +function HighlightSelectionClass:SetEnabled(enabled: boolean) |
| 63 | + if enabled == true then |
| 64 | + self._highlight.Enabled = false |
| 65 | + else |
| 66 | + self._highlight.Enabled = true |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +-- Set highlight's parent to the new selection. |
| 71 | +function HighlightSelectionClass:Set(selection: Instance) |
| 72 | + self._highlight.Parent = selection |
| 73 | +end |
| 74 | + |
| 75 | +-- Add instances to the selection. |
| 76 | +function HighlightSelectionClass:Add(instance: Instance) |
| 77 | + if self._highlight ~= nil then |
| 78 | + self._highlight.Adornee = instance |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +-- Add table to the selection. |
| 83 | +function HighlightSelectionClass:AddTable(table) |
| 84 | + for _, instance in ipairs(table) do |
| 85 | + self:Add(instance) |
| 86 | + end |
| 87 | +end |
| 88 | + |
| 89 | +-- Returns selection of HighlightSelectionClass. |
| 90 | +function HighlightSelectionClass:GetSelection(): Instance? |
| 91 | + return self._selection |
| 92 | +end |
| 93 | + |
| 94 | +-- Returns the highlight of HighlightSelectionClass. |
| 95 | +function HighlightSelectionClass:GetHighlight(): Highlight? |
| 96 | + return self._highlight |
| 97 | +end |
| 98 | + |
| 99 | +return HighlightSelectionClass |
0 commit comments