1 /** 2 * Input Source 3 */ 4 module d2d.sdl2.InputSource; 5 6 import std.algorithm; 7 import std.array; 8 import std.datetime; 9 import d2d.sdl2; 10 11 /** 12 * A pressable input source that stores it's own state 13 * State gets updated by an InputSource class that contains the pressable 14 */ 15 class Pressable(T) { 16 17 immutable T id; ///The identifier for the pressable 18 SysTime lastPressed; ///The time at which this pressable was pressed 19 SysTime lastReleased; ///The time at which this pressable was released 20 21 /** 22 * Constructor for a pressable takes its id 23 */ 24 this(T id) { 25 this.id = id; 26 } 27 28 /** 29 * Returns whether or not this pressable is currently being held 30 */ 31 @property bool isPressed() { 32 return this.lastReleased < this.lastPressed; 33 } 34 35 /** 36 * Checks if this pressable is pressed 37 * If it is, it will mark it as released 38 * Returns whether this was actually pressed or not 39 */ 40 bool testAndRelease() { 41 if (!this.isPressed) { 42 return false; 43 } 44 this.lastReleased = Clock.currTime(); 45 return true; 46 } 47 48 } 49 50 /** 51 * A source of input from the user 52 * Handles acculmulating events and storing all the states of all the pressables 53 * The template parameter is the type for the identifier of all pressables 54 */ 55 abstract class InputSource(T) : EventHandler { 56 57 @property Pressable!T[T] allPressables(); ///Return a list of all of the pressables that can be accessed by the template type 58 59 /** 60 * Returns a list of all of the pressables that are held down. 61 */ 62 Pressable!T[] getPressedPressables() { 63 return this.allPressables.values.filter!(pressable => pressable.isPressed).array; 64 } 65 66 }