Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
Moved applying to godot inputmap code

Fixed comments

Made more things static

Moved Children modification code

Refactoring

Code cleanup

Created BuildGUI methods

cleanup

Removed unneccecary back reference
  • Loading branch information
FuJa0815 authored and hhyyrylainen committed Dec 8, 2020
1 parent 0af3873 commit 3cf396e
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 204 deletions.
6 changes: 3 additions & 3 deletions Thrive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
<Compile Include="src\engine\input\key_mapping\InputEventItem.cs" />
<Compile Include="src\engine\input\key_mapping\InputGroupItem.cs" />
<Compile Include="src\engine\input\key_mapping\InputGroupList.cs" />
<Compile Include="src\engine\input\key_mapping\serialisation\NamedInputAction.cs" />
<Compile Include="src\engine\input\key_mapping\serialisation\NamedInputGroup.cs" />
<Compile Include="src\engine\input\key_mapping\ThriveInputEventWithModifiers.cs" />
<Compile Include="src\engine\input\key_mapping\NamedInputAction.cs" />
<Compile Include="src\engine\input\key_mapping\NamedInputGroup.cs" />
<Compile Include="src\engine\input\key_mapping\SpecifiedInputKey.cs" />
<Compile Include="src\engine\Invoke.cs" />
<Compile Include="src\engine\LoadingScreen.cs" />
<Compile Include="src\engine\PostStartupActions.cs" />
Expand Down
18 changes: 2 additions & 16 deletions src/engine/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private Settings()
/// <summary>
/// The current controls of the game.
/// It stores the godot actions like g_move_left and
/// their associated <see cref="ThriveInputEventWithModifiers">ThriveInputEventWithModifiers</see>
/// their associated <see cref="SpecifiedInputKey">ThriveInputEventWithModifiers</see>
/// </summary>
public SettingValue<InputDataList> CurrentControls { get; set; } =
new SettingValue<InputDataList>(InputGroupList.GetDefaultControls());
Expand Down Expand Up @@ -400,21 +400,7 @@ public void ApplySoundSettings()
/// </summary>
public void ApplyInputSettings()
{
foreach (var action in CurrentControls.Value.Data)
{
// Clear all old input events
InputMap.ActionEraseEvents(action.Key);

// Add the new input events
foreach (var inputEvent in action.Value)
{
// If the game is waiting for an input
if (inputEvent == null)
return;

InputMap.ActionAddEvent(action.Key, inputEvent.ToGodotObject());
}
}
CurrentControls.Value.ApplyToGodotInputMap();
}

/// <summary>
Expand Down
26 changes: 20 additions & 6 deletions src/engine/input/key_mapping/InputActionItem.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Godot;

/// <summary>
/// Defines one specific input.
/// Defines one input aspect like g_move_forward.
/// Each InputActionItem has <see cref="InputEventItem">InputEventItems</see> associated with it.
/// </summary>
public class InputActionItem : VBoxContainer
Expand All @@ -18,15 +20,15 @@ public class InputActionItem : VBoxContainer
[Export]
public NodePath InputEventsContainerPath;

/// <summary>
/// The group in which this action is defined.
/// </summary>
internal InputGroupItem AssociatedGroup;

private Label inputActionHeader;
private HBoxContainer inputEventsContainer;
private Button addInputEvent;

/// <summary>
/// The group in which this action is defined.
/// </summary>
public InputGroupItem AssociatedGroup { get; set; }

/// <summary>
/// The godot specific action name
/// </summary>
Expand Down Expand Up @@ -87,6 +89,18 @@ public override int GetHashCode()
return InputName != null ? InputName.GetHashCode() : 0;
}

internal static InputActionItem BuildGUI(InputGroupItem caller, NamedInputAction data, IEnumerable<SpecifiedInputKey> inputs)
{
var inputActionItem = (InputActionItem)InputGroupList.InputActionItemScene.Instance();

inputActionItem.InputName = data.InputName;
inputActionItem.DisplayName = data.Name;
inputActionItem.AssociatedGroup = caller;
inputActionItem.Inputs = new ObservableCollection<InputEventItem>(inputs.Select(d => InputEventItem.BuildGUI(inputActionItem, d)));

return inputActionItem;
}

/// <summary>
/// The small + button has been pressed
/// </summary>
Expand Down
37 changes: 30 additions & 7 deletions src/engine/input/key_mapping/InputDataList.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
using System;
using System.Collections.Generic;
using Godot;

/// <summary>
/// Stores an godot input action and its associated events
/// Stores godot input actions and their associated events
/// </summary>
public class InputDataList : ICloneable
{
public InputDataList(Dictionary<string, List<ThriveInputEventWithModifiers>> data)
public InputDataList(Dictionary<string, List<SpecifiedInputKey>> data)
{
Data = data;
}

public Dictionary<string, List<ThriveInputEventWithModifiers>> Data { get; }
public Dictionary<string, List<SpecifiedInputKey>> Data { get; }

public List<ThriveInputEventWithModifiers> this[string index] => Data[index];
public List<SpecifiedInputKey> this[string index] => Data[index];

public object Clone()
{
var result = new Dictionary<string, List<ThriveInputEventWithModifiers>>();
var result = new Dictionary<string, List<SpecifiedInputKey>>();
foreach (var keyValuePair in Data)
{
result[keyValuePair.Key] = new List<ThriveInputEventWithModifiers>();
result[keyValuePair.Key] = new List<SpecifiedInputKey>();
foreach (var inputEventWithModifiers in keyValuePair.Value)
{
result[keyValuePair.Key].Add((ThriveInputEventWithModifiers)inputEventWithModifiers.Clone());
result[keyValuePair.Key].Add((SpecifiedInputKey)inputEventWithModifiers.Clone());
}
}

return new InputDataList(result);
}

/// <summary>
/// Applies the current controls to the InputMap.
/// </summary>
internal void ApplyToGodotInputMap()
{
foreach (var action in Data)
{
// Clear all old input events
InputMap.ActionEraseEvents(action.Key);

// Add the new input events
foreach (var inputEvent in action.Value)
{
// If the game is waiting for an input
if (inputEvent == null)
return;

InputMap.ActionAddEvent(action.Key, inputEvent.ToInputEvent());
}
}
}
}
34 changes: 21 additions & 13 deletions src/engine/input/key_mapping/InputEventItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class InputEventItem : Node
public bool WaitingForInput { get; private set; }

