VladimirPirozhenko 0fb508126d Updated InputTranslator and View manager, it fixed few bugs with allowing controls
InputTranslator now can check whether is translation is restricted, added new method to ViewManager - IsActive - to check if view is active or not
2022-08-13 02:45:01 +03:00

86 lines
2.5 KiB
C#

using System.Collections.Generic;
public class InputTranslator<T> where T : IBinding
{
private List<ICommandTranslator> commandTranslators;
private IBindingHolder<T> bindingHolder;
public void Init(IBindingHolder<T> holder)
{
commandTranslators = new List<ICommandTranslator>();
bindingHolder = holder;
bindingHolder.Init();
}
public void AddCommandTranslator(ICommandTranslator translator)
{
if (commandTranslators.Contains(translator))
return;
commandTranslators.Add(translator);
}
public void RemoveCommandTranslator(ICommandTranslator translator)
{
if (commandTranslators.Contains(translator))
commandTranslators.Remove(translator);
}
public void RestictTranslation(List<ECommand> commands, bool isRestricted)
{
foreach (var keyBinding in bindingHolder.InputBindings)
{
if (commands.Contains(keyBinding.Key))
{
keyBinding.Value.IsRestricted = isRestricted;
}
}
}
public bool IsTranslationResticted(List<ECommand> commands)
{
foreach (ECommand command in commands)
{
if (bindingHolder.InputBindings.ContainsKey(command))
{
return bindingHolder.InputBindings[command].IsRestricted;
}
}
return false;
}
public void Tick()
{
if (commandTranslators.Count == 0)
return;
var commands = new Dictionary<ECommand, PressedState>();
foreach (var keyBinding in bindingHolder.InputBindings)
{
if (keyBinding.Value.IsRestricted)
continue;
if (keyBinding.Value.IsPressed)
commands.Add(keyBinding.Key, new PressedState(keyBinding.Value.IsPressed, keyBinding.Value.IsReleased));
if (keyBinding.Value.IsReleased)
commands.Add(keyBinding.Key, new PressedState(keyBinding.Value.IsPressed, keyBinding.Value.IsReleased));
}
if (commands.Count == 0)
return;
foreach (var commandTranslator in commandTranslators)
{
foreach (var command in commands)
{
var eCommand = command.Key;
var pressedState = command.Value;
commandTranslator.TranslateCommand(eCommand, pressedState);
}
}
}
}