diff options
author | Hop311 <Hop3114@gmail.com> | 2023-06-24 00:55:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-24 00:55:45 +0200 |
commit | 432000a3ab73980fc6421b3587de4b97af30d3ad (patch) | |
tree | 4b80d98261252f25011e34b22a8d49767525559f /game/src/Game/Autoload | |
parent | 40cf0fa95e325f3bf875e42c11254da23192f506 (diff) | |
parent | 206cafc8bba310e4d4f35f4898ef3ac289abe81a (diff) |
Merge pull request #131 from OpenVicProject/ui-cleanup
Localisation and UI focus cleanup
Diffstat (limited to 'game/src/Game/Autoload')
-rw-r--r-- | game/src/Game/Autoload/Events/Localisation.gd | 11 | ||||
-rw-r--r-- | game/src/Game/Autoload/Resolution.gd | 145 |
2 files changed, 94 insertions, 62 deletions
diff --git a/game/src/Game/Autoload/Events/Localisation.gd b/game/src/Game/Autoload/Events/Localisation.gd index 91f9ca0..37b550d 100644 --- a/game/src/Game/Autoload/Events/Localisation.gd +++ b/game/src/Game/Autoload/Events/Localisation.gd @@ -15,10 +15,10 @@ func get_default_locale() -> String: return ProjectSettings.get_setting("internationalization/locale/fallback", "en_GB") func load_localisation(dir_path : String) -> void: - if LoadLocalisation.load_localisation_dir(dir_path) == OK: - print("loaded locales: ", TranslationServer.get_loaded_locales()) - else: - push_error("Failed to load localisation directory: ", dir_path) + if LoadLocalisation.load_localisation_dir(dir_path) != OK: + push_error("Error loading localisation directory: ", dir_path) + var loaded_locales : PackedStringArray = TranslationServer.get_loaded_locales() + print("Loaded ", loaded_locales.size(), " locales: ", loaded_locales) # REQUIREMENTS # * SS-57 @@ -29,3 +29,6 @@ func _init(): push_error("Missing localisation_path setting!") else: load_localisation(localisation_dir_path) + +func tr_number(num) -> String: + return TextServerManager.get_primary_interface().format_number(str(num)) diff --git a/game/src/Game/Autoload/Resolution.gd b/game/src/Game/Autoload/Resolution.gd index c973ba9..1c7add7 100644 --- a/game/src/Game/Autoload/Resolution.gd +++ b/game/src/Game/Autoload/Resolution.gd @@ -1,28 +1,26 @@ extends Node -signal resolution_added(value : Vector2i, name : StringName, display_name : StringName) -signal resolution_changed(value : Vector2i) -signal window_mode_changed(value : Window.Mode) +signal resolution_added(value : Vector2i) const error_resolution : Vector2i = Vector2i(-1,-1) @export var minimum_resolution : Vector2i = Vector2i(1,1) -const _starting_resolutions : Dictionary = { - Vector2i(3840,2160): &"4K", - Vector2i(2560,1080): &"UW1080p", - Vector2i(1920,1080): &"1080p", - Vector2i(1366,768) : &"", - Vector2i(1536,864) : &"", - Vector2i(1280,720) : &"720p", - Vector2i(1440,900) : &"", - Vector2i(1600,900) : &"", - Vector2i(1024,600) : &"", - Vector2i(800,600) : &"" -} - -var _resolutions : Dictionary +const _starting_resolutions : Array[Vector2i] = [ + Vector2i(3840,2160), + Vector2i(2560,1080), + Vector2i(1920,1080), + Vector2i(1366,768), + Vector2i(1536,864), + Vector2i(1280,720), + Vector2i(1440,900), + Vector2i(1600,900), + Vector2i(1024,600), + Vector2i(800,600) +] + +var _resolutions : Array[Vector2i] const _regex_pattern : String = "(\\d+)\\s*[xX,]\\s*(\\d+)" var _regex : RegEx @@ -30,72 +28,103 @@ var _regex : RegEx func _ready(): assert(minimum_resolution.x > 0 and minimum_resolution.y > 0, "Minimum resolution must be positive!") for resolution_value in _starting_resolutions: - add_resolution(resolution_value, _starting_resolutions[resolution_value]) + add_resolution(resolution_value) assert(not _resolutions.is_empty(), "No valid starting resolutions!") _regex = RegEx.new() var err := _regex.compile(_regex_pattern) assert(err == OK, "Resolution RegEx failed to compile!") - func has_resolution(resolution_value : Vector2i) -> bool: return resolution_value in _resolutions -func add_resolution(resolution_value : Vector2i, resolution_name : StringName = &"") -> bool: +func add_resolution(resolution_value : Vector2i) -> bool: if has_resolution(resolution_value): return true - var res_dict := { value = resolution_value, name = &"" } - var display_name := "%sx%s" % [resolution_value.x, resolution_value.y] - if not resolution_name.is_empty(): - res_dict.name = resolution_name - display_name = "%s (%s)" % [display_name, resolution_name] - res_dict.display_name = StringName(display_name) if resolution_value.x < minimum_resolution.x or resolution_value.y < minimum_resolution.y: - push_error("Resolution %s is smaller than minimum (%sx%s)" % [res_dict.display_name, minimum_resolution.x, minimum_resolution.y]) + push_error("Resolution %dx%d is smaller than minimum (%dx%d)" % [resolution_value.x, resolution_value.y, minimum_resolution.x, minimum_resolution.y]) return false - resolution_added.emit(resolution_value, resolution_name, display_name) - _resolutions[resolution_value] = res_dict + _resolutions.append(resolution_value) + resolution_added.emit(resolution_value) return true -func get_resolution_value_list() -> Array: - var list := _resolutions.keys() +func get_resolution_value_list() -> Array[Vector2i]: + var list : Array[Vector2i] = [] + # Return a sorted copy instead of a reference to the private array + list.append_array(_resolutions) list.sort_custom(func(a, b): return a > b) return list -func get_resolution_name(resolution_value : Vector2i) -> StringName: - return _resolutions.get(resolution_value, { name = &"unknown resolution" }).name - -func get_resolution_display_name(resolution_value : Vector2i) -> StringName: - return _resolutions.get(resolution_value, { display_name = &"unknown resolution" }).display_name - func get_resolution_value_from_string(resolution_string : String) -> Vector2i: if not resolution_string.is_empty(): - for resolution in _resolutions.values(): - if resolution_string == resolution.name or resolution_string == resolution.display_name: - return resolution.value var result := _regex.search(resolution_string) if result: return Vector2i(result.get_string(1).to_int(), result.get_string(2).to_int()) return error_resolution func get_current_resolution() -> Vector2i: - var window := get_viewport().get_window() - match window.mode: - Window.MODE_EXCLUSIVE_FULLSCREEN, Window.MODE_FULLSCREEN: - return window.content_scale_size - _: - return window.size + var viewport := get_viewport() + if viewport != null: + var window := viewport.get_window() + if window != null: + match window.mode: + Window.MODE_EXCLUSIVE_FULLSCREEN, Window.MODE_FULLSCREEN: + return window.content_scale_size + _: + return window.size + push_error("Trying to get resolution before window exists!") + return error_resolution func set_resolution(resolution : Vector2i) -> void: if not has_resolution(resolution): push_warning("Setting resolution to non-standard value %sx%s" % [resolution.x, resolution.y]) - var window := get_viewport().get_window() - if get_current_resolution() != resolution: - resolution_changed.emit(resolution) - match window.mode: - Window.MODE_EXCLUSIVE_FULLSCREEN, Window.MODE_FULLSCREEN: - window.content_scale_size = resolution - _: - window.size = resolution - window.content_scale_size = Vector2i(0,0) - -func reset_resolution() -> void: - set_resolution(get_current_resolution()) + var viewport := get_viewport() + if viewport != null: + var window := viewport.get_window() + if window != null: + match window.mode: + Window.MODE_EXCLUSIVE_FULLSCREEN, Window.MODE_FULLSCREEN: + window.content_scale_size = resolution + _: + window.size = resolution + window.content_scale_size = Vector2i(0,0) + return + push_error("Trying to set resolution before window exists!") + +func get_current_window_mode() -> Window.Mode: + var viewport := get_viewport() + if viewport != null: + var window := viewport.get_window() + if window != null: + return window.mode + push_error("Trying to get window mode before it exists!") + return Window.MODE_WINDOWED + +func set_window_mode(mode : Window.Mode) -> void: + var viewport := get_viewport() + if viewport != null: + var window := viewport.get_window() + if window != null: + var current_resolution := get_current_resolution() + var current_monitor := window.current_screen + window.mode = mode + window.current_screen = current_monitor + set_resolution(current_resolution) + return + push_error("Trying to set window mode before it exists!") + +func get_current_monitor() -> int: + var viewport := get_viewport() + if viewport != null: + var window := viewport.get_window() + if window != null: + return window.current_screen + push_error("Trying to get monitor index before window exists!") + return 0 + +func set_monitor(index : int) -> void: + var viewport := get_viewport() + if viewport != null: + var window := viewport.get_window() + if window != null: + window.current_screen = index + return + push_error("Trying to set monitor index before window exists!") |