Fancier Graphic Objects
Listing Six contains definitions of four more complicated types of graphic objects. The point differs slightly from simple in that it expects the object to which it's attached to contain a list of at least two numbers. These become the x and y coordinates of the point. Gobs like this one are useful simulations of movement when you put several in one window.
(defgob 'point #'nilfn #.'(lambda (val window scale) (point window (* (car val) scale) (* (cadr val) scale)))) (setq dialmin 0 dialmax 100) (defgob 'dial #'(lambda (val window scale) (circle window (* 50 scale) (* 50 scale) (* 48 scale))) #'(lambda (val window scale) (let ((theta (+ pi (' pi (div (- val dialmin) (- dialmax dialmin))))) (r (* 48 scale)) (x1 (* 50 scale)) (y1 (* 50 scale))) (line window x1 y1 (+ x1 (* r (cos theta))) (+ y1 (* r (sin theta))))))) (defgob 'function #'nilfn #'(lambda (vals window scale) (plotcurve window (sort vals #'(lambda (x y) (< (car x) (car y)))) scale))) (defgob 'parametric #'nilfn #'(lambda (vals window scale) (plotcurve window vals scale))) (defun plotcurve (window points scale) (unless < (length points) 2) (let ((p1 (car points)) (p2 (cadr points))) (line window (* (car p1) scale) (* (- 100 (cadr p1)) scale) (* (car p2) scale) (* (- 100 (cadr p2)) scale))) (plotcurve window (cdr points) scale)))
The second gob in Listing Six is the long-awaited dial. This version is reduced to its essentials, consisting only of a circle and a line. In actual use, dials have sort of minimum and maximum range -- here, dialmin and dialmax are by first set to 0 and 100, repectively. This range occupies the top 180 degrees of the dial, a situation easily altered by changing the culation of theta.
Finally, note that the dial, like all gobs for which size is an issue, is designed to fit by default into a 100-x-100 square, so that's how big it will be with scale 1.
The remaining gobs, function and parametric, assume that they are attached to an entry containing a list of (x,y) pairs. The only difference between the two is the order in which they connect these points: A function connects the order of increasing x coordinates; and a parametric connects them in the order in which they occur. Gobs like these take only slightly more effort to make than the simple gob, but a whole screen full of them working at runtime is a striking sight.
The graphic objects we've seen are really very simple. The point of this article is that making more complex ones does not mean starting over from scratch. When an interface is written in the style presented here, making it more sophisticated is largely a process of elaboration.