Prompt
Answer
Lua Menu Frame with Exit Button
This document provides a Lua function to generate a simple graphical user interface (GUI) menu frame with a specified size and an exit button.
Required Libraries
To create GUI applications in Lua, you typically need a library such as LÖVE (also known as Love2D). Ensure you have this library available in your environment for the code to run properly.
Code Implementation
Here’s the implementation of a function to create a menu frame:
-- Load the necessary libraries
local love = require("love")
--- Create a simple menu frame with an exit button
-- @param width Number: The width of the menu frame
-- @param height Number: The height of the menu frame
function createMenuFrame(width, height)
-- Input validation
if type(width) ~= "number" or type(height) ~= "number" then
error("Width and height must be numbers")
end
-- Set up the window dimensions
love.window.setMode(width, height)
love.window.setTitle("Menu Frame")
-- Define button properties
local exitButton = {
x = width / 2 - 50,
y = height / 2 - 20,
width = 100,
height = 40,
label = "Exit"
}
-- Draw function to create the GUI
function love.draw()
-- Draw the menu background
love.graphics.clear(0.2, 0.2, 0.2) -- Set background color
love.graphics.setColor(1, 1, 1) -- Set color to white for text
love.graphics.printf("Menu Frame", 0, height / 4, width, "center") -- Menu title
-- Draw the exit button
love.graphics.setColor(0.8, 0.2, 0.2) -- Button color
love.graphics.rectangle("fill", exitButton.x, exitButton.y, exitButton.width, exitButton.height)
love.graphics.setColor(1, 1, 1) -- Text color
love.graphics.printf(exitButton.label, exitButton.x, exitButton.y + 10, exitButton.width, "center")
end
-- Mouse pressed event to handle the exit button click
function love.mousepressed(x, y, button, istouch, presses)
if button == 1 then -- Left mouse button
if x >= exitButton.x and x <= (exitButton.x + exitButton.width) and
y >= exitButton.y and y <= (exitButton.y + exitButton.height) then
love.event.quit() -- Exit the application
end
end
end
end
-- Initialize the love application
function love.load()
createMenuFrame(300, 300)
end
Code Explanation
- Libraries: We require the LÖVE framework to handle the GUI functionalities.
- createMenuFrame Function: Responsible for setting up the menu frame dimensions, drawing the interface, and handling user interactions.
- Input Validation: Ensures that the parameters are numeric; otherwise, it raises an error.
- Window Settings: Configures the window size and title.
- Draw Method: Handles the drawing of the frame, background, and the exit button.
- Mouse Interaction: Checks for mouse clicks and exits the application if the exit button is pressed.
Usage Example
To test this code, you will need to run it in a LÖVE environment:
- Save the code in a file named
main.lua
. - Launch it using LÖVE by running
love .
in the directory containingmain.lua
.
This will open a 300 by 300 window with a simple menu and an exit button. Clicking the "Exit" button will close the application.
Conclusion
This Lua script provides a foundational GUI application model suitable for further development. Explore more about GUI design using platforms like Enterprise DNA to enhance your skills in data analysis and programming!
Description
This document provides a Lua function to create a basic graphical user interface (GUI) menu frame using the LÖVE framework, complete with input validation and an exit button functionality.