Last active
April 28, 2024 21:23
-
-
Save itn3000/0726f0aab1430cd4db55a986f4928a8a to your computer and use it in GitHub Desktop.
how to retrieve input event with R3 and Stride3d
This file contains hidden or 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
using System; | |
using Stride.Engine; | |
using Stride.Input; | |
using R3; | |
namespace RetrieveInputKeyEventWithR3; | |
public class RetriveKeyEventScript: StartupScript | |
{ | |
public override Start() | |
{ | |
Observable.Create<KeyEvent>(obs => | |
{ | |
var listener = new MyKeyInputListener(obs, new WeakReference<InputManager>(Input)); | |
Input.AddListener(listener); | |
return listener; | |
}) | |
.Subscribe(keyEvent => Log.Info($"{keyEvent.Key}, {keyEvent.RepeatCount}")); | |
} | |
internal sealed class MyKeyInputListener(Observer<KeyEvent> observer, WeakReference<InputManager> inputManager) : IInputEventListener<KeyEvent>, IDisposable | |
{ | |
bool disposed = false; | |
public void Dispose() | |
{ | |
if(!disposed) | |
{ | |
disposed = true; | |
observer.Dispose(); | |
if(inputManager.TryGetTarget(out var input)) | |
{ | |
input.RemoveListener(this); | |
} | |
} | |
} | |
public void ProcessEvent(KeyEvent inputEvent) | |
{ | |
if(!disposed) | |
{ | |
observer.OnNext(inputEvent); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment