deploy: 1c90c6cf04
This commit is contained in:
@@ -1,135 +1,44 @@
|
||||
local VisualElement = require("elements/VisualElement")
|
||||
# List : VisualElement
|
||||
|
||||
---@class List : VisualElement
|
||||
local List = setmetatable({}, VisualElement)
|
||||
List.__index = List
|
||||
## Properties
|
||||
|
||||
---@property items table List of items to display
|
||||
List.defineProperty(List, "items", {default = {}, type = "table", canTriggerRender = true})
|
||||
---@property selectedIndex number Currently selected item index
|
||||
List.defineProperty(List, "selectedIndex", {default = 0, type = "number", canTriggerRender = true})
|
||||
---@property selectable boolean Whether items can be selected
|
||||
List.defineProperty(List, "selectable", {default = true, type = "boolean"})
|
||||
---@property offset number Scrolling offset
|
||||
List.defineProperty(List, "offset", {default = 0, type = "number", canTriggerRender = true})
|
||||
---@property selectedColor color Color for selected item
|
||||
List.defineProperty(List, "selectedColor", {default = colors.blue, type = "number"})
|
||||
|Property|Type|Default|Description|
|
||||
|---|---|---|---|
|
||||
|items|table|List|of items to display
|
||||
|selectedIndex|number|Currently|selected item index
|
||||
|selectable|boolean|Whether|items can be selected
|
||||
|offset|number|Scrolling|offset
|
||||
|selectedColor|color|Color|for selected item
|
||||
|
||||
List.listenTo(List, "mouse_click")
|
||||
List.listenTo(List, "mouse_scroll")
|
||||
## Functions
|
||||
|
||||
function List.new()
|
||||
local self = setmetatable({}, List):__init()
|
||||
self.set("width", 16)
|
||||
self.set("height", 8)
|
||||
self.set("background", colors.gray)
|
||||
return self
|
||||
end
|
||||
|Method|Returns|Description|
|
||||
|---|---|---|
|
||||
|[List.new](#List.new)|-|
|
||||
|[List:addItem](#List:addItem)|-|
|
||||
|[List:clear](#List:clear)|-|
|
||||
|[List:init](#List:init)|-|
|
||||
|[List:mouse_click](#List:mouse_click)|-|
|
||||
|[List:mouse_scroll](#List:mouse_scroll)|-|
|
||||
|[List:onSelect](#List:onSelect)|-|
|
||||
|[List:removeItem](#List:removeItem)|-|
|
||||
|[List:render](#List:render)|-|
|
||||
|
||||
function List:init(props, basalt)
|
||||
VisualElement.init(self, props, basalt)
|
||||
self.set("type", "List")
|
||||
end
|
||||
## List.new()
|
||||
|
||||
function List:addItem(text)
|
||||
local items = self.get("items")
|
||||
table.insert(items, text)
|
||||
self:updateRender()
|
||||
return self
|
||||
end
|
||||
## List:addItem()
|
||||
|
||||
function List:removeItem(index)
|
||||
local items = self.get("items")
|
||||
table.remove(items, index)
|
||||
self:updateRender()
|
||||
return self
|
||||
end
|
||||
## List:clear()
|
||||
|
||||
function List:clear()
|
||||
self.set("items", {})
|
||||
self.set("selectedIndex", 0)
|
||||
self:updateRender()
|
||||
return self
|
||||
end
|
||||
## List:init()
|
||||
|
||||
function List:mouse_click(button, x, y)
|
||||
if button == 1 and self:isInBounds(x, y) and self.get("selectable") then
|
||||
local _, index = self:getRelativePosition(x, y)
|
||||
|
||||
local adjustedIndex = index + self.get("offset")
|
||||
local items = self.get("items")
|
||||
## List:mouse_click()
|
||||
|
||||
if adjustedIndex <= #items then
|
||||
local item = items[adjustedIndex]
|
||||
self.set("selectedIndex", adjustedIndex)
|
||||
## List:mouse_scroll()
|
||||
|
||||
if type(item) == "table" and item.callback then
|
||||
item.callback(self)
|
||||
end
|
||||
## List:onSelect()
|
||||
|
||||
self:fireEvent("select", adjustedIndex, item)
|
||||
self:updateRender()
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
## List:removeItem()
|
||||
|
||||
function List:mouse_scroll(direction, x, y)
|
||||
if self:isInBounds(x, y) then
|
||||
local offset = self.get("offset")
|
||||
local maxOffset = math.max(0, #self.get("items") - self.get("height"))
|
||||
|
||||
offset = math.min(maxOffset, math.max(0, offset + direction))
|
||||
self.set("offset", offset)
|
||||
return true
|
||||
end
|
||||
end
|
||||
## List:render()
|
||||
|
||||
function List:onSelect(callback)
|
||||
self:registerCallback("select", callback)
|
||||
return self
|
||||
end
|
||||
|
||||
function List:render()
|
||||
VisualElement.render(self)
|
||||
|
||||
local items = self.get("items")
|
||||
local height = self.get("height")
|
||||
local offset = self.get("offset")
|
||||
local selected = self.get("selectedIndex")
|
||||
local width = self.get("width")
|
||||
|
||||
for i = 1, height do
|
||||
local itemIndex = i + offset
|
||||
local item = items[itemIndex]
|
||||
|
||||
if item then
|
||||
if type(item) == "table" and item.separator then
|
||||
local separatorChar = (item.text or "-"):sub(1,1)
|
||||
local separatorText = string.rep(separatorChar, width)
|
||||
local fg = item.foreground or self.get("foreground")
|
||||
local bg = item.background or self.get("background")
|
||||
|
||||
self:textBg(1, i, string.rep(" ", width), bg)
|
||||
self:textFg(1, i, separatorText, fg)
|
||||
else
|
||||
local text = type(item) == "table" and item.text or item
|
||||
local isSelected = itemIndex == selected
|
||||
|
||||
local bg = isSelected and
|
||||
(item.selectedBackground or self.get("selectedColor")) or
|
||||
(item.background or self.get("background"))
|
||||
|
||||
local fg = isSelected and
|
||||
(item.selectedForeground or colors.white) or
|
||||
(item.foreground or self.get("foreground"))
|
||||
|
||||
self:textBg(1, i, string.rep(" ", width), bg)
|
||||
self:textFg(1, i, text, fg)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return List
|
||||
|
||||
Reference in New Issue
Block a user