khanat-client/ressources/scripts/config/config.gd

519 lines
16 KiB
GDScript

extends Node
# Localization configuration on linux
# $HOME/.local/share/godot/app_userdata/Khanat
var font_size = 14
var SettingsFile = "user://settings.cfg"
var screen_number_default = OS.current_screen
var screen_number = OS.current_screen
var screen_orientation = 0
var window_maximized:bool = false
var video_default:bool = true
var window_fullscreen:bool = true
var window_borderless:bool = false
var window_resizable:bool = true
var window_size_x = 0
var window_size_y = 0
var window_always_on_top:bool = true
var debug:bool = true
var sound_lvl_music = 50
var sound_lvl_effect = 50
var sound_lvl_global = 50
var mute:bool = false
var playermusic:bool = false
var queuemusic = []
var control_system_conf = {}
onready var rand = RandomNumberGenerator.new()
func msg_debug(text):
if debug:
var frame = get_stack()[1]
print("DEBUG [%s:%d] %s" % [frame.source, frame.line, text] )
func msg_info(text):
var frame = get_stack()[1]
print("INFO [%s:%d] %s" % [frame.source, frame.line, text] )
func msg_error(text):
var frame = get_stack()[1]
print("ERROR [%s:%d] %s" % [frame.source, frame.line, text] )
# Called when the node enters the scene tree for the first time.
func _ready():
load_control_system()
load_config()
rand.randomize()
msg_debug("load end")
func set_playermusic(state):
Config.msg_debug("player music:" + str(state))
self.playermusic = state
func get_playermusic():
return self.playermusic
func set_window_fullscreen(value: bool):
window_fullscreen = value
if video_default == false:
OS.window_fullscreen = window_fullscreen
func set_window_borderless(value: bool):
window_borderless = value
if video_default == false:
OS.window_borderless = window_borderless
func set_window_resizable(value: bool):
window_resizable = value
if video_default == false:
OS.window_resizable = window_resizable
func set_window_width(value: int):
window_size_x = value
if video_default == false:
OS.window_size.x = window_size_x
func set_window_height(value: int):
window_size_y = value
if video_default == false:
OS.window_size.y = window_size_y
func set_font_size(value: int):
font_size = value
if video_default == false:
pass
func set_video_default(value):
video_default = value
func set_window_always_on_top(value: bool):
window_always_on_top = value
if video_default == false:
OS.set_window_always_on_top(window_always_on_top)
func set_current_screen(value: int):
screen_number = int(value)
if video_default == false:
OS.current_screen = screen_number
func set_screen_orientation(value: int):
if video_default == false:
screen_orientation = int(value)
OS.set_screen_orientation(screen_orientation)
func set_window_maximized(value: bool):
if value:
window_maximized = true
else:
window_maximized = false
if not video_default:
OS.set_window_maximized(window_maximized)
func enable_window_default():
# display/window/handheld/orientation
if ProjectSettings.has_setting( "display/window/handheld/orientation" ):
var num = 0
var default_orientation = ProjectSettings.get_setting("display/window/handheld/orientation")
match default_orientation:
"landscape":
num = 0
"portrait":
num = 1
"reverse_landscape":
num = 2
"reverse_portrait":
num = 3
"sensor_landscape":
num = 4
"sensor_portrait":
num = 5
"sensor":
num = 6
_:
num = 6
OS.set_screen_orientation(num)
OS.set_window_maximized(window_maximized)
OS.current_screen = screen_number_default
# display/window/size/always_on_top
if ProjectSettings.has_setting( "display/window/size/always_on_top" ):
OS.set_window_always_on_top( ProjectSettings.get_setting( "display/window/size/always_on_top" ) )
#OS.window_size.x = window_size_x
#OS.window_size.y = window_size_y
if ProjectSettings.has_setting( "display/window/size/fullscreen" ):
OS.window_fullscreen = ProjectSettings.get_setting( "display/window/size/fullscreen" )
if ProjectSettings.has_setting( "display/window/size/borderless" ):
OS.window_borderless = ProjectSettings.get_setting( "display/window/size/borderless" )
if ProjectSettings.has_setting( "display/window/size/resizable" ):
OS.window_resizable = ProjectSettings.get_setting( "display/window/size/resizable" )
func disable_window_default():
OS.set_screen_orientation(screen_orientation)
OS.set_window_maximized(window_maximized)
OS.current_screen = screen_number
OS.set_window_always_on_top(window_always_on_top)
#OS.window_size.x = window_size_x
#OS.window_size.y = window_size_y
OS.window_fullscreen = window_fullscreen
OS.window_borderless = window_borderless
OS.window_resizable = window_resizable
func set_sound_mute(value: bool):
Config.msg_debug("mute:" + str(value))
mute = value
func save_config():
var config_file = ConfigFile.new()
config_file.set_value( "theme", "font_size", 14 )
config_file.set_value( "display", "default", video_default )
config_file.set_value( "display", "fullscreen", OS.window_fullscreen )
config_file.set_value( "display", "borderless", OS.window_borderless )
config_file.set_value( "display", "resizable", OS.window_resizable )
#config_file.set_value( "display", "width", int( OS.window_size.x ) )
#config_file.set_value( "display", "height", int( OS.window_size.y ) )
config_file.set_value( "display", "window_always_on_top", OS.keep_screen_on )
config_file.set_value( "display", "screen_orientation", OS.get_screen_orientation() )
config_file.set_value( "display", "current_screen", screen_number )
config_file.set_value( "display", "window_maximized", window_maximized )
config_file.set_value( "sound", "global_level", sound_lvl_global )
config_file.set_value( "sound", "music_level", sound_lvl_music )
config_file.set_value( "sound", "effect_level", sound_lvl_effect )
config_file.set_value( "sound", "mute", mute )
config_file.set_value( "sound", "playermusic", playermusic )
var posmusic = 0
for child in queuemusic:
#Config.msg_debug("" + child )
config_file.set_value( "playermusic", str(posmusic), child )
posmusic += 1
config_file.set_value("debug", "console", debug)
var control_config = load_current_control()
config_file.set_value("control", "config", control_config)
Config.msg_debug(str(control_config))
config_file.save( SettingsFile )
#print("[res://ressources/scripts/config/config.gd] save_config")
func load_config():
var config_file = ConfigFile.new()
var err = config_file.load( SettingsFile )
if err == ERR_CANT_OPEN or err == ERR_FILE_NOT_FOUND:
# On suppose que le fichier n'existe pas encore, donc on le crée.
#print("[res://ressources/scripts/config/config.gd] Error to read, recreate config")
save_config()
elif not err == OK:
print("[res://ressources/scripts/config/config.gd] Error code when loading user://settings.cfg file: ", err)
font_size = config_file.get_value("theme", "font_size", false)
video_default = config_file.get_value("display", "default", true)
set_window_fullscreen(config_file.get_value("display", "fullscreen", true))
set_window_borderless(config_file.get_value("display", "borderless", false))
set_window_resizable(config_file.get_value("display", "resizable", true))
set_window_always_on_top(config_file.get_value("display", "window_always_on_top", true))
set_screen_orientation(config_file.get_value("display", "screen_orientation", 0))
set_current_screen(config_file.get_value("display", "current_screen", 0))
#set_window_width(config_file.get_value("display", "width", OS.window_size.x))
#set_window_height(config_file.get_value("display", "height", OS.window_size.y))
set_window_maximized(config_file.get_value("display", "window_maximized", OS.window_maximized))
sound_lvl_global = config_file.get_value("sound", "global_level", 100)
sound_lvl_music = config_file.get_value("sound", "music_level", 100)
sound_lvl_effect = config_file.get_value("sound", "effect_level", 100)
mute = config_file.get_value("sound", "mute", false)
playermusic = config_file.get_value("sound", "playermusic", false)
if config_file.has_section("playermusic"):
for key in config_file.get_section_keys( "playermusic" ):
var file = config_file.get_value( "playermusic", key, "" )
# Config.msg_debug( "playermusic:" + key + " " + str(file) )
queuemusic.append(file)
var control_config = config_file.get_value("control", "config", {})
if not control_config.empty():
#Config.msg_debug("Load default configuration : " + str(control_config))
load_control_config(control_config)
debug = config_file.get_value("debug", "console", false)
static func delete_children(node):
for n in node.get_children():
node.remove_child(n)
n.queue_free()
func get_dict_inputevent(event):
if event is InputEventKey:
var unicode = 0
if OS.get_ime_selection():
unicode = event.get_unicode()
return { 'type' : 'InputEventKey',
'physical_scancode' : event.get_physical_scancode(),
'scancode' : event.get_scancode(),
'echo' : event.is_echo(),
'unicode' : unicode,
'alt' : event.get_alt(),
'command' : event.get_command(),
'control' : event.get_control(),
'meta' : event.get_metakey(),
'shift' : event.get_shift()
}
elif event is InputEventMouseButton:
return { 'type' : 'InputEventMouseButton', 'value' : event.get_button_index() }
elif event is InputEventJoypadButton:
return { 'type' : 'InputEventJoypadButton', 'value' : event.get_button_index() }
return { 'type' : 'Unknown'}
func get_hash_inputevent(event):
var head:String
if event is InputEventKey:
head = 'a'
elif event is InputEventMouseButton:
head = 'm'
elif event is InputEventJoypadButton:
head = 'j'
else:
head ='z'
return head + str(get_string_input(event))
func compare_dict_inputevent(ref, cmp):
if ref.size() != cmp.size():
return false
for key in ref:
if ref[key] != cmp[key]:
return false
return true
func load_current_control():
var conf = {}
for key in InputMap.get_actions():
var a = InputMap.get_action_list(key)
var beta = []
for z in a:
beta.append( get_dict_inputevent(z) )
conf[key] = beta
return conf
func load_control_system():
control_system_conf = load_current_control()
func generate_inputevent(param):
if param['type'] == 'InputEventKey':
#print("---> InputEventKey")
var ele:InputEventKey = InputEventKey.new()
ele.set_physical_scancode(param['physical_scancode'])
ele.set_scancode(param['scancode'])
ele.set_echo(param['echo'])
if OS.get_ime_selection():
ele.set_unicode(param['unicode'])
ele.set_alt(param['alt'])
ele.set_command(param['command'])
ele.set_control(param['control'])
ele.set_metakey(param['meta'])
ele.set_shift(param['shift'])
return ele
elif param['type'] == 'InputEventMouseButton':
#print("---> InputEventMouseButton")
var ele:InputEventMouseButton = InputEventMouseButton.new()
ele.set_button_index(param['value'])
return ele
elif param['type'] == 'InputEventJoypadButton':
#print("---> InputEventJoypadButton")
var ele:InputEventJoypadButton = InputEventJoypadButton.new()
ele.set_button_index(param['value'])
return ele
return null
func reload_control_system():
#Config.msg_debug("= control_current =")
# Adding new Action
for key in control_system_conf:
#Config.msg_debug(key)
if not InputMap.has_action(key):
InputMap.add_action(key)
Config.msg_debug("Add Action:" + str(key))
# Adding new event
for newevent in control_system_conf[key]:
var ele = generate_inputevent(newevent)
if not InputMap.action_has_event(key, ele):
InputMap.action_add_event(key, ele)
Config.msg_debug("Add Event:" + str(ele))
# Remove action/event not used
#Config.msg_debug("= Remove =")
for keyInput in InputMap.get_actions():
var foundAction:bool = false
for key in control_system_conf:
if key == keyInput:
foundAction = true
break
if not foundAction:
InputMap.erase_action(keyInput)
Config.msg_debug("Del Action:" + str(keyInput))
continue
var listInput = InputMap.get_action_list(keyInput)
for eventInput in listInput:
var foundEvent:bool = false
var vis = get_dict_inputevent(eventInput)
for v in control_system_conf[keyInput]:
if compare_dict_inputevent(v, vis):
foundEvent = true
#Config.msg_debug("- ok Event:" + str(v) + " --- " + str(vis))
break
if not foundEvent:
InputMap.action_erase_event(keyInput, eventInput)
Config.msg_debug("Del Event:" + str(eventInput))
func load_control_config(conf):
#Config.msg_debug("= control_current =")
# Adding new Action
for key in conf:
#Config.msg_debug(key)
if not InputMap.has_action(key):
InputMap.add_action(key)
Config.msg_debug("Add Action:" + str(key))
# Adding new event
for newevent in conf[key]:
var ele = generate_inputevent(newevent)
if not InputMap.action_has_event(key, ele):
InputMap.action_add_event(key, ele)
Config.msg_debug("Add Event:" + str(ele))
# Remove action/event not used
#Config.msg_debug("= Remove =")
for keyInput in InputMap.get_actions():
var foundAction:bool = false
for key in conf:
if key == keyInput:
foundAction = true
break
if not foundAction:
InputMap.erase_action(keyInput)
Config.msg_debug("Del Action:" + str(keyInput))
continue
var listInput = InputMap.get_action_list(keyInput)
for eventInput in listInput:
var foundEvent:bool = false
var vis = get_dict_inputevent(eventInput)
for v in conf[keyInput]:
#Config.msg_debug("? Event:" + str(v) + " --- " + str(vis))
if compare_dict_inputevent(v, vis):
foundEvent = true
#Config.msg_debug("- ok Event:" + str(v) + " --- " + str(vis))
break
if not foundEvent:
InputMap.action_erase_event(keyInput, eventInput)
Config.msg_debug("Del Event:" + str(eventInput))
func add_input(action: String, event):
Config.msg_debug("Add Input:" + str(action) + " - " + str(event))
if not InputMap.has_action(action):
InputMap.add_action(action)
#var inputEvent = generate_inputevent(event)
if not InputMap.action_has_event(action, event):
InputMap.action_add_event(action, event)
func del_input(action: String, event):
Config.msg_debug("Del Input:" + str(action) + " - " + str(event))
if not InputMap.has_action(action):
return
if InputMap.action_has_event(action, event):
InputMap.action_erase_event(action, event)
func get_string_input_mousse_button(event:InputEventMouseButton):
match event.get_button_index():
BUTTON_LEFT:
return "BUTTON_LEFT"
BUTTON_MIDDLE:
return "BUTTON_MIDDLE"
BUTTON_RIGHT:
return "BUTTON_RIGHT"
BUTTON_WHEEL_UP:
return "BUTTON_WHEEL_UP"
BUTTON_WHEEL_DOWN:
return "BUTTON_WHEEL_DOWN"
BUTTON_WHEEL_LEFT:
return "BUTTON_WHEEL_LEFT"
BUTTON_WHEEL_RIGHT:
return "BUTTON_WHEEL_RIGHT"
BUTTON_XBUTTON1:
return "BUTTON_XBUTTON1"
BUTTON_XBUTTON2:
return "BUTTON_XBUTTON2"
BUTTON_WHEEL_LEFT:
return "BUTTON_WHEEL_LEFT"
BUTTON_WHEEL_RIGHT:
return "BUTTON_WHEEL_RIGHT"
_:
return "MOUSSE BUTTON: " + str(event.get_button_index())
func get_string_input_joypad_button(event:InputEventJoypadButton):
match event.get_button_index():
0:
return "JOY_BUTTON_A"
1:
return "JOY_BUTTON_B"
2:
return "JOY_BUTTON_X"
3:
return "JOY_BUTTON_Y"
4:
return "JOY_BUTTON_BACK"
5:
return "JOY_BUTTON_GUIDE"
6:
return "JOY_BUTTON_START"
7:
return "JOY_BUTTON_LEFT_STICK"
8:
return "JOY_BUTTON_RIGHT_STICK"
9:
return "JOY_BUTTON_LEFT_SHOULDER"
10:
return "JOY_BUTTON_RIGHT_SHOULDER"
11:
return "JOY_BUTTON_DPAD_UP"
12:
return "JOY_BUTTON_DPAD_DOWN"
13:
return "JOY_BUTTON_DPAD_LEFT"
14:
return "JOY_BUTTON_DPAD_RIGHT"
_:
return "JOYPAD BUTTON: " + str(event.get_button_index())
func get_string_input(event):
if event is InputEventKey:
return OS.get_scancode_string(event.get_scancode_with_modifiers())
elif event is InputEventMouseButton:
return get_string_input_mousse_button(event)
elif event is InputEventJoypadButton:
return get_string_input_joypad_button(event)
else:
return str(event)