Hopson97 / open-builder

Open "Minecraft-like" game with multiplayer support and Lua scripting support for the both client and server
https://github.com/Hopson97/open-builder/projects/3
GNU General Public License v3.0
703 stars 80 forks source link

Feature/lua gui v3 #163

Closed Hopson97 closed 4 years ago

Hopson97 commented 4 years ago

Finally managed to get some kind of API for creating menus etc through the Lua code.

The way it works is using a few objects as created as part of this PR

  1. Overlay

This is the main thing that contains all of the GUI widgets, and also contains the info about the primitive types that make up the GUI.

Examples of overlays would be the main menu, the settings menu, the player's inventory, and the player's HUD (crosshair, healthbar etc)

  1. OverlayStack

This contains all the current overlays that are being displayed. For example, during gameplay it would store just the HUD, but if the player opens the inventory then it would contain the HUD UI, and also the inventory UI

  1. OverlayDefintion

These are created in the in the Lua code, and are tables that contain a "GUI ID" and a Lua function for creation of the GUI

  1. OverlayFactory

This stores all the OverlayDefintion tables created in the Lua.

It also has an operation for creating a new overlay, which you pass in a "GUI ID" such as "main_menu", and it would look up for the OverlayDefintion with that ID, create the overlay using the lua function, before returning to caller.

  1. GUIRenderer

Does what it says on the tin.

Iterates the OverlayStack, rendering all the GUIs

Etc etc etc

This took a long time and idk, end of the day its just a thing that allows an api in the lua to create menus and such as described below:

I mean the idea is

  1. Define a gui

    game.gui.addGui{
    id = "menu_thing",
    title = "Main Menu",
    create = onCreate,
    }
  2. Create a function to make the GUI

    local function onCreate(gui)
    local button = gui:addButton()
    button.size = GuiDim.new(0, 50, 0, 50)
    button.position = GuiDim.new(0, 50, 0, 50)
    button.text = "Click me!"
    button.textSize = 40
    button.onClick = function() print("Hello world!") end
    end
  3. Add the GUI to the game when needed

game.gui.push("menu_thing")