Blame mono/Assistance/InputState.cs

Ivan Mahonin a6a507
using System;
Ivan Mahonin a6a507
using System.Collections.Generic;
Ivan Mahonin a6a507
Ivan Mahonin a6a507
namespace Assistance {
Ivan Mahonin a6a507
	public class InputState {
Ivan Mahonin a6a507
		public long ticks;
Ivan Mahonin a6a507
		public KeyHistory<Gdk.Key> keyHistory = new KeyHistory<Gdk.Key>();
Ivan Mahonin a6a507
		public readonly Dictionary<Gdk.Device, KeyHistory<uint>> buttonHistories = new Dictionary<Gdk.Device, KeyHistory<uint>>();
Ivan Mahonin a6a507
		
Ivan Mahonin a6a507
		public void touch(long ticks) {
Ivan Mahonin a6a507
			if (this.ticks < ticks)
Ivan Mahonin a6a507
				this.ticks = ticks;
Ivan Mahonin a6a507
			else
Ivan Mahonin a6a507
				++this.ticks;
Ivan Mahonin a6a507
		}
Ivan Mahonin a6a507
		
Ivan Mahonin a6a507
		public KeyState<Gdk.Key> keyState
Ivan Mahonin a6a507
			{ get { return keyHistory.current; } }
Ivan Mahonin a6a507
		
Ivan Mahonin a6a507
		public void keyEvent(bool press, Gdk.Key key, long ticks) {
Ivan Mahonin a6a507
			touch(ticks);
Ivan Mahonin a6a507
			keyHistory.change(press, key, this.ticks);
Ivan Mahonin a6a507
		}
Ivan Mahonin a6a507
		public void keyPress(Gdk.Key key, long ticks)
Ivan Mahonin a6a507
			{ keyEvent(true, key, ticks); }
Ivan Mahonin a6a507
		public void keyRelease(Gdk.Key key, long ticks)
Ivan Mahonin a6a507
			{ keyEvent(false, key, ticks); }
Ivan Mahonin a6a507
		
Ivan Mahonin a6a507
		public KeyState<Gdk.Key> keyFind(Gdk.Key key)
Ivan Mahonin a6a507
			{ return keyState.find(key); }
Ivan Mahonin a6a507
		public bool isKeyPressed(Gdk.Key key)
Ivan Mahonin a6a507
			{ return keyFind(key) != null; }
Ivan Mahonin a6a507
		public double howLongKeyPressed(Gdk.Key key, long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return KeyState<Gdk.Key>.Holder.howLongPressed(keyFind(key), ticks, timeOffset); }
Ivan Mahonin a6a507
		public double howLongKeyPressed(Gdk.Key key)
Ivan Mahonin a6a507
			{ return howLongKeyPressed(key, ticks); }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public KeyHistory<uint> buttonHistory(Gdk.Device device) {
Ivan Mahonin a6a507
			KeyHistory<uint> history;
Ivan Mahonin a6a507
			if (!buttonHistories.TryGetValue(device, out history))
Ivan Mahonin a6a507
				history = new KeyHistory<uint>();
Ivan Mahonin a6a507
				buttonHistories[device] = history;
Ivan Mahonin a6a507
			return history;
Ivan Mahonin a6a507
		}
Ivan Mahonin a6a507
		public KeyState<uint> buttonState(Gdk.Device device)
Ivan Mahonin 589f9a
			{ return buttonHistory(device).current; }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public void buttonEvent(bool press, Gdk.Device device, uint button, long ticks) {
Ivan Mahonin a6a507
			touch(ticks);
Ivan Mahonin a6a507
			buttonHistory(device).change(press, button, this.ticks);
Ivan Mahonin a6a507
		}
Ivan Mahonin a6a507
		public void buttonPress(Gdk.Device device, uint button, long ticks)
Ivan Mahonin a6a507
			{ buttonEvent(true, device, button, ticks); }
Ivan Mahonin a6a507
		public void buttonRelease(Gdk.Device device, uint button, long ticks)
Ivan Mahonin a6a507
			{ buttonEvent(false, device, button, ticks); }
Ivan Mahonin a6a507
		public void buttonEvent(bool press, uint button, long ticks)
Ivan Mahonin a6a507
			{ buttonEvent(press, null, button, ticks); }
Ivan Mahonin a6a507
		public void buttonPress(uint button, long ticks)
Ivan Mahonin a6a507
			{ buttonEvent(true, button, ticks); }
Ivan Mahonin a6a507
		public void buttonRelease(uint button, long ticks)
Ivan Mahonin a6a507
			{ buttonEvent(false, button, ticks); }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public KeyState<uint> buttonFind(Gdk.Device device, uint button)
Ivan Mahonin a6a507
			{ return buttonState(device).find(button); }
Ivan Mahonin a6a507
		public bool isButtonPressed(Gdk.Device device, uint button)
Ivan Mahonin a6a507
			{ return buttonFind(device, button) != null; }
Ivan Mahonin a6a507
		public double howLongButtonPressed(Gdk.Device device, uint button, long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return KeyState<uint>.Holder.howLongPressed(buttonFind(device, button), ticks, timeOffset); }
Ivan Mahonin a6a507
		public double howLongButtonPressed(Gdk.Device device, uint button)
Ivan Mahonin 589f9a
			{ return howLongButtonPressed(device, button, ticks); }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public KeyState<uint> buttonFindDefault(uint button)
Ivan Mahonin a6a507
			{ return buttonFind(null, button); }
Ivan Mahonin a6a507
		public bool isButtonPressedDefault(uint button)
Ivan Mahonin a6a507
			{ return isButtonPressed(null, button); }
Ivan Mahonin a6a507
		public double howLongButtonPressedDefault(uint button, long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return howLongButtonPressed(null, button, ticks, timeOffset); }
Ivan Mahonin a6a507
		public double howLongButtonPressedDefault(uint button)
Ivan Mahonin a6a507
			{ return howLongButtonPressedDefault(button, ticks); }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public KeyState<uint> buttonFindAny(uint button, out Gdk.Device device) {
Ivan Mahonin a6a507
			device = null;
Ivan Mahonin a6a507
			KeyState<uint> state = null;
Ivan Mahonin a6a507
			foreach(KeyValuePair<Gdk.Device, KeyHistory<uint>> pair in buttonHistories) {
Ivan Mahonin a6a507
				KeyState<uint> s = pair.Value == null ? null : pair.Value.current.find(button);
Ivan Mahonin a6a507
				if (s != null && (state == null || s.ticks < state.ticks))
Ivan Mahonin a6a507
					{ state = s; device = pair.Key; }
Ivan Mahonin a6a507
			}
Ivan Mahonin a6a507
			return state;
Ivan Mahonin a6a507
		}
Ivan Mahonin a6a507
		public KeyState<uint> buttonFindAny(uint button)
Ivan Mahonin a6a507
			{ Gdk.Device device; return buttonFindAny(button, out device); }
Ivan Mahonin a6a507
		public bool isButtonPressedAny(uint button)
Ivan Mahonin a6a507
			{ return buttonFindAny(button) != null; }
Ivan Mahonin a6a507
		public double howLongButtonPressedAny(uint button, long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return KeyState<uint>.Holder.howLongPressed(buttonFindAny(button), ticks, timeOffset); }
Ivan Mahonin a6a507
		public double howLongButtonPressedAny(uint button)
Ivan Mahonin a6a507
			{ return howLongButtonPressedAny(button, ticks); }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public KeyState<Gdk.Key>.Holder keyStateHolder(long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return new KeyState<Gdk.Key>.Holder(keyState, ticks, timeOffset); }
Ivan Mahonin a6a507
		public KeyState<Gdk.Key>.Holder keyStateHolder()
Ivan Mahonin a6a507
			{ return keyStateHolder(ticks); }
Ivan Mahonin 61bedf
		public KeyHistory<Gdk.Key>.Holder keyHistoryHolder(long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return new KeyHistory<Gdk.Key>.Holder(keyHistory, ticks, timeOffset); }
Ivan Mahonin 61bedf
		public KeyHistory<Gdk.Key>.Holder keyHistoryHolder()
Ivan Mahonin 61bedf
			{ return keyHistoryHolder(ticks); }
Ivan Mahonin a6a507
Ivan Mahonin a6a507
		public KeyState<uint>.Holder buttonStateHolder(Gdk.Device device, long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return new KeyState<uint>.Holder(buttonState(device), ticks, timeOffset); }
Ivan Mahonin a6a507
		public KeyState<uint>.Holder buttonStateHolder(Gdk.Device device)
Ivan Mahonin a6a507
			{ return buttonStateHolder(device, ticks); }
Ivan Mahonin a6a507
		public KeyHistory<uint>.Holder buttonHistoryHolder(Gdk.Device device, long ticks, double timeOffset = 0.0)
Ivan Mahonin a6a507
			{ return new KeyHistory<uint>.Holder(buttonHistory(device), ticks, timeOffset); }
Ivan Mahonin a6a507
		public KeyHistory<uint>.Holder buttonHistoryHolder(Gdk.Device device)
Ivan Mahonin 589f9a
			{ return buttonHistoryHolder(device, ticks); }
Ivan Mahonin a6a507
	}
Ivan Mahonin a6a507
}
Ivan Mahonin a6a507