1 module armos.events.events; 2 import armos.events; 3 import armos.utils; 4 5 class KeyPressedEventArg : armos.events.EventArg{ 6 KeyType key; 7 }; 8 9 class KeyReleasedEventArg : armos.events.EventArg{ 10 KeyType key; 11 }; 12 13 class UnicodeInputtedEventArg : armos.events.EventArg{ 14 uint key; 15 }; 16 17 @disable class MessageReceivedEventArg :armos.events.EventArg {} 18 19 mixin template MouseEvent(){ 20 int x, y, button; 21 this(int x, int y, int button){ 22 this.x = x; 23 this.y = y; 24 this.button = button; 25 }; 26 } 27 28 class MouseDraggedEventArg :armos.events.EventArg {mixin MouseEvent;} 29 class MouseEnteredEventArg :armos.events.EventArg {mixin MouseEvent;} 30 class MouseExitedEventArg :armos.events.EventArg {mixin MouseEvent;} 31 class MouseMovedEventArg :armos.events.EventArg {mixin MouseEvent;} 32 class MousePressedEventArg :armos.events.EventArg {mixin MouseEvent;} 33 class MouseReleasedEventArg :armos.events.EventArg {mixin MouseEvent;} 34 class MouseScrolledEventArg :armos.events.EventArg {mixin MouseEvent;} 35 36 mixin template TouchEvent(){ 37 int x, y, id; 38 this(int x, int y, int id){ 39 this.x = x; 40 this.y = y; 41 this.id = id; 42 }; 43 } 44 45 class TouchCancelledEventArg{mixin TouchEvent;} 46 class TouchDoubleTapEventArg{mixin TouchEvent;} 47 class TouchDownEventArg{mixin TouchEvent;} 48 class TouchMovedEventArg{mixin TouchEvent;} 49 class TouchUpEventArg{mixin TouchEvent;} 50 51 class WindowResizeEventArg{ 52 int w, h; 53 this(int w, int h){ 54 this.w = w; 55 this.h = h; 56 } 57 } 58 59 class CoreEvents { 60 private armos.events.EventArg voidEventArg; 61 62 armos.events.Event!(armos.events.EventArg) setup; 63 armos.events.Event!(armos.events.EventArg) update; 64 armos.events.Event!(armos.events.EventArg) draw; 65 armos.events.Event!(armos.events.EventArg) exit; 66 67 armos.events.Event!(KeyPressedEventArg) keyPressed; 68 armos.events.Event!(KeyReleasedEventArg) keyReleased; 69 armos.events.Event!(UnicodeInputtedEventArg) unicodeInputted; 70 71 armos.events.Event!(MouseDraggedEventArg) mouseDragged; 72 armos.events.Event!(MouseEnteredEventArg) mouseEntered; 73 armos.events.Event!(MouseMovedEventArg) mouseMoved; 74 armos.events.Event!(MousePressedEventArg) mousePressed; 75 armos.events.Event!(MouseReleasedEventArg) mouseReleased; 76 armos.events.Event!(MouseScrolledEventArg) mouseScrolled; 77 78 armos.events.Event!(TouchCancelledEventArg) touchCancelled; 79 armos.events.Event!(TouchDoubleTapEventArg) touchDoubleTap; 80 armos.events.Event!(TouchDownEventArg) touchDown; 81 armos.events.Event!(TouchMovedEventArg) touchMoved; 82 armos.events.Event!(TouchUpEventArg) touchUp; 83 84 armos.events.Event!(WindowResizeEventArg) windowResize; 85 86 this(){ 87 setup = new armos.events.Event!(armos.events.EventArg); 88 update= new armos.events.Event!(armos.events.EventArg); 89 draw = new armos.events.Event!(armos.events.EventArg); 90 exit = new armos.events.Event!(armos.events.EventArg); 91 92 keyPressed = new armos.events.Event!(KeyPressedEventArg); 93 keyReleased = new armos.events.Event!(KeyReleasedEventArg); 94 95 unicodeInputted = new armos.events.Event!(UnicodeInputtedEventArg); 96 97 mouseDragged = new armos.events.Event!(MouseDraggedEventArg); 98 mouseEntered = new armos.events.Event!(MouseEnteredEventArg); 99 mouseMoved = new armos.events.Event!(MouseMovedEventArg); 100 mousePressed = new armos.events.Event!(MousePressedEventArg); 101 mouseReleased = new armos.events.Event!(MouseReleasedEventArg); 102 mouseScrolled = new armos.events.Event!(MouseScrolledEventArg); 103 104 touchCancelled = new armos.events.Event!(TouchCancelledEventArg); 105 touchDoubleTap = new armos.events.Event!(TouchDoubleTapEventArg); 106 touchDown = new armos.events.Event!(TouchDownEventArg); 107 touchMoved= new armos.events.Event!(TouchMovedEventArg); 108 touchUp = new armos.events.Event!(TouchUpEventArg); 109 110 windowResize = new armos.events.Event!(WindowResizeEventArg); 111 } 112 void notifySetup(){ 113 armos.events.notifyEvent(setup, voidEventArg); 114 }; 115 116 void notifyUpdate(){ 117 armos.events.notifyEvent(update, voidEventArg); 118 }; 119 120 void notifyDraw(){ 121 armos.events.notifyEvent(draw, voidEventArg); 122 }; 123 124 void notifyExit(){ 125 armos.events.notifyEvent(exit, voidEventArg); 126 }; 127 128 void notifyKeyPressed(KeyType key){ 129 auto obj = new KeyPressedEventArg; 130 obj.key = key; 131 armos.events.notifyEvent(keyPressed, obj); 132 } 133 134 void notifyKeyReleased(KeyType key){ 135 auto obj = new KeyReleasedEventArg; 136 obj.key = key; 137 armos.events.notifyEvent(keyReleased, obj); 138 } 139 140 void notifyUnicodeInput(uint key){ 141 auto obj = new UnicodeInputtedEventArg; 142 obj.key = key; 143 armos.events.notifyEvent(unicodeInputted, obj); 144 } 145 146 void notifyMouseDragged(int x, int y, int button){ 147 auto obj = new MouseDraggedEventArg(x, y, button); 148 armos.events.notifyEvent(mouseDragged, obj); 149 } 150 151 void notifyMouseEntered(int x, int y, int button){ 152 auto obj = new MouseEnteredEventArg(x, y, button); 153 armos.events.notifyEvent(mouseEntered, obj); 154 } 155 156 void notifyMouseMoved(int x, int y, int button){ 157 auto obj = new MouseMovedEventArg(x, y, button); 158 armos.events.notifyEvent(mouseMoved, obj); 159 } 160 161 void notifyMousePressed(int x, int y, int button){ 162 auto obj = new MousePressedEventArg(x, y, button); 163 armos.events.notifyEvent(mousePressed, obj); 164 } 165 166 void notifyMouseReleased(int x, int y, int button){ 167 auto obj = new MouseReleasedEventArg(x, y, button); 168 armos.events.notifyEvent(mouseReleased, obj); 169 } 170 171 void notifyMouseScrolled(int x, int y, int button){ 172 auto obj = new MouseScrolledEventArg(x, y, button); 173 armos.events.notifyEvent(mouseScrolled, obj); 174 } 175 176 void notifyTouchCancelled(int x, int y, int id){ 177 auto obj = new TouchCancelledEventArg(x, y, id); 178 armos.events.notifyEvent(touchCancelled, obj); 179 } 180 181 void notifyTouchDoubleTap(int x, int y, int id){ 182 auto obj = new TouchDoubleTapEventArg(x, y, id); 183 armos.events.notifyEvent(touchDoubleTap, obj); 184 } 185 186 void notifyTouchDown(int x, int y, int id){ 187 auto obj = new TouchDownEventArg(x, y, id); 188 armos.events.notifyEvent(touchDown, obj); 189 } 190 191 void notifyTouchMoved(int x, int y, int id){ 192 auto obj = new TouchMovedEventArg(x, y, id); 193 armos.events.notifyEvent(touchMoved, obj); 194 } 195 196 void notifyTouchUp(int x, int y, int id){ 197 auto obj = new TouchUpEventArg(x, y, id); 198 armos.events.notifyEvent(touchUp, obj); 199 } 200 201 void notifyWindowResize(int w, int h){ 202 auto obj = new WindowResizeEventArg(w, h); 203 armos.events.notifyEvent(windowResize, obj); 204 } 205 } 206 207 unittest{ 208 import armos.app; 209 210 auto core_events = new CoreEvents; 211 bool executedSetup = false; 212 int pressedKey = false; 213 import armos.app; 214 class TestApp: armos.app.BaseApp { 215 //i want to write like c++. the following is verbose... 216 //should i use template mixin instead alias? 217 alias armos.app.BaseApp.setup setup; 218 alias armos.app.BaseApp.update update; 219 alias armos.app.BaseApp.draw draw; 220 alias armos.app.BaseApp.keyPressed keyPressed; 221 222 override void setup(){ 223 224 executedSetup = true; 225 } 226 override void update(){} 227 228 override void draw(){} 229 230 231 override void keyPressed(KeyType key) { 232 233 char str_key = cast(char)key; 234 pressedKey = key; 235 } 236 237 } 238 239 auto app = new TestApp; 240 armos.events.addListener(core_events.setup, app, &app.setup); 241 armos.events.addListener(core_events.keyPressed, app, &app.keyPressed); 242 core_events.notifySetup(); 243 core_events.notifyKeyPressed(KeyType.A); 244 assert(executedSetup); 245 assert(pressedKey == KeyType.A); 246 }