Skip to content

Commit

Permalink
Correct typing. Export types.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNexusAvenger committed Jan 4, 2023
1 parent 434fa5e commit fa79295
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sourcemap.json
2 changes: 1 addition & 1 deletion docs/code-examples/nexusbutton.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ local ScreenGui = Instance.new("ScreenGui")
ScreenGui.Parent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")

--Create a button.
for i, ThemeName in pairs({"CutCorners","CutTopLeftCorner","CutBottomRightCorner","RoundedCorners"}) do
for i, ThemeName in {"CutCorners","CutTopLeftCorner","CutBottomRightCorner","RoundedCorners"} do
local Button = NexusButton.new()
Button.Size = UDim2.new(0,350,0,40)
Button.Position = UDim2.new(0,50,0,60 * i)
Expand Down
1 change: 0 additions & 1 deletion sourcemap.json

This file was deleted.

23 changes: 16 additions & 7 deletions src/ControllerIcon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ TheNexusAvenger
Class representing a controller icon.
--]]
--!strict

local BASE_ICON_SIZE_RELATIVE = 0.9
local CUSTOM_MULTIPLIERS = {
Expand All @@ -20,6 +21,14 @@ local NexusInstance = require(script.Parent:WaitForChild("NexusWrappedInstance")
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



--[[
Expand Down Expand Up @@ -50,7 +59,7 @@ end
--[[
Updates the visibility of the icon.
--]]
function ControllerIcon:UpdateVisibility(): nil
function ControllerIcon:UpdateVisibility(): ()
--Set the visibility to false if there is no icon.
if not self.Icon then
self.AdornFrame.Visible = false
Expand All @@ -69,7 +78,7 @@ end
--[[
Sets the icon.
--]]
function ControllerIcon:SetIcon(KeyCode: Enum.KeyCode | string): nil
function ControllerIcon:SetIcon(KeyCode: Enum.KeyCode | string): ()
--Return if the KeyCode is nil.
if KeyCode == nil then
self.KeyCode = nil
Expand All @@ -81,7 +90,7 @@ function ControllerIcon:SetIcon(KeyCode: Enum.KeyCode | string): nil

--Covert the KeyCode from a string.
if type(KeyCode) == "string" then
KeyCode = Enum.KeyCode[KeyCode]
KeyCode = (Enum.KeyCode :: any)[KeyCode]
end

--Destroy the existing icon.
Expand All @@ -104,7 +113,7 @@ end
--[[
Sets the scale of the icon.
--]]
function ControllerIcon:SetScale(NewScale: number): nil
function ControllerIcon:SetScale(NewScale: number): ()
self.IconScale = NewScale

--Set the size.
Expand All @@ -117,11 +126,11 @@ end
--[[
Destroys the frame.
--]]
function ControllerIcon:Destroy(): nil
function ControllerIcon:Destroy(): ()
NexusInstance.Destroy(self)

--Disconnect the events.
for _,Event in pairs(self.Events) do
for _,Event in self.Events do
Event:Disconnect()
end
self.Events = {}
Expand All @@ -132,4 +141,4 @@ end



return ControllerIcon
return ControllerIcon :: ControllerIcon
23 changes: 17 additions & 6 deletions src/Factory/ButtonFactory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ TheNexusAvenger
"Factory" for creating buttons. Used to be able
to set defaults once.
--]]
--!strict

local BORDER_COLOR_OFFSET = Color3.new(-30 / 255, -30 / 255, -30 / 255)

Expand All @@ -17,6 +18,16 @@ local NexusButton = require(RootModule)
local ButtonFactory = NexusObject:Extend()
ButtonFactory:SetClassName("ButtonFactory")

export type ButtonFactory = {
new: () -> ButtonFactory,
Extend: (self: ButtonFactory) -> ButtonFactory,
CreateDefault: (Color: Color3) -> ButtonFactory,

Create: (self: ButtonFactory) -> NexusButton.NexusButton,
SetDefault: (self: ButtonFactory, PropertyName: string, Property: any) -> (),
UnsetDefault: (self: ButtonFactory, PropertyName: string) -> (),
} & NexusObject.NexusObject



--[[
Expand Down Expand Up @@ -56,13 +67,13 @@ end
--[[
Creates a button instance.
--]]
function ButtonFactory:Create()
function ButtonFactory:Create(): NexusButton.NexusButton
--Create the button.
local Button = NexusButton.new()

--Set the defaults.
for PropertyName,PropertyValue in pairs(self.Defaults) do
Button[PropertyName] = PropertyValue
for PropertyName,PropertyValue in self.Defaults do
(Button :: any)[PropertyName] = PropertyValue
end

--Return the button.
Expand All @@ -72,17 +83,17 @@ end
--[[
Sets a default property.
--]]
function ButtonFactory:SetDefault(PropertyName: string, Property: any): nil
function ButtonFactory:SetDefault(PropertyName: string, Property: any): ()
self.Defaults[PropertyName] = Property
end

--[[
Unsets a default property.
--]]
function ButtonFactory:UnsetDefault(PropertyName: string): nil
function ButtonFactory:UnsetDefault(PropertyName: string): ()
self.Defaults[PropertyName] = nil
end



return ButtonFactory
return ButtonFactory :: ButtonFactory
20 changes: 15 additions & 5 deletions src/Factory/TextButtonFactory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ TheNexusAvenger
"Factory" for creating text buttons. Used to be
able to set defaults once.
--]]
--!strict

local BORDER_COLOR_OFFSET = Color3.new(-30 / 255, -30 / 255, -30 / 255)

Expand All @@ -16,6 +17,15 @@ local ButtonFactory = require(RootModule:WaitForChild("Factory"):WaitForChild("B
local TextButtonFactory = ButtonFactory:Extend()
TextButtonFactory:SetClassName("TextButtonFactory")

export type TextButtonFactory = {
new: () -> TextButtonFactory,
Extend: (self: TextButtonFactory) -> TextButtonFactory,
CreateDefault: (Color: Color3) -> TextButtonFactory,

SetTextDefault: (self: TextButtonFactory, PropertyName: string, Property: any) -> (),
UnsetTextDefault: (self: TextButtonFactory, PropertyName: string) -> (),
} & ButtonFactory.ButtonFactory



--[[
Expand Down Expand Up @@ -74,8 +84,8 @@ function TextButtonFactory:Create()
TextLabel.Parent = Button:GetAdornFrame()

--Set the text defaults.
for PropertyName,PropertyValue in pairs(self.TextDefaults) do
TextLabel[PropertyName] = PropertyValue
for PropertyName,PropertyValue in self.TextDefaults do
(TextLabel :: any)[PropertyName] = PropertyValue
end

--Return the button and textlabel.
Expand All @@ -85,17 +95,17 @@ end
--[[
Sets a default text property.
--]]
function ButtonFactory:SetTextDefault(PropertyName: string, Property: any): nil
function ButtonFactory:SetTextDefault(PropertyName: string, Property: any): ()
self.TextDefaults[PropertyName] = Property
end

--[[
Unsets a default text property.
--]]
function ButtonFactory:UnsetTextDefault(PropertyName: string)
function ButtonFactory:UnsetTextDefault(PropertyName: string): ()
self.TextDefaults[PropertyName] = nil
end



return TextButtonFactory
return TextButtonFactory :: TextButtonFactory
12 changes: 10 additions & 2 deletions src/ThemedFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ local NexusWrappedInstance = require(script.Parent:WaitForChild("NexusWrappedIns
local ThemedFrame = NexusWrappedInstance:Extend()
ThemedFrame:SetClassName("ThemedFrame")

export type ThemedFrame = {
new: () -> ThemedFrame,
Extend: (self: ThemedFrame) -> ThemedFrame,

Theme: NexusButton.NexusButtonTheme,
} & "ImageLabel"



--[[
Creates the themed frame.
Expand Down Expand Up @@ -65,7 +73,7 @@ end
--[[
Updates the slice scale of the frame.
--]]
function ThemedFrame:UpdateSliceScale(): nil
function ThemedFrame:UpdateSliceScale(): ()
if not self.Theme then return end
local Theme = NexusButton.Themes[self.Theme]
self.SliceScale = math.min(self.AbsoluteSize.X, self.AbsoluteSize.Y) * Theme.MainButton.SliceScaleMultiplier * (self.SliceScaleMultiplier or 1)
Expand All @@ -74,4 +82,4 @@ end



return ThemedFrame
return ThemedFrame :: ThemedFrame
52 changes: 39 additions & 13 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Main module representing the button class. This button
is meant to provide an easy way to make "good looking",
cross platform buttons.
--]]
--!strict

local HOVER_COLOR_MULTIPLIER = 0.7
local PRESS_COLOR_MULTIPLIER = 1 / 0.7
Expand Down Expand Up @@ -73,6 +74,31 @@ local NexusButton = NexusWrappedInstance:Extend()
NexusButton.Themes = DEFAULT_THEMES
NexusButton:SetClassName("NexusButton")

export type NexusButtonTheme = {
MainButton: {
Image: string,
SliceCenter: Rect,
SliceScaleMultiplier: number,
},
GamepadIconBackground: {
Image: string,
SliceCenter: Rect,
SliceScaleMultiplier: number,
},
}
export type NexusButton = {
new: () -> NexusButton,
Extend: (self: NexusButton) -> NexusButton,

TweenDuration: number,
Theme: string,
OverrideButtonProperty: (self: NexusButton, PropertyName: string, SetFunction: (any) -> ()) -> (),
GetAdornFrame: (self: NexusButton) -> Frame,
SetControllerIcon: (self: NexusButton, KeyCode: Enum.KeyCode | string) -> (),
MapKey: (self: NexusButton, KeyCode: Enum.KeyCode | string, MouseInput: Enum.UserInputType | string) -> (),
UnmapKey: (KeyCode: Enum.KeyCode | string) -> (),
} & NexusWrappedInstance.NexusWrappedInstance & TextButton



--[[
Expand All @@ -85,7 +111,7 @@ end
--[[
Creates a Nexus Button object.
--]]
function NexusButton:__new()
function NexusButton:__new(): ()
NexusWrappedInstance.__new(self, "TextButton")

--Create the frames.
Expand Down Expand Up @@ -254,15 +280,15 @@ end
--[[
Overrides the replication of a property for the button.
--]]
function NexusButton:OverrideButtonProperty(PropertyName: string, SetFunction: (any) -> nil): nil
function NexusButton:OverrideButtonProperty(PropertyName: string, SetFunction: (any) -> ()): ()
self:DisableChangeReplication(PropertyName)
self.ButtonPropertyOverrides[PropertyName] = SetFunction
end

--[[
Updates the slice scales of the background and border.
--]]
function NexusButton:UpdateSliceScale(): nil
function NexusButton:UpdateSliceScale(): ()
local Theme = NexusButton.Themes[self.Theme]
local ButtonSize = math.min(self.AbsoluteSize.X, self.AbsoluteSize.Y)
local SliceScale = ButtonSize * Theme.MainButton.SliceScaleMultiplier
Expand All @@ -274,7 +300,7 @@ end
--[[
Updates the background and border properties.
--]]
function NexusButton:UpdateBorder(Tween: boolean?): nil
function NexusButton:UpdateBorder(Tween: boolean?): ()
--Get the border size.
if not self.BorderSize then return end
if not self.Theme then return end
Expand Down Expand Up @@ -322,20 +348,20 @@ end
--[[
Sets the controller icon for the button.
--]]
function NexusButton:SetControllerIcon(KeyCode: Enum.KeyCode | string): nil
function NexusButton:SetControllerIcon(KeyCode: Enum.KeyCode | string): ()
self.GamepadIcon:SetIcon(KeyCode)
end

--[[
Maps a key input to a mouse input for clicking.
--]]
function NexusButton:MapKey(KeyCode: Enum.KeyCode | string, MouseInput: Enum.UserInputType | string): nil
function NexusButton:MapKey(KeyCode: Enum.KeyCode | string, MouseInput: Enum.UserInputType | string): ()
--Correct the inputs.
if typeof(KeyCode) == "string" then
KeyCode = Enum.KeyCode[KeyCode]
KeyCode = (Enum.KeyCode :: any)[KeyCode]
end
if typeof(MouseInput) == "string" then
MouseInput = Enum.UserInputType[MouseInput]
MouseInput = (Enum.UserInputType :: any)[MouseInput]
end

--Throw an error if the mouse input is invalid.
Expand All @@ -350,10 +376,10 @@ end
--[[
Unmaps a key input to a mouse input for clicking.
--]]
function NexusButton:UnmapKey(KeyCode: Enum.KeyCode | string): nil
function NexusButton:UnmapKey(KeyCode: Enum.KeyCode | string): ()
--Correct the input.
if typeof(KeyCode) == "string" then
KeyCode = Enum.KeyCode[KeyCode]
KeyCode = (Enum.KeyCode :: any)[KeyCode]
end

--Remove the mapped input.
Expand All @@ -363,17 +389,17 @@ end
--[[
Destroys the button and disconnects the events.
--]]
function NexusButton:Destroy(): nil
function NexusButton:Destroy(): ()
NexusWrappedInstance.Destroy(self)
self.GamepadIcon:Destroy()

--Disconnect the events.
for _, Event in pairs(self.Events) do
for _, Event in self.Events do
Event:Disconnect()
end
self.Events = {}
end



return NexusButton
return NexusButton :: NexusButton
2 changes: 1 addition & 1 deletion test/Factory/ButtonFactoryTests.nexusspec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Test that the CreateDefault method.
--]]
NexusUnitTesting:RegisterUnitTest(ButtonFactoryTest.new("CreateDefault"):SetRun(function(self)
--Create the component under testing.
local CuT = ButtonFactory.CreateDefault(Color3.new(0,170/255,1))
local CuT = ButtonFactory.CreateDefault(Color3.new(0, 170 / 255, 1))

--Create a button and assert the defaults are correct.
local Button = CuT:Create()
Expand Down

0 comments on commit fa79295

Please sign in to comment.