Added menu resource and menu node

This commit is contained in:
2ndbeam 2024-05-18 02:00:41 +03:00
commit 1fa10004b3
4 changed files with 126 additions and 1 deletions

View file

@ -0,0 +1,37 @@
extends Node
@onready var actions: Array[Button] = [
$Action1,
$Action2,
$Action3,
$Action4,
$Action5,
$Action6,
$Action7,
$Action8
]
@export var menu: Menu
func _ready():
load_menu()
## Called when menu is changed
func load_menu():
for i in range(len(menu.item_ids)):
if actions[i] is TransitButton:
actions[i].button_up.disconnect(transit_menu)
match menu.item_actions[i]:
Menu.Action.TransitAction:
actions[i].set_script("res://scripts/Classes/transit_button.gd")
actions[i].text = menu.item_ids[i]
Menu.Action.ButtonAction:
actions[i].set_script(menu.item_data[i])
actions[i].text = menu.item_ids[i]
## Called with transit_button, changes current menu
func transit_menu(id: int):
assert(id < len(menu.item_ids) and id > 0)
var new_menu = menu.item_data[id].duplicate()
menu = new_menu
load_menu()

24
scripts/Classes/menu.gd Normal file
View file

@ -0,0 +1,24 @@
extends Resource
## Represents menu with actions, each action should map to a string id and data (if needed)
class_name Menu
enum Action {
## Represents when this menu should be replaced with another.
## Attach Menu next to it in item_data
TransitAction,
## Represents scripts which should be attached to button on menu startup
## Attach Script next to it in item_data
ButtonAction,
## WIP, should represent LineEdit or some other input control
InputAction,
## WIP
OtherAction
}
## Action string IDs. Should have same size as item_actions
@export var item_ids: Array[String]
## Action type. Should have same size as item_ids
@export var item_actions: Array[Action]
## Should have same size as other arrays
@export var item_data: Array

View file

@ -0,0 +1,10 @@
extends Button
## Represents button which changes menu resource on parent Menu
class_name TransitButton
## This is used when button_up is activated
var id: int = -1
func _ready():
button_up.connect(get_parent().transit_menu.bind(id))