93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using StgSharp.Controlling.UsrActivity;
|
|
using StgSharp.Graphics;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace StgSharp
|
|
{
|
|
public abstract class ViewResponder<TView> where TView: View
|
|
{
|
|
private View _viewbinding;
|
|
private Dictionary<ITrigger, Action> keyCallbackIndex;
|
|
private Dictionary<string, Action> nameToCallbackMap;
|
|
private bool _isInitialized;
|
|
|
|
|
|
protected ViewResponder()
|
|
{
|
|
keyCallbackIndex = new Dictionary<ITrigger, Action>();
|
|
nameToCallbackMap = new Dictionary<string, Action>();
|
|
}
|
|
|
|
internal void ProcessUserInput()
|
|
{
|
|
foreach (var pair in keyCallbackIndex)
|
|
{
|
|
ITrigger t = pair.Key;
|
|
if (InternalIO.glfwGetKey(_viewbinding.ViewHandle, t.TargetKeyOrButtonID) == t.TriggeredStatus)
|
|
{
|
|
pair.Value();
|
|
}
|
|
}
|
|
}
|
|
|
|
public Action this[ITrigger trigger]
|
|
{
|
|
get => keyCallbackIndex.TryGetValue(trigger, out Action callback) ? callback : null;
|
|
set
|
|
{
|
|
if (keyCallbackIndex.TryGetValue(trigger, out Action callback))
|
|
{
|
|
callback += value;
|
|
}
|
|
else
|
|
{
|
|
keyCallbackIndex.Add(trigger, value);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void Init(Func<string, Action> keyGetter)
|
|
{
|
|
if (_isInitialized)
|
|
{
|
|
return;
|
|
}
|
|
CustomizedInitialize(
|
|
(trigger, name) =>
|
|
{
|
|
if (!nameToCallbackMap.TryGetValue(name, out Action ret))
|
|
{
|
|
ret = keyGetter.Invoke(name)??
|
|
throw new InvalidOperationException($"Cannot find method named {name} in binded view model");
|
|
}
|
|
this[trigger] = ret;
|
|
return ret;
|
|
}
|
|
);
|
|
}
|
|
|
|
public void ProcessInput()
|
|
{
|
|
foreach (var pair in keyCallbackIndex)
|
|
{
|
|
if (InternalIO.glfwGetKey(_viewbinding.ViewHandle,(int)pair.Key.TargetKeyOrButtonID) == pair.Key.TriggeredStatus)
|
|
{
|
|
pair.Value();
|
|
}
|
|
}
|
|
}
|
|
|
|
public abstract void CustomizedInitialize(Func<ITrigger, string, Action> keyRegister);
|
|
|
|
public abstract bool ValidateInitializing();
|
|
|
|
}
|
|
|
|
|
|
}
|