From 82b4fc6d84b876cf84c784a33755477bbdb2d851 Mon Sep 17 00:00:00 2001 From: teatov Date: Mon, 28 Jul 2025 18:39:27 +1000 Subject: [PATCH] finish basic layout generation --- scenes/game_key.tscn | 2 +- scenes/test.tscn | 8 +++- scripts/game_key.gd | 6 ++- scripts/game_keyboard.gd | 33 +++++++++++----- scripts/key_props.gd | 15 ++++++- scripts/layouts/layout_ansi.gd | 71 +++++++++++++++++++++++++++++++++- 6 files changed, 119 insertions(+), 16 deletions(-) diff --git a/scenes/game_key.tscn b/scenes/game_key.tscn index a5f6576..21951ea 100644 --- a/scenes/game_key.tscn +++ b/scenes/game_key.tscn @@ -14,7 +14,7 @@ main_label = NodePath("MainLabel") mesh = SubResource("BoxMesh_72e4x") [node name="MainLabel" type="Label3D" parent="."] -transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, -0.0669861, 0.075748, -0.0306985) +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0.076, 0) pixel_size = 0.0015 shaded = true double_sided = false diff --git a/scenes/test.tscn b/scenes/test.tscn index 8069f21..8d4c344 100644 --- a/scenes/test.tscn +++ b/scenes/test.tscn @@ -15,8 +15,14 @@ orientation = 1 [node name="Test" type="Node3D"] -[node name="Camera3D" type="Camera3D" parent="."] +[node name="OrthogonalCamera" type="Camera3D" parent="."] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 4.76838e-07, 4.90848, -0.212414) +projection = 1 +size = 2.895 + +[node name="PerspectiveCamera" type="Camera3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.48725, 0.873262, 0, -0.873262, 0.48725, 2.38419e-07, 2.79306, 0.864002) +current = true [node name="FloorMesh" type="MeshInstance3D" parent="."] mesh = SubResource("QuadMesh_ij1v8") diff --git a/scripts/game_key.gd b/scripts/game_key.gd index a6a8b31..f2e0284 100644 --- a/scripts/game_key.gd +++ b/scripts/game_key.gd @@ -33,7 +33,11 @@ func _unhandled_input(event: InputEvent) -> void: var event_key := event as InputEventKey - if event_key.physical_keycode != props.physical_keycode: + if ( + event_key.physical_keycode != props.physical_keycode + or event_key.location != props.location + ): return _is_pressed = event_key.is_pressed() + # scale.z = 0.75 if _is_pressed else 1.0 diff --git a/scripts/game_keyboard.gd b/scripts/game_keyboard.gd index d50bf1f..22d729b 100644 --- a/scripts/game_keyboard.gd +++ b/scripts/game_keyboard.gd @@ -1,7 +1,9 @@ class_name GameKeyboard extends Node3D @export var _key_size: float = 0.25 -@export var _key_gap: float = 0.1 +@export var _key_gap: float = 0.05 + +@onready var _gap_to_size_ratio: float = _key_gap / _key_size func _ready() -> void: @@ -10,23 +12,28 @@ func _ready() -> void: func _generate_keys() -> void: var layout_rows := LayoutAnsi.rows() + + for row in layout_rows: + _set_width_ratios_with_gaps(row) + var rows_amount := layout_rows.size() + var row_width := ( + _get_row_width(layout_rows[0]) + (layout_rows[0].size() - 1) * _key_gap + ) for i in range(rows_amount): var row := layout_rows[i] as Array[KeyProps] - _generate_row(row as Array[KeyProps], i, rows_amount) + _generate_row(row as Array[KeyProps], i, row_width, rows_amount) -func _generate_row(row: Array[KeyProps], row_index: int, rows_amount: int) -> void: - var keys_amount: int = row.size() - var row_width := _get_row_width(row) - - var offset_x: float = ((row_width - _key_size) + (keys_amount - 1) * _key_gap) / 2 +func _generate_row( + row: Array[KeyProps], row_index: int, row_width: float, rows_amount: int +) -> void: var offset_z: float = ( ((rows_amount - 1) * _key_size + (rows_amount - 1) * _key_gap) / 2 ) - var key_pos_x: float = -offset_x + var key_pos_x: float = -row_width / 2 + _key_size / 2 var key_pos_z: float = row_index * _key_size + row_index * _key_gap - offset_z for key_props in row: @@ -41,8 +48,16 @@ func _generate_row(row: Array[KeyProps], row_index: int, rows_amount: int) -> vo key_pos_x += _key_size * key_props.width_ratio + _key_gap +func _set_width_ratios_with_gaps(row: Array[KeyProps]) -> void: + for key_props in row: + if key_props.width_ratio_init > 1: + key_props.width_ratio += ( + _gap_to_size_ratio * (key_props.width_ratio_init - 1) + ) + + func _get_row_width(row: Array[KeyProps]) -> float: var width: float = 0 for key_props in row: - width += _key_size * key_props.width_ratio + width += _key_size * key_props.width_ratio_init return width diff --git a/scripts/key_props.gd b/scripts/key_props.gd index 67e7a35..2279692 100644 --- a/scripts/key_props.gd +++ b/scripts/key_props.gd @@ -1,12 +1,23 @@ class_name KeyProps var physical_keycode: Key +var location: KeyLocation var input_event: InputEventKey +var width_ratio_init: float var width_ratio: float -func _init(_physical_keycode: Key, _width_ratio: float = 1) -> void: +func _init( + _physical_keycode: Key, + _width_ratio: float = 1, + _location: KeyLocation = KEY_LOCATION_UNSPECIFIED +) -> void: physical_keycode = _physical_keycode + location = _location + input_event = InputEventKey.new() input_event.physical_keycode = physical_keycode - width_ratio = _width_ratio + input_event.location = _location + + width_ratio_init = _width_ratio + width_ratio = width_ratio_init diff --git a/scripts/layouts/layout_ansi.gd b/scripts/layouts/layout_ansi.gd index 86b584d..4815a8a 100644 --- a/scripts/layouts/layout_ansi.gd +++ b/scripts/layouts/layout_ansi.gd @@ -18,8 +18,75 @@ static func rows() -> Array[Array]: KeyProps.new(KEY_0), KeyProps.new(KEY_MINUS), KeyProps.new(KEY_EQUAL), - KeyProps.new(KEY_BACKSPACE, 2), + KeyProps.new(KEY_BACKSPACE, 3.0 / 2.0), ] as Array[KeyProps] - ) + ), + ( + [ + KeyProps.new(KEY_TAB, 3.0 / 2.0), + KeyProps.new(KEY_Q), + KeyProps.new(KEY_W), + KeyProps.new(KEY_E), + KeyProps.new(KEY_R), + KeyProps.new(KEY_T), + KeyProps.new(KEY_Y), + KeyProps.new(KEY_U), + KeyProps.new(KEY_I), + KeyProps.new(KEY_O), + KeyProps.new(KEY_P), + KeyProps.new(KEY_BRACKETLEFT), + KeyProps.new(KEY_BRACKETRIGHT), + KeyProps.new(KEY_BACKSLASH), + ] + as Array[KeyProps] + ), + ( + [ + KeyProps.new(KEY_CAPSLOCK, 7.0 / 4.0), + KeyProps.new(KEY_A), + KeyProps.new(KEY_S), + KeyProps.new(KEY_D), + KeyProps.new(KEY_F), + KeyProps.new(KEY_G), + KeyProps.new(KEY_H), + KeyProps.new(KEY_J), + KeyProps.new(KEY_K), + KeyProps.new(KEY_L), + KeyProps.new(KEY_SEMICOLON), + KeyProps.new(KEY_APOSTROPHE), + KeyProps.new(KEY_ENTER, 7.0 / 4.0), + ] + as Array[KeyProps] + ), + ( + [ + KeyProps.new(KEY_SHIFT, 9.0 / 4.0, KEY_LOCATION_LEFT), + KeyProps.new(KEY_Z), + KeyProps.new(KEY_X), + KeyProps.new(KEY_C), + KeyProps.new(KEY_V), + KeyProps.new(KEY_B), + KeyProps.new(KEY_N), + KeyProps.new(KEY_M), + KeyProps.new(KEY_COMMA), + KeyProps.new(KEY_PERIOD), + KeyProps.new(KEY_SLASH), + KeyProps.new(KEY_SHIFT, 9.0 / 4.0, KEY_LOCATION_RIGHT), + ] + as Array[KeyProps] + ), + ( + [ + KeyProps.new(KEY_CTRL, 7.0 / 4.0, KEY_LOCATION_LEFT), + KeyProps.new(KEY_META, 1, KEY_LOCATION_LEFT), + KeyProps.new(KEY_ALT, 1, KEY_LOCATION_LEFT), + KeyProps.new(KEY_SPACE, 6), + KeyProps.new(KEY_ALT, 1, KEY_LOCATION_RIGHT), + KeyProps.new(KEY_META, 1, KEY_LOCATION_RIGHT), + KeyProps.new(KEY_MENU), + KeyProps.new(KEY_CTRL, 7.0 / 4.0, KEY_LOCATION_RIGHT), + ] + as Array[KeyProps] + ), ]