extends Control signal character_selected( slot ) signal return_button_pressed var preview_slot = 0 var character_preview = null var character_mesh = null var character_face_camera = null var character_box = null var name_input = null var turn_direction = null var zoom_direction = null 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 self.character_box.has_node( "character_slots/slot_box_"+section ): last_slot = int(section) var character_name = config_file.get_value( section, "name" ) if not character_name: character_name = "NameNotFound" 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) self.character_box.get_node( "character_slots" ).add_child( slot_box ) slot_box.connect( "mouse_entered", self, "_on_name_mouse_entered_pressed", [int(section)] ) var label_name = Label.new() label_name.text = character_name#+ " ("+section+")" label_name.size_flags_horizontal = SIZE_EXPAND slot_box.add_child( label_name ) # label_name.connect( "mouse_entered", self, "_on_name_mouse_entered_pressed", [int(section)] ) var choose_button = Button.new() choose_button.text = "Choisir" slot_box.add_child( choose_button ) 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" slot_box.add_child( delete_button ) 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 self.character_box.has_node( "character_slots/creation_button" ): self.character_box.get_node( "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" create_new_characer_button.hint_tooltip = "Create a new character" self.character_box.get_node( "character_slots" ).add_child( create_new_characer_button ) create_new_characer_button.connect( "pressed", self, "_on_create_pressed", [next_slot] ) func _ready(): # $viewport/character/face_camera.make_current() self.character_preview = $viewport/character # self.character_preview.get_node( "Camera_rotation_helper/face_camera" ).make_current() self.character_preview.get_node( "face_camera" ).make_current() self.character_mesh = $viewport/character.get_node("MeshInstance") # self.character_face_camera = $viewport/character/Camera_rotation_helper/face_camera self.character_box = $margin_container/character_box self.name_input = self.character_box.get_node( "character_creation_box/margin_container/v_box_container/name_box/line_edit" ) self.character_preview.update() update_character_slots() func _on_create_pressed( slot ): self.character_preview.slot = slot self.character_box.get_node( "character_slots" ).hide() self.character_box.get_node( "character_creation_box" ).show() self.character_preview.update() 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 ): $viewport/character.show() 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) ): self.character_preview.slot = slot # name var pseudo = config_file.get_value( str(slot), "name", "Player" ) self.character_preview.pseudo = pseudo # gender var gender = config_file.get_value( str(slot), "gender", 1 ) self.character_preview.gender = gender # Color var color = config_file.get_value( str(slot), "color", Color( 1, 0.25, 0.25, 1 ) ) self.character_preview.color = color # Size var size = config_file.get_value( str(slot), "size", 1.0 ) self.character_preview.size = size self.character_preview.update() func _on_character_slots_return_button_pressed(): emit_signal( "return_button_pressed" ) func _on_character_creation_box_return_button_pressed(): self.character_box.get_node( "character_slots" ).show() self.character_box.get_node( "character_creation_box" ).hide() func _on_character_creation_box_creation_button_pressed(): if not self.name_input.text or self.name_input.text == "": self.character_box.get_node( "character_creation_box" ).show_error( "You need to choose a character's name." ) return self.character_preview.pseudo = self.name_input.text 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(self.character_preview.slot), "name", self.character_preview.pseudo) config_file.set_value( str(self.character_preview.slot), "gender", self.character_preview.gender) config_file.set_value( str(self.character_preview.slot), "color", self.character_preview.color) config_file.set_value( str(self.character_preview.slot), "size", self.character_preview.size) config_file.save( "user://player.cfg" ) self.character_box.get_node( "character_slots" ).show() self.character_box.get_node( "character_creation_box" ).hide() update_character_slots() func _on_character_creation_box_sex_bar_changed( value ): if value == 0: self.character_preview.gender = 0 self.character_preview.color = Color( 0.0, 0.0, 1.0, 1.0 ) else: self.character_preview.gender = 1 self.character_preview.color = Color( 1.0, 0.25, 0.25, 1.0 ) self.character_preview.update() func _on_character_creation_box_size_bar_changed( value ): self.character_preview.size = value self.character_preview.update() func _process( delta ): if turn_direction == "left": self.character_mesh.rotation.y -= delta elif turn_direction == "right": self.character_mesh.rotation.y += delta if zoom_direction == "front": $viewport/character/face_camera.size += delta elif zoom_direction == "back": $viewport/character/face_camera.size -= delta func _on_turn_left_button_down(): turn_direction = "left" func _on_turn_left_button_up(): turn_direction = null func _on_turn_right_button_down(): turn_direction = "right" func _on_turn_right_button_up(): turn_direction = null func _on_zoom_out_button_down(): zoom_direction = "front" func _on_zoom_out_button_up(): zoom_direction = null func _on_zoom_in_button_down(): zoom_direction = "back" func _on_zoom_in_button_up(): zoom_direction = null func _on_lights_button_up(): $viewport/character/face_lights.hide() func _on_lights_button_down(): $viewport/character/face_lights.show() func _on_lights_toggled(button_pressed): if button_pressed: $viewport/character/face_lights.show() else: $viewport/character/face_lights.hide() func _on_character_preview_image_resized(): $viewport.size = $margin_container/character_box/v_box_container/character_preview_image.rect_size