This commit is contained in:
Robert Jelic
2025-02-10 06:23:21 +01:00
parent 0cd0112092
commit c2e8d09f10
8 changed files with 370 additions and 404 deletions

View File

@@ -1,12 +1,22 @@
local PropertySystem = require("propertySystem") -- muss geändert werden.
local PropertySystem = require("propertySystem")
--- The base class for all UI elements in Basalt
-- @module BaseElement
local BaseElement = setmetatable({}, PropertySystem)
BaseElement.__index = BaseElement
BaseElement._events = {}
--- @property type string BaseElement The type identifier of the element
BaseElement.defineProperty(BaseElement, "type", {default = "BaseElement", type = "string"})
--- @property eventCallbacks table {} Table containing all registered event callbacks
BaseElement.defineProperty(BaseElement, "eventCallbacks", {default = {}, type = "table"})
--- Creates a new BaseElement instance
--- @param id string The unique identifier for this element
--- @param basalt table The basalt instance
--- @return table The newly created BaseElement instance
--- @usage local element = BaseElement.new("myId", basalt)
function BaseElement.new(id, basalt)
local self = setmetatable({}, BaseElement):__init()
self:init(id, basalt)
@@ -14,6 +24,10 @@ function BaseElement.new(id, basalt)
return self
end
--- Initializes the BaseElement instance
--- @param id string The unique identifier for this element
--- @param basalt table The basalt instance
--- @return table self The initialized instance
function BaseElement:init(id, basalt)
self.id = id
self.basalt = basalt
@@ -32,6 +46,10 @@ function BaseElement:init(id, basalt)
return self
end
--- Registers an event that this class can listen to
--- @param class table The class to add the event to
--- @param eventName string The name of the event to register
--- @usage BaseElement.listenTo(MyClass, "mouse_click")
function BaseElement.listenTo(class, eventName)
if not class._events then
class._events = {}
@@ -39,6 +57,11 @@ function BaseElement.listenTo(class, eventName)
class._events[eventName] = true
end
--- Enables or disables event listening for a specific event
--- @param eventName string The name of the event to listen for
--- @param enable? boolean Whether to enable or disable the event (default: true)
--- @return table self The BaseElement instance
--- @usage element:listenEvent("mouse_click", true)
function BaseElement:listenEvent(eventName, enable)
enable = enable ~= false
if enable ~= (self._registeredEvents[eventName] or false) then
@@ -57,6 +80,11 @@ function BaseElement:listenEvent(eventName, enable)
return self
end
--- Registers a callback function for an event
--- @param event string The event to register the callback for
--- @param callback function The callback function to register
--- @return table self The BaseElement instance
--- @usage element:registerCallback("mouse_click", function(self, ...) end)
function BaseElement:registerCallback(event, callback)
if not self._registeredEvents[event] then
self:listenEvent(event, true)
@@ -70,6 +98,11 @@ function BaseElement:registerCallback(event, callback)
return self
end
--- Triggers an event and calls all registered callbacks
--- @param event string The event to fire
--- @param ... any Additional arguments to pass to the callbacks
--- @return table self The BaseElement instance
--- @usage element:fireEvent("mouse_click", 1, 2)
function BaseElement:fireEvent(event, ...)
if self._values.eventCallbacks[event] then
for _, callback in ipairs(self._values.eventCallbacks[event]) do
@@ -80,6 +113,8 @@ function BaseElement:fireEvent(event, ...)
return self
end
--- Requests a render update for this element
--- @usage element:updateRender()
function BaseElement:updateRender()
if(self.parent) then
self.parent:updateRender()

View File

@@ -1,25 +1,12 @@
--- Basalt UI Framework main module.
-- This is the main entry point for the Basalt UI Framework.
-- It provides functions for creating and managing UI elements and handling events.
-- @module basalt
-- @usage
-- local basalt = require("basalt")
-- local mainFrame = basalt.createFrame()
-- mainFrame:show()
-- basalt.run()
local elementManager = require("elementManager")
local expect = require("libraries/expect")
local errorManager = require("errorManager")
--- The main Basalt instance
-- Contains all core functionality and management functions
-- @type Basalt
-- @field traceback boolean Enable/disable error tracing
-- @field _events table Internal events storage
-- @field _schedule table Internal schedule storage
-- @field _plugins table Plugins storage
-- @field LOGGER table Logging instance
--- This is the UI Manager and the starting point for your project. The following functions allow you to influence the default behavior of Basalt.
---
--- Before you can access Basalt, you need to add the following code on top of your file:
--- @usage local basalt = require("basalt")
--- What this code does is it loads basalt into the project, and you can access it by using the variable defined as "basalt".
-- @module Basalt
local basalt = {}
basalt.traceback = true
basalt._events = {}
@@ -31,6 +18,7 @@ local mainFrame = nil
local updaterActive = false
--- Creates and returns a new UI element of the specified type
--- @shortDescription Creates a new UI element
--- @param type string The type of element to create (e.g. "Button", "Label", "BaseFrame")
--- @param id? string Optional unique identifier for the element
--- @return table element The created element instance
@@ -96,8 +84,7 @@ function basalt.removeSchedule(id)
basalt._schedule[id] = nil
end
--- Internal event handler
--- @local
--- @local Internal event handler
local function updateEvent(event, ...)
if(event=="terminate")then basalt.stop() end
@@ -120,8 +107,7 @@ local function updateEvent(event, ...)
end
end
--- Internal render function
--- @local
--- @local Internal render function
local function renderFrames()
if(mainFrame)then
mainFrame:render()