/// <summary>
/// The currently assigned godot input event.
/// The currently assigned input event.
/// null if this event was just created
/// </summary>
public ThriveInputEventWithModifiers AssociatedEvent { get; set; }
public SpecifiedInputKey AssociatedEvent { get; set; }

/// <summary>
/// The action this event is associated with
Expand All @@ -37,12 +37,12 @@ public class InputEventItem : Node
internal bool JustAdded { get; set; }

/// <summary>
/// Deleted this event from the associated action and updated the godot InputMap
/// Delete this event from the associated action and updated the godot InputMap
/// </summary>
public void Delete()
{
AssociatedAction.Inputs.Remove(this);
InputGroupList.Instance.ControlsChanged();
InputGroupList.ControlsChanged();
}

public override void _Ready()
Expand All @@ -65,7 +65,7 @@ public override void _Ready()
/// </summary>
public override void _Input(InputEvent @event)
{
if (InputGroupList.Instance.IsConflictDialogOpen())
if (InputGroupList.IsConflictDialogOpen())
return;

if (!WaitingForInput)
Expand Down Expand Up @@ -99,7 +99,7 @@ public override void _Input(InputEvent @event)
return;
}

InputGroupList.Instance.WasListeningForInput = true;
InputGroupList.WasListeningForInput = true;
WaitingForInput = false;
UpdateButtonText();
return;
Expand All @@ -112,16 +112,16 @@ public override void _Input(InputEvent @event)

// The old godot input event. Null if this event is assigned a value the first time.
var old = AssociatedEvent;
AssociatedEvent = new ThriveInputEventWithModifiers((InputEventWithModifiers)@event);
AssociatedEvent = new SpecifiedInputKey((InputEventWithModifiers)@event);

// Get the conflicts with the new input.
var conflict = InputGroupList.Instance.Conflicts(this);
var conflict = InputGroupList.Conflicts(this);
if (conflict != null)
{
AssociatedEvent = old;

// If there are conflicts detected reset the changes and ask the user.
InputGroupList.Instance.ShowInputConflictDialog(this, conflict, (InputEventWithModifiers)@event);
InputGroupList.ShowInputConflictDialog(this, conflict, (InputEventWithModifiers)@event);
return;
}

Expand All @@ -142,10 +142,10 @@ public override void _Input(InputEvent @event)

WaitingForInput = false;
JustAdded = false;
InputGroupList.Instance.WasListeningForInput = false;
InputGroupList.WasListeningForInput = false;

// Update the godot InputMap
InputGroupList.Instance.ControlsChanged();
InputGroupList.ControlsChanged();

// Update the button text
UpdateButtonText();
Expand All @@ -164,6 +164,14 @@ public override int GetHashCode()
return AssociatedEvent.GetHashCode();
}

internal static InputEventItem BuildGUI(InputActionItem caller, SpecifiedInputKey @event)
{
var res = (InputEventItem)InputGroupList.InputEventItemScene.Instance();
res.AssociatedAction = caller;
res.AssociatedEvent = @event;
return res;
}

protected override void Dispose(bool disposing)
{
AssociatedAction = null;
Expand All @@ -174,10 +182,10 @@ protected override void Dispose(bool disposing)

private void OnButtonPressed(InputEvent @event)
{
if (InputGroupList.Instance.IsConflictDialogOpen())
if (InputGroupList.IsConflictDialogOpen())
return;

if (InputGroupList.Instance.ListeningForInput)
if (InputGroupList.ListeningForInput)
return;

if (!(@event is InputEventMouseButton inputButton))
Expand Down
13 changes: 11 additions & 2 deletions src/engine/input/key_mapping/InputGroupItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Godot;

/// <summary>
Expand All @@ -24,8 +25,6 @@ public class InputGroupItem : VBoxContainer
/// </example>
public string GroupName { get; set; }

// ReSharper disable once CollectionNeverUpdated.Global (Reason: Loaded from json)

/// <summary>
/// The associated actions the group contains
/// </summary>
Expand All @@ -51,6 +50,16 @@ public override void _Ready()
}
}

internal static InputGroupItem BuildGUI(InputGroupList caller, NamedInputGroup data, InputDataList inputData)
{
var result = (InputGroupItem)InputGroupList.InputGroupItemScene.Instance();

result.EnvironmentId = data.EnvironmentId.ToList();
result.GroupName = data.GroupName;
result.Actions = data.Actions.Select(x => InputActionItem.BuildGUI(result, x, inputData[x.InputName])).ToList();
return result;
}

protected override void Dispose(bool disposing)
{
inputGroupHeader?.Dispose();
Expand Down
Loading

0 comments on commit 3cf396e

Please sign in to comment.