Guest User

Untitled

a guest
Mar 22nd, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.10 KB | None | 0 0
  1. (in-package :anna)
  2.  
  3. (defgeneric update (arg0)
  4.   (:documentation "Updates the state of arg0."))
  5.  
  6. (defgeneric display (arg0 window x y)
  7.   (:documentation "Displays arg0 on the window arg1 at (arg2, arg3)."))(defpackage :anna (:use :cl))
  8. (load "primitives.lisp")
  9. (load "generics.lisp")
  10. (load "prog-bar.lisp")
  11. (load "menu.lisp")
  12.  
  13. (in-package :anna)
  14.  
  15. #|(let* ((bar (make-instance 'prog-bar :size 30))
  16.       (menu (make-instance 'menu :items '("one" "two" "three" "longggg"))))
  17.       (screen:with-window
  18.         (loop
  19.            (update menu)
  20.            (unless (update bar)
  21.              (return))
  22.            (display bar screen:*window* 5 5)
  23.            (display menu screen:*window* 6 5)))))|#
  24. (in-package :anna)
  25.  
  26. (defclass menu ()
  27.   ((items
  28.     :initarg :items
  29.     :initform nil)
  30.    (current
  31.     :initarg :current
  32.     :initform 0)))
  33.  
  34. (defmethod update ((arg0 menu))
  35.   (with-key
  36.     (cond ((or (equal key #\w) (equal key :UP))
  37.            (unless (< (- (slot-value arg0 'current) 1) 0)
  38.                (decf (slot-value arg0 'current))))
  39.           ((or (equal key #\s) (equal key :DOWN))
  40.            (unless (= (+ (slot-value arg0 'current) 1) (length (slot-value arg0 'items)))
  41.                (incf (slot-value arg0 'current))))
  42.           ((equal key #\Return)
  43.            (if (stringp (elt (slot-value arg0 'items) (slot-value arg0 'current)))
  44.                (elt (slot-value arg0 'items) (slot-value arg0 'current))
  45.                (if (equal "back" (first (elt (slot-value arg0 'items) (slot-value arg0 'current))))
  46.                    (setf (slot-value arg0 'items) (rest (first (elt (slot-value arg0 'items) (slot-value arg0 'current)))))
  47.                    (setf (slot-value arg0 'items)
  48.  
  49. (defmethod display ((arg0 menu) window x y)
  50.   (dotimes (n (length (slot-value arg0 'items)))
  51.     (if (equal (slot-value arg0 'current) n)
  52.         (with-coordinates (window (+ x (+ n 1)) y)
  53.           (screen:highlight-on window)
  54.           (if (stringp (elt (slot-value arg0 'items) n))
  55.               (format window "~A" (elt (slot-value arg0 'items) n))
  56.               (format window "~A" (first (elt (slot-value arg0 'items) n))))
  57.           (screen:highlight-off window))
  58.         (with-coordinates (window (+ x (+ n 1)) y)
  59.           (if (stringp (elt (slot-value arg0 'items) n))
  60.               (format window "~A" (elt (slot-value arg0 'items) n))
  61.               (format window "~A" (first (elt (slot-value arg0 'items) n))))))))(in-package :anna)
  62.  
  63. (defun string-n-times (s n &optional o)
  64.   (if (equal n 1)
  65.          s
  66.          (if (equal nil o)
  67.              (string-n-times (concatenate 'string s s) (- n 1) s)
  68.              (string-n-times (concatenate 'string s o) (- n 1) o))))
  69.  
  70. (defun get-key ()
  71.   (ext:with-keyboard
  72.     (let ((key (read-char ext:*keyboard-input*)))
  73.       (or
  74.        (ignore-errors (character key))
  75.        (ignore-errors (ext:char-key key))))))
  76.  
  77. (defmacro with-key (&body body)
  78.   `(let* ((key (get-key)))
  79.      ,@body))
  80.  
  81. (defmacro with-coordinates ((window x y) &body body)
  82.   `(let* ((old-pos (multiple-value-list (screen:window-cursor-position ,window))))
  83.      (screen:set-window-cursor-position ,window ,x ,y)
  84.      ,@body
  85.      (screen:set-window-cursor-position ,window (car old-pos) (second old-pos))))
  86. (in-package :anna)
  87.  
  88. (defclass prog-bar ()
  89.     ((total-steps
  90.       :initarg :total-steps
  91.       :initform 10)
  92.      (current-step
  93.       :initarg :current-step
  94.       :initform 0)
  95.      (size
  96.       :initarg :size
  97.        :initform 10)))
  98.  
  99. (defmethod update ((arg0 prog-bar))
  100.   (if (= (slot-value arg0 'current-step) (slot-value arg0 'total-steps))
  101.       nil
  102.       (incf (slot-value arg0 'current-step))))
  103.  
  104. (defmethod display ((arg0 prog-bar) window x y)
  105.   (let* ((open "[")
  106.          (done (string-n-times "#"
  107.                                (floor (* (/ (slot-value arg0 'current-step) (slot-value arg0 'total-steps)) (slot-value arg0 'size)))))
  108.          (left (string-n-times "."
  109.                                (- (slot-value arg0 'size) (length done))))
  110.          (close "]"))
  111.     (with-coordinates (window x y)
  112.       (format window "~A" (concatenate 'string open done left close)))))
Advertisement
Add Comment
Please, Sign In to add comment