<html><body><div>Greetings,<br></div><div><br></div><div>Many thanks for all your work on McClim. I have been enjoying using it while trying to work my way through Tilings and Patterns by Grunbaum and Shepard.<br></div><div><br></div><div>I think I have gotten a workable approach to panning and zooming. Any comments or suggestions would be greatly appreciated, but my main purpose in posting this rather long email is so that future users might save themselves some work. Thanks again.<br></div><div><br></div><div>Robert<br></div><div><br></div><div>;;; This is a first attempt at implementing panning and zooming in<br></div><div>;;; McClim. It mostly works, but after a few (say 10) zooming<br></div><div>;;; operations (in or out), the displayed region is not correct ---<br></div><div>;;; subsequent panning operations can leave a part of the window<br></div><div>;;; unpainted. This may be because of the accumulating inacuracy<br></div><div>;;; inherent in repeated floating-point operations.<br></div><div><br></div><div>(defpackage #:pan-and-zoom-1<br></div><div> (:use #:clim-lisp #:clim))<br></div><div><br></div><div>(in-package #:pan-and-zoom-1)<br></div><div><br></div><div>;;; This class will be our model for the infinite Euclidean plane.<br></div><div><br></div><div>;;; N.B. I do not understand the McClim pane hierarchy. The below may<br></div><div>;;; not behave correctly if plane inherits from classes of panes other<br></div><div>;;; than basic-pane. In particular, application-pane did odd things.<br></div><div>(defclass plane (standard-extended-input-stream<br></div><div> basic-pane)<br></div><div> ())<br></div><div><br></div><div>;;; Draw a bunch of squares, so that we can examine the effect of<br></div><div>;;; panning and zooming.<br></div><div>(defmethod handle-repaint ((plane plane) region)<br></div><div> (declare (ignore region))<br></div><div> (window-clear plane)<br></div><div> (dotimes (i 40)<br></div><div> (dotimes (j 40)<br></div><div> (let ((c-x (- (* i 40) 800))<br></div><div> (c-y (- (* j 40) 800)))<br></div><div> (draw-polygon* plane `(,c-x ,c-y<br></div><div> ,(+ c-x 40) ,c-y<br></div><div> ,(+ c-x 40) ,(+ c-y 40)<br></div><div> ,c-x ,(+ c-y 40))<br></div><div> :filled 'NIL<br></div><div> :line-thickness 2<br></div><div> :ink +black+)<br></div><div> (draw-text* plane<br></div><div> (format nil "~A, ~A" i j)<br></div><div> (+ c-x 20) (+ c-y 20)<br></div><div> :align-x :center<br></div><div> :align-y :center)))))<br></div><div><br></div><div>(defun pan (dx dy)<br></div><div> (let* ((plane (find-pane-named *application-frame* 'plane))<br></div><div> (plane-transformation (sheet-transformation plane))<br></div><div> (translation-transformation (make-translation-transformation dx dy))<br></div><div> (new-plane-transformation (compose-transformations<br></div><div> translation-transformation<br></div><div> plane-transformation))<br></div><div> (sheet-region (sheet-region plane))<br></div><div> ;; We transform sheets, and untransform (apply the inverse<br></div><div> ;; transformation) to regions. I keep getting this confused.<br></div><div> (new-sheet-region (untransform-region translation-transformation<br></div><div> sheet-region)))<br></div><div> (setf (sheet-transformation plane) new-plane-transformation<br></div><div> (sheet-region plane) new-sheet-region)))<br></div><div><br></div><div>(defun zoom (factor)<br></div><div> (let* ((plane (find-pane-named *application-frame* 'plane))<br></div><div> (plane-transformation (sheet-transformation plane))<br></div><div> (scaling-transformation (make-scaling-transformation* factor<br></div><div> factor))<br></div><div> (new-plane-transformation (compose-transformations<br></div><div> scaling-transformation<br></div><div> plane-transformation))<br></div><div> (sheet-region (sheet-region plane))<br></div><div> (new-sheet-region (untransform-region scaling-transformation<br></div><div> sheet-region)))<br></div><div> (setf (sheet-transformation plane) new-plane-transformation<br></div><div> (sheet-region plane) new-sheet-region)))<br></div><div><br></div><div>(define-application-frame tiling ()<br></div><div> ()<br></div><div> (:panes (plane plane<br></div><div> :width 400<br></div><div> :height 400))<br></div><div> (:menu-bar t)<br></div><div> (:layouts (default<br></div><div> plane)))<br></div><div><br></div><div>;;; Without these next two methods (or an interactor pane), the<br></div><div>;;; panning/zooming commands will not be executed!<br></div><div><br></div><div>;;; From Franz's User Guide: You will often implement a method for<br></div><div>;;; this generic function for an application frame, since CLIM cannot<br></div><div>;;; always reliably determine which pane to use for *standard-input*.<br></div><div>(defmethod frame-standard-input ((tiling tiling))<br></div><div> (find-pane-named tiling 'plane))<br></div><div><br></div><div>(defmethod frame-standard-output ((tiling tiling))<br></div><div> (find-pane-named tiling 'plane))<br></div><div><br></div><div>(define-tiling-command (com-quit :menu t) ()<br></div><div> (frame-exit *application-frame*))<br></div><div><br></div><div>(define-tiling-command (com-pan-up :keystroke :up) ()<br></div><div> (pan 0 10))<br></div><div><br></div><div>(define-tiling-command (com-pan-down :keystroke :down) ()<br></div><div> (pan 0 -10))<br></div><div><br></div><div>(define-tiling-command (com-pan-right :keystroke :right) ()<br></div><div> (pan -10 0))<br></div><div><br></div><div>(define-tiling-command (com-pan-left :keystroke :left) ()<br></div><div> (pan 10 0))<br></div><div><br></div><div>(define-tiling-command (com-zoom-in :keystroke (:up :shift)) ()<br></div><div> (zoom 1.1))<br></div><div><br></div><div>(define-tiling-command (com-zoom-out :keystroke (:down :shift)) ()<br></div><div> (zoom 0.9))<br></div><div><br></div><div>(defun tiling ()<br></div><div> (flet ((run ()<br></div><div> (let ((frame (make-application-frame 'tiling)))<br></div><div> (setq *test-frame* frame)<br></div><div> (run-frame-top-level frame))))<br></div><div> (clim-sys:make-process #'run)))<br></div><div><br></div><div><br></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br></div><div><br></div><div>;;; This is a second attempt at implementing panning and zooming in<br></div><div>;;; McClim. It works, but is a little messy.<br></div><div><br></div><div>(defpackage #:pan-and-zoom-2<br></div><div> (:use #:clim-lisp #:clim))<br></div><div><br></div><div>(in-package #:pan-and-zoom-2)<br></div><div><br></div><div>;;; This class will be our model for the infinite Euclidean plane.<br></div><div><br></div><div>;;; We avoid the problems with pan-and-zoom-1's accumulating<br></div><div>;;; floating-point inaccuracies by keeping track of the plane's<br></div><div>;;; scaling and translations, and making the appropriate<br></div><div>;;; transformations when needed. We also keep track of the "raw"<br></div><div>;;; sheet-region so that we can transform (really untransform) it<br></div><div>;;; appropriately.<br></div><div><br></div><div>;;; N.B. I do not understand the McClim pane hierarchy. The below may<br></div><div>;;; not behave correctly if plane inherits from classes of panes other<br></div><div>;;; than basic-pane. In particular, application-pane did odd things.<br></div><div>(defclass plane (standard-extended-input-stream<br></div><div> basic-pane)<br></div><div> ((scale :initform 1<br></div><div> :accessor scale)<br></div><div> (dx :initform 0<br></div><div> :accessor dx)<br></div><div> (dy :initform 0<br></div><div> :accessor dy)<br></div><div> (raw-sheet-region :initform 'NIL<br></div><div> :accessor raw-sheet-region)))<br></div><div><br></div><div>;;; We catch sheet resize events here so that we can update the<br></div><div>;;; raw-sheet-region. We transform it here so that when we untransform<br></div><div>;;; it later, we get the original region back. This is needed because<br></div><div>;;; generally we will transform sheets and untransform regions.<br></div><div>(defmethod resize-sheet :after ((plane plane) width height)<br></div><div> (let ((transformation (sheet-transformation plane)))<br></div><div> (setf (raw-sheet-region plane) (transform-region transformation<br></div><div> (sheet-region plane)))))<br></div><div><br></div><div>;;; Draw a bunch of squares, so that we can examine the effect of<br></div><div>;;; panning and zooming.<br></div><div>(defmethod handle-repaint ((plane plane) region)<br></div><div> (declare (ignore region))<br></div><div> (unless (raw-sheet-region plane)<br></div><div> ;; Set the raw-sheet-region. This is not available at the time of<br></div><div> ;; the plane's initialization.<br></div><div> (setf (raw-sheet-region plane) (sheet-region plane)))<br></div><div> (window-clear plane)<br></div><div> (dotimes (i 40)<br></div><div> (dotimes (j 40)<br></div><div> (let ((c-x (- (* i 40) 800))<br></div><div> (c-y (- (* j 40) 800)))<br></div><div> (draw-polygon* plane `(,c-x ,c-y<br></div><div> ,(+ c-x 40) ,c-y<br></div><div> ,(+ c-x 40) ,(+ c-y 40)<br></div><div> ,c-x ,(+ c-y 40))<br></div><div> :filled 'NIL<br></div><div> :line-thickness 2<br></div><div> :ink +black+)<br></div><div> (draw-text* plane<br></div><div> (format nil "~A, ~A" i j)<br></div><div> (+ c-x 20) (+ c-y 20)<br></div><div> :align-x :center<br></div><div> :align-y :center)))))<br></div><div><br></div><div>(defun update-plane (plane)<br></div><div> (let* ((scaling (make-scaling-transformation* (scale plane)<br></div><div> (scale plane)))<br></div><div> (translation (make-translation-transformation (dx plane)<br></div><div> (dy plane)))<br></div><div> (transformation (compose-transformations scaling<br></div><div> translation)))<br></div><div> (setf (sheet-transformation plane) transformation<br></div><div> (sheet-region plane) (untransform-region transformation<br></div><div> (raw-sheet-region plane)))))<br></div><div><br></div><div>(defun pan (dx dy)<br></div><div> (let ((plane (find-pane-named *application-frame* 'plane)))<br></div><div> (setf (dx plane) (+ (dx plane) dx)<br></div><div> (dy plane) (+ (dy plane) dy))<br></div><div> (update-plane plane)))<br></div><div><br></div><div>(defun zoom (factor)<br></div><div> (let ((plane (find-pane-named *application-frame* 'plane)))<br></div><div> (setf (scale plane) (* factor (scale plane)))<br></div><div> (update-plane plane)))<br></div><div><br></div><div>(define-application-frame tiling ()<br></div><div> ()<br></div><div> (:panes (plane plane<br></div><div> :width 400<br></div><div> :height 400))<br></div><div> (:menu-bar t)<br></div><div> (:layouts (default plane)))<br></div><div><br></div><div>;;; Without these next two methods (or an interactor pane), the<br></div><div>;;; panning/zooming commands will not be executed!<br></div><div><br></div><div>;;; From Franz's User Guide: You will often implement a method for<br></div><div>;;; this generic function for an application frame, since CLIM cannot<br></div><div>;;; always reliably determine which pane to use for *standard-input*.<br></div><div>(defmethod frame-standard-input ((tiling tiling))<br></div><div> (find-pane-named tiling 'plane))<br></div><div><br></div><div>(defmethod frame-standard-output ((tiling tiling))<br></div><div> (find-pane-named tiling 'plane))<br></div><div><br></div><div>(define-tiling-command (com-quit :menu t) ()<br></div><div> (frame-exit *application-frame*))<br></div><div><br></div><div>(define-tiling-command (com-up :keystroke :up) ()<br></div><div> (pan 0 10))<br></div><div><br></div><div>(define-tiling-command (com-down :keystroke :down) ()<br></div><div> (pan 0 -10))<br></div><div><br></div><div>(define-tiling-command (com-right :keystroke :right) ()<br></div><div> (pan -10 0))<br></div><div><br></div><div>(define-tiling-command (com-left :keystroke :left) ()<br></div><div> (pan 10 0))<br></div><div><br></div><div>(define-tiling-command (com-zoom-in :keystroke (:up :shift)) ()<br></div><div> (zoom 1.1))<br></div><div><br></div><div>(define-tiling-command (com-zoom-out :keystroke (:down :shift)) ()<br></div><div> (zoom 0.9))<br></div><div><br></div><div>(defun tiling ()<br></div><div> (flet ((run ()<br></div><div> (let ((frame (make-application-frame 'tiling)))<br></div><div> (setq *test-frame* frame)<br></div><div> (run-frame-top-level frame))))<br></div><div> (clim-sys:make-process #'run)))<br></div><div><br></div></body></html>