add kle key location recognition

This commit is contained in:
Teatov 2025-07-31 05:29:22 +10:00
parent c51b9a10ae
commit 56250d3236

View File

@ -49,6 +49,9 @@ const H2 := "h2"
const X2 := "x2"
const Y2 := "y2"
const KEY_DICT := "key_dict"
const POS := "pos"
var has_errors: bool = false
var _name: String
@ -108,32 +111,49 @@ func _load_json_file(path: String) -> Array:
func _deserialize(data: Array) -> Array[Array]:
var layout_rows: Array[Array] = []
var key_pos_dicts: Dictionary[Key, Array] = {}
var row_index: int = 0
for data_row: Variant in data:
if data_row is Array:
var layout_row := _deserialize_row(data_row as Array)
var layout_row := _deserialize_row(
data_row as Array, key_pos_dicts, row_index
)
layout_rows.append(layout_row)
row_index += 1
_get_key_locations(key_pos_dicts)
return layout_rows
func _deserialize_row(data_row: Array) -> Array[Dictionary]:
func _deserialize_row(
data_row: Array, key_pos_dicts: Dictionary[Key, Array], row_index: int
) -> Array[Dictionary]:
var layout_row: Array[Dictionary] = []
var key_pos := Vector2(0, row_index)
var current_key_data_dict: Dictionary = {}
for key_data: Variant in data_row:
if key_data is Dictionary:
current_key_data_dict = key_data as Dictionary
if key_data is String:
var key_dict := {
KeyProps.KEY:
_get_keycode_from_legend(
(key_data as String).split("\n"), current_key_data_dict
)
}
var legend := (key_data as String).split("\n")
var keycode := _get_keycode_from_legend(legend, current_key_data_dict)
var key_dict := {KeyProps.KEY: keycode}
key_dict.merge(_deserialize_key(current_key_data_dict))
layout_row.append(key_dict)
key_pos.x += key_dict[KeyProps.X] if key_dict.has(KeyProps.X) else 0.0
var key_pos_dict := {KEY_DICT: key_dict, POS: key_pos}
if key_pos_dicts.has(keycode):
key_pos_dicts[keycode].append(key_pos_dict)
else:
key_pos_dicts[keycode] = [key_pos_dict] as Array[Dictionary]
key_pos.x += key_dict[KeyProps.W] if key_dict.has(KeyProps.W) else 1.0
current_key_data_dict = _cleanup_key_data_dict(current_key_data_dict)
return layout_row
@ -220,3 +240,28 @@ func _get_numpad_keycode_from_legend(legend: Array[String]) -> Key:
return LABEL_TO_NUMPAD_KEYCODE_MAP[legend[0]]
return KEY_NONE
func _get_key_locations(key_pos_dicts: Dictionary[Key, Array]) -> void:
for dicts: Array[Dictionary] in key_pos_dicts.values():
if dicts.size() == 1:
continue
var key_pos_dict_left: Dictionary
var key_pos_dict_right: Dictionary
for key_pos_dict in dicts:
var key_pos := key_pos_dict[POS] as Vector2
if (
not key_pos_dict_left
or key_pos.x < (key_pos_dict_left[POS] as Vector2).x
):
key_pos_dict_left = key_pos_dict
continue
if (
not key_pos_dict_right
or key_pos.x > (key_pos_dict_right[POS] as Vector2).x
):
key_pos_dict_right = key_pos_dict
continue
if key_pos_dict_left != key_pos_dict_right:
key_pos_dict_left[KEY_DICT][KeyProps.LOC] = KEY_LOCATION_LEFT
key_pos_dict_right[KEY_DICT][KeyProps.LOC] = KEY_LOCATION_RIGHT