Skip to content

Commit

Permalink
Auto-layout selector based on device; keybinding works
Browse files Browse the repository at this point in the history
  • Loading branch information
qkmaxware committed Jul 19, 2024
1 parent 453c031 commit 12e6c55
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 4 deletions.
11 changes: 11 additions & 0 deletions Gameboy.Player.Godot/Prefabs/GodotBoy/GodotBoy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public override void _Ready() {
}

this.Screen = this.GetNode<Screen>("Screen Layouts");
const int DesktopLayout = 0;
const int TouchDesktopLayout = 1;
const int SkinnedLayout = 2;
this.Screen.CurrentTab = OS.GetName().ToLower() switch {
string s when s.Contains("android") => SkinnedLayout,
string s when s.Contains("ios") => SkinnedLayout,
_ => DisplayServer.IsTouchscreenAvailable() switch {
true => TouchDesktopLayout,
_ => DesktopLayout
}
};
this.Screen.Redraw(intro);
}

Expand Down
63 changes: 59 additions & 4 deletions Gameboy.Player.Godot/Prefabs/GodotBoy/GodotBoy.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=38 format=3 uid="uid://lagkciw3arws"]
[gd_scene load_steps=39 format=3 uid="uid://lagkciw3arws"]

[ext_resource type="Script" path="res://Prefabs/GodotBoy/GodotBoy.cs" id="1_807qv"]
[ext_resource type="Texture2D" uid="uid://dfkwdfgk0dp8n" path="res://icon.svg" id="1_fcyi5"]
Expand All @@ -20,6 +20,7 @@
[ext_resource type="Texture2D" uid="uid://dma6vgdj4ntir" path="res://Prefabs/GodotBoy/UI/layout_screen_control.svg" id="17_arug4"]
[ext_resource type="Texture2D" uid="uid://5ynu1mc2abvi" path="res://Prefabs/GodotBoy/UI/layout_skin.svg" id="18_sw6ge"]
[ext_resource type="Theme" uid="uid://dcduu766csudy" path="res://Theme.tres" id="20_0wny8"]
[ext_resource type="PackedScene" uid="uid://c51yw0q5xqybk" path="res://Prefabs/KeyRebind/keybind.tscn" id="21_1cq1c"]
[ext_resource type="PackedScene" uid="uid://7wdw54d0sdlj" path="res://Prefabs/ThemePicker/ThemePicker.tscn" id="21_xltfd"]
[ext_resource type="PackedScene" uid="uid://t7khq15qfjuw" path="res://Debuggers/CartInfoDebug.tscn" id="21_y5t5s"]
[ext_resource type="PackedScene" uid="uid://b5psa1c0pvkya" path="res://Debuggers/PaletteDebugger.tscn" id="22_e37a5"]
Expand Down Expand Up @@ -77,12 +78,10 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_styles/panel = SubResource("StyleBoxEmpty_o72x6")
current_tab = 2
tabs_visible = false
script = ExtResource("2_36eiy")

[node name="Screen Only" type="Control" parent="Screen Layouts"]
visible = false
layout_mode = 2

[node name="Background" type="ColorRect" parent="Screen Layouts/Screen Only"]
Expand Down Expand Up @@ -234,6 +233,7 @@ ignore_texture_size = true
stretch_mode = 5

[node name="Skinned Controls" type="Control" parent="Screen Layouts"]
visible = false
layout_mode = 2

[node name="Background" type="ColorRect" parent="Screen Layouts/Skinned Controls"]
Expand Down Expand Up @@ -433,6 +433,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
theme_override_constants/margin_left = 6
theme_override_constants/margin_right = 6

Expand Down Expand Up @@ -503,6 +504,58 @@ layout_mode = 2
layout_mode = 2
text = "Keybindings"

[node name="Heading" type="HBoxContainer" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds"]
layout_mode = 2

[node name="Label" type="Label" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds/Heading"]
custom_minimum_size = Vector2(128, 0)
layout_mode = 2
text = "Key"
vertical_alignment = 1
text_overrun_behavior = 3

[node name="Label2" type="Label" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds/Heading"]
layout_mode = 2
size_flags_horizontal = 3
text = "Primary Key"

[node name="Label3" type="Label" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds/Heading"]
layout_mode = 2
size_flags_horizontal = 3
text = "Secondary Key"

[node name="Keybind" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "move_left"

[node name="Keybind2" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "move_right"

[node name="Keybind3" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "move_up"

[node name="Keybind4" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "move_down"

[node name="Keybind5" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "button_a"

[node name="Keybind6" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "button_b"

[node name="Keybind7" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "button_start"

[node name="Keybind8" parent="Options/MarginContainer/ScrollContainer/ScrollContents/Keybinds" instance=ExtResource("21_1cq1c")]
layout_mode = 2
ActionName = "button_select"

[node name="Developer" type="VBoxContainer" parent="Options/MarginContainer/ScrollContainer/ScrollContents"]
layout_mode = 2

Expand Down Expand Up @@ -537,6 +590,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
theme_override_constants/margin_left = 6
theme_override_constants/margin_right = 6

Expand Down Expand Up @@ -586,8 +640,9 @@ anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -116.0
offset_top = 12.0
offset_right = 116.0
offset_bottom = 48.0
offset_bottom = 60.0
grow_horizontal = 2

[node name="Open ROM" type="Button" parent="Loader"]
Expand Down
109 changes: 109 additions & 0 deletions Gameboy.Player.Godot/Prefabs/KeyRebind/Keybind.cs
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 Gameboy.Player.Godot/Prefabs/KeyRebind/RebindButtonTheme.tres
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")
26 changes: 26 additions & 0 deletions Gameboy.Player.Godot/Prefabs/KeyRebind/keybind.tscn
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

0 comments on commit 12e6c55

Please sign in to comment.