Add optional “RETURN” pair to menu alists

In case you don't want a menu to return the
function's value when selected — or if you
don't want to have to add a function at all.
This commit is contained in:
Jaidyn Ann 2023-06-09 18:22:56 -05:00
parent 0cfd2deae0
commit 8cbd029d6d
2 changed files with 26 additions and 14 deletions

View File

@ -73,10 +73,10 @@ or something ¯\_(ツ)_/¯"
(let ((main-menu (let ((main-menu
`(((LABEL . "PLAY") `(((LABEL . "PLAY")
(SELECTION . 100) (SELECTED . T) (SELECTION . 100) (SELECTED . T)
(FUNCTION . ,(lambda () (make-main-overworld-state)))) (FUNCTION . ,#'make-main-overworld-state))
((LABEL . "SUBMENU") ((LABEL . "SUBMENU")
(FUNCTION . ,(lambda () (make-options-menu-state)))) (FUNCTION . ,#'make-options-menu-state))
((LABEL . "QUIT") (FUNCTION . ,(lambda () nil)))))) ((LABEL . "QUIT") (RETURN . NIL)))))
(lambda (matrix) (lambda (matrix)
(menu-state matrix main-menu)))) (menu-state matrix main-menu))))
@ -88,7 +88,7 @@ or something ¯\_(ツ)_/¯"
(SELECTION . 100) (SELECTED . T) (SELECTION . 100) (SELECTED . T)
(FUNCTION . ,(lambda () (print "¯\_(ツ)_/¯")))) (FUNCTION . ,(lambda () (print "¯\_(ツ)_/¯"))))
((LABEL . "GO BACK") ((LABEL . "GO BACK")
(FUNCTION . ,(lambda () nil)))))) (RETURN . ,NIL)))))
(lambda (matrix) (lambda (matrix)
(menu-state matrix options-menu)))) (menu-state matrix options-menu))))

32
ui.lisp
View File

@ -171,13 +171,24 @@ That is, 0 for non-selected items and 100 for selected items."
(let* ((input (normalize-char-plist (read-char-plist))) (let* ((input (normalize-char-plist (read-char-plist)))
(selected-item (nth (selected-menu-item-position menu-alist) (selected-item (nth (selected-menu-item-position menu-alist)
menu-alist)) menu-alist))
(func (cdr (assoc 'function selected-item)))) (func (cdr (assoc 'function selected-item)))
(return-val (assoc 'return selected-item)))
(case (getf input :char) (case (getf input :char)
(#\→ (select-right-menu-item menu-alist)) (#\→ (progn (select-right-menu-item menu-alist)
(#\← (select-left-menu-item menu-alist))) 't))
(if (and func (eq (getf input :char) #\return)) (#\← (progn (select-left-menu-item menu-alist)
(apply func '()) 't))
't)) (#\return
(cond ((and func return-val)
(apply func '())
(cdr return-val))
(func
(apply func '()))
(return-val
(cdr return-val))
('t
't)))
(otherwise 't)))
't)) 't))
@ -212,10 +223,11 @@ That is, 0 for non-selected items and 100 for selected items."
(defun selected-menu-item-position (menu-alist) (defun selected-menu-item-position (menu-alist)
"Returns the index of the menu alist's selected item." "Returns the index of the menu alist's selected item."
(position (or (position
't menu-alist 't menu-alist
:test (lambda (ignore list-item) :test (lambda (ignore list-item)
(cdr (assoc 'selected list-item))))) (cdr (assoc 'selected list-item))))
0))