Public repository for MUR pre alpha
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

118 lines
3.5 KiB

extends Node
const FILE_PATH = "user://local_storage"
const RESOLUTIONS = [Vector2(1920,1080),Vector2(1600,900),Vector2(1366,758),Vector2(1280,720),Vector2(1136,640),Vector2(1024,576)]
const CONTROL_ACTIONS = ["controls_thrust", "controls_break", "controls_add_road", "controls_next_road_type", "controls_prev_road_type", "controls_next_road_variant", "controls_prev_road_variant", "controls_reset", "controls_menu"]
const INPUT_UI_MAPPING = {"ui_accept" : "controls_add_road", "ui_select" : "controls_add_road", "ui_up" : "controls_next_road_variant", "ui_down" : "controls_prev_road_variant", "ui_left" : "controls_prev_road_type", "ui_right" : "controls_next_road_type"}
onready var view_port = get_tree().get_root()
func _ready():
apply_settings()
func read_config():
var f = File.new()
var err = f.open(FILE_PATH, File.READ)
var config = {}
if err == OK:
config = parse_json(f.get_as_text())
f.close()
if config == null:
config = {}
return config
func write_config(config:Dictionary):
var f = File.new()
var err = f.open(FILE_PATH, File.WRITE)
#var err = f.open_encrypted_with_pass(FILE_PATH, File.WRITE, OS.get_unique_id())
f.store_string(to_json(config))
f.close()
func write_value(key:String, value):
var config = read_config()
config[key] = value
write_config(config)
func read_value(key:String, default = null):
var config = read_config()
if config.has(key) && config.get(key) != null:
return config.get(key)
else:
return default
func delete_value(key:String):
var config = read_config()
config.erase(key)
write_config(config)
func write_values(new_config:Dictionary):
var config = read_config()
for key in new_config:
config[key] = new_config[key]
write_config(config)
func read_values():
return read_config()
func apply_settings():
apply_locale()
apply_game_server()
apply_resolution()
apply_controls()
func apply_locale():
TranslationServer.set_locale(read_value("locale","en"))
func apply_game_server():
var server_addr = read_value("server_addr")
if server_addr != null && not server_addr.empty():
game_server.set_server_addr(server_addr)
var api_addr = read_value("api_addr")
if api_addr != null && not api_addr.empty():
game_server.set_api_addr(api_addr)
func apply_resolution():
OS.set_window_fullscreen(read_value("fullscreen", true))
if OS.is_window_fullscreen():
var base_size = Vector2(1920, 1080)
var scale= base_size.x / RESOLUTIONS[read_value("resolution",0)].x
get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_2D,SceneTree.STRETCH_ASPECT_EXPAND,base_size,scale)
else:
OS.set_window_size(RESOLUTIONS[read_value("resolution",0)])
get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,SceneTree.STRETCH_ASPECT_IGNORE,OS.get_window_size(),1)
func apply_controls():
InputMap.load_from_globals()
var control_map = read_value("controls",{})
for action in InputMap.get_actions():
if control_map.has(action):
for event in InputMap.get_action_list(action):
if event is InputEventKey && control_map[action].has("key"):
InputMap.action_erase_event(action, event)
event.set_scancode(OS.find_scancode_from_string(control_map[action]["key"]))
InputMap.action_add_event(action,event)
elif event is InputEventJoypadButton && control_map[action].has("joypad"):
InputMap.action_erase_event(action, event)
event.set_button_index(control_map[action]["joypad"])
InputMap.action_add_event(action,event)
for ui_mapping in INPUT_UI_MAPPING:
for event in InputMap.get_action_list(INPUT_UI_MAPPING[ui_mapping]):
InputMap.action_add_event(ui_mapping, event)