From f5c5d45ec4e27af2259a381459db659ca85b5028 Mon Sep 17 00:00:00 2001 From: TheNexusAvenger <13441476+TheNexusAvenger@users.noreply.github.com> Date: Sun, 3 Dec 2023 01:40:36 -0500 Subject: [PATCH] Support PlayStation icons. --- src/ControllerIcon.lua | 342 ++++++++++++++++++ .../ControllerIconCreator/Spritesheet.lua | 40 -- .../Spritesheets/XboxOne/Dark.lua | 37 -- .../Spritesheets/XboxOne/Light.lua | 37 -- .../ControllerIconCreator/init.lua | 33 -- src/ControllerIcon/init.lua | 144 -------- test/ControllerIcon.spec.lua | 10 +- test/ControllerIconTests.nexusspec.lua | 77 ---- 8 files changed, 347 insertions(+), 373 deletions(-) create mode 100644 src/ControllerIcon.lua delete mode 100644 src/ControllerIcon/ControllerIconCreator/Spritesheet.lua delete mode 100644 src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Dark.lua delete mode 100644 src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Light.lua delete mode 100644 src/ControllerIcon/ControllerIconCreator/init.lua delete mode 100644 src/ControllerIcon/init.lua delete mode 100644 test/ControllerIconTests.nexusspec.lua diff --git a/src/ControllerIcon.lua b/src/ControllerIcon.lua new file mode 100644 index 0000000..64a58ad --- /dev/null +++ b/src/ControllerIcon.lua @@ -0,0 +1,342 @@ +--[[ +TheNexusAvenger + +Class representing a controller icon. +--]] +--!strict + +local BASE_ICON_SIZE_RELATIVE = 0.9 +local XBOX_SPRITESHEET = "rbxassetid://408444495" +local PLAYSTATION_SPRITESHEET = "rbxassetid://15530886548" + +export type IconProperties = { + Image: string, + Size: Vector2, + Offset: Vector2, + Color: Color3?, +} + +local GAMEPAD_ICONS = { + Xbox = { + ButtonA = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(95, 95), + Offset = Vector2.new(318, 416), + }, + ButtonB = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(95, 95), + Offset = Vector2.new(520, 522), + }, + ButtonX = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(95, 95), + Offset = Vector2.new(510, 416), + }, + ButtonY = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(95, 95), + Offset = Vector2.new(616, 318), + }, + DPadUp = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(616, 530), + }, + DPadDown = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(212, 522), + }, + DPadLeft = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(318, 522), + }, + DPadRight = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(212, 416), + }, + ButtonSelect = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(95, 95), + Offset = Vector2.new(424, 522), + }, + ButtonLB = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(115, 64), + Offset = Vector2.new(116, 628), + }, + ButtonRB = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(115, 64), + Offset = Vector2.new(0, 628), + }, + ButtonLT = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 115), + Offset = Vector2.new(616, 0), + }, + ButtonRT = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 115), + Offset = Vector2.new(616, 414), + }, + ButtonLS = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(0, 522), + }, + ButtonRS = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(0, 416), + }, + Thumbstick1 = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(616, 116), + }, + Thumbstick2 = { + Image = XBOX_SPRITESHEET, + Size = Vector2.new(105, 105), + Offset = Vector2.new(106, 522), + }, + }, + PlayStation = { + ButtonCross = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(0, 0), + }, + ButtonCircle = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(58, 0), + }, + ButtonSquare = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(116, 0), + }, + ButtonTriangle = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(0, 58), + }, + ButtonL1 = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(58, 58), + }, + ButtonR1 = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(116, 58), + }, + ButtonL2 = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(0, 116), + }, + ButtonR2 = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(58, 116), + }, + ButtonTouchpad = { + Image = PLAYSTATION_SPRITESHEET, + Size = Vector2.new(58, 58), + Offset = Vector2.new(116, 116), + }, + }, +} :: {[string]: {[string]: IconProperties}} +GAMEPAD_ICONS.Default = GAMEPAD_ICONS.Xbox +GAMEPAD_ICONS.Default.ButtonL1 = GAMEPAD_ICONS.Xbox.ButtonLB +GAMEPAD_ICONS.Default.ButtonR1 = GAMEPAD_ICONS.Xbox.ButtonRB +GAMEPAD_ICONS.Default.ButtonL2 = GAMEPAD_ICONS.Xbox.ButtonLT +GAMEPAD_ICONS.Default.ButtonR2 = GAMEPAD_ICONS.Xbox.ButtonRT +GAMEPAD_ICONS.Default.ButtonL3 = GAMEPAD_ICONS.Xbox.ButtonLS +GAMEPAD_ICONS.Default.ButtonR3 = GAMEPAD_ICONS.Xbox.ButtonRS + + + +local UserInputService = game:GetService("UserInputService") + +local NexusInstance = require(script.Parent:WaitForChild("NexusWrappedInstance"):WaitForChild("NexusInstance"):WaitForChild("NexusInstance")) + +local ControllerIcon = NexusInstance:Extend() +ControllerIcon:SetClassName("ControllerIcon") + +local WarnedPlatformNames = {} + +export type ControllerIcon = { + new: () -> ControllerIcon, + Extend: (self: ControllerIcon) -> ControllerIcon, + + SetIcon: (self: ControllerIcon, KeyCode: Enum.KeyCode | string) -> (), + SetScale: (self: ControllerIcon, NewScale: number) -> (), +} & NexusInstance.NexusInstance + + + +--[[ +Resolves the gamepad image properties for a KeyCode. +--]] +function ControllerIcon.ResolveImage(KeyCode: Enum.KeyCode): IconProperties + --Get the name and fallback image. + local PlatformKeyCodeName = UserInputService:GetStringForKeyCode(KeyCode) + local FallbackImage = UserInputService:GetImageForKeyCode(KeyCode) + + --Return if a group matches. + for GroupName, Images in GAMEPAD_ICONS do + if not string.find(string.lower(FallbackImage), string.lower(GroupName)) then continue end + if not Images[PlatformKeyCodeName] then continue end + return Images[PlatformKeyCodeName] + end + + --Return if the default exists. + if GAMEPAD_ICONS.Default[PlatformKeyCodeName] then + return GAMEPAD_ICONS.Default[PlatformKeyCodeName] + end + + --Return the fallback case. + if not WarnedPlatformNames[PlatformKeyCodeName] then + warn(`No override exists for {PlatformKeyCodeName} (from {KeyCode.Name}) with {FallbackImage}. Returning default image.`) + WarnedPlatformNames[PlatformKeyCodeName] = true + end + return { + Image = FallbackImage, + Size = Vector2.zero, + Offset = Vector2.zero, + Color = Color3.fromRGB(60, 60, 60), + } +end + +--[[ +Constructor of the Controller Icon class. +--]] +function ControllerIcon:__new() + NexusInstance.__new(self) + + --Create the adorn frame. + local AdornFrame = Instance.new("ImageLabel") + AdornFrame.BackgroundTransparency = 1 + self.AdornFrame = AdornFrame + self.IconScale = BASE_ICON_SIZE_RELATIVE + + --Connect the events. + self.Events = {} + table.insert(self.Events, UserInputService.GamepadConnected:Connect(function() + self:UpdateVisibility() + end)) + table.insert(self.Events, UserInputService.GamepadDisconnected:Connect(function() + self:UpdateVisibility() + end)) + + --Update the visibility. + self:UpdateVisibility() +end + +--[[ +Updates the visibility of the icon. +--]] +function ControllerIcon:UpdateVisibility(): () + --Set the visibility to false if there is no icon. + if not self.Icon then + self.AdornFrame.Visible = false + self.IconVisible = false + return + end + + --Determine if a controller is connected. + local ControllerConnected = (#UserInputService:GetConnectedGamepads() ~= 0) + + --Set the visibility. + self.AdornFrame.Visible = ControllerConnected + self.IconVisible = ControllerConnected +end + +--[[ +Sets the icon. +--]] +function ControllerIcon:SetIcon(KeyCode: Enum.KeyCode | string): () + --Return if the KeyCode is nil. + if KeyCode == nil then + self.KeyCode = nil + self.Icon:Destroy() + self.Icon = nil + self:UpdateVisibility() + return + end + + --Covert the KeyCode from a string. + if type(KeyCode) == "string" then + KeyCode = (Enum.KeyCode :: any)[KeyCode] + end + + --Destroy the existing icon. + if self.Icon then + self.Icon:Destroy() + end + + --Create the new icon. + local IconData = self.ResolveImage(KeyCode) + local Icon = Instance.new("ImageLabel") + Icon.BackgroundTransparency = 1 + Icon.AnchorPoint = Vector2.new(0.5, 0.5) + Icon.Position = UDim2.new(0.5, 0, 0.5, 0) + if IconData.Size.X > IconData.Size.Y then + Icon.Size = UDim2.new(1, 0, IconData.Size.Y / IconData.Size.X, 0) + elseif IconData.Size.X < IconData.Size.Y then + Icon.Size = UDim2.new(IconData.Size.X / IconData.Size.Y, 0, 1, 0) + else + Icon.Size = UDim2.new(1, 0, 1, 0) + end + Icon.ZIndex = self.AdornFrame.ZIndex + Icon.Image = IconData.Image + Icon.ImageRectSize = IconData.Size + Icon.ImageRectOffset = IconData.Offset + Icon.ImageColor3 = IconData.Color or Color3.fromRGB(255, 255, 255) + Icon.Parent = self.AdornFrame + + local IconUIScale = Instance.new("UIScale") + IconUIScale.Scale = self.IconScale or 1 + IconUIScale.Parent = Icon + self.IconUIScale = IconUIScale + + self.Icon = Icon + self.KeyCode = KeyCode + self:UpdateVisibility() +end + +--[[ +Sets the scale of the icon. +--]] +function ControllerIcon:SetScale(NewScale: number): () + self.IconScale = NewScale + if self.IconUIScale then + self.IconUIScale.Scale = NewScale + end +end + +--[[ +Destroys the frame. +--]] +function ControllerIcon:Destroy(): () + NexusInstance.Destroy(self) + + --Disconnect the events. + for _,Event in self.Events do + Event:Disconnect() + end + self.Events = {} + + --Destroy the adorn frame. + self.AdornFrame:Destroy() +end + + + +return ControllerIcon :: ControllerIcon \ No newline at end of file diff --git a/src/ControllerIcon/ControllerIconCreator/Spritesheet.lua b/src/ControllerIcon/ControllerIconCreator/Spritesheet.lua deleted file mode 100644 index 2ca10cd..0000000 --- a/src/ControllerIcon/ControllerIconCreator/Spritesheet.lua +++ /dev/null @@ -1,40 +0,0 @@ -local Spritesheet = {} -Spritesheet.__index = Spritesheet - -function Spritesheet.new(texture) - local newSpritesheet = {} - setmetatable(newSpritesheet, Spritesheet) - - newSpritesheet.Texture = texture - newSpritesheet.Sprites = {} - - return newSpritesheet -end - -function Spritesheet:AddSprite(index, position, size) - local Sprite = {Position=position,Size=size} - self.Sprites[index] = Sprite -end - -function Spritesheet:GetSprite(instanceType, index) - if not index then - warn("Image name cannot be nil") - return false - end - local sprite = self.Sprites[index] - if not sprite then - warn("Could not find sprite for: " .. index) - return false - end - local element = Instance.new(instanceType) - element.BackgroundTransparency = 1 - element.BorderSizePixel = 1 - element.Image = self.Texture - element.Size = UDim2.new(0, sprite.Size.X, 0, sprite.Size.Y) - element.ImageRectOffset = sprite.Position - element.ImageRectSize = sprite.Size - - return element -end - -return Spritesheet diff --git a/src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Dark.lua b/src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Dark.lua deleted file mode 100644 index 5bdd21d..0000000 --- a/src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Dark.lua +++ /dev/null @@ -1,37 +0,0 @@ -local Spritesheet = require(script.Parent.Parent.Parent.Spritesheet) -local Dark = {} -Dark.__index = Dark -setmetatable(Dark, Spritesheet) - -local darkTexture = "rbxassetid://408444495" - -function Dark.new() - local newDark = Spritesheet.new(darkTexture) - setmetatable(newDark, Dark) - - newDark:AddSprite("ButtonX", Vector2.new(510, 416), Vector2.new(95, 95)) - newDark:AddSprite("ButtonY", Vector2.new(616, 318), Vector2.new(95, 95)) - newDark:AddSprite("ButtonA", Vector2.new(318, 416), Vector2.new(95, 95)) - newDark:AddSprite("ButtonB", Vector2.new(520, 522), Vector2.new(95, 95)) - newDark:AddSprite("ButtonR1", Vector2.new(0, 628), Vector2.new(115, 64)) - newDark:AddSprite("ButtonL1", Vector2.new(116, 628), Vector2.new(115, 64)) - newDark:AddSprite("ButtonR2", Vector2.new(616, 414), Vector2.new(105, 115)) - newDark:AddSprite("ButtonL2", Vector2.new(616, 0), Vector2.new(105, 115)) - newDark:AddSprite("ButtonR3", Vector2.new(0, 416), Vector2.new(105, 105)) - newDark:AddSprite("ButtonL3", Vector2.new(0, 522), Vector2.new(105, 105)) - newDark:AddSprite("ButtonSelect", Vector2.new(424, 522), Vector2.new(95, 95)) - newDark:AddSprite("DPadLeft", Vector2.new(318, 522), Vector2.new(105, 105)) - newDark:AddSprite("DPadRight", Vector2.new(212, 416), Vector2.new(105, 105)) - newDark:AddSprite("DPadUp", Vector2.new(616, 530), Vector2.new(105, 105)) - newDark:AddSprite("DPadDown", Vector2.new(212, 522), Vector2.new(105, 105)) - newDark:AddSprite("Thumbstick1", Vector2.new(616, 116), Vector2.new(105, 105)) - newDark:AddSprite("Thumbstick2", Vector2.new(106, 522), Vector2.new(105, 105)) - newDark:AddSprite("DPad", Vector2.new(106, 416), Vector2.new(105, 105)) - newDark:AddSprite("Controller", Vector2.new(0, 0), Vector2.new(615, 415)) - newDark:AddSprite("RotateThumbstick1", Vector2.new(414, 416), Vector2.new(95, 95)) - newDark:AddSprite("RotateThumbstick2", Vector2.new(616, 222), Vector2.new(95, 95)) - - return newDark -end - -return Dark diff --git a/src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Light.lua b/src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Light.lua deleted file mode 100644 index 2e8d6f6..0000000 --- a/src/ControllerIcon/ControllerIconCreator/Spritesheets/XboxOne/Light.lua +++ /dev/null @@ -1,37 +0,0 @@ -local Spritesheet = require(script.Parent.Parent.Parent.Spritesheet) -local Light = {} -Light.__index = Light -setmetatable(Light, Spritesheet) - -local lightTexture = "rbxassetid://408462759" - -function Light.new() - local newLight = Spritesheet.new(lightTexture) - setmetatable(newLight, Light) - - newLight:AddSprite("ButtonX", Vector2.new(318, 481), Vector2.new(95, 95)) - newLight:AddSprite("ButtonY", Vector2.new(500, 587), Vector2.new(95, 95)) - newLight:AddSprite("ButtonA", Vector2.new(308, 587), Vector2.new(95, 95)) - newLight:AddSprite("ButtonB", Vector2.new(510, 481), Vector2.new(95, 95)) - newLight:AddSprite("ButtonR1", Vector2.new(0, 416), Vector2.new(115, 64)) - newLight:AddSprite("ButtonL1", Vector2.new(116, 416), Vector2.new(115, 64)) - newLight:AddSprite("ButtonR2", Vector2.new(616, 0), Vector2.new(105, 115)) - newLight:AddSprite("ButtonL2", Vector2.new(616, 328), Vector2.new(105, 115)) - newLight:AddSprite("ButtonR3", Vector2.new(616, 550), Vector2.new(105, 105)) - newLight:AddSprite("ButtonL3", Vector2.new(616, 116), Vector2.new(105, 105)) - newLight:AddSprite("ButtonSelect", Vector2.new(404, 587), Vector2.new(95, 95)) - newLight:AddSprite("DPadLeft", Vector2.new(616, 444), Vector2.new(105, 105)) - newLight:AddSprite("DPadRight", Vector2.new(0, 587), Vector2.new(105, 105)) - newLight:AddSprite("DPadUp", Vector2.new(616, 222), Vector2.new(105, 105)) - newLight:AddSprite("DPadDown", Vector2.new(212, 481), Vector2.new(105, 105)) - newLight:AddSprite("Thumbstick1", Vector2.new(0, 481), Vector2.new(105, 105)) - newLight:AddSprite("Thumbstick2", Vector2.new(106, 587), Vector2.new(105, 105)) - newLight:AddSprite("DPad", Vector2.new(106, 481), Vector2.new(105, 105)) - newLight:AddSprite("Controller", Vector2.new(0, 0), Vector2.new(615, 415)) - newLight:AddSprite("RotateThumbstick1", Vector2.new(414, 481), Vector2.new(95, 95)) - newLight:AddSprite("RotateThumbstick2", Vector2.new(212, 587), Vector2.new(95, 95)) - - return newLight -end - -return Light diff --git a/src/ControllerIcon/ControllerIconCreator/init.lua b/src/ControllerIcon/ControllerIconCreator/init.lua deleted file mode 100644 index 816cefd..0000000 --- a/src/ControllerIcon/ControllerIconCreator/init.lua +++ /dev/null @@ -1,33 +0,0 @@ -local ControllerImageLibrary = {} - -local spritesheets = {} -for _, platform in pairs(script.Spritesheets:GetChildren()) do - spritesheets[platform.Name] = {} - for _, style in pairs(platform:GetChildren()) do - spritesheets[platform.Name][style.Name] = require(style).new() - end -end - -local function getImageInstance(instanceType, index, style) - local platform = "XboxOne" - if type(index)== "userdata" then - index = string.sub(tostring(index), 14) - end - local sheet = spritesheets[platform][style] - if not sheet then - warn("Could not find style: " .. style) - return - end - local element = sheet:GetSprite(instanceType, index) - return element -end - -function ControllerImageLibrary:GetImageLabel(index, style, platform) - return getImageInstance("ImageLabel", index, style, platform) -end - -function ControllerImageLibrary:GetImageButton(index, style, platform) - return getImageInstance("ImageButton", index, style, platform) -end - -return ControllerImageLibrary diff --git a/src/ControllerIcon/init.lua b/src/ControllerIcon/init.lua deleted file mode 100644 index 2f42dcd..0000000 --- a/src/ControllerIcon/init.lua +++ /dev/null @@ -1,144 +0,0 @@ ---[[ -TheNexusAvenger - -Class representing a controller icon. ---]] ---!strict - -local BASE_ICON_SIZE_RELATIVE = 0.9 -local CUSTOM_MULTIPLIERS = { - [Enum.KeyCode.ButtonL1] = {1, 0.5}, - [Enum.KeyCode.ButtonR1] = {1, 0.5}, -} - - - -local UserInputService = game:GetService("UserInputService") - -local ControllerIconCreator = require(script:WaitForChild("ControllerIconCreator")) -local NexusInstance = require(script.Parent:WaitForChild("NexusWrappedInstance"):WaitForChild("NexusInstance"):WaitForChild("NexusInstance")) - -local ControllerIcon = NexusInstance:Extend() -ControllerIcon:SetClassName("ControllerIcon") - -export type ControllerIcon = { - new: () -> ControllerIcon, - Extend: (self: ControllerIcon) -> ControllerIcon, - - SetIcon: (self: ControllerIcon, KeyCode: Enum.KeyCode | string) -> (), - SetScale: (self: ControllerIcon, NewScale: number) -> (), -} & NexusInstance.NexusInstance - - - ---[[ -Constructor of the Controller Icon class. ---]] -function ControllerIcon:__new() - NexusInstance.__new(self) - - --Create the adorn frame. - local AdornFrame = Instance.new("ImageLabel") - AdornFrame.BackgroundTransparency = 1 - self.AdornFrame = AdornFrame - self.IconScale = BASE_ICON_SIZE_RELATIVE - - --Connect the events. - self.Events = {} - table.insert(self.Events, UserInputService.GamepadConnected:Connect(function() - self:UpdateVisibility() - end)) - table.insert(self.Events, UserInputService.GamepadDisconnected:Connect(function() - self:UpdateVisibility() - end)) - - --Update the visibility. - self:UpdateVisibility() -end - ---[[ -Updates the visibility of the icon. ---]] -function ControllerIcon:UpdateVisibility(): () - --Set the visibility to false if there is no icon. - if not self.Icon then - self.AdornFrame.Visible = false - self.IconVisible = false - return - end - - --Determine if a controller is connected. - local ControllerConnected = (#UserInputService:GetConnectedGamepads() ~= 0) - - --Set the visibility. - self.AdornFrame.Visible = ControllerConnected - self.IconVisible = ControllerConnected -end - ---[[ -Sets the icon. ---]] -function ControllerIcon:SetIcon(KeyCode: Enum.KeyCode | string): () - --Return if the KeyCode is nil. - if KeyCode == nil then - self.KeyCode = nil - self.Icon:Destroy() - self.Icon = nil - self:UpdateVisibility() - return - end - - --Covert the KeyCode from a string. - if type(KeyCode) == "string" then - KeyCode = (Enum.KeyCode :: any)[KeyCode] - end - - --Destroy the existing icon. - if self.Icon then - self.Icon:Destroy() - end - - --Create the new icon. - local Icon = ControllerIconCreator:GetImageLabel(KeyCode, "Dark", "XboxOne") - Icon.Position = UDim2.new(0.5, 0, 0.5, 0) - Icon.AnchorPoint = Vector2.new(0.5, 0.5) - Icon.ZIndex = self.AdornFrame.ZIndex - Icon.Parent = self.AdornFrame - self.Icon = Icon - self.KeyCode = KeyCode - self:UpdateVisibility() - self:SetScale(self.IconScale) -end - ---[[ -Sets the scale of the icon. ---]] -function ControllerIcon:SetScale(NewScale: number): () - self.IconScale = NewScale - - --Set the size. - if self.KeyCode and self.Icon then - local ScaleMultipliers = CUSTOM_MULTIPLIERS[self.KeyCode] or {1, 1} - self.Icon.Size = UDim2.new(self.IconScale * ScaleMultipliers[1], 0, self.IconScale * ScaleMultipliers[2], 0) - end -end - ---[[ -Destroys the frame. ---]] -function ControllerIcon:Destroy(): () - NexusInstance.Destroy(self) - - --Disconnect the events. - for _,Event in self.Events do - Event:Disconnect() - end - self.Events = {} - - --Destroy the adorn frame. - self.AdornFrame:Destroy() -end - - - -return ControllerIcon :: ControllerIcon \ No newline at end of file diff --git a/test/ControllerIcon.spec.lua b/test/ControllerIcon.spec.lua index 5a0526a..4488db2 100644 --- a/test/ControllerIcon.spec.lua +++ b/test/ControllerIcon.spec.lua @@ -21,9 +21,11 @@ return function() expect(TestControllerIcon.Icon).to.equal(nil) TestControllerIcon:SetIcon(Enum.KeyCode.ButtonA) - expect(TestControllerIcon.Icon.Size).to.equal(UDim2.new(0.9, 0, 0.9, 0)) + expect(TestControllerIcon.Icon.Size).to.equal(UDim2.new(1, 0, 1, 0)) + expect(TestControllerIcon.IconUIScale.Scale).to.be.near(0.9) TestControllerIcon:SetIcon(Enum.KeyCode.ButtonL1) - expect(TestControllerIcon.Icon.Size).to.equal(UDim2.new(0.9, 0, 0.45, 0)) + expect(TestControllerIcon.Icon.Size).to.equal(UDim2.new(1, 0, 64 / 115, 0)) + expect(TestControllerIcon.IconUIScale.Scale).to.be.near(0.9) TestControllerIcon:SetIcon(nil) expect(TestControllerIcon.Icon).to.equal(nil) @@ -33,9 +35,7 @@ return function() TestControllerIcon:SetIcon(Enum.KeyCode.ButtonA) TestControllerIcon:SetScale(0.6) - expect(TestControllerIcon.Icon.Size).to.equal(UDim2.new(0.6, 0, 0.6, 0)) - TestControllerIcon:SetIcon(Enum.KeyCode.ButtonL1) - expect(TestControllerIcon.Icon.Size).to.equal(UDim2.new(0.6, 0, 0.3, 0)) + expect(TestControllerIcon.IconUIScale.Scale).to.be.near(0.6) end) end) end \ No newline at end of file diff --git a/test/ControllerIconTests.nexusspec.lua b/test/ControllerIconTests.nexusspec.lua deleted file mode 100644 index 7fbbdd4..0000000 --- a/test/ControllerIconTests.nexusspec.lua +++ /dev/null @@ -1,77 +0,0 @@ ---[[ -TheNexusAvenger - -Unit tests for the ControllerIcon class. ---]] - -local NexusUnitTesting = require("NexusUnitTesting") -_G.EnsureNexusWrappedInstanceSingleton = false - -local NexusButton = game:GetService("ReplicatedStorage"):WaitForChild("NexusButton") -local ControllerIcon = require(NexusButton:WaitForChild("ControllerIcon")) -local ControllerIconTest = NexusUnitTesting.UnitTest:Extend() - - - ---[[ -Sets up the test. ---]] -function ControllerIconTest:Setup() - self.CuT = ControllerIcon.new() -end - ---[[ -Cleans up the test. ---]] -function ControllerIconTest:Teardown() - self.CuT:Destroy() -end - ---[[ -Test that the constructor works without failing. ---]] -NexusUnitTesting:RegisterUnitTest(ControllerIconTest.new("Constructor"):SetRun(function(self) - --Run the assertions. - self:AssertEquals(self.CuT.ClassName, "ControllerIcon", "ClassName is incorrect.") - - --Clean up the component under testing. - self.CuT:Destroy() -end)) - ---[[ -Tests the SetIcon method. ---]] -NexusUnitTesting:RegisterUnitTest(ControllerIconTest.new("SetIcon"):SetRun(function(self) - self:AssertNil(self.CuT.Icon,"Icon already exists.") - - --Set the icon to an enum and assert the size is correct. - self.CuT:SetIcon(Enum.KeyCode.ButtonA) - self:AssertEquals(self.CuT.Icon.Size,UDim2.new(0.9, 0, 0.9, 0), "Size is incorrect.") - - --Set the icon to a string and assert the size is correct. - self.CuT:SetIcon(Enum.KeyCode.ButtonL1) - self:AssertEquals(self.CuT.Icon.Size,UDim2.new(0.9, 0, 0.45, 0), "Size is incorrect.") - - --Set the icon to nil and assert the size is correct. - self.CuT:SetIcon(nil) - self:AssertNil(self.CuT.Icon, "Icon exists.") -end)) - ---[[ -Tests the SetScale method. ---]] -NexusUnitTesting:RegisterUnitTest(ControllerIconTest.new("SetScale"):SetRun(function(self) - self.CuT:SetIcon(Enum.KeyCode.ButtonA) - - --Set the scale and assert the size is correct. - self.CuT:SetScale(0.6) - self:AssertEquals(self.CuT.Icon.Size, UDim2.new(0.6, 0, 0.6, 0), "Size is incorrect.") - - --Set the icon to a string and assert the size is correct. - self.CuT:SetIcon(Enum.KeyCode.ButtonL1) - self:AssertEquals(self.CuT.Icon.Size, UDim2.new(0.6, 0, 0.3, 0), "Size is incorrect.") -end)) - - - -return true \ No newline at end of file