Renamed elements
This commit is contained in:
81
src/elements/CheckBox.lua
Normal file
81
src/elements/CheckBox.lua
Normal file
@@ -0,0 +1,81 @@
|
||||
local VisualElement = require("elements/VisualElement")
|
||||
---@cofnigDescription This is a checkbox. It is a visual element that can be checked.
|
||||
|
||||
--- The CheckBox is a visual element that can be checked.
|
||||
---@class CheckBox : VisualElement
|
||||
local CheckBox = setmetatable({}, VisualElement)
|
||||
CheckBox.__index = CheckBox
|
||||
|
||||
---@property checked boolean Whether checkbox is checked
|
||||
CheckBox.defineProperty(CheckBox, "checked", {default = false, type = "boolean", canTriggerRender = true})
|
||||
---@property text string empty Text to display
|
||||
CheckBox.defineProperty(CheckBox, "text", {default = " ", type = "string", canTriggerRender = true, setter=function(self, value)
|
||||
local checkedText = self.get("checkedText")
|
||||
local width = math.max(#value, #checkedText)
|
||||
if(self.get("autoSize"))then
|
||||
self.set("width", width)
|
||||
end
|
||||
return value
|
||||
end})
|
||||
---@property checkedText string Text when checked
|
||||
CheckBox.defineProperty(CheckBox, "checkedText", {default = "x", type = "string", canTriggerRender = true, setter=function(self, value)
|
||||
local text = self.get("text")
|
||||
local width = math.max(#value, #text)
|
||||
if(self.get("autoSize"))then
|
||||
self.set("width", width)
|
||||
end
|
||||
return value
|
||||
end})
|
||||
---@property autoSize boolean true Whether to automatically size the checkbox
|
||||
CheckBox.defineProperty(CheckBox, "autoSize", {default = true, type = "boolean"})
|
||||
|
||||
CheckBox.defineEvent(CheckBox, "mouse_click")
|
||||
CheckBox.defineEvent(CheckBox, "mouse_up")
|
||||
|
||||
--- @shortDescription Creates a new CheckBox instance
|
||||
--- @return CheckBox self The created instance
|
||||
--- @protected
|
||||
function CheckBox.new()
|
||||
local self = setmetatable({}, CheckBox):__init()
|
||||
self.class = CheckBox
|
||||
self.set("backgroundEnabled", false)
|
||||
return self
|
||||
end
|
||||
|
||||
--- @shortDescription Initializes the CheckBox instance
|
||||
--- @param props table The properties to initialize the element with
|
||||
--- @param basalt table The basalt instance
|
||||
--- @protected
|
||||
function CheckBox:init(props, basalt)
|
||||
VisualElement.init(self, props, basalt)
|
||||
self.set("type", "CheckBox")
|
||||
end
|
||||
|
||||
--- @shortDescription Handles mouse click events
|
||||
--- @param button number The button that was clicked
|
||||
--- @param x number The x position of the click
|
||||
--- @param y number The y position of the click
|
||||
--- @return boolean Clicked Whether the event was handled
|
||||
--- @protected
|
||||
function CheckBox:mouse_click(button, x, y)
|
||||
if VisualElement.mouse_click(self, button, x, y) then
|
||||
self.set("checked", not self.get("checked"))
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
--- @shortDescription Renders the CheckBox
|
||||
--- @protected
|
||||
function CheckBox:render()
|
||||
VisualElement.render(self)
|
||||
|
||||
local checked = self.get("checked")
|
||||
local defaultText = self.get("text")
|
||||
local checkedText = self.get("checkedText")
|
||||
local text = string.sub(checked and checkedText or defaultText, 1, self.get("width"))
|
||||
|
||||
self:textFg(1, 1, text, self.get("foreground"))
|
||||
end
|
||||
|
||||
return CheckBox
|
||||
Reference in New Issue
Block a user