2012-04-09 183 views
3

我想我會嘗試一個簡單的GUI應用程序,使用世界/宇宙無突變方法,但試圖自己實現'世界/宇宙'程序設計。超越世界/宇宙

我在下面得到了我的小圖,但我很快得出結論,雖然我可以使用教學包,但我不知道如何自己實現教學包功能。

我猜我應該使用continuations,但這似乎並不是universe.rkt源文件中的方法。

我總是可以將程序加入畫布類(就像之前的幻燈片和同樣的遊戲似的),但我真的想要掌握如何實現「世界/宇宙」風格的程序控制。

;;;;---- 
#lang racket/gui 

; simple drawing program 
; mousedown starts recording a list of points 
; mousechanged starts recording a new list 
; paint callback paints the list of lists as lines. 

(define diagramframe (new frame% [label "paint"] [width 300] 
           [height 300] [x 1000][y 300])) 

;(define lines '(((0 . 0) (0 . 300) (250 . 250) (150 . 176)))) 
(define lines '(((0 . 0) (0 . 300) (250 . 250) (150 . 176)) 
                ((10 . 4) (280 . 10)))) 

(define paintcanvas% 
  (class canvas% 
    (init-field mouse-event-callback) 
    (super-new) 
    (define dc (send this get-dc)) 
    (define/override (on-event mouse-event) 
      (mouse-event-callback mouse-event)))) 

(define (paint-cb c dc)  
  (for-each (λ (line) (send dc draw-lines line)) lines)) 

(define (me-cb mouse-event) 
    (let ((x (send mouse-event get-x)) 
        (y (send mouse-event get-y))) 
    (when (and (send mouse-event get-left-down) 
               (send mouse-event moving?)) 
      (if (send mouse-event button-changed?) 
          ; if true append as new list 
          '()  
          ; if false append existing list 
          '())))) 

(define Paintcanvas (new paintcanvas% 
                         [parent diagramframe] 
         [paint-callback paint-cb] 
         [mouse-event-callback me-cb])) 

(define (main world) 
 (when world (main (??? world))) 
  (send diagramframe show #t)) 
  
(main lines) 

(send diagramframe show #t) 

;;----- 

回答

5

這是我該怎麼做的。

請注意,GUI事件循環充當程序主體。 使用計時器事件來實現掛機。

#lang racket/gui 

;;; 
;;; WORLD 
;;; 

(define-struct world (lines)) 
(define the-world (make-world '((0 . 0) (0 . 300) (250 . 250) (150 . 176) (10 . 4) (280 . 10)))) 

;;; 
;;; USER LAND 
;;; 

(define (on-mouse-event world event) 
    (if (and (send event get-left-down) 
      (send event moving?) 
      #; (send event button-changed?)) 
     (let ((x (send event get-x)) 
      (y (send event get-y))) 
     (make-world (cons (cons x y) (world-lines world)))) 
     world)) 

(define (on-paint world dc) 
    (send dc draw-lines 
     (map pair->point (world-lines world)))) 

(define (pair->point p) 
    (make-object point% (car p) (cdr p))) 


;;; 
;;; SYSTEM 
;;; 

(define user:on-paint on-paint) 

(define diagramframe (new frame% [label "paint"] [width 300] [height 300] [x 1000][y 300])) 

(define paintcanvas% 
    (class canvas% 
    (inherit get-dc refresh) 
    (super-new) 

    (define/override (on-paint) 
     (send (get-dc) suspend-flush) 
     (user:on-paint the-world (get-dc)) 
     (send (get-dc) resume-flush)) 

    (define/override (on-event mouse-event) 
     (let* ([old-world the-world] 
      [new-world (on-mouse-event the-world mouse-event)]) 
     (if (eq? old-world new-world) 
      (super on-event mouse-event) 
      (begin 
       (set! the-world new-world) 
       (refresh))))))) 

(define paintcanvas (new paintcanvas% [parent diagramframe])) 
(send diagramframe show #t) 
+0

謝謝 - 很好的迴應。 – Stephen 2012-04-10 00:35:05