82 lines
2.0 KiB
Lua
82 lines
2.0 KiB
Lua
-- annotation.lua contains EmmyLua annotations for cpp_function & cpp_variable\
|
|
|
|
function create_button(x, y, width, height, text)
|
|
return {
|
|
x = x,
|
|
y = y,
|
|
width = width,
|
|
height = height,
|
|
text = text,
|
|
hovered = false,
|
|
was_pressed = false,
|
|
onpressed = function() end
|
|
}
|
|
end
|
|
|
|
function draw_button(button)
|
|
if button.hovered then
|
|
Engine:setColor(Color.new(0, 255, 0))
|
|
else
|
|
Engine:setColor(Color.new(255, 0, 0))
|
|
end
|
|
Engine:fillRect(button.x, button.y, button.width, button.height)
|
|
Engine:setColor(Color.new(0, 0, 0))
|
|
Engine:drawText(button.text, button.x + 10, button.y + 10)
|
|
end
|
|
|
|
function is_mouse_in_button(button, x, y)
|
|
return x >= button.x and x <= button.x + button.width and y >= button.y and y <= button.y + button.height
|
|
end
|
|
|
|
testButton = create_button(100, 100, 100, 50, "Test Button")
|
|
|
|
function update_button(button)
|
|
if is_mouse_in_button(button, Engine:getMouseX(), Engine:getMouseY()) then
|
|
button.hovered = true
|
|
if Engine:isMouseLeftDown() and not button.was_pressed then
|
|
button.onpressed()
|
|
button.was_pressed = true
|
|
elseif not Engine:isMouseLeftDown() then
|
|
button.was_pressed = false
|
|
end
|
|
else
|
|
button.hovered = false
|
|
button.was_pressed = false
|
|
end
|
|
end
|
|
|
|
--- the setup function
|
|
--- @return nil
|
|
function setup_window()
|
|
Engine:setTitle("Hello World new")
|
|
Engine:setWidth(800)
|
|
Engine:setHeight(600)
|
|
Engine:setFrameRate(60)
|
|
|
|
testButton.onpressed = function()
|
|
Engine:messageBox("Button Pressed")
|
|
end
|
|
end
|
|
|
|
--- the set_keylist function
|
|
--- @return string
|
|
function set_keylist()
|
|
return "WASD"
|
|
end
|
|
|
|
--- the update function
|
|
--- @param Engine GameEngine # The GameEngine instance.
|
|
--- @return nil
|
|
function update()
|
|
update_button( testButton)
|
|
end
|
|
|
|
|
|
--- the draw function
|
|
--- @return nil
|
|
function draw()
|
|
Engine:setColor(Color.new(0, 0, 0))
|
|
Engine:fillScreen(Color.new(255, 255, 255))
|
|
|
|
draw_button(testButton)
|
|
end |