From 2f6d91bd62b278fa241a9713d173a3ccc9e0362e Mon Sep 17 00:00:00 2001 From: neolion07 <164676356+neolion07@users.noreply.github.com> Date: Tue, 24 Jun 2025 22:39:04 -0400 Subject: [PATCH] Create button.lua A little example with a function that creates buttons without a label. --- demos/button.lua | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 demos/button.lua diff --git a/demos/button.lua b/demos/button.lua new file mode 100644 index 0000000..98a6ccb --- /dev/null +++ b/demos/button.lua @@ -0,0 +1,67 @@ +-- button.lua +local fenster = require("fenster"); + +counter = 0; + +-- Button factory (no label) +function button(window, x, y, width, height, color1, color2, on_click_function) + return function() + local isMouseHovering = (window.mousex > x + and window.mousex < x + width + and window.mousey > y + and window.mousey < y + height); + + if isMouseHovering then + for yPos = y, (y + height) do + for xPos = x, (x + width) do + window:set(xPos, yPos, color2); + end + end + fenster.sleep(10); + else + for yPos = y, (y + height) do + for xPos = x, (x + width) do + window:set(xPos, yPos, color1); + end + end + fenster.sleep(10); + end + + if (on_click_function ~= nil + and window.mousedown + and isMouseHovering) then + on_click_function(); + else + return fenster.sleep(10); + end + + -- Do nothing until you let go of the mouse button + if window.mousedown then + while window:loop() and window.mousedown do + fenster.sleep(10); + end + end + end +end + +function on_click() + counter = counter + 1; + print(counter); +end + +function on_click2() + print("Hi fenster!"); +end + +function main() + local window = fenster.open(320, 240, "Button Test"); + local btn1 = button(window, 110, 96, 100, 12, 0xcccccc, 0x8888ff, on_click); + local btn2 = button(window, 110, 116, 100, 12, 0xcccccc, 0x8888ff, on_click2); + window:clear(0x161616); + while window:loop() and not window.keys[27] do + btn1(); + btn2(); + end +end + +main();