Initial commit

This commit is contained in:
Matt Thorson
2020-05-04 20:50:38 -07:00
commit 4a44eb0f26
15 changed files with 1457 additions and 0 deletions

194
src/Input/VirtualAxis.bf Normal file
View File

@ -0,0 +1,194 @@
using System.Collections;
using System;
namespace Strawberry
{
public class VirtualAxis : VirtualInput
{
public enum OverlapBehaviors { TakeNewer, TakeOlder, CancelOut }
public float Value { get; private set; }
public int IntValue { get; private set; }
public bool Pressed { get; private set; }
public bool Released { get; private set; }
public bool Repeating { get; private set; }
private List<Node> nodes;
private float pressBuffer;
private float releaseBuffer;
private float repeatStart;
private float repeatInterval;
private float lastPress;
private float lastRelease;
private float lastPressClear;
private float lastReleaseClear;
public this()
{
nodes = new List<Node>();
}
public ~this()
{
for (var n in nodes)
delete n;
delete nodes;
}
public override void Update()
{
for (var n in nodes)
n.Update();
//Value
let last = IntValue;
Value = 0;
for (var n in nodes)
{
if (n.Value != 0)
{
Value = n.Value;
break;
}
}
IntValue = Math.Sign(Value);
//Press
if (last != IntValue && IntValue != 0)
lastPress = Game.Time;
Pressed = IntValue != 0 && lastPress > lastPressClear && Game.Time - lastPress <= pressBuffer;
//Repeat
if (IntValue != 0 && repeatStart > 0 && Game.Time - lastPress >= repeatStart)
{
Repeating = true;
int a = (int)((Game.PreviousTime - lastPress) / repeatInterval);
int b = (int)((Game.Time - lastPress) / repeatInterval);
if (a != b)
Pressed = true;
}
else
Repeating = false;
//Release
if (last != 0 && IntValue == 0)
lastRelease = Game.Time;
Released = IntValue == 0 && lastRelease > lastReleaseClear && Game.Time - lastRelease <= releaseBuffer;
}
public void ClearPressBuffer()
{
lastPressClear = Game.Time;
}
public void ClearReleaseBuffer()
{
lastReleaseClear = Game.Time;
}
// Setup Calls
public VirtualAxis Keys(SDL2.SDL.Scancode negativeKey, SDL2.SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
{
nodes.Add(new KeyboardKeys(negativeKey, positiveKey, overlapBehavior));
return this;
}
public VirtualAxis PressBuffer(float time)
{
pressBuffer = time;
return this;
}
public VirtualAxis ReleaseBuffer(float time)
{
releaseBuffer = time;
return this;
}
public VirtualAxis Repeat(float start, float interval)
{
repeatStart = start;
repeatInterval = interval;
return this;
}
// Nodes
private abstract class Node
{
public abstract float Value { get; }
public virtual void Update() { }
}
private class KeyboardKeys : Node
{
public OverlapBehaviors OverlapBehavior;
public SDL2.SDL.Scancode NegativeKeycode;
public SDL2.SDL.Scancode PositiveKeycode;
private float value;
private bool turned;
public this(SDL2.SDL.Scancode negativeKey, SDL2.SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
{
NegativeKeycode = negativeKey;
PositiveKeycode = positiveKey;
OverlapBehavior = overlapBehavior;
}
public override void Update()
{
if (Game.IsKeyDown(PositiveKeycode))
{
if (Game.IsKeyDown(NegativeKeycode))
{
switch (OverlapBehavior)
{
case OverlapBehaviors.CancelOut:
value = 0;
break;
case OverlapBehaviors.TakeNewer:
if (!turned)
{
value *= -1;
turned = true;
}
break;
case OverlapBehaviors.TakeOlder:
//value stays the same
break;
}
}
else
{
turned = false;
value = 1;
}
}
else if (Game.IsKeyDown(NegativeKeycode))
{
turned = false;
value = -1;
}
else
{
turned = false;
value = 0;
}
}
public override float Value
{
get
{
return value;
}
}
}
}
}

137
src/Input/VirtualButton.bf Normal file
View File

@ -0,0 +1,137 @@
using System.Collections;
using System;
using System.Diagnostics;
namespace Strawberry
{
public class VirtualButton : VirtualInput
{
public bool Check { get; private set; }
public bool Pressed { get; private set; }
public bool Released { get; private set; }
public bool Repeating { get; private set; }
private List<Node> nodes;
private float pressBuffer;
private float releaseBuffer;
private float repeatStart;
private float repeatInterval;
private float lastPress;
private float lastRelease;
private float lastPressClear;
private float lastReleaseClear;
public this()
{
nodes = new List<Node>();
}
public ~this()
{
for (var n in nodes)
delete n;
delete nodes;
}
override public void Update()
{
//Check
let last = Check;
Check = false;
for (var n in nodes)
{
if (n.Check)
{
Check = true;
break;
}
}
//Press
if (!last && Check)
lastPress = Game.Time;
Pressed = Check && lastPress > lastPressClear && Game.Time - lastPress <= pressBuffer;
//Repeat
if (Check && repeatStart > 0 && Game.Time - lastPress >= repeatStart)
{
Repeating = true;
int a = (int)((Game.PreviousTime - lastPress) / repeatInterval);
int b = (int)((Game.Time - lastPress) / repeatInterval);
if (a != b)
Pressed = true;
}
else
Repeating = false;
//Release
if (last && !Check)
lastRelease = Game.Time;
Released = !Check && lastRelease > lastReleaseClear && Game.Time - lastRelease <= releaseBuffer;
}
public void ClearPressBuffer()
{
lastPressClear = Game.Time;
}
public void ClearReleaseBuffer()
{
lastReleaseClear = Game.Time;
}
// Setup Calls
public VirtualButton Key(SDL2.SDL.Scancode keycode)
{
nodes.Add(new KeyboardKey(keycode));
return this;
}
public VirtualButton PressBuffer(float time)
{
pressBuffer = time;
return this;
}
public VirtualButton ReleaseBuffer(float time)
{
releaseBuffer = time;
return this;
}
public VirtualButton Repeat(float start, float interval)
{
repeatStart = start;
repeatInterval = interval;
return this;
}
// Nodes
private abstract class Node
{
public abstract bool Check { get; }
}
private class KeyboardKey : Node
{
public SDL2.SDL.Scancode Keycode;
public this(SDL2.SDL.Scancode keycode)
{
Keycode = keycode;
}
override public bool Check
{
get
{
return Game.IsKeyDown(Keycode);
}
}
}
}
}

20
src/Input/VirtualInput.bf Normal file
View File

@ -0,0 +1,20 @@
namespace Strawberry
{
public abstract class VirtualInput
{
public this()
{
Game.VirtualInputs.Add(this);
}
public ~this()
{
Game.VirtualInputs.Remove(this);
}
public virtual void Update()
{
}
}
}