Blame mono/Assistance/InputState.cs

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