-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-layout selector based on device; keybinding works
- Loading branch information
Showing
5 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using Godot; | ||
using System; | ||
using System.Linq; | ||
|
||
public partial class Keybind : HBoxContainer { | ||
|
||
[Export] public string ActionName; | ||
|
||
private Label KeyName; | ||
private Button Primary; | ||
private Button Secondary; | ||
|
||
// Called when the node enters the scene tree for the first time. | ||
public override void _Ready() { | ||
this.KeyName = GetNode<Label>("Label"); | ||
this.Primary = GetNode<Button>("Primary"); | ||
this.Secondary = GetNode<Button>("Secondary"); | ||
|
||
if (!string.IsNullOrEmpty(ActionName)) { | ||
this.SetAction(ActionName); | ||
} | ||
} | ||
|
||
private static readonly string scancode = "scancode"; | ||
|
||
private string action; | ||
private InputEventKey key_primary; | ||
private InputEventKey key_secondary; | ||
|
||
public void MakeAction(string name) { | ||
InputMap.AddAction(name); | ||
this.action = name; | ||
var e = new InputEventKey(); | ||
InputMap.ActionAddEvent(name, e); | ||
key_primary = e; | ||
new InputEventKey(); | ||
InputMap.ActionAddEvent(name, e); | ||
key_secondary = e; | ||
} | ||
|
||
public void SetAction(string name) { | ||
/*if (!InputMap.HasAction(action)) { | ||
GD.PrintErr("Cannot create rebinding for action ; action '" + name + "' doesn't exist (similar: " + string.Join(',', InputMap.GetActions().Where(x => x.ToString().StartsWith(name)).Select(x => x.ToString())) + ")"); | ||
return; | ||
}*/ | ||
|
||
this.action = name; | ||
KeyName.Text = name; | ||
var events = InputMap.ActionGetEvents(name); | ||
key_primary = events.OfType<InputEventKey>().FirstOrDefault(); | ||
key_secondary = events.OfType<InputEventKey>().Skip(1).FirstOrDefault(); | ||
|
||
Primary.Visible = key_primary is not null; | ||
Secondary.Visible = key_secondary is not null; | ||
|
||
Primary.Text = EventString(key_primary); | ||
Secondary.Text = EventString(key_secondary); | ||
} | ||
|
||
private static string EventString(InputEventKey key) { | ||
if (key is null) | ||
return string.Empty; | ||
if (key.Keycode != Key.None) | ||
return key.AsTextKeycode(); | ||
if (key.PhysicalKeycode != Key.None) | ||
return key.AsTextPhysicalKeycode(); | ||
return "(Unset)"; | ||
} | ||
|
||
public override void _Input(InputEvent @event) { | ||
base._Input(@event); | ||
|
||
if (@event is not InputEventKey key_press) { | ||
return; | ||
} | ||
|
||
// Cancel rebind | ||
if (key_press.Keycode == Key.Escape) { | ||
Primary.ButtonPressed = false; | ||
Secondary.ButtonPressed = false; | ||
return; | ||
} | ||
|
||
// Rebind | ||
if (Primary.ButtonPressed) { | ||
if (key_primary is not null) { | ||
Primary.ButtonPressed = false; | ||
key_primary.Keycode = key_press.Keycode; | ||
key_primary.PhysicalKeycode = key_press.PhysicalKeycode; | ||
Primary.Text = EventString(key_primary); | ||
GD.Print("Rebound primary event"); | ||
} else { | ||
GD.PrintErr("No primary event to rebind key to"); | ||
} | ||
} | ||
|
||
if (Secondary.ButtonPressed) { | ||
if (key_secondary is not null) { | ||
Secondary.ButtonPressed = false; | ||
key_secondary.Keycode = key_press.Keycode; | ||
key_secondary.PhysicalKeycode = key_press.PhysicalKeycode; | ||
Secondary.Text = EventString(key_secondary); | ||
GD.Print("Rebound secondary event"); | ||
} else { | ||
GD.PrintErr("No secondary event to rebind key to"); | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Gameboy.Player.Godot/Prefabs/KeyRebind/RebindButtonTheme.tres
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[gd_resource type="Theme" load_steps=2 format=3 uid="uid://cm80ju1d6rsye"] | ||
|
||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_bk100"] | ||
bg_color = Color(0.805699, 0.390695, 0.13034, 1) | ||
|
||
[resource] | ||
Button/colors/font_pressed_color = Color(1, 1, 1, 0) | ||
Button/styles/pressed = SubResource("StyleBoxFlat_bk100") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[gd_scene load_steps=3 format=3 uid="uid://c51yw0q5xqybk"] | ||
|
||
[ext_resource type="Script" path="res://Prefabs/KeyRebind/Keybind.cs" id="1_wcb04"] | ||
[ext_resource type="Theme" uid="uid://cm80ju1d6rsye" path="res://Prefabs/KeyRebind/RebindButtonTheme.tres" id="2_phmuq"] | ||
|
||
[node name="Keybind" type="HBoxContainer"] | ||
script = ExtResource("1_wcb04") | ||
|
||
[node name="Label" type="Label" parent="."] | ||
custom_minimum_size = Vector2(128, 0) | ||
layout_mode = 2 | ||
text = "KeyName" | ||
vertical_alignment = 1 | ||
text_overrun_behavior = 3 | ||
|
||
[node name="Primary" type="Button" parent="."] | ||
layout_mode = 2 | ||
size_flags_horizontal = 3 | ||
theme = ExtResource("2_phmuq") | ||
toggle_mode = true | ||
|
||
[node name="Secondary" type="Button" parent="."] | ||
layout_mode = 2 | ||
size_flags_horizontal = 3 | ||
theme = ExtResource("2_phmuq") | ||
toggle_mode = true |