ChocolateBird/scripts/loaders/common.gd

72 lines
2.4 KiB
GDScript

extends Node
var base_resolution: Vector2 = Vector2(ProjectSettings.get_setting('display/window/size/width'), ProjectSettings.get_setting('display/window/size/height')) # Vector2(640, 360)
var shrink_timer := Timer.new()
static func load_json(filename: String) -> Dictionary:
var file := File.new()
var error := file.open(filename, File.READ)
if error == OK:
var result = JSON.parse(file.get_as_text())
if result.error == OK:
return result.result
else:
print_debug(result.error_string)
print_debug(result.error_line)
return {}
static func load_tsv(filename: String, delimiter: String = '\t') -> Dictionary:
var file := File.new()
var error := file.open(filename, File.READ)
if error == OK:
var headers := file.get_csv_line(delimiter)
var n := headers.size()
var output = {}
while file.get_position() < file.get_len():
var line := file.get_csv_line(delimiter)
var entry := {}
for i in range(1, n):
if line.size() > i:
var token := line[i]
entry[headers[i]] = token
if token.begins_with('0x'):
var hex := token.hex_to_int()
if hex > 0:
entry[headers[i]] = hex
output[line[0]] = entry
return output
print_debug(error)
return {}
var SNES_PSX_addresses := load_tsv('res://data/SNES_PSX_addresses.tsv')
func shrink_to_integer():
var size := OS.get_window_size()
var scale_vec := size / base_resolution
var scale_f := max(min(scale_vec.x, scale_vec.y), 1)
var scale := int(scale_f)
var new_window_size := base_resolution*scale
if OS.get_window_size() != new_window_size: # avoid retriggering this event forever
OS.set_window_size(new_window_size)
print('resized to scale %d %s' % [scale, new_window_size])
func update_window_scale():
if OS.window_size < base_resolution*2:
OS.window_size = base_resolution*2
var size := OS.get_window_size()
var scale_vec := size / base_resolution
var scale_f := max(min(scale_vec.x, scale_vec.y), 1)
var scale := int(scale_f)
if ProjectSettings.get_setting('display/window/size/snap_to_integer'):
# OS.window_size = base_resolution*scale
shrink_timer.paused = false
shrink_timer.start(1)
get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_DISABLED, SceneTree.STRETCH_ASPECT_KEEP, base_resolution*scale, scale)
func _ready():
shrink_timer.connect('timeout', self, 'shrink_to_integer')
shrink_timer.one_shot = true
add_child(shrink_timer)
get_tree().connect('screen_resized', self, 'update_window_scale')
OS.set_min_window_size(base_resolution*2)