Page 1 of 1

[11.2] Litte Menu Engine

Posted: Tue Jul 17, 2018 8:40 pm
by Astorek86
Hello @ all,

I've written a little Menu Engine and thought, it can be useful for others. So I share it here^^.

With this Menu Engine, it is possible to create a *dun dun duuun* Menu, that contains Entries which can be navigated & selected through Keyboard (WASD, Arrow-Keys, NumPad) and Mouse.

I've opened a github-Page for this (including a so-called API-Reference), which can be found here:
https://github.com/Astorek86/love2d-menuengine

Example Screenshot:
Image

Sourcecode for this Example:

Code: Select all

local menuengine = require "menuengine"

local text = "Nothing was selected."

-- Mainmenu
local mainmenu

-- Start Game
local function start_game()
    text = "Start Game was selected!"
end

-- Options
local function options()
    text = "Options was selected!"
end

-- Quit Game
local function quit_game()
    text = "Quit Game was selected!"
end

 -- ----------

function love.load()
    love.window.setMode(600,400)
    love.graphics.setFont(love.graphics.newFont(20))

    mainmenu = menuengine.new(200,100)
    mainmenu:addEntry("Start Game", start_game)
    mainmenu:addEntry("Options", options)
    mainmenu:addSep()
    mainmenu:addEntry("Quit Game", quit_game)
end

function love.update(dt)
    mainmenu:update()
end

function love.draw()
    love.graphics.clear()
    love.graphics.print(text)
    mainmenu:draw()
end

function love.keypressed(key, scancode, isrepeat)
    menuengine.keypressed(scancode)

    if scancode == "escape" then
        love.event.quit()
    end
end

function love.mousemoved(x, y, dx, dy, istouch)
    menuengine.mousemoved(x, y)
end
More Screens
- Supports different Colors:
Image

- Different Fonts, Sizes...
Image

- ...and Positions for every Entry
Image

- It can handle with LÖVE's scaling-Options:
Image

- It works with Sound-Objects: One for Moving through the Menu, one for Selecting an Entry. Every Entry can have it's own Sound-Object.
- You can hide and unhide Entries on-the-fly as you wish.

Alle Examples can be found in the menu-examples.zip.

Feel free to comment, and if you found bugs, please tell it :) .

Re: [11.1] Litte Menu Engine

Posted: Wed Jul 18, 2018 1:38 pm
by Мэтю
Really cool and simple, nice work.
Just took a brief look at your code, and I saw you made the table menuengine global. What are the reasons about this choice?
Actually I'm not a very good programmer, I'm just used to use local variables almost all the time.
I'm going to use your library and give a more consistent feedback later :)

Re: [11.1] Litte Menu Engine

Posted: Thu Jul 19, 2018 2:02 pm
by Astorek86
Thanks. :)
Мэтю wrote: Wed Jul 18, 2018 1:38 pm Just took a brief look at your code, and I saw you made the table menuengine global. What are the reasons about this choice?
Actually I'm not a very good programmer, I'm just used to use local variables almost all the time.
To be honest, I didn't fully understand how global and/or local Variable works in Lua (I'm not a beginner in Programming, but I am a Beginner in Lua^^). I've just updated a new version which doesn't use a global menuengine anymore^^. (I also replaced the Attachments on the 1st Post)
I'm going to use your library and give a more consistent feedback later :)
That's great, looking forward to hear about it. :)

Re: [11.1] Litte Menu Engine

Posted: Tue Jul 24, 2018 2:02 am
by Мэтю
Quick update:
I used it briefly, and something I missed was passing an argument to the entry function. So, I made a pull request to your repository on github
You might review if it's suitable for your library xD

Re: [11.1] Litte Menu Engine

Posted: Thu Jul 26, 2018 9:37 pm
by Astorek86
That's a good idea, I've just updated Github + Attachments; now it supports Arguments.

Code: Select all

-- before:
menu:addEntry(text, [func], [font], [colorNormal], [colorSelected])
-- now:
menu:addEntry(text, [func], [args], [font], [colorNormal], [colorSelected])

Thank you very much :) . I've totally forgotten that use case^^...

Re: [11.1] Litte Menu Engine

Posted: Mon Jan 07, 2019 6:26 pm
by coolphill
This menu engine is nice. I've been making my own now for a while. I'm kind of shock how you got the the feel and look I was trying to achieve with mine.

Re: [11.1] Litte Menu Engine

Posted: Mon Jan 07, 2019 11:29 pm
by 4vZEROv
You don't have to declare local i / local k , v when you do

for i = 0, 10 do ... end
&
for k,v in pairs(tbl) do ... end