Racket: using events in the frame window%

I am studying Racket (formerly PLT Scheme, LISP dialogs) and trying to figure out how to handle events other than paint-callback (maybe it's not just one).

I was hoping a lot for this part of the document , but on-char and on-event do not seem to do anything that might interest me (or nothing).

In addition, I do not understand eventpaces, queue-callback, and their use. An example would be a cool thing! I will be grateful to the good person who will write me one :).

Here is my code:

 (define game (new frame%)) (define gameLay (class canvas% (super-new))) (new gameLay [parent game] [paint-callback (canvas dc) #|draw things|#)]) 

I want to use something like "on-mouse-click-left" (which does not exist), as I use "paint-callback", but I think I need to add steps (I read about eventpaces, etc. ).). I know this will not work, but here is the hypothetical code I'm looking for:

 (new gameLay [parent game] [paint-callback (λ (canvas dc) #|draw things|#)] [on-mouse-click-left (λ (canvas dc) #|do other things|#)]) 
+6
source share
1 answer

Here's a small program that uses canvases and keyboard events. When you press the arrow key, the last one you clicked will appear on the canvas.

 #lang racket/gui (define game-canvas% (class canvas% (inherit get-width get-height refresh) ;; direction : one of #f, 'left, 'right, 'up, 'down (define direction #f) (define/override (on-char ke) (case (send ke get-key-code) [(left right up down) (set! direction (send ke get-key-code)) (refresh)] [else (void)])) (define/private (my-paint-callback self dc) (let ([w (get-width)] [h (get-height)]) (when direction (let ([dir-text (format "going ~a" direction)]) (let-values ([(tw th _ta _td) (send dc get-text-extent dir-text)]) (send dc draw-text dir-text (max 0 (/ (- w tw) 2)) (max 0 (/ (- h th) 2)))))))) (super-new (paint-callback (lambda (c dc) (my-paint-callback c dc)))))) (define game-frame (new frame% (label "game") (width 600) (height 400))) (define game-canvas (new game-canvas% (parent game-frame))) (send game-frame show #t) 

Each frame has an event space that controls event scheduling. The on-char method is an event handler; it runs in the event handler thread. No more events will be processed until your on-char method completes. Therefore, if you want to do something complicated, you may need to create a separate thread and perform the calculations there. One easy way to do this is to create another event space that does not process events for any frame, but processes the "events" that you send using queue-callback . For example, replace the on-char definition with the following:

 (define aux-eventspace (make-eventspace)) (define/override (on-char ke) (parameterize ((current-eventspace aux-eventspace)) (queue-callback (lambda () (case (send ke get-key-code) ((left right up down) (set! direction (send ke get-key-code)) (refresh)) (else (void))))))) 

The function specified by queue-callback is run in a separate thread. You can insert some printouts, delays, or something else to convince yourself that the main event space can still handle events, and the other is a callback.

+10
source

Source: https://habr.com/ru/post/896554/


All Articles