Skip to content

Commit

Permalink
Rebindable Inputs update
Browse files Browse the repository at this point in the history
Rebindable Inputs:
- This should now work! (please report any issues)
- When you first try this, you'll get an error that now keybindings are found in your config file, this is to be expected.
- Once you save your keybindings (press "Apply" in the options menu), this error should no longer occur.
- This was only tested with the standard button sets. Special buttons for individual gamepads (like Playstations touchpad button) have not been tested and might display the wrong icon when used.
- Be aware that these bindings get saved/loaded on runtime, so they will not show up as changed in the Godot Editor input map!
- If you own a PS5 gamepad and would like to contribute, please let me know (open a thread or issue), thanks!
  • Loading branch information
Phazorknight committed Nov 11, 2024
1 parent bbe1575 commit 1f960d7
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 126 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![COGITO_banner](docs/Cogito_capsule_202402_jpg.jpg)
# COGITO
[![GodotEngine](https://img.shields.io/badge/Godot_4.3_stable-blue?logo=godotengine&logoColor=white)](https://godotengine.org/) [![COGITO](https://img.shields.io/badge/beta_202410-35A1D7?label=COGITO&labelColor=0E887A)](https://github.com/Phazorknight/Cogito)
beta 202410.02
[![GodotEngine](https://img.shields.io/badge/Godot_4.3_stable-blue?logo=godotengine&logoColor=white)](https://godotengine.org/) [![COGITO](https://img.shields.io/badge/beta_202411-35A1D7?label=COGITO&labelColor=0E887A)](https://github.com/Phazorknight/Cogito)
beta 202411.01

## What is it?
COGITO is a first Person Immersive Sim Template Project for Godot Engine 4.
Expand Down
57 changes: 23 additions & 34 deletions addons/cogito/Components/DynamicInputIcon.gd
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ func update_icon_kbm(): # Sets the bound action to keyboard and mouse icon
set_texture(keyboard_icons)

var keyboard_input = InputHelper.get_keyboard_input_for_action(action_name)
print("Dynamic Input Icon: update_icon_kbm: keyboard_input for action ", action_name, " = ", keyboard_input)
if keyboard_input is InputEventKey:
frame = keycode_to_frame_index(OS.get_keycode_string(keyboard_input.get_physical_keycode()))
print("Dynamic Input Icon: update_icon_kbm: Keycode string for action ", action_name, " is ", OS.get_keycode_string(keyboard_input.keycode) )
frame = keycode_to_frame_index(OS.get_keycode_string(keyboard_input.keycode))
elif keyboard_input is InputEventMouseButton:
if keyboard_input.get_button_index() == 2:
frame = keycode_to_frame_index("Mouse Right")
Expand All @@ -115,31 +117,6 @@ func update_icon_kbm(): # Sets the bound action to keyboard and mouse icon
return


func update_icon_generic_gamepad():
hframes = 10
vframes = 10
set_texture(gamepad_icons)

var joypad_input = InputHelper.get_joypad_input_for_action(action_name)
if joypad_input is InputEventJoypadButton:
#print("DynamicInputIcon: Action=", action_name, ". Joypad btn=", joypad_input.button_index)
set_texture(gamepad_icons)
frame = joypad_input.button_index

elif joypad_input is InputEventJoypadMotion:
#print("DynamicInputIcon: Action=", action_name, ". Joypad motion=", joypad_motion.axis)
set_texture(gamepad_icons)
if joypad_input.axis == 0 or joypad_input.axis == 1:
frame = 8
if joypad_input.axis == 2 or joypad_input.axis == 3:
frame = 9

if joypad_input.axis == 5:
frame = 18 #Sets icon to RT
if joypad_input.axis == 4:
frame = 17 #Sets icon to LT


func _is_steam_deck() -> bool:
if RenderingServer.get_rendering_device() == null:
print("No rendering device detected.")
Expand All @@ -155,17 +132,29 @@ func _is_steam_deck() -> bool:
func gamepad_motion_to_frame_index(joypad_input_motion: InputEventJoypadMotion):
match joypad_input_motion.axis:
0: # LEFT STICK H AXIS
return 41
if joypad_input_motion.axis_value > 0: # LEFT STICK RIGHT
return 38
else: # LEFT STICK LEFT
return 37
1: # LEFT STICK V AXIS
return 42
if joypad_input_motion.axis_value < 0: # LEFT STICK UP
return 39
else: # LEFT STICK DOWN
return 40
2: # RIGHT STICK H AXIS
return 53
if joypad_input_motion.axis_value > 0: # RIGHT STICK RIGHT
return 49
else: # RIGHT STICK LEFT
return 50
3: # RIGHT STICK V AXIS
return 54
if joypad_input_motion.axis_value < 0: # RIGHT STICK UP
return 51
else: # RIGHT STICK DOWN
return 52
5: # RIGHT TRIGGER
return 8
6: # LEFT TRIGGER
return 9
return 34
4: # LEFT TRIGGER
return 35
null:
return 10
_:
Expand Down Expand Up @@ -296,7 +285,7 @@ func keycode_to_frame_index(key_code_string: String) -> int:
return 57
"Escape":
return 60
"Control":
"Ctrl":
return 61
"Alt":
return 62
Expand Down
4 changes: 2 additions & 2 deletions addons/cogito/Components/DynamicInputIcon.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

[node name="DynamicInputIcon" type="Sprite2D"]
position = Vector2(50, 50)
texture = ExtResource("1_b735f")
texture = ExtResource("1_n4wi0")
hframes = 12
vframes = 12
frame = 110
frame = 38
region_rect = Rect2(0, 0, 100, 100)
script = ExtResource("1_7t81w")
gamepad_icons = ExtResource("3_hxvpc")
Expand Down
8 changes: 4 additions & 4 deletions addons/cogito/DemoScenes/COGITO_3_Lobby.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ volumetric_fog_temporal_reprojection_amount = 0.85
density = 0.04
albedo = Color(0.694118, 0.694118, 0.694118, 1)

[sub_resource type="Resource" id="Resource_y2rpe"]
[sub_resource type="Resource" id="Resource_rrue1"]
resource_local_to_scene = true
script = ExtResource("4_0kggm")
grid = true
Expand Down Expand Up @@ -2425,7 +2425,7 @@ shadow_mesh = SubResource("ArrayMesh_3ecdw")
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_upcaa"]
data = PackedVector3Array(0.6928, 0.0903, -1.6, 0, 0.0903, -1.2, 0.6128, 0.0903, -0.88, 0.6128, 0.0903, -0.88, 0.7728, 0.0903, -0.88, 0.6928, 0.0903, -1.6, 0.7728, 0.0903, -0.88, 1.3856, 0.0903, -1.2, 0.6928, 0.0903, -1.6, 0.7728, 0.0903, -0.88, 0.7728, 0.0903, -0.72, 1.3856, 0.0903, -1.2, 0.6128, 0.0903, -0.72, 0.6128, 0.0903, -0.88, 0, 0.0903, -1.2, 0.6928, 0.0903, 0, 0.6128, 0.0903, -0.72, 0, 0.0903, -1.2, 0.6928, 0.0903, 0, 0.7728, 0.0903, -0.72, 0.6128, 0.0903, -0.72, 0, 0.0903, -0.4, 0.6928, 0.0903, 0, 0, 0.0903, -1.2, 0, 0.0903, -1.2, 0, 0.0903, -0.8, 0, 0.0903, -0.4, 0.6928, 0.0903, 0, 1.3856, 0.0903, -0.4, 0.7728, 0.0903, -0.72, 1.3856, 0.0903, -0.4, 1.3856, 0.0903, -1.2, 0.7728, 0.0903, -0.72, 1.3856, 0.0903, -0.4, 1.3856, 0.0903, -0.8, 1.3856, 0.0903, -1.2, 0.6928, 0.1935, 0, 0, 0.1935, -0.4, 0, 0.1935, -1.2, 0, 0.1935, -1.2, 0.6928, 0.1935, -1.6, 0.6928, 0.1935, 0, 0.6928, 0.1935, -1.6, 1.3856, 0.1935, -0.4, 0.6928, 0.1935, 0, 0.6928, 0.1935, -1.6, 1.3856, 0.1935, -1.2, 1.3856, 0.1935, -0.4, 0.6928, 0.1935, -1.6, 0.6928, 0.0903, -1.6, 1.3856, 0.0903, -1.2, 1.3856, 0.0903, -1.2, 1.3856, 0.1935, -1.2, 0.6928, 0.1935, -1.6, 1.3856, 0.0903, -0.8, 1.3856, 0.0903, -0.4, 1.3856, 0.1935, -0.4, 1.3856, 0.1935, -0.4, 1.3856, 0.1935, -1.2, 1.3856, 0.0903, -0.8, 1.3856, 0.1935, -1.2, 1.3856, 0.0903, -1.2, 1.3856, 0.0903, -0.8, 1.3856, 0.1935, -0.4, 1.3856, 0.0903, -0.4, 0.6928, 0.0903, 0, 0.6928, 0.0903, 0, 0.6928, 0.1935, 0, 1.3856, 0.1935, -0.4, 0.6928, 0.1935, 0, 0.6928, 0.0903, 0, 0, 0.0903, -0.4, 0, 0.0903, -0.4, 0, 0.1935, -0.4, 0.6928, 0.1935, 0, 0, 0.1935, -1.2, 0, 0.1935, -0.4, 0, 0.0903, -0.4, 0, 0.0903, -0.4, 0, 0.0903, -0.8, 0, 0.1935, -1.2, 0, 0.0903, -0.8, 0, 0.0903, -1.2, 0, 0.1935, -1.2, 0, 0.1935, -1.2, 0, 0.0903, -1.2, 0.6928, 0.0903, -1.6, 0.6928, 0.0903, -1.6, 0.6928, 0.1935, -1.6, 0, 0.1935, -1.2, 0.7728, 0.0903, -0.72, 0.7728, -0.4, -0.72, 0.6128, -0.4, -0.72, 0.6128, -0.4, -0.72, 0.6128, 0.0903, -0.72, 0.7728, 0.0903, -0.72, 0.6128, 0.0903, -0.88, 0.6128, 0.0903, -0.72, 0.6128, -0.4, -0.72, 0.6128, -0.4, -0.72, 0.6128, -0.4, -0.88, 0.6128, 0.0903, -0.88, 0.6128, 0.0903, -0.88, 0.6128, -0.4, -0.88, 0.7728, -0.4, -0.88, 0.7728, -0.4, -0.88, 0.7728, 0.0903, -0.88, 0.6128, 0.0903, -0.88, 0.7728, -0.4, -0.88, 0.7728, -0.4, -0.72, 0.7728, 0.0903, -0.72, 0.7728, 0.0903, -0.72, 0.7728, 0.0903, -0.88, 0.7728, -0.4, -0.88, 0.8128, -0.4, -0.68, 0.8128, -0.44, -0.68, 0.5728, -0.44, -0.68, 0.5728, -0.44, -0.68, 0.5728, -0.4, -0.68, 0.8128, -0.4, -0.68, 0.5728, -0.4, -0.92, 0.5728, -0.4, -0.68, 0.5728, -0.44, -0.68, 0.5728, -0.44, -0.68, 0.5728, -0.44, -0.92, 0.5728, -0.4, -0.92, 0.8128, -0.44, -0.92, 0.8128, -0.44, -0.68, 0.8128, -0.4, -0.68, 0.8128, -0.4, -0.68, 0.8128, -0.4, -0.92, 0.8128, -0.44, -0.92, 0.5728, -0.4, -0.68, 0.5728, -0.4, -0.92, 0.6128, -0.4, -0.72, 0.6128, -0.4, -0.72, 0.8128, -0.4, -0.68, 0.5728, -0.4, -0.68, 0.6128, -0.4, -0.72, 0.7728, -0.4, -0.72, 0.8128, -0.4, -0.68, 0.7728, -0.4, -0.72, 0.7728, -0.4, -0.88, 0.8128, -0.4, -0.68, 0.6128, -0.4, -0.88, 0.6128, -0.4, -0.72, 0.5728, -0.4, -0.92, 0.5728, -0.4, -0.92, 0.8128, -0.4, -0.92, 0.6128, -0.4, -0.88, 0.8128, -0.4, -0.92, 0.7728, -0.4, -0.88, 0.6128, -0.4, -0.88, 0.8128, -0.4, -0.92, 0.8128, -0.4, -0.68, 0.7728, -0.4, -0.88, 0.4928, -0.44, -1, 0.4928, -0.44, -0.6, 0.4928, -0.5, -0.6, 0.4928, -0.5, -0.6, 0.4928, -0.5, -1, 0.4928, -0.44, -1, 0.4928, -0.44, -1, 0.4928, -0.5, -1, 0.8928, -0.5, -1, 0.8928, -0.5, -1, 0.8928, -0.44, -1, 0.4928, -0.44, -1, 0.8928, -0.44, -0.6, 0.8928, -0.5, -0.6, 0.4928, -0.5, -0.6, 0.4928, -0.5, -0.6, 0.4928, -0.44, -0.6, 0.8928, -0.44, -0.6, 0.8928, -0.5, -1, 0.8928, -0.5, -0.6, 0.8928, -0.44, -0.6, 0.8928, -0.44, -0.6, 0.8928, -0.44, -1, 0.8928, -0.5, -1, 0.4928, -0.44, -0.6, 0.4928, -0.44, -1, 0.5728, -0.44, -0.68, 0.5728, -0.44, -0.68, 0.8928, -0.44, -0.6, 0.4928, -0.44, -0.6, 0.5728, -0.44, -0.68, 0.8128, -0.44, -0.68, 0.8928, -0.44, -0.6, 0.8128, -0.44, -0.68, 0.8128, -0.44, -0.92, 0.8928, -0.44, -0.6, 0.5728, -0.44, -0.92, 0.5728, -0.44, -0.68, 0.4928, -0.44, -1, 0.4928, -0.44, -1, 0.8928, -0.44, -1, 0.5728, -0.44, -0.92, 0.8928, -0.44, -1, 0.8128, -0.44, -0.92, 0.5728, -0.44, -0.92, 0.8928, -0.44, -1, 0.8928, -0.44, -0.6, 0.8128, -0.44, -0.92, 0.9128, -0.54, -1.02, 0.4728, -0.54, -1.02, 0.4728, -0.54, -0.58, 0.4728, -0.54, -0.58, 0.9128, -0.54, -0.58, 0.9128, -0.54, -1.02, 0.4928, -0.5, -1, 0.4728, -0.54, -1.02, 0.9128, -0.54, -1.02, 0.9128, -0.54, -1.02, 0.8928, -0.5, -1, 0.4928, -0.5, -1, 0.9128, -0.54, -1.02, 0.9128, -0.54, -0.58, 0.8928, -0.5, -0.6, 0.8928, -0.5, -0.6, 0.8928, -0.5, -1, 0.9128, -0.54, -1.02, 0.8928, -0.5, -0.6, 0.9128, -0.54, -0.58, 0.4728, -0.54, -0.58, 0.4728, -0.54, -0.58, 0.4928, -0.5, -0.6, 0.8928, -0.5, -0.6, 0.4928, -0.5, -0.6, 0.4728, -0.54, -0.58, 0.4728, -0.54, -1.02, 0.4728, -0.54, -1.02, 0.4928, -0.5, -1, 0.4928, -0.5, -0.6, 0.5728, -0.4, -0.92, 0.5728, -0.44, -0.92, 0.8128, -0.44, -0.92, 0.8128, -0.44, -0.92, 0.8128, -0.4, -0.92, 0.5728, -0.4, -0.92)

[sub_resource type="Resource" id="Resource_j3vw2"]
[sub_resource type="Resource" id="Resource_wc1fr"]
resource_local_to_scene = true
script = ExtResource("4_0kggm")
grid = true
Expand Down Expand Up @@ -3167,7 +3167,7 @@ material = SubResource("FogMaterial_4avjx")

[node name="Player" parent="." instance=ExtResource("3_mgle8")]
transform = Transform3D(-1, 0, 7.45058e-07, 0, 1, 0, -7.45058e-07, 0, -1, 3.03073, 0.905039, -17.9321)
inventory_data = SubResource("Resource_y2rpe")
inventory_data = SubResource("Resource_rrue1")
step_height_camera_lerp = 1.5

[node name="QUESTS" type="Node3D" parent="."]
Expand Down Expand Up @@ -6991,7 +6991,7 @@ shape = SubResource("ConcavePolygonShape3D_upcaa")

[node name="kitchenFridgeContainer2" parent="BREAK_ROOM" instance=ExtResource("57_fdyfl")]
transform = Transform3D(-1, 0, -1.50996e-07, 0, 1, 0, 1.50996e-07, 0, -1, 3.65301, -4.84288e-08, 6.17191)
inventory_data = SubResource("Resource_j3vw2")
inventory_data = SubResource("Resource_wc1fr")

[node name="kitchenMicrowave" parent="BREAK_ROOM" instance=ExtResource("43_rl17a")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 1.98491, 0.84, 6.39503)
Expand Down
4 changes: 2 additions & 2 deletions addons/cogito/DemoScenes/COGITO_4_Laboratory.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ volumetric_fog_ambient_inject = 0.1
volumetric_fog_sky_affect = 0.1
volumetric_fog_temporal_reprojection_amount = 0.85

[sub_resource type="Resource" id="Resource_v8bab"]
[sub_resource type="Resource" id="Resource_gtshc"]
resource_local_to_scene = true
script = ExtResource("4_hlewe")
grid = true
Expand Down Expand Up @@ -899,7 +899,7 @@ environment = SubResource("Environment_obnk3")
[node name="Player" parent="." instance=ExtResource("2_7qwrr")]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 5.13854, 0.8, -5.43073)
inventory_data = SubResource("Resource_v8bab")
inventory_data = SubResource("Resource_gtshc")
[node name="CONNECTOR_TO_LOBBY" type="Node3D" parent="."]
Expand Down
20 changes: 20 additions & 0 deletions addons/cogito/EasyMenus/Components/MenuSeparator.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[gd_scene load_steps=2 format=3 uid="uid://cbv85ok0isnb3"]

[ext_resource type="Script" path="res://addons/cogito/EasyMenus/Components/menu_separator.gd" id="1_6oy01"]

[node name="MenuSeparator" type="HBoxContainer"]
alignment = 1
script = ExtResource("1_6oy01")

[node name="HSeparator" type="HSeparator" parent="."]
layout_mode = 2
size_flags_horizontal = 3

[node name="Label" type="Label" parent="."]
layout_mode = 2
text = "Input Bindings (WIP!)"
horizontal_alignment = 1

[node name="HSeparator2" type="HSeparator" parent="."]
layout_mode = 2
size_flags_horizontal = 3
24 changes: 18 additions & 6 deletions addons/cogito/EasyMenus/Components/gamepad_bind_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class_name GamepadBindButton

@onready var gamepad_input_icon: Sprite2D = $MarginContainer/DynamicInputIcon

var is_remapping: bool = false

func _init():
toggle_mode = true
theme_type_variation = "RemapButton"
Expand All @@ -19,7 +21,7 @@ func _ready():


func _toggled(button_pressed):
set_process_unhandled_input(button_pressed)
is_remapping = button_pressed
if button_pressed:
text = "..."
gamepad_input_icon.visible = false
Expand All @@ -29,19 +31,29 @@ func _toggled(button_pressed):
grab_focus()


func _unhandled_input(event):
if event is InputEventJoypadMotion:
get_viewport().set_input_as_handled()
func _input(event):
if !is_remapping:
return

if event is InputEventJoypadMotion:
if abs(event.axis_value) > .1: # Adding threshold for joystick axis input mapping
InputHelper.set_joypad_input_for_action(action,event,false)
text = ""
gamepad_input_icon.visible = true
update_icon()
is_remapping = false
button_pressed = false

if event is InputEventJoypadButton:
if event.pressed:
InputHelper.set_joypad_input_for_action(action,event,true)
get_viewport().set_input_as_handled()
InputHelper.set_joypad_input_for_action(action,event,false)
text = ""
gamepad_input_icon.visible = true
update_icon()
is_remapping = false
button_pressed = false

accept_event()


func update_icon():
Expand Down
49 changes: 42 additions & 7 deletions addons/cogito/EasyMenus/Components/input_bindings.gd
Original file line number Diff line number Diff line change
@@ -1,37 +1,72 @@
extends Node

@export var remap_entry: PackedScene
@export var separator_entry: PackedScene

@onready var bindings_container: VBoxContainer = %BindingsContainer

var config = ConfigFile.new()


var input_actions = {
"separator_movement": "MOVEMENT",
"forward": "Move forward",
"back": "Move back",
"left": "Move left",
"right": "Move right",
"jump": "Jump",
"crouch": "Crouch",
"separator_actions": "ACTIONS",
"action_primary": "Primary Action",
"action_secondary": "Secondary Action",
"interact": "Interact 1",
"interact2": "Interact 2",
"menu": "Pause",
"inventory": "Inventory",
"reload": "Reload",
"quickslot_1": "Quickslot 1",
"quickslot_2": "Quickslot 2",
"quickslot_3": "Quickslot 3",
"quickslot_4": "Quickslot 4",
"separator_inventory": "MENUS",
"menu": "Pause",
"inventory": "Inventory",
"inventory_drop_item": "Quick Drop Item",
"inventory_move_item": "Move Item",
"inventory_use_item": "Use Item",
}

func _ready() -> void:
load_keybindings_from_config()
create_action_remap_items()



func load_keybindings_from_config():
var err = config.load(OptionsConstants.config_file_name)
if err != 0:
print("Keybindings: Loading options config failed.")
#save_keybindings_to_config()

var serialized_inputs = config.get_value(OptionsConstants.key_binds, OptionsConstants.input_helper_string)
if serialized_inputs:
InputHelper.deserialize_inputs_for_actions(serialized_inputs)
else:
print("Keybindings: No saved bindings found.")



func save_keybindings_to_config():
var serialized_inputs = InputHelper.serialize_inputs_for_actions()
config.set_value(OptionsConstants.key_binds, OptionsConstants.input_helper_string, serialized_inputs)
config.save(OptionsConstants.config_file_name)


func create_action_remap_items() -> void:
for action in input_actions:
var input_entry = remap_entry.instantiate()
input_entry.action = action
bindings_container.add_child(input_entry)
input_entry.label.text = input_actions[action]
if action.contains("separator"):
var separator = separator_entry.instantiate()
bindings_container.add_child(separator)
separator.separator_text = input_actions[action]
else:
var input_entry = remap_entry.instantiate()
input_entry.action = action
bindings_container.add_child(input_entry)
input_entry.label.text = input_actions[action]
2 changes: 1 addition & 1 deletion addons/cogito/EasyMenus/Components/kbm_bind_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func _input(event):
return

if event is InputEventKey || ( event is InputEventMouseButton && event.pressed):
InputHelper.set_keyboard_input_for_action(action,event,true)
InputHelper.set_keyboard_input_for_action(action,event,false)
text = ""
kbm_input_icon.visible = true
update_icon()
Expand Down
7 changes: 7 additions & 0 deletions addons/cogito/EasyMenus/Components/menu_separator.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends HBoxContainer
@onready var label: Label = $Label

var separator_text: String = "Section":
set(value):
separator_text = value
label.text = separator_text
Loading

0 comments on commit 1f960d7

Please sign in to comment.