Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to simulate VirtualInput activation #693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions Nez.Portable/Input/Virtual/VirtualAxis.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace Nez
public class VirtualAxis : VirtualInput
{
public List<Node> Nodes = new List<Node>();

float _simulatedValue;
int _simulationStep;

public float Value
{
Expand All @@ -22,7 +25,7 @@ public float Value
return val;
}

return 0;
return _simulatedValue;
}
}

Expand All @@ -38,10 +41,28 @@ public VirtualAxis(params Node[] nodes)
}


public void Simulate(float value)
{
_simulatedValue = value;
_simulationStep = 1;
}


public override void Update()
{
for (var i = 0; i < Nodes.Count; i++)
Nodes[i].Update();

switch (_simulationStep)
{
case 1:
_simulationStep++;
break;
case 2:
_simulatedValue = 0;
_simulationStep = 0;
break;
}
}


Expand Down Expand Up @@ -256,4 +277,4 @@ public override void Update()

#endregion
}
}
}
42 changes: 38 additions & 4 deletions Nez.Portable/Input/Virtual/VirtualButton.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class VirtualButton : VirtualInput
float _repeatCounter;
bool _willRepeat;

bool _simulatePress;
bool _simulateDown;
bool _simulateRelease;
int _simulationStep;


public VirtualButton(float bufferTime)
{
Expand Down Expand Up @@ -62,6 +67,12 @@ public void SetRepeat(float firstRepeatTime, float multiRepeatTime)
}


public void Simulate()
{
_simulationStep = 1;
}


public override void Update()
{
_bufferCounter -= Time.UnscaledDeltaTime;
Expand Down Expand Up @@ -103,6 +114,29 @@ public override void Update()
}
}
}

switch (_simulationStep)
{
case 1:
_simulatePress = true;
_simulateDown = true;
_simulationStep++;
break;
case 2:
_simulatePress = false;
_simulationStep++;
break;
case 3:
_simulateDown = false;
_simulateRelease = true;
_simulationStep++;
break;
case 4:
_simulateRelease = false;
_simulationStep = -1;
break;
}

}


Expand All @@ -114,7 +148,7 @@ public bool IsDown
if (node.IsDown)
return true;

return false;
return _simulateDown;
}
}

Expand All @@ -130,7 +164,7 @@ public bool IsPressed
if (node.IsPressed)
return true;

return false;
return _simulatePress;
}
}

Expand All @@ -143,7 +177,7 @@ public bool IsReleased
if (node.IsReleased)
return true;

return false;
return _simulateRelease;
}
}

Expand Down Expand Up @@ -576,4 +610,4 @@ public class MouseSecondExtendedButton : Node

#endregion
}
}
}
23 changes: 22 additions & 1 deletion Nez.Portable/Input/Virtual/VirtualIntegerAxis.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Nez
public class VirtualIntegerAxis : VirtualInput
{
public List<VirtualAxis.Node> Nodes = new List<VirtualAxis.Node>();

int _simulatedValue;
int _simulationStep;

public int Value
{
Expand All @@ -24,7 +27,7 @@ public int Value
return Math.Sign(val);
}

return 0;
return _simulatedValue;
}
}

Expand All @@ -40,10 +43,28 @@ public VirtualIntegerAxis(params VirtualAxis.Node[] nodes)
}


public void Simulate(int value)
{
_simulatedValue = value;
_simulationStep = 1;
}


public override void Update()
{
for (var i = 0; i < Nodes.Count; i++)
Nodes[i].Update();

switch (_simulationStep)
{
case 1:
_simulationStep++;
break;
case 2:
_simulatedValue = 0;
_simulationStep = 0;
break;
}
}


Expand Down
24 changes: 23 additions & 1 deletion Nez.Portable/Input/Virtual/VirtualJoystick.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class VirtualJoystick : VirtualInput
public List<Node> Nodes = new List<Node>();
public bool Normalized;

Vector2 _simulatedValue = Vector2.Zero;
int _simulationStep;

public Vector2 Value
{
get
Expand All @@ -28,7 +31,7 @@ public Vector2 Value
}
}

return Vector2.Zero;
return _simulatedValue;
}
}

Expand All @@ -46,10 +49,29 @@ public VirtualJoystick(bool normalized, params Node[] nodes) : base()
}


public void Simulate(Vector2 value)
{
_simulatedValue = value;
_simulationStep = 1;
}


public override void Update()
{
for (int i = 0; i < Nodes.Count; i++)
Nodes[i].Update();


switch (_simulationStep)
{
case 1:
_simulationStep++;
break;
case 2:
_simulatedValue = Vector2.Zero;
_simulationStep = 0;
break;
}
}


Expand Down