1 module ui.Area;
2 
3 import ui.Control;
4 
5 class Area : Control {
6     protected uiArea * _area;
7     protected uiAreaHandler * _handler;
8 
9     private {
10         void initHandler() {
11             _handler = new uiAreaHandler();
12             _handler.Draw = &DrawCallback;
13             _handler.MouseEvent = &MouseEventCallback;
14             _handler.MouseCrossed = &MouseCrossedCallback;
15             _handler.DragBroken = &DragBrokenCallback;
16             _handler.KeyEvent = &KeyEventCallback;
17         }
18 
19         static Area[uiArea*] _areaMap;
20         void delegate(Area a, DrawParams params)[] DrawListeners;
21         void delegate(Area a, MouseEvent event)[] MouseEventListeners;
22         void delegate(Area a, int left)[] MouseCrossedListeners;
23         void delegate(Area a)[] DragBrokenListeners;
24         void delegate(Area a, KeyEvent event)[] KeyEventListeners;
25 
26         extern(C) static {
27             void DrawCallback(uiAreaHandler *ah, uiArea *a, uiAreaDrawParams *p) {
28                 auto area = _areaMap[a];
29                 foreach (dlg; area.DrawListeners) {
30                     dlg(area, DrawParams(p));
31                 }
32             }
33             void MouseEventCallback(uiAreaHandler *ah, uiArea *a, uiAreaMouseEvent *e) {
34                 auto area = _areaMap[a];
35                 foreach (dlg; area.MouseEventListeners) {
36                     dlg(area, MouseEvent(e));
37                 }
38             }
39             void MouseCrossedCallback(uiAreaHandler *ah, uiArea *a, int left) {
40                 auto area = _areaMap[a];
41                 foreach (dlg; area.MouseCrossedListeners) {
42                     dlg(area, left);
43                 }
44             }
45             void DragBrokenCallback(uiAreaHandler *ah, uiArea *a) {
46                 auto area = _areaMap[a];
47                 foreach (dlg; area.DragBrokenListeners) {
48                     dlg(area);
49                 }
50             }
51             int KeyEventCallback(uiAreaHandler *ah, uiArea *a, uiAreaKeyEvent *e) {
52                 auto area = _areaMap[a];
53                 foreach (dlg; area.KeyEventListeners) {
54                     dlg(area, KeyEvent(e));
55                 }
56                 return 1;
57             }
58         }
59     }
60 
61     public {
62         Area addDraw(void delegate(Area a, DrawParams params) f) {
63             DrawListeners ~= f;
64             return this;
65         }
66         Area addMouseEvent(void delegate(Area a, MouseEvent event) f) {
67             MouseEventListeners ~= f;
68             return this;
69         }
70         Area addMouseCrossed(void delegate(Area a, int left) f) {
71             MouseCrossedListeners ~= f;
72             return this;
73         }
74         Area addDragBroken(void delegate(Area a) f) {
75             DragBrokenListeners ~= f;
76             return this;
77         }
78         Area addKeyEvent(void delegate(Area a, KeyEvent event) f) {
79             KeyEventListeners ~= f;
80             return this;
81         }
82     }
83 
84     this(bool scrolling = false, int width = 240, int height = 180) {
85         initHandler;
86         if (!scrolling) {
87             _area = uiNewArea(_handler);
88         } else {
89             _area = uiNewScrollingArea(_handler, width, height);
90         }
91         _areaMap[_area] = this;
92         super(cast(uiControl *) _area);
93     }
94 
95     Area setSize(int width, int height) {
96         uiAreaSetSize(_area, width, height);
97         return this;
98     }
99 
100     Area queueRedrawAll() {
101         uiAreaQueueRedrawAll(_area);
102         return this;
103     }
104 
105     Area scrollTo(double x, double y, double width, double height) {
106         uiAreaScrollTo(_area, x, y, width, height);
107         return this;
108     }
109 }
110 
111 struct DrawParams {
112     private uiAreaDrawParams * _params;
113 
114     @property pragma(inline, true) {
115         import ui.Draw: Context;
116         auto context() {
117             return Context(_params.Context);
118         }
119         void context(Context context) {
120             _params.Context = context._context;
121         }
122 
123         ref areaWidth() {
124             return _params.AreaWidth;
125         }
126         ref areaHeight() {
127             return _params.AreaHeight;
128         }
129 
130         ref clipX() {
131             return _params.ClipX;
132         }
133         ref clipY() {
134             return _params.ClipY;
135         }
136         ref clipWidth() {
137             return _params.ClipWidth;
138         }
139         ref clipHeight() {
140             return _params.ClipHeight;
141         }
142     }
143 
144     this(uiAreaDrawParams * params) {
145         _params = params;
146     }
147 }
148 
149 struct MouseEvent {
150     private uiAreaMouseEvent * _event;
151 
152     @property pragma(inline, true) {
153         ref x() {
154             return _event.X;
155         }
156         ref y() {
157             return _event.Y;
158         }
159 
160         ref areaWidth() {
161             return _event.AreaWidth;
162         }
163         ref areaHeight() {
164             return _event.AreaHeight;
165         }
166 
167         ref down() {
168             return _event.Down;
169         }
170         ref up() {
171             return _event.Up;
172         }
173 
174         ref count() {
175             return _event.Count;
176         }
177 
178         ref modifiers() {
179             return _event.Modifiers;
180         }
181 
182         ref held1to64() {
183             return _event.Held1To64;
184         }
185     }
186 
187     this(uiAreaMouseEvent * event) {
188         _event = event;
189     }
190 }
191 
192 struct KeyEvent {
193     private uiAreaKeyEvent * _event;
194 
195     @property pragma(inline, true) {
196         ref key() {
197             return _event.Key;
198         }
199         ref extKey() {
200             return _event.ExtKey;
201         }
202         ref modifier() {
203             return _event.Modifier;
204         }
205 
206         ref modifiers() {
207             return _event.Modifiers;
208         }
209 
210         ref up() {
211             return _event.Up;
212         }
213     }
214 
215     this(uiAreaKeyEvent * event) {
216         _event = event;
217     }
218 }