1 module graphite.events.events;
2 
3 import carbon.event;
4 //import graphite.types.point;
5 
6 import std.array,
7        std.format,
8        std.string,
9        std.variant;
10 
11 /+
12 struct Mouse
13 {
14     enum Type { pressed, moved, released, dragged, }
15 
16   static:
17     bool pressed(int button = -1);
18     int x() @property;
19     int y() @property;
20     int previousX() @property;
21     int previousY() @property;
22 }
23 
24 
25 struct KeyBoard
26 {
27   static:
28     bool pressed(int key = -1);
29 }
30 
31 
32 struct AppRuntime
33 {
34   static:
35     void exit();
36     void setEscapeQuitsApp(bool bQuitOnEscape);
37 }
38 
39 
40 struct DragInfo
41 {
42     string[] files;
43     Point position;
44 }
45 
46 
47 struct EntryEventInfo
48 {
49     int state;
50 }
51 
52 
53 struct KeyEventInfo
54 {
55     enum Type{ pressed, released }
56     Type type;
57     int key;
58 }
59 
60 
61 struct MouseEventInfo
62 {
63     enum Type{ pressed, moved, released, dragged }
64     Type type;
65     int button;
66 }
67 
68 
69 struct TouchEventInfo
70 {
71     enum Type{down, up, move, doubleTap, cancel}
72     Type type;
73 
74     int id;
75     int time;
76     int numTouches;
77     float width, height;
78     float angle;
79     float minoraxis, majoraxis;
80     float pressure;
81     float xspeed, yspeed;
82     float xaccel, yaccel;
83 }
84 
85 
86 struct AudioEventInfo
87 {
88     float[] buffer;
89     int nChannels;
90 }
91 
92 
93 struct ResizeEventInfo
94 {
95     int width;
96     int height;
97 }
98 
99 
100 final class CoreEvents
101 {
102     this(){}
103 
104     Event!(Variant) setup, update, draw, exit;
105 
106     Event!(EntryEventInfo) windowEntered;
107     Event!(ResizeEventInfo) windowResized;
108 
109     Event!(KeyEventInfo) keyPressed, keyReleased;
110 
111     Event!(MouseEventInfo) mouseMoved, mouseDragged, mousePressed, mouseReleased;
112 
113     Event!(AudioEventInfo) audioReceived, audioRequested;
114 
115     Event!(TouchEventInfo) touchDown, touchUp, touchMoved, touchDoubleTap, touchCancelled;
116 
117     Event!(string) messageEvent;
118     Event!(DragInfo) fileDragEvent;
119 
120 
121     void disable()
122     {
123         setup.disable();
124         update.disable();
125         draw.disable();
126         exit.disable();
127         //windowEntered.disable();
128         //windowResized.disable();
129         keyPressed.disable();
130         keyReleased.disable();
131         mouseMoved.disable();
132         mouseDragged.disable();
133         mousePressed.disable();
134         mouseReleased.disable();
135         audioReceived.disable();
136         audioRequested.disable();
137         touchDown.disable();
138         touchUp.disable();
139         touchMoved.disable();
140         touchDoubleTap.disable();
141         touchCancelled.disable();
142         messageEvent.disable();
143         fileDragEvent.disable();
144     }
145 
146 
147     void enable()
148     {
149         setup.enable();
150         update.enable();
151         draw.enable();
152         exit.enable();
153         //windowEntered.enable();
154         //windowResized.enable();
155         keyPressed.enable();
156         keyReleased.enable();
157         mouseMoved.enable();
158         mouseDragged.enable();
159         mousePressed.enable();
160         mouseReleased.enable();
161         audioReceived.enable();
162         audioRequested.enable();
163         touchDown.enable();
164         touchUp.enable();
165         touchMoved.enable();
166         touchDoubleTap.enable();
167         touchCancelled.enable();
168         messageEvent.enable();
169         fileDragEvent.enable();
170     }
171 
172 
173     mixin(genMethods("MouseEvents", ["mouseMoved", "mouseDragged", "mousePressed", "mouseReleased"]));
174     mixin(genMethods("KeyEvents", ["keyPressed", "keyReleased"]));
175     mixin(genMethods("TouchEvents", ["touchDoubleTap", "touchDown", "touchMoved", "touchUp", "touchCancelled"]));
176     //mixin(genMethods("GetMessage", ["messageEvent"]));
177     //mixin(genMethods("DragEvents", ["fileDragEvent"]));
178 
179 
180   private:
181     static string genMethods(string name, string[] slots)
182     {
183         return format(q{
184             void connect%1$s(string method, ClassType)(ClassType obj)
185             {
186                 foreach(e; TypeTuple!(%2$(%s,%)))
187                     mixin("this." ~ e ~ ".connect!method(obj);");
188             }
189 
190 
191             void connect%1$s(ClassType, Args...)(ClassType obj, void delegate(ClassType obj, Args) dg)
192             {
193                 foreach(e; TypeTuple!(%2$(%s,%)))
194                     mixin("this." ~ e ~ ".connect(obj, dg);");
195             }
196 
197 
198             void strongConnect%1$s(Args...)(void delegate(ClassType obj, Args) dg)
199             {
200                 foreach(e; TypeTuple!(%2$(%s,%)))
201                     mixin("this." ~ e ~ ".strongConnect(dg);");
202             }
203 
204 
205             void disconnect%1$s(string method, ClassType)(ClassType obj)
206             {
207                 foreach(e; TypeTuple!(%2$(%s,%)))
208                     mixin("this." ~ e ~ ".disconnect!method(obj);");
209             }
210 
211 
212             void disconnect%1$s(ClassType, Args...)(ClassType obj, void delegate(ClassType obj, Args) dg)
213             {
214                 foreach(e; TypeTuple!(%2$(%s,%)))
215                     mixin("this." ~ e ~ ".disconnect!method(obj, dg);");
216             }
217 
218 
219             void disconnect%1$s(ClassType)(ClassType obj)
220             {
221                 foreach(e; TypeTuple!(%2$(%s,%)))
222                     mixin("this." ~ e ~ ".disconnect(obj);");
223             }
224 
225 
226             void strongDisconnect%1$s(Args...)(void delegate(Args) dg)
227             {
228                 foreach(e; TypeTuple!(%2$(%s,%)))
229                     mixin("this." ~ e ~ ".strongDisconnect(dg);");
230             }
231         }, name, slots);
232     }
233 }
234 
235 
236 CoreEvents globalEvents() @property
237 {
238     static CoreEvents events;
239     if(events is null)
240         events = new CoreEvents();
241 
242     return events;
243 }
244 +/