aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/events/event-handler.lisp
blob: a7f1b1bd18be983e6d8cdac18a9c114fe45fb6b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
;;;; event-handler.lisp

(in-package #:wheelwork)


(defclass/std event-handler ()
  ((event-type handler-function :ri))
  (:metaclass closer-mop:funcallable-standard-class))

(defmethod initialize-instance :after ((eh event-handler) &key)
  (with-slots (handler-function) eh
    (closer-mop:set-funcallable-instance-function eh handler-function)))


(defmacro defhandler (name handler)
  "Defines a handler - binds (FDEFINITION NAME) to HANDLER, which
should be an expression that evaluates to an instance of
EVENT-HANDLER, which is funcallable.  It is define such that handlers
can be redefined using this form to support interactive development."
  (let ((handler-var (gensym)))
    `(let ((,handler-var ,handler))
       (if-let (extant (and (fboundp ',name)
                            (fdefinition ',name)))
         (closer-mop:set-funcallable-instance-function extant (handler-function ,handler-var))
         (setf (fdefinition ',name) ,handler-var)))))


(defmacro on-perframe
    ((&optional (target 'target) (time 'time)) &body body)
  "Creates a handler for 'PERFRAME events.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

 TARGET - the object currently handling the event 
 TIME - The current time in milliseconds
"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::perframe
    :handler-function (lambda (,(intern (symbol-name target))
                               ,(intern (symbol-name time)))
                        (declare (ignorable ,(intern (symbol-name target))
                                            ,(time (intern (symbol-name time)))))
                        ,@body)))

(defmacro on-keydown
    ((&optional (target 'target) (scancode 'scancode) (modifiers 'modifiers)) &body body)
  "Creates a lambda suitable for the value of a keydown event
  handler. 

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

 The function accepts two positional arguments TARGET and SCANCODE and
 one &REST argument MODIFIERS.

 SCANCODE will be a keyword of the form :SCANCODE-A, :SCANCODE-B ...

 The members of MODIFIERS look like :LSHIFT, :RCTRL, RALT, etc"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::keydown
    :handler-function (lambda (,(intern (symbol-name target))
                               ,(intern (symbol-name scancode))
                               &rest ,(intern (symbol-name modifiers)))
                        (declare (ignorable ,(intern (symbol-name target))
                                            ,(intern (symbol-name scancode))
                                            ,(intern (symbol-name modifiers))))
                        ,@body)))

(defmacro on-keyup
    ((&optional (target 'target) (scancode 'scancode) (modifiers 'modifiers)) &body body)
  "Creates a lambda suitable for the value of a keyup event
  handler. 

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.


  The function accepts two positional arguments TARGET and
  SCANCODE and one &REST argument MODIFIERS.

  SCANCODE will be a keyword of the form :SCANCODE-A, :SCANCODE-B ...

  The members of MODIFIERS look like :LSHIFT, :RCTRL, RALT, etc"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::keyup
    :handler-function (lambda (,(intern (symbol-name target))
                               ,(intern (symbol-name scancode))
                               &rest ,(intern (symbol-name modifiers)))
                        (declare (ignorable ,(intern (symbol-name target))
                                            ,(intern (symbol-name scancode))
                                            ,(intern (symbol-name modifiers))))
                        ,@body)))

(defmacro on-mousemotion
    ((&optional
        (target 'target)
        (x 'x) (y 'y)
        (xrel 'xrel) (yrel 'yrel)
        (state 'state)
        (win-x 'win-x) (win-y 'win-y)
        (win-xrel 'win-xrel) (win-yrel 'win-yrel))
     &body body)
    "ON-MOUSEMOTION defines a mouse motion event handler.  

 All variable arguments supplied to this handler form are optional. You
 may supply your own variables to use in your BODY or you may just
 refer to the defaults - they will be interned in the appropriate
 package.

 - TARGET is the object ontowhich the handler was installed
 - X and Y are the scaled screen coordinates 
 - XREL and YREL are the relative motion of the X and Y positions since
   the last event, in scaled coordinates
 - STATE is the button state, see the SDL2 docs
 - WIN-* variables are the unscaled event values, if you require them.
"

  `(make-instance
    'event-handler
    :event-type 'wheelwork::mousemotion
    :handler-function (lambda (,(intern (symbol-name target))
                               ,(intern (symbol-name x))
                               ,(intern (symbol-name y))
                               ,(intern (symbol-name xrel))
                               ,(intern (symbol-name yrel))
                               ,(intern (symbol-name state))
                               ,(intern (symbol-name win-x))
                               ,(intern (symbol-name win-y))
                               ,(intern (symbol-name win-xrel))
                               ,(intern (symbol-name win-yrel)))
                        (declare (ignorable ,(intern (symbol-name target))
                                            ,(intern (symbol-name x))
                                            ,(intern (symbol-name y))
                                            ,(intern (symbol-name xrel))
                                            ,(intern (symbol-name yrel))
                                            ,(intern (symbol-name state))
                                            ,(intern (symbol-name win-x))
                                            ,(intern (symbol-name win-y))
                                            ,(intern (symbol-name win-xrel))
                                            ,(intern (symbol-name win-yrel))))
                        ,@body)))

(defmacro on-mousedown
    ((&optional (target 'target)
        (x 'x) (y 'y)
        (clicks 'clicks) (button 'button)
        (win-x 'win-x) (win-y 'win-y))
     &body body)
  "Creates a handler for MOUSEDOWN events.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

 - TARGET is the object ontowhich the handler was installed
 - X and Y are the scaled screen coordinates 
 - BUTTON is a code for the mouse button pressed (see sdl docs)
 - CLICKS is the number of clicks 1 for single, 2 for double.
 - WIN-* variables are the unscaled event values, if you require them.
"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::mousedown
    :handler-function (lambda
                          (,(intern (symbol-name target))
                               ,(intern (symbol-name x))
                               ,(intern (symbol-name y))
                               ,(intern (symbol-name clicks))
                               ,(intern (symbol-name button))
                               ,(intern (symbol-name win-x))
                               ,(intern (symbol-name win-y)))
                        (declare
                         (ignorable ,(intern (symbol-name target))
                                            ,(intern (symbol-name x))
                                            ,(intern (symbol-name y))
                                            ,(intern (symbol-name clicks))
                                            ,(intern (symbol-name button))
                                            ,(intern (symbol-name win-x))
                                            ,(intern (symbol-name win-y))))
                        ,@body)))

(defmacro on-mouseup
    ((&optional (target 'target)
        (x 'x) (y 'y)
        (clicks 'clicks) (button 'button)
        (win-x 'win-x) (win-y 'win-y))
     &body body)
  "Creates a handler for MOUSEUP events

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

 - TARGET is the object ontowhich the handler was installed
 - X and Y are the scaled screen coordinates 
 - BUTTON is a code for the mouse button pressed (see sdl docs)
 - CLICKS is the number of clicks 1 for single, 2 for double.
 - WIN-* variables are the unscaled event values, if you require them.

"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::mouseup
    :handler-function (lambda
                          (,(intern (symbol-name target))
                               ,(intern (symbol-name x))
                               ,(intern (symbol-name y))
                               ,(intern (symbol-name clicks))
                               ,(intern (symbol-name button))
                               ,(intern (symbol-name win-x))
                               ,(intern (symbol-name win-y)))
                        (declare
                         (ignorable ,(intern (symbol-name target))
                                            ,(intern (symbol-name x))
                                            ,(intern (symbol-name y))
                                            ,(intern (symbol-name clicks))
                                            ,(intern (symbol-name button))
                                            ,(intern (symbol-name win-x))
                                            ,(intern (symbol-name win-y))))
                        ,@body)))

(defmacro on-mousewheel
    ((&optional (target 'target) (horiz 'horiz) (vert 'vert) (dir 'dir)) &body body)
  "Creates a handler for MOUSEWHEEL events

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

 - TARGET is the object ontowhich the handler was installed
 - HORIZ, VERT are 1 or -1
 - DIR is normal or flipped, see https://wiki.libsdl.org/SDL_MouseWheelEvent

"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::mousewheel
    :handler-function (lambda
                          (,(intern (symbol-name target))
                               ,(intern (symbol-name horiz))
                               ,(intern (symbol-name vert))
                               ,(intern (symbol-name dir)))
                        (declare
                         (ignorable ,(intern (symbol-name target))
                                            ,(intern (symbol-name horiz))
                                            ,(intern (symbol-name vert))
                                            ,(intern (symbol-name dir))))
                        ,@body)))

(defmacro on-blur
    ((&optional (target 'target)) &body body)
  "Creates a handler for BLUR events. BLUR is a psuedo event that
fires whenever an object loses focus.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::blur
    :handler-function (lambda
                          (,(intern (symbol-name target)))
                        (declare
                         (ignorable ,(intern (symbol-name target))))
                        ,@body)))

(defmacro on-focus
    ((&optional (target 'target)) &body body)
  "Creates a handler for a FOCUS event. FOCUS is a pusedo event that
fires when the FOCUS slot of the current APPLICATION instance is changed.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::focus
    :handler-function (lambda
                          (,(intern (symbol-name target)))
                        (declare
                         (ignorable ,(intern (symbol-name target))))
                        ,@body)))

(defmacro on-before-dropped
    ((&optional (target 'target)) &body body)
  "Creates a handler for BEFORE-DROPPED events, which fire before a
  unit is removed from its container.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.

"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::before-dropped
    :handler-function (lambda
                          (,(intern (symbol-name target)))
                        (declare
                         (ignorable ,(intern (symbol-name target))))
                        ,@body)))

(defmacro on-before-added
    ((&optional  (target 'target) (container 'container)) &body body)
  "Creates a handler for BEFORE-ADDED events, which fire before a unit
  TARGET is added to CONTAINER.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.
"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::before-added
    :handler-function (lambda
                          (,(intern (symbol-name container))
                           ,(intern (symbol-name target)))
                        (declare
                         (ignorable
                          ,(intern (symbol-name container))
                          ,(intern (symbol-name target))))
                        ,@body)))


(defmacro on-after-added
    ((&optional  (target 'target) (container 'container)) &body body)
  "Creates a handler for AFTER-ADDED events, which fire after a unit
  is added to a container.

 All variable arguments supplied to this handler form are
 optional. You may supply your own variables to use in your BODY or
 you may just refer to the defaults - they will be interned in the
 appropriate package.
"
  `(make-instance
    'event-handler
    :event-type 'wheelwork::after-added
    :handler-function (lambda
                          (,(intern (symbol-name container))
                           ,(intern (symbol-name target)))
                        (declare
                         (ignorable
                          ,(intern (symbol-name container))
                          ,(intern (symbol-name target))))
                        ,@body)))