about summary refs log tree commit diff stats
path: root/apworld/client/textclient.gd
blob: 1a0ce5cc225da0ece73c255afc4b0316d4dee875 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
extends CanvasLayer

var tabs
var panel
var label
var entry
var tracker_label
var is_open = false

var locations_overlay

var location_texture
var worldport_texture


func _ready():
	process_mode = ProcessMode.PROCESS_MODE_ALWAYS
	layer = 2

	locations_overlay = RichTextLabel.new()
	locations_overlay.name = "LocationsOverlay"
	locations_overlay.offset_top = 220
	locations_overlay.offset_bottom = 720
	locations_overlay.offset_left = 20
	locations_overlay.anchor_right = 1.0
	locations_overlay.offset_right = -10
	locations_overlay.scroll_active = false
	locations_overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE
	locations_overlay.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
	add_child(locations_overlay)
	update_locations_visibility()

	tabs = TabContainer.new()
	tabs.name = "Tabs"
	tabs.offset_left = 100
	tabs.offset_right = 1820
	tabs.offset_top = 100
	tabs.offset_bottom = 980
	tabs.visible = false
	tabs.theme = preload("res://assets/themes/baseUI.tres")
	tabs.add_theme_font_size_override("font_size", 36)
	add_child(tabs)

	panel = MarginContainer.new()
	panel.name = "Text Client"
	panel.add_theme_constant_override("margin_top", 60)
	panel.add_theme_constant_override("margin_left", 60)
	panel.add_theme_constant_override("margin_right", 60)
	panel.add_theme_constant_override("margin_bottom", 60)
	tabs.add_child(panel)

	label = RichTextLabel.new()
	label.set_name("Label")
	label.scroll_following = true
	label.selection_enabled = true
	label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	label.size_flags_vertical = Control.SIZE_EXPAND_FILL
	label.push_font(preload("res://assets/fonts/Lingo2.ttf"))
	label.push_font_size(36)

	var entry_style = StyleBoxFlat.new()
	entry_style.bg_color = Color(0.9, 0.9, 0.9, 1)

	entry = LineEdit.new()
	entry.set_name("Entry")
	entry.add_theme_font_override("font", preload("res://assets/fonts/Lingo2.ttf"))
	entry.add_theme_font_size_override("font_size", 36)
	entry.add_theme_color_override("font_color", Color(0, 0, 0, 1))
	entry.add_theme_color_override("cursor_color", Color(0, 0, 0, 1))
	entry.add_theme_stylebox_override("focus", entry_style)
	entry.text_submitted.connect(text_entered)

	var tc_arranger = VBoxContainer.new()
	tc_arranger.add_child(label)
	tc_arranger.add_child(entry)
	tc_arranger.add_theme_constant_override("separation", 40)
	panel.add_child(tc_arranger)

	var tracker_margins = MarginContainer.new()
	tracker_margins.name = "Locations"
	tracker_margins.add_theme_constant_override("margin_top", 60)
	tracker_margins.add_theme_constant_override("margin_left", 60)
	tracker_margins.add_theme_constant_override("margin_right", 60)
	tracker_margins.add_theme_constant_override("margin_bottom", 60)
	tabs.add_child(tracker_margins)

	tracker_label = RichTextLabel.new()
	tracker_margins.add_child(tracker_label)

	var runtime = global.get_node("Runtime")
	var location_image = Image.new()
	location_image.load_png_from_buffer(runtime.read_path("assets/location.png"))
	location_texture = ImageTexture.create_from_image(location_image)

	var worldport_image = Image.new()
	worldport_image.load_png_from_buffer(runtime.read_path("assets/worldport.png"))
	worldport_texture = ImageTexture.create_from_image(worldport_image)


func _input(event):
	if global.loaded and event is InputEventKey and event.pressed:
		if event.keycode == KEY_TAB and !Input.is_key_pressed(KEY_SHIFT):
			if !get_tree().paused:
				is_open = true
				get_tree().paused = true
				Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
				tabs.visible = true
				entry.grab_focus()
				get_viewport().set_input_as_handled()
			else:
				dismiss()
		elif event.keycode == KEY_ESCAPE:
			if is_open:
				dismiss()
				get_viewport().set_input_as_handled()


func dismiss():
	if is_open:
		get_tree().paused = false
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
		tabs.visible = false
		is_open = false


func parse_printjson(text):
	label.append_text("[p]" + text + "[/p]")


func text_entered(text):
	var ap = global.get_node("Archipelago")
	var cmd = text.trim_suffix("\n")
	entry.text = ""
	if OS.is_debug_build():
		if cmd.begins_with("/tp_map "):
			var new_map = cmd.substr(8)
			global.map = new_map
			global.sets_entry_point = false
			switcher.switch_map("res://objects/scenes/%s.tscn" % new_map)
			return

	ap.client.say(cmd)


func update_locations():
	var ap = global.get_node("Archipelago")
	var gamedata = global.get_node("Gamedata")

	tracker_label.clear()
	tracker_label.push_font(preload("res://assets/fonts/Lingo2.ttf"))
	tracker_label.push_font_size(24)

	locations_overlay.clear()
	locations_overlay.push_font(preload("res://assets/fonts/Lingo2.ttf"))
	locations_overlay.push_font_size(24)
	locations_overlay.push_color(Color(0.9, 0.9, 0.9, 1))
	locations_overlay.push_outline_color(Color(0, 0, 0, 1))
	locations_overlay.push_outline_size(2)

	const kLocation = 0
	const kWorldport = 1

	var location_names = []
	var type_by_name = {}
	for location_id in ap.client._accessible_locations:
		if not ap.client._checked_locations.has(location_id):
			var location_name = gamedata.location_name_by_id.get(location_id, "(Unknown)")
			location_names.append(location_name)
			type_by_name[location_name] = kLocation

	for port_id in ap.client._accessible_worldports:
		if not ap.client._checked_worldports.has(port_id):
			var port_name = gamedata.get_worldport_display_name(port_id)
			location_names.append(port_name)
			type_by_name[port_name] = kWorldport

	location_names.sort()

	var count = 0
	for location_name in location_names:
		tracker_label.append_text("[p]%s[/p]" % location_name)
		if count < 18:
			locations_overlay.push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT)
			locations_overlay.append_text(location_name)
			locations_overlay.append_text(" ")
			if type_by_name[location_name] == kLocation:
				locations_overlay.add_image(location_texture)
			elif type_by_name[location_name] == kWorldport:
				locations_overlay.add_image(worldport_texture)
			locations_overlay.pop()
		count += 1

	if count > 18:
		locations_overlay.append_text("[p align=right][lb]...[rb][/p]")


func update_locations_visibility():
	var ap = global.get_node("Archipelago")
	locations_overlay.visible = ap.show_locations


func reset():
	locations_overlay.clear()