Advertisement
Oladon

Weblocks Widgets, Take 1

Mar 1st, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.12 KB | None | 0 0
  1. ; Prompt is not currently in use, and I don't /think/ actually need a slot (choice) for the result...?
  2. (defwidget choice-list ()
  3.   ((choices :accessor choices
  4.             :initarg :choices)
  5.    (prompt :accessor prompt
  6.            :initarg :prompt
  7.            :initform "Choose an action:")
  8.    (choice :accessor choice
  9.            :initform nil)))
  10.  
  11. (defclass web-player ()
  12.   ((choice-widget :accessor choice-widget
  13.                   :initarg :choice-widget)
  14.    (difficulty :accessor difficulty
  15.                :initarg :difficulty)))
  16.  
  17. (defmethod render-widget-body ((widget choice-list) &rest args)
  18.   (declare (ignore args))
  19.   (with-html
  20.     (:p (esc (format nil "The current choice is ~A." (choice widget))))
  21.     (:pre :class "choices"
  22.           (loop for choice in (choices widget)
  23.              do (let ((x choice))
  24.                   (render-link (lambda (&rest args)
  25.                                  (declare (ignore args))
  26. ;                                 (setf (choice widget) x)
  27. ;                                 (mark-dirty widget)
  28.                                  (answer widget x))
  29.                                choice))
  30.                (htm :br)))))
  31.  
  32. (defmethod offer-choice ((it web-player) actions &key prompt &allow-other-keys)
  33.   (let ((new-widget (make-instance 'choice-list :choices actions :prompt prompt))
  34.         (old-widget (choice-widget it)))
  35.     (with-flow old-widget
  36.       (setf (choice new-widget) (yield new-widget))
  37.       (setf (choice-widget it) new-widget))))
  38.  
  39. (defun run-game (pc)
  40.   (setf (difficulty pc) (offer-choice pc (list "easy" "medium" "hard") :prompt "Select difficulty:"))
  41.   (offer-choice pc (list "G3" "H3" "I3")))
  42.  
  43. (defun make-main-page ()
  44.   (let* ((choice-widget (make-instance 'choice-list
  45.                                        :choices (list "A1" "B1" "C1")))
  46.          (pc (make-instance 'web-player
  47.                             :choice-widget choice-widget))
  48.          (comp (make-instance 'composite :widgets
  49.                               (list
  50.                                "Some text."
  51.                                choice-widget))))
  52.     (prog1 comp
  53.       (run-game pc))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement