diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-12-30 15:15:01 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-12-30 15:15:01 +0100 |
commit | e08a29e73ea4f7e6d78e8e7f5a6e7033dbc1f542 (patch) | |
tree | 966b95a7cdad16f7658d2e10cec6e549f3b98c17 /ui/login/login_character_selection_list.gd | |
parent | 6e2deea3d1b2fb4d79dac02a0d4310936c7f317c (diff) |
next commit
Diffstat (limited to 'ui/login/login_character_selection_list.gd')
-rw-r--r-- | ui/login/login_character_selection_list.gd | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/ui/login/login_character_selection_list.gd b/ui/login/login_character_selection_list.gd new file mode 100644 index 0000000..221c47f --- /dev/null +++ b/ui/login/login_character_selection_list.gd @@ -0,0 +1,111 @@ +class_name CharacterSelectionList +extends Control + + +signal selected(slot_idx: int) + +var item_scene := preload("res://ui/login/character_selection_item.tscn") + +var slot_offset := 0 + +@export var displayed_slots_count: int = 3: + set = set_displayed_slots_count + +@export var selected_slot_index: int = -1: + set = set_selected_slot_index + +@export var login_character_list: LoginCharacterList: + set = set_login_character_list + + +func _ready() -> void: + if %CharacterList.get_child_count() != displayed_slots_count: + set_displayed_slots_count(displayed_slots_count) + + +func set_displayed_slots_count(value: int) -> void: + displayed_slots_count = value + + for node in %CharacterList.get_children(): + node.queue_free() + + for _idx in displayed_slots_count: + var item = preload("res://ui/login/character_selection_item.tscn").instantiate() + %CharacterList.add_child(item) + + draw() + + +func set_selected_slot_index(value: int) -> void: + if selected_slot_index == value: + return + + var character_list := login_character_list.character_information + var maximum_slot_count := login_character_list.slot_count + + %CharacterList.get_child(selected_slot_index - slot_offset).is_selected = false + + if value >= maximum_slot_count: + selected_slot_index = 0 + elif value < 0: + selected_slot_index = maximum_slot_count - 1 + else: + selected_slot_index = value + + if selected_slot_index > slot_offset + (displayed_slots_count - 1): + #slot_offset = selected_slot_index - (displayed_slots_count - 1) + slot_offset = min(selected_slot_index, maximum_slot_count - displayed_slots_count) + elif selected_slot_index < slot_offset: + #slot_offset = selected_slot_index + slot_offset = max(0, selected_slot_index - displayed_slots_count) + + draw() + %CharacterList.get_child(selected_slot_index - slot_offset).is_selected = true + + if selected_slot_index < character_list.size(): + %CharacterSelectionStatus.set_info(character_list[selected_slot_index]) + else: + $CharacterSelectionStatus.clear() + + selected.emit(selected_slot_index) + + +func set_login_character_list(value: LoginCharacterList) -> void: + login_character_list = value + draw() + + +func draw(): + if not login_character_list: + return + + var character_information: Array[CharacterInformation] = login_character_list.character_information + + for display_slot_idx in displayed_slots_count: + var item: CharacterSelectionItem = %CharacterList.get_child(display_slot_idx) + var slot_idx = slot_offset + display_slot_idx + + if slot_idx < character_information.size(): + var info: CharacterInformation = character_information[slot_idx] + item.initialize_with_info(info) + else: + item.clear() + + if item.selected.is_connected(_on_item_selected): + item.selected.disconnect(_on_item_selected) + item.selected.connect(_on_item_selected.bind(slot_idx)) + + +func select(slot_idx: int): + selected_slot_index = slot_idx + + +func _on_item_selected(slot_idx: int): + selected_slot_index = slot_idx + + +func _on_button_left_pressed() -> void: + selected_slot_index -= 1 + +func _on_button_right_pressed() -> void: + selected_slot_index += 1 |