aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/events/event-handler.lisp
blob: a88f63d274615b9a8dfc8d75664accb058427a5f (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
;;;; event-handler.lisp

(in-package #:wheelwork)


(defclass/std event-handler ()
  ((event-type handler-function tag :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)))))

(eval-when (:compile-toplevel :load-toplevel :execute) 
  (defun get-tag-from-handler-body (body)
    (when (eq (first body) :tag)
      (second body)))

  (defun get-body-from-handler-body (body)
    (if (get-tag-from-handler-body body)
        (nthcdr 2 body)
        body)))

(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
    :tag ,(get-tag-from-handler-body body)
    :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)))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :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))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :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))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :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))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :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))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :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))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :event-type 'wheelwork::blur
    :handler-function (lambda
                          (,(intern (symbol-name target)))
                        (declare
                         (ignorable ,(intern (symbol-name target))))
                        ,@(get-body-from-handler-body 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
    :tag ,(get-tag-from-handler-body body)
    :event-type 'wheelwork::focus
    :handler-function (lambda
                          (,(intern (symbol-name target)))
                        (declare
                         (ignorable ,(intern (symbol-name target))))
                        ,@(get-body-from-handler-body body))))