Last active
May 29, 2022 08:00
-
-
Save mnikn/9bb4126977bb67be64526eb1b8083242 to your computer and use it in GitHub Desktop.
godot utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
class_name ArrayUtils | |
static func filter(arr: Array, prop: Dictionary) -> Array: | |
var res = [] | |
for item in arr: | |
for key in prop.keys(): | |
if prop[key] is Array and item[key] in prop[key]: | |
res.push_back(item) | |
elif (!prop[key] is Array) and prop[key] == item[key]: | |
res.push_back(item) | |
return res | |
static func find(arr: Array, prop: Dictionary): | |
var res = filter(arr, prop) | |
if len(res) > 0: | |
return res[0] | |
return null | |
static func concat(arr1: Array, arr2: Array) -> Array: | |
var res = [] | |
for i in arr1: | |
res.push_back(i) | |
for i in arr2: | |
res.push_back(i) | |
return res | |
static func join(arr: PoolStringArray, code: String): | |
var res = "" | |
for item in arr: | |
if len(item) > 0: | |
res += item + code | |
return res.substr(0, len(res) - len(code)) | |
static func map(arr: Array, property: String) -> Array: | |
var res = [] | |
for item in arr: | |
res.push_back(item[property]) | |
return res | |
static func intersect(arr1: Array, arr2: Array) -> Array: | |
var res = [] | |
for item in arr1: | |
if arr2.find(item) != -1: | |
res.push_back(item) | |
return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
class_name DictUtils | |
static func get_value(obj, property: String, default_value = null): | |
var arr = property.split(".") | |
if len(arr) <= 0: | |
return null | |
return _do_get_value(obj, arr, default_value) | |
static func _do_get_value(obj, property_arr: PoolStringArray, default_value): | |
if len(property_arr) <= 0 or obj == null: | |
return default_value | |
var tmp = default_value | |
if obj.has(property_arr[0]): | |
tmp = obj[property_arr[0]] | |
if len(property_arr) == 1: | |
return tmp | |
var sub_arr = [] | |
for i in range(1, len(property_arr)): | |
sub_arr.push_back(property_arr[i]) | |
return _do_get_value(tmp, sub_arr, default_value) | |
static func assign(obj: Dictionary, obj2: Dictionary) -> Dictionary: | |
var res = {} | |
for key in obj.keys(): | |
res[key] = obj[key] | |
for key in obj2.keys(): | |
res[key] = obj2[key] | |
return res | |
static func copy(obj: Dictionary) -> Dictionary: | |
var res = {} | |
for key in obj.keys(): | |
res[key] = _do_copy(obj[key]) | |
return res | |
static func _do_copy(obj): | |
if obj is Dictionary: | |
var res = {} | |
for key in obj.keys(): | |
res[key] = _do_copy(obj[key]) | |
return res | |
elif obj is Array: | |
var res = [] | |
for item in obj: | |
res.push_back(_do_copy(item)) | |
return res | |
else: | |
return obj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
const LANG_EN = "en" | |
const LANG_ZH = "zh" | |
var current_lang = LANG_EN if OS.get_locale_language() != "zh" else LANG_ZH | |
var lang_data = {} | |
func _ready(): | |
self.set_lang(LANG_ZH) | |
func set_lang(lang: String): | |
if lang == LANG_ZH: | |
self.lang_data = JsonUtils.load_json_file("res://data/i18n/zh.json") | |
else: | |
self.lang_data = JsonUtils.load_json_file("res://data/i18n/en.json") | |
self.current_lang = lang | |
func t(key: String) -> String: | |
if not self.lang_data.has(key): | |
return key | |
return self.lang_data[key] | |
func translate(obj: Dictionary) -> String: | |
if obj.has(current_lang): | |
return obj[current_lang] | |
return "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
class_name JsonUtils | |
static func load_json_file(path: String) -> Dictionary: | |
var file = File.new() | |
file.open(path, File.READ) | |
var content = file.get_as_text() | |
var res = JSON.parse(content).result | |
file.close() | |
return res | |
static func write_json_file(path: String, data: Dictionary): | |
var file = File.new() | |
file.open(path, File.WRITE) | |
var content = JSON.print(data, " ") | |
file.store_string(content) | |
file.close() | |
return content | |
static func exits_json_file(path: String): | |
var file = File.new() | |
return file.file_exists(path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node | |
class_name NodeUtils | |
static func clear_children(node: Node): | |
for child in node.get_children(): | |
child.queue_free() | |
static func get_all_children(node: Node): | |
var arr = [] | |
_do_get_all_children(node, arr) | |
return arr | |
static func _do_get_all_children(node: Node, arr: Array): | |
if node == null: | |
return arr | |
arr.push_back(node) | |
for child in node.get_children(): | |
_do_get_all_children(child, arr) | |
return arr | |
static func get_control_children(node: Node, arr: Array): | |
if node == null: | |
return arr | |
for child in node.get_children(): | |
if child is Control: | |
arr.push_back(child) | |
if child.get_child_count() > 0: | |
get_control_children(child, arr) | |
return arr | |
static func children_ready(node): | |
for child_node in node.get_children(): | |
yield(child_node, "ready") | |
yield(node.get_tree().create_timer(0.00001), "timeout") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment