Created message class and updated menu system

This commit is contained in:
2ndbeam 2024-05-19 16:09:50 +03:00
commit 7134891e55
12 changed files with 62 additions and 13 deletions

View file

@ -15,5 +15,5 @@ item = &"res://scripts/Base/Menu/test.gd"
[resource] [resource]
script = ExtResource("3_ow6va") script = ExtResource("3_ow6va")
item_ids = Array[String](["BASE_TEST_TRANSIT", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BUTTON", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BLANK"]) item_ids = Array[String](["BASE_TEST_TRANSIT", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BUTTON", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BLANK"])
item_actions = Array[int]([0, 3, 3, 3, 1, 3, 3, 3]) item_actions = Array[int]([1, 0, 0, 0, 2, 0, 0, 0])
item_data = Array[Resource("res://scripts/Classes/Menu/menu_resource.gd")]([SubResource("Resource_jjcgb"), null, null, null, SubResource("Resource_b58fs"), null, null, null]) item_data = Array[Resource("res://scripts/Classes/Menu/menu_resource.gd")]([SubResource("Resource_jjcgb"), null, null, null, SubResource("Resource_b58fs"), null, null, null])

View file

@ -15,5 +15,5 @@ item = &"res://menus/Base Menu/test_menu_1.tres"
[resource] [resource]
script = ExtResource("3_frt6d") script = ExtResource("3_frt6d")
item_ids = Array[String](["BASE_TEST_BLANK", "BASE_TEST_BUTTON", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_TRANSIT", "BASE_TEST_BLANK", "BASE_TEST_BLANK"]) item_ids = Array[String](["BASE_TEST_BLANK", "BASE_TEST_BUTTON", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_BLANK", "BASE_TEST_TRANSIT", "BASE_TEST_BLANK", "BASE_TEST_BLANK"])
item_actions = Array[int]([3, 1, 3, 3, 3, 0, 3, 3]) item_actions = Array[int]([0, 2, 0, 0, 0, 1, 0, 0])
item_data = Array[Resource("res://scripts/Classes/Menu/menu_resource.gd")]([null, SubResource("Resource_dbknc"), null, null, null, SubResource("Resource_rxr54"), null, null]) item_data = Array[Resource("res://scripts/Classes/Menu/menu_resource.gd")]([null, SubResource("Resource_dbknc"), null, null, null, SubResource("Resource_rxr54"), null, null])

View file

@ -0,0 +1,7 @@
extends MenuAction
## Message which will be send on click
var msg: Message
func action():
get_parent().transit_menu(id)

View file

@ -1,5 +1,6 @@
extends Node extends Node
## Array of menu actions represented as buttons
@onready var actions: Array[Button] = [ @onready var actions: Array[Button] = [
$Action1, $Action1,
$Action2, $Action2,
@ -11,6 +12,7 @@ extends Node
$Action8 $Action8
] ]
## Script attached to transit buttons
const TRANSIT_BUTTON_SCRIPT = preload("res://scripts/Classes/Menu/transit_button.gd") const TRANSIT_BUTTON_SCRIPT = preload("res://scripts/Classes/Menu/transit_button.gd")
@export var menu: Menu = null @export var menu: Menu = null
@ -22,22 +24,27 @@ func _ready():
func load_menu(): func load_menu():
# iterating through all actions # iterating through all actions
for i in range(len(menu.item_ids)): for i in range(len(menu.item_ids)):
# disconnect previous action
if actions[i] is TransitButton: if actions[i] is TransitButton:
actions[i].button_up.disconnect(transit_menu) actions[i].button_up.disconnect(transit_menu)
elif actions[i] is MenuAction: elif actions[i] is MenuAction:
actions[i].button_up.disconnect(actions[i].action) actions[i].button_up.disconnect(actions[i].action)
# disconnect previous script
actions[i].set_script(null) actions[i].set_script(null)
actions[i].text = menu.item_ids[i] actions[i].text = menu.item_ids[i]
# assign new script
match menu.item_actions[i]: match menu.item_actions[i]:
Menu.Action.TransitAction: Menu.Action.TransitAction:
actions[i].set_script(TRANSIT_BUTTON_SCRIPT) actions[i].set_script(TRANSIT_BUTTON_SCRIPT)
actions[i].id = i
Menu.Action.ButtonAction: Menu.Action.ButtonAction:
actions[i].set_script(menu.item_data[i].load()) actions[i].set_script(menu.item_data[i].load_script())
Menu.Action.ComboAction:
actions[i].set_script(menu.item_data[i].load_script())
actions[i].id = i
## Called with transit_button, changes current menu ## Called with transit_button, changes current menu
func transit_menu(id: int): func transit_menu(id: int):
assert(id < len(menu.item_ids) and id >= 0) assert(id < len(menu.item_ids) and id >= 0)
var new_menu = menu.item_data[id].load().duplicate() var new_menu = menu.item_data[id].load_menu().duplicate()
menu = new_menu menu = new_menu
load_menu() load_menu()

View file

@ -0,0 +1,16 @@
extends MenuResource
class_name ComboMenuResource
## Script path
@export var item_script: StringName
## Menu that will be transitioned to
@export var item_menu: StringName
## Returns script resource
func load_script() -> GDScript:
return ResourceLoader.load(item_script)
## Returns menu resource
func load_menu() -> Menu:
return ResourceLoader.load(item_menu)

View file

@ -4,16 +4,17 @@ extends Resource
class_name Menu class_name Menu
enum Action { enum Action {
## Represents lack of action, should not have resource in item_data
NoneAction,
## Represents when this menu should be replaced with another. ## Represents when this menu should be replaced with another.
## Attach Menu next to it in item_data ## Attach MenuMenuResource next to it in item_data
TransitAction, TransitAction,
## Represents scripts which should be attached to button on menu startup ## Represents scripts which should be attached to button on menu startup
## Attach Script next to it in item_data ## Attach ScriptMenuResource next to it in item_data
ButtonAction, ButtonAction,
## WIP, should represent LineEdit or some other input control ## Represents button script with transition after its execution
InputAction, ## Attach ComboMenuResource next to it in item_data
## WIP ComboAction
OtherAction
} }
## Action string IDs. Should have same size as item_actions ## Action string IDs. Should have same size as item_actions

View file

@ -2,6 +2,8 @@ extends Button
class_name MenuAction class_name MenuAction
var id: int = -1
func _init(): func _init():
get_tree().create_timer(0.05).timeout.connect(_ready) get_tree().create_timer(0.05).timeout.connect(_ready)

View file

@ -4,5 +4,6 @@ class_name MenuMenuResource
@export var item: StringName @export var item: StringName
func load() -> Menu: ## Returns menu resource
func load_menu() -> Menu:
return ResourceLoader.load(item) return ResourceLoader.load(item)

View file

@ -4,5 +4,6 @@ class_name ScriptMenuResource
@export var item: StringName @export var item: StringName
func load() -> GDScript: ## Returns script resource
func load_script() -> GDScript:
return ResourceLoader.load(item) return ResourceLoader.load(item)

View file

@ -0,0 +1,14 @@
## Represents string that differs in fact value and a value which should be displayed to player
class_name Message
## Fact value
var fact: String
## Display value
var shown: String
## Returns message with set fact and shown strings
static func create(fact: String, shown: String) -> Message:
var msg = Message.new()
msg.fact = fact
msg.shown = shown
return msg

Binary file not shown.

Binary file not shown.