test-client-godot/login_scene/character_selection_menu.gd
2018-08-09 16:14:11 +02:00

160 lines
6.3 KiB
GDScript

extends Control
signal character_selected( slot )
signal return_button_pressed
var preview_slot = 0
var character_mesh = null
var name_input = null
onready var error_label = $h_box_container/character_creation_box/margin_container/v_box_container/error_label
func update_character_slots():
var config_file = ConfigFile.new()
var err = config_file.load( "user://player.cfg" )
if err:
print("Error code when loading player config file: ", err)
var useds_slot = []
var last_slot = 0
for section in config_file.get_sections():
useds_slot.append( int(section) )
if not has_node( "h_box_container/character_slots/slot_box_"+section ):
last_slot = int(section)
var character_name = config_file.get_value( section, "name" )
var slot_box = HBoxContainer.new()
slot_box.name = "slot_box_"+section
slot_box.size_flags_horizontal = SIZE_FILL
slot_box.size_flags_vertical = SIZE_EXPAND
slot_box.set( "custom_constants/separation", 8)
$h_box_container/character_slots.add_child( slot_box )
slot_box.set_owner( $h_box_container/character_slots )
slot_box.connect( "mouse_entered", self, "_on_name_mouse_entered_pressed", [int(section)] )
var label_name = Label.new()
label_name.text = character_name#+ " ("+section+")"
slot_box.add_child( label_name )
label_name.set_owner( slot_box )
# label_name.connect( "mouse_entered", self, "_on_name_mouse_entered_pressed", [int(section)] )
var choose_button = Button.new()
choose_button.text = "Choisir"#+ " ("+section+")"
# choose_button.mouse_filter = MOUSE_FILTER_PASS
slot_box.add_child( choose_button )
choose_button.set_owner( slot_box )
choose_button.connect( "pressed", self, "_on_choose_pressed", [int(section)] )
choose_button.connect( "mouse_entered", self, "_on_name_mouse_entered_pressed", [int(section)] )
var delete_button = Button.new()
delete_button.text = "Supprimer"#+ " ("+section+")"
# delete_button.mouse_filter = MOUSE_FILTER_PASS
slot_box.add_child( delete_button )
delete_button.set_owner( slot_box )
delete_button.connect( "pressed", self, "_on_delete_pressed", [int(section), slot_box] )
delete_button.connect( "mouse_entered", self, "_on_name_mouse_entered_pressed", [int(section)] )
if has_node( "h_box_container/character_slots/creation_button" ):
$h_box_container/character_slots/creation_button.free()
var index = 0
while index in useds_slot:
index += 1
var next_slot = index
var create_new_characer_button = Button.new()
create_new_characer_button.name = "creation_button"
create_new_characer_button.text = "Créer"#+" ("+str(next_slot)+")"
create_new_characer_button.hint_tooltip = "Create a new character"
$h_box_container/character_slots.add_child( create_new_characer_button )
create_new_characer_button.set_owner( $h_box_container/character_slots )
create_new_characer_button.connect( "pressed", self, "_on_create_pressed", [next_slot] )
func _ready():
$viewport/character_preview.show()
character_mesh = $viewport/character_preview/character
name_input = $h_box_container/character_creation_box/margin_container/v_box_container/name_box/line_edit
update_character_slots()
func _on_character_slots_return_button_pressed():
emit_signal( "return_button_pressed" )
func _on_create_pressed( slot ):
global.character_slot = slot
$h_box_container/character_slots.hide()
$h_box_container/character_creation_box.show()
_on_character_creation_box_gender_value_changed( $h_box_container/character_creation_box/margin_container/v_box_container/sexe_box/h_box_container/h_scroll_bar.value )
func _on_choose_pressed( slot ):
emit_signal( "character_selected", slot )
func _on_delete_pressed( slot, node_to_delete ):
var config_file = ConfigFile.new()
var err = config_file.load( "user://player.cfg" )
if err:
print("Error code when loading player config file: ", err)
config_file.erase_section( str(slot) )
config_file.save( "user://player.cfg" )
var parent = node_to_delete.get_parent()
parent.remove_child( node_to_delete )
update_character_slots()
func _on_name_mouse_entered_pressed( slot ):
preview_slot = slot
update_preview()
func update_preview():
var slot = preview_slot
var config_file = ConfigFile.new()
var err = config_file.load( "user://player.cfg" )
if err:
print("Error code when loading player config file: ", err)
if config_file.has_section( str(slot) ):
$viewport/character_preview/character.get_surface_material(0).albedo_color = config_file.get_value( str(slot), "color" )
func _on_character_creation_box_gender_value_changed(value):
if value == 1:
global.character_gender = 1
character_mesh.get_surface_material(0).albedo_color = Color( 1.0, 0.25, 0.25, 1.0 )
else:
global.character_gender = 0
character_mesh.get_surface_material(0).albedo_color = Color( 0.0, 0.0, 1.0, 1.0 )
func _on_character_creation_box_return_button_pressed():
$h_box_container/character_slots.show()
$h_box_container/character_creation_box.hide()
func _on_character_creation_box_creation_button_pressed():
if not name_input.text or name_input.text == "":
error_label.text = "You need to choose a character's name."
return
global.character_name = name_input.text
global.character_color = character_mesh.get_surface_material(0).albedo_color
var config_file = ConfigFile.new()
var err = config_file.load( "user://player.cfg" )
if err:
print("Error code when loading player config file: ", err)
config_file.set_value( str(global.character_slot), "name", global.character_name)
config_file.set_value( str(global.character_slot), "color", global.character_color)
config_file.save( "user://player.cfg" )
$h_box_container/character_slots.show()
$h_box_container/character_creation_box.hide()
update_character_slots()