summaryrefslogtreecommitdiff
path: root/ui/login/login_character_selection_list.gd
blob: bd19fa1d560a2cd7ae3a4e3517c310dada605238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class_name CharacterSelectionList
extends Control


signal selected(slot_idx: int)
signal requested_login(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 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 - 1))
	
	draw()
	%CharacterList.get_child(selected_slot_index - slot_offset).is_selected = true
	
	var info = login_character_list.get_info_for_slot(selected_slot_index)
	if info:
		%CharacterSelectionStatus.set_info(info)
	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
	
	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
		
		var info = login_character_list.get_info_for_slot(slot_idx)
		if info:
			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))
		
		if item.gui_input.is_connected(_on_item_gui_input):
			item.gui_input.disconnect(_on_item_gui_input)
		item.gui_input.connect(_on_item_gui_input.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_item_gui_input(event: InputEvent, slot_idx: int) -> void:
	if event is InputEventMouseButton and event.double_click:
		requested_login.emit(slot_idx)


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_accept"):
		requested_login.emit(selected_slot_index)
	
	if event.is_action_pressed("ui_left"):
		%ButtonLeft.pressed.emit()
	if event.is_action_pressed("ui_right"):
		%ButtonRight.pressed.emit()


func _on_button_left_pressed() -> void:
	selected_slot_index -= 1

func _on_button_right_pressed() -> void:
	selected_slot_index += 1