1 /** virtual button input */ 2 3 module re.input.virtual.button; 4 5 import re.input; 6 import std.algorithm; 7 8 /// a virtual button 9 class VirtualButton : VirtualInput { 10 /// monitors a single button 11 static abstract class Node : VirtualInput.Node { 12 @property public bool is_down(); 13 @property public bool is_up(); 14 @property public bool is_pressed(); 15 @property public bool is_released(); 16 } 17 18 /// logic-controllable button 19 static class LogicButton : Node { 20 public bool logic_pressed = false; 21 22 @property public override bool is_down() { return logic_pressed; } 23 @property public override bool is_up() { return !logic_pressed; } 24 @property public override bool is_pressed() { return logic_pressed; } 25 @property public override bool is_released() { return !logic_pressed; } 26 } 27 28 /// monitors a keyboard key 29 static class KeyboardKey : Node { 30 /// the key being monitored 31 public Keys key; 32 33 /// creates a keyboard key node 34 this(Keys key) { 35 this.key = key; 36 } 37 38 @property public override bool is_down() { 39 return Input.is_key_down(key); 40 } 41 42 @property public override bool is_up() { 43 return Input.is_key_up(key); 44 } 45 46 @property public override bool is_pressed() { 47 return Input.is_key_pressed(key); 48 } 49 50 @property public override bool is_released() { 51 return Input.is_key_released(key); 52 } 53 } 54 55 @property public bool is_down() { 56 return nodes.any!(x => (cast(Node) x).is_down); 57 } 58 59 @property public bool is_up() { 60 return nodes.any!(x => (cast(Node) x).is_up); 61 } 62 63 @property public bool is_pressed() { 64 return nodes.any!(x => (cast(Node) x).is_pressed); 65 } 66 67 @property public bool is_released() { 68 return nodes.any!(x => (cast(Node) x).is_released); 69 } 70 } 71 72 @("input-button") 73 unittest { 74 auto the_button = new VirtualButton(); 75 the_button.nodes ~= new VirtualButton.KeyboardKey(Keys.KEY_E); 76 77 assert(the_button.nodes.length > 0); 78 }