-
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.
- Loading branch information
Showing
3 changed files
with
488 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public enum JoystickEventType | ||
{ | ||
KeyDown, | ||
KeyUp, | ||
Continuous | ||
} | ||
|
||
[Serializable] | ||
public class JoystickInputEvent | ||
{ | ||
public string name; // Name for the input event | ||
public string axisName; // Name of the Unity Input axis | ||
public string keyName; // Name of the KLEP key | ||
public bool isVector2; // Whether the input is a Vector2 (e.g., joystick stick) | ||
public JoystickEventType eventType; // Type of event | ||
} | ||
|
||
public class S_Joystick : KLEPExecutableBase | ||
{ | ||
public List<JoystickInputEvent> joystickInputs = new List<JoystickInputEvent>(); | ||
|
||
private Dictionary<string, Vector2> lastVector2Values = new Dictionary<string, Vector2>(); | ||
private Dictionary<string, float> lastFloatValues = new Dictionary<string, float>(); | ||
|
||
public override bool IsComplete() | ||
{ | ||
return true; // This sensor is always active and doesn't have a completion state | ||
} | ||
|
||
public override void ExecutableUpdates() | ||
{ | ||
if (parentNeuron == null) | ||
{ | ||
Debug.Log("Parent neuron is null."); | ||
return; | ||
} | ||
|
||
foreach (JoystickInputEvent inputEvent in joystickInputs) | ||
{ | ||
CheckJoystickInput(inputEvent); | ||
} | ||
} | ||
|
||
private void CheckJoystickInput(JoystickInputEvent inputEvent) | ||
{ | ||
if (inputEvent.isVector2) | ||
{ | ||
Vector2 input = new Vector2(Input.GetAxis(inputEvent.axisName + "X"), Input.GetAxis(inputEvent.axisName + "Y")); | ||
HandleVector2Input(inputEvent, input); | ||
} | ||
else | ||
{ | ||
float input = Input.GetAxis(inputEvent.axisName); | ||
HandleFloatInput(inputEvent, input); | ||
} | ||
} | ||
|
||
private void HandleVector2Input(JoystickInputEvent inputEvent, Vector2 input) | ||
{ | ||
switch (inputEvent.eventType) | ||
{ | ||
case JoystickEventType.KeyDown: | ||
if (input.magnitude > 0 && (!lastVector2Values.ContainsKey(inputEvent.keyName) || lastVector2Values[inputEvent.keyName] == Vector2.zero)) | ||
{ | ||
CreateAndPushKeyData(inputEvent.keyName, input); | ||
} | ||
break; | ||
|
||
case JoystickEventType.KeyUp: | ||
if (input.magnitude == 0 && lastVector2Values.ContainsKey(inputEvent.keyName) && lastVector2Values[inputEvent.keyName] != Vector2.zero) | ||
{ | ||
CreateAndPushKeyData(inputEvent.keyName, input); | ||
} | ||
break; | ||
|
||
case JoystickEventType.Continuous: | ||
if (input.magnitude > 0) | ||
{ | ||
CreateAndPushKeyData(inputEvent.keyName, input); | ||
} | ||
break; | ||
} | ||
|
||
lastVector2Values[inputEvent.keyName] = input; | ||
} | ||
|
||
private void HandleFloatInput(JoystickInputEvent inputEvent, float input) | ||
{ | ||
switch (inputEvent.eventType) | ||
{ | ||
case JoystickEventType.KeyDown: | ||
if (Mathf.Abs(input) > 0.1f && (!lastFloatValues.ContainsKey(inputEvent.keyName) || Mathf.Abs(lastFloatValues[inputEvent.keyName]) <= 0.1f)) | ||
{ | ||
CreateAndPushKeyData(inputEvent.keyName, input); | ||
} | ||
break; | ||
|
||
case JoystickEventType.KeyUp: | ||
if (Mathf.Abs(input) <= 0.1f && lastFloatValues.ContainsKey(inputEvent.keyName) && Mathf.Abs(lastFloatValues[inputEvent.keyName]) > 0.1f) | ||
{ | ||
CreateAndPushKeyData(inputEvent.keyName, input); | ||
} | ||
break; | ||
|
||
case JoystickEventType.Continuous: | ||
if (Mathf.Abs(input) > 0.1f) | ||
{ | ||
CreateAndPushKeyData(inputEvent.keyName, input); | ||
} | ||
break; | ||
} | ||
|
||
lastFloatValues[inputEvent.keyName] = input; | ||
} | ||
|
||
private void CreateAndPushKeyData(string keyName, Vector2 value) | ||
{ | ||
KeyCreationData creationData = new KeyCreationData(keyName, 1f, keyLoader.properties, keyLoader); | ||
creationData.SetProperty("Value", value); | ||
PushKey(creationData); | ||
} | ||
|
||
private void CreateAndPushKeyData(string keyName, float value) | ||
{ | ||
KeyCreationData creationData = new KeyCreationData(keyName, 1f, keyLoader.properties, keyLoader); | ||
creationData.SetProperty("Value", value); | ||
PushKey(creationData); | ||
} | ||
} |
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,176 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEngine; | ||
|
||
public enum KeyEventType | ||
{ | ||
KeyDown, | ||
KeyUp, | ||
Continuous, | ||
DoubleTap, | ||
HoldDuration | ||
} | ||
|
||
[Serializable] | ||
public class KeyEvent | ||
{ | ||
public KeyCode key; | ||
public KeyEventType eventType; | ||
public float doubleTapThreshold = 0.25f; // Time in seconds to consider it a double tap | ||
public float holdDurationThreshold = 1f; // Duration to track key held | ||
} | ||
|
||
public class S_Keyboard : KLEPExecutableBase | ||
{ | ||
public List<KeyEvent> keyEvents = new List<KeyEvent>(); | ||
|
||
private Dictionary<KeyCode, float> keyLastPressTime = new Dictionary<KeyCode, float>(); | ||
private Dictionary<KeyCode, float> keyHoldDuration = new Dictionary<KeyCode, float>(); | ||
|
||
public bool MouseWheel = false; | ||
public GameEnums.KeyNames mouseZoomIn; | ||
public GameEnums.KeyNames mouseZoomOut; | ||
|
||
public override bool IsComplete() | ||
{ | ||
return true; // This sensor is always active and doesn't have a completion state | ||
} | ||
|
||
public override void ExecutableUpdates() | ||
{ | ||
if (parentNeuron == null) | ||
{ | ||
Debug.Log("Parent neuron is null."); | ||
return; | ||
} | ||
|
||
foreach (KeyEvent keyEvent in keyEvents) | ||
{ | ||
CheckKeyEvent(keyEvent); | ||
} | ||
|
||
if (MouseWheel) PushMouseWheel(); | ||
} | ||
|
||
private void CheckKeyEvent(KeyEvent keyEvent) | ||
{ | ||
KeyCode key = keyEvent.key; | ||
KeyEventType eventType = keyEvent.eventType; | ||
|
||
switch (eventType) | ||
{ | ||
case KeyEventType.KeyDown: | ||
if (Input.GetKeyDown(key)) | ||
{ | ||
CreateAndPushKeyData(key.ToString() + "_Down_KEY"); | ||
} | ||
break; | ||
|
||
case KeyEventType.KeyUp: | ||
if (Input.GetKeyUp(key)) | ||
{ | ||
CreateAndPushKeyData(key.ToString() + "_Up_KEY"); | ||
} | ||
break; | ||
|
||
case KeyEventType.Continuous: | ||
if (Input.GetKey(key)) | ||
{ | ||
CreateAndPushKeyData(key.ToString() + "_KEY"); | ||
} | ||
break; | ||
|
||
case KeyEventType.DoubleTap: | ||
if (Input.GetKeyDown(key)) | ||
{ | ||
if (keyLastPressTime.ContainsKey(key) && Time.time - keyLastPressTime[key] < keyEvent.doubleTapThreshold) | ||
{ | ||
CreateAndPushKeyData(key.ToString() + "_DoubleTap_KEY"); | ||
} | ||
keyLastPressTime[key] = Time.time; | ||
} | ||
break; | ||
|
||
case KeyEventType.HoldDuration: | ||
if (Input.GetKeyDown(key)) | ||
{ | ||
keyHoldDuration[key] = 0f; | ||
} | ||
if (Input.GetKey(key)) | ||
{ | ||
keyHoldDuration[key] += Time.deltaTime; | ||
if (keyHoldDuration[key] >= keyEvent.holdDurationThreshold) | ||
{ | ||
CreateAndPushKeyData(key.ToString() + "_HoldDuration_" + keyEvent.holdDurationThreshold + "_KEY"); | ||
keyHoldDuration[key] = 0f; // Reset duration to avoid multiple events | ||
} | ||
} | ||
if (Input.GetKeyUp(key)) | ||
{ | ||
if (keyHoldDuration.ContainsKey(key) && keyHoldDuration[key] > 0) | ||
{ | ||
CreateAndPushKeyData(key.ToString() + "_HeldFor_" + keyHoldDuration[key] + "_KEY"); | ||
keyHoldDuration.Remove(key); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
void PushMouseWheel() | ||
{ | ||
float scrollDelta = Input.GetAxis("Mouse ScrollWheel"); | ||
|
||
if (scrollDelta != 0) | ||
{ | ||
KeyCreationData zoomKeyData; | ||
if (scrollDelta > 0) | ||
{ | ||
// Scroll delta positive: mouse wheel scrolled up | ||
zoomKeyData = KeyCreationService.CreateKeyData(mouseZoomIn.ToString(), 1, keyLoader); | ||
PushKey(zoomKeyData); | ||
} | ||
else | ||
{ | ||
// Scroll delta negative: mouse wheel scrolled down | ||
zoomKeyData = KeyCreationService.CreateKeyData(mouseZoomOut.ToString(), 1, keyLoader); | ||
PushKey(zoomKeyData); | ||
} | ||
|
||
// Assuming HandleKeyCreationEvent_PushToNeuron is a method in your key manager that handles the key pushing | ||
parentNeuron.keyManager.HandleKeyCreationEvent_PushToNeuron("Zoom", zoomKeyData); | ||
} | ||
} | ||
|
||
private void CreateAndPushKeyData(string keyName) | ||
{ | ||
// Create the KeyCreationData with the found loader and the prepared properties. | ||
KeyCreationData creationData = new KeyCreationData(keyName, 1f, keyLoader.properties, keyLoader); | ||
PushKey(creationData); | ||
} | ||
} | ||
|
||
//******************************* | ||
// how to parse the held key data for that time value | ||
//******************************* | ||
/*private void ParseKeyData(string keyData, out string keyName, out float? holdDuration, out float? heldDuration) | ||
{ | ||
keyName = keyData; | ||
holdDuration = null; | ||
heldDuration = null; | ||
var matchHold = System.Text.RegularExpressions.Regex.Match(keyData, @"_HoldDuration_(\d+(\.\d+)?)_KEY$"); | ||
var matchHeld = System.Text.RegularExpressions.Regex.Match(keyData, @"_HeldFor_(\d+(\.\d+)?)_KEY$"); | ||
if (matchHold.Success) | ||
{ | ||
keyName = keyData.Substring(0, matchHold.Index) + "_KEY"; | ||
holdDuration = float.Parse(matchHold.Groups[1].Value); | ||
} | ||
else if (matchHeld.Success) | ||
{ | ||
keyName = keyData.Substring(0, matchHeld.Index) + "_KEY"; | ||
heldDuration = float.Parse(matchHeld.Groups[1].Value); | ||
} | ||
}*/ |
Oops, something went wrong.