Skip to content

Commit f10413d

Browse files
committed
Added basalt.onEvent(event, callback)
Added basalt.removeEvent(eventcallback) basalt.triggerEvent(event, ...) Fixed a event is nil error in Container.lua
1 parent d2d5f8c commit f10413d

File tree

5 files changed

+132
-14
lines changed

5 files changed

+132
-14
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ testWorkflows
99
todo.txt
1010
Flexbox2.lua
1111
markdown2.lua
12-
BasaltDoc
12+
BasaltDoc
13+
generate-docs.lua
14+
io.lua

src/elements/Container.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ end
347347

348348
local function convertMousePosition(self, event, ...)
349349
local args = {...}
350-
if event:find("mouse_") then
350+
if event and event:find("mouse_") then
351351
local button, absX, absY = ...
352352
local xOffset, yOffset = self.get("offsetX"), self.get("offsetY")
353353
local relX, relY = self:getRelativePosition(absX + xOffset, absY + yOffset)

src/main.lua

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,62 @@ function basalt.getAPI(name)
374374
return elementManager.getAPI(name)
375375
end
376376

377+
--- Registers a callback function for a specific event
378+
--- @shortDescription Registers an event callback
379+
--- @param eventName string The name of the event to listen for (e.g. "mouse_click", "key", "timer")
380+
--- @param callback function The callback function to execute when the event occurs
381+
--- @usage basalt.onEvent("mouse_click", function(button, x, y) basalt.debug("Clicked at", x, y) end)
382+
function basalt.onEvent(eventName, callback)
383+
expect(1, eventName, "string")
384+
expect(2, callback, "function")
385+
386+
if not basalt._events[eventName] then
387+
basalt._events[eventName] = {}
388+
end
389+
390+
table.insert(basalt._events[eventName], callback)
391+
end
392+
393+
--- Removes a callback function for a specific event
394+
--- @shortDescription Removes an event callback
395+
--- @param eventName string The name of the event
396+
--- @param callback function The callback function to remove
397+
--- @return boolean success Whether the callback was found and removed
398+
function basalt.removeEvent(eventName, callback)
399+
expect(1, eventName, "string")
400+
expect(2, callback, "function")
401+
402+
if not basalt._events[eventName] then
403+
return false
404+
end
405+
406+
for i, registeredCallback in ipairs(basalt._events[eventName]) do
407+
if registeredCallback == callback then
408+
table.remove(basalt._events[eventName], i)
409+
return true
410+
end
411+
end
412+
413+
return false
414+
end
415+
416+
--- Triggers a custom event and calls all registered callbacks
417+
--- @shortDescription Triggers a custom event
418+
--- @param eventName string The name of the event to trigger
419+
--- @vararg any Arguments to pass to the event callbacks
420+
--- @usage basalt.triggerEvent("custom_event", "data1", "data2")
421+
function basalt.triggerEvent(eventName, ...)
422+
expect(1, eventName, "string")
423+
424+
if basalt._events[eventName] then
425+
for _, callback in ipairs(basalt._events[eventName]) do
426+
local ok, err = pcall(callback, ...)
427+
if not ok then
428+
errorManager.header = "Basalt Event Callback Error"
429+
errorManager.error("Error in event callback for '" .. eventName .. "': " .. tostring(err))
430+
end
431+
end
432+
end
433+
end
434+
377435
return basalt

src/plugins/animation.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,27 @@ function Animation:event(event, timerId)
270270
end
271271
end
272272

273+
--- Stops the animation immediately: cancels timers, completes running anim instances and clears the element property
274+
--- @shortDescription Stops the animation
275+
function Animation:stop()
276+
if self.timer then
277+
pcall(os.cancelTimer, self.timer)
278+
self.timer = nil
279+
end
280+
281+
for _, seq in ipairs(self.sequences) do
282+
for _, anim in ipairs(seq) do
283+
pcall(function()
284+
if anim and anim.complete then anim:complete() end
285+
end)
286+
end
287+
end
288+
289+
if self.element and type(self.element.set) == "function" then
290+
pcall(function() self.element.set("animation", nil) end)
291+
end
292+
end
293+
273294
Animation.registerAnimation("move", {
274295
start = function(anim)
275296
anim.startX = anim.element.get("x")
@@ -519,6 +540,18 @@ function VisualElement.setup(element)
519540
element.defineEvent(element, "timer")
520541
end
521542

543+
-- Convenience to stop animations from the element
544+
function VisualElement.stopAnimation(self)
545+
local anim = self.get("animation")
546+
if anim and type(anim.stop) == "function" then
547+
anim:stop()
548+
else
549+
-- fallback: clear property
550+
self.set("animation", nil)
551+
end
552+
return self
553+
end
554+
522555
--- Creates a new Animation Object
523556
--- @shortDescription Creates a new animation
524557
--- @return Animation animation The new animation

tools/generate-docs.lua

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
1-
local markdown = require("tools/markdown")
21

3-
local function ensureDirectory(path)
4-
local dir = path:match("(.*)/[^/]*$")
5-
if dir then
6-
os.execute('mkdir -p "' .. dir .. '"')
2+
local oldPath = package.path
3+
4+
local scriptSource = debug.getinfo(1, "S").source
5+
local scriptDir = nil
6+
if scriptSource and scriptSource:sub(1,1) == "@" then
7+
local scriptPath = scriptSource:sub(2)
8+
scriptDir = scriptPath:match("^(.*)[/\\]") or "."
9+
else
10+
scriptDir = "."
11+
end
12+
13+
local parserSrc = scriptDir .. "/BasaltDoc/ldoc-markdown-parser/src/"
14+
package.path = package.path .. ";" .. parserSrc .. "?.lua;" .. parserSrc .. "?/init.lua"
15+
local ok, parser = pcall(require, "parser.init")
16+
local ioAdaptor = require("tools.io")
17+
package.path = oldPath
18+
19+
if not ok or not parser then
20+
-- try dofile fallback
21+
local initPath = parserSrc .. "/parser/init.lua"
22+
local ok2, module = pcall(dofile, initPath)
23+
if ok2 and module then
24+
parser = module
25+
else
26+
error("Failed to load parser.init via require and dofile (tried: "..tostring(initPath)..")")
727
end
828
end
929

1030
local function processFile(inputFile)
11-
local parsed = markdown.parseFile(inputFile)
12-
local md = markdown.makeMarkdown(parsed)
13-
14-
local outputFile = "build_docs/docs/references/" .. inputFile:match("^src/(.+)"):gsub("%.lua$", "")
31+
local content = ioAdaptor.readFile(inputFile)
32+
if not content then
33+
io.stderr:write("Failed to read: " .. tostring(inputFile) .. "\n")
34+
return
35+
end
1536

16-
ensureDirectory(outputFile)
37+
local commentBlocks = parser.extractComments(content)
38+
local md = parser.generateMarkdown(commentBlocks)
1739

18-
markdown.saveToFile(outputFile, md)
40+
local outputFile = "build_docs/docs/references/" .. inputFile:match("^src/(.+)"):gsub("%.lua$", "")
41+
ioAdaptor.ensureDirectory(outputFile)
42+
ioAdaptor.writeFile(outputFile .. ".md", md)
1943
end
2044

21-
for file in io.popen('find "src" -type f -name "*.lua"'):lines() do
45+
local files = ioAdaptor.listFiles("src", "*.lua")
46+
for _, file in ipairs(files) do
2247
if not file:match("LuaLS.lua$") then
2348
processFile(file)
2449
end

0 commit comments

Comments
 (0)