Added basalt.getElementClass(name)

Added Graph
Added List:getSelectedItem()
Fixed Image:addFrame()
This commit is contained in:
Robert Jelic
2025-03-03 17:38:30 +01:00
parent 21f16996e7
commit 373bd7b485
4 changed files with 91 additions and 4 deletions

64
src/elements/Graph.lua Normal file
View File

@@ -0,0 +1,64 @@
local elementManager = require("elementManager")
local VisualElement = elementManager.getElement("elements/VisualElement")
---@configDescription
---@configDefault false
---@class Graph : VisualElement
local Graph = setmetatable({}, VisualElement)
Graph.__index = Graph
Graph.defineProperty(Graph, "data", {default = {}, type = "table", canTriggerRender = true})
Graph.defineProperty(Graph, "minValue", {default = 0, type = "number", canTriggerRender = true})
Graph.defineProperty(Graph, "maxValue", {default = 100, type = "number", canTriggerRender = true})
Graph.defineProperty(Graph, "graphColor", {default = colors.yellow, type = "color", canTriggerRender = true})
Graph.defineProperty(Graph, "graphSymbol", {default = "\127", type = "string", canTriggerRender = true}) -- Default: "|"
function Graph.new()
local self = setmetatable({}, Graph):__init()
return self
end
function Graph:init(props, basalt)
VisualElement.init(self, props, basalt)
self.set("type", "Graph")
return self
end
function Graph:setPoint(index, value)
local data = self.get("data")
data[index] = value
self:updateRender()
end
function Graph:addPoint(value)
local data = self.get("data")
table.insert(data, value)
while #data > self.get("width") do
table.remove(data, 1)
end
self:updateRender()
end
function Graph:render()
VisualElement.render(self)
local data = self.get("data")
local width = self.get("width")
local height = self.get("height")
local minVal = self.get("minValue")
local maxVal = self.get("maxValue")
local symbol = self.get("graphSymbol")
local graphColor = self.get("graphColor")
for x = 1, width do
if data[x] then
local normalizedValue = (data[x] - minVal) / (maxVal - minVal)
local y = math.floor(height - (normalizedValue * (height-1)))
y = math.max(1, math.min(y, height))
self:textFg(x, y, symbol, graphColor)
end
end
end
return Graph

View File

@@ -325,11 +325,13 @@ end
--- @return Image self The Image instance
function Image:addFrame()
local frames = self.get("bimg")
local width = frames.width or #frames[1][1][1]
local height = frames.height or #frames[1]
local frame = {}
local text = string.rep(" ", self.get("width"))
local fg = string.rep("f", self.get("width"))
local bg = string.rep("0", self.get("width"))
for y = 1, self.get("height") do
local text = string.rep(" ", width)
local fg = string.rep("f", width)
local bg = string.rep("0", width)
for y = 1, height do
frame[y] = {text, fg, bg}
end
table.insert(frames, frame)

View File

@@ -99,6 +99,19 @@ function List:getSelectedItems()
return selected
end
--- Gets first selected item
--- @shortDescription Gets first selected item
--- @return table? selected The first item
function List:getSelectedItem()
local items = self.get("items")
for i, item in ipairs(items) do
if type(item) == "table" and item.selected then
return item
end
end
return nil
end
--- @shortDescription Handles mouse click events
--- @param button number The mouse button that was clicked
--- @param x number The x-coordinate of the click

View File

@@ -244,6 +244,14 @@ function basalt.run(isActive)
end
end
--- Returns an element class
--- @shortDescription Returns an element class
--- @param name string The name of the element
--- @return table Element The element class
function basalt.getElementClass(name)
return elementManager.getElement(name)
end
--- Returns a Plugin API
--- @shortDescription Returns a Plugin API
--- @param name string The name of the plugin