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
`(((LABEL . "PLAY")
(SELECTION . 100) (SELECTED . T)
(FUNCTION . ,(lambda () (make-main-overworld-state))))
(FUNCTION . ,#'make-main-overworld-state))
((LABEL . "SUBMENU")
(FUNCTION . ,(lambda () (make-options-menu-state))))
((LABEL . "QUIT") (FUNCTION . ,(lambda () nil))))))
(FUNCTION . ,#'make-options-menu-state))
((LABEL . "QUIT") (RETURN . NIL)))))
(lambda (matrix)
(menu-state matrix main-menu))))
@ -88,7 +88,7 @@ or something ¯\_(ツ)_/¯"
(SELECTION . 100) (SELECTED . T)
(FUNCTION . ,(lambda () (print "¯\_(ツ)_/¯"))))
((LABEL . "GO BACK")
(FUNCTION . ,(lambda () nil))))))
(RETURN . ,NIL)))))
(lambda (matrix)
(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)))
(selected-item (nth (selected-menu-item-position 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)
(#\→ (select-right-menu-item menu-alist))
(#\← (select-left-menu-item menu-alist)))
(if (and func (eq (getf input :char) #\return))
(apply func '())
't))
(#\→ (progn (select-right-menu-item menu-alist)
't))
(#\← (progn (select-left-menu-item menu-alist)
'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))
@ -212,10 +223,11 @@ That is, 0 for non-selected items and 100 for selected items."
(defun selected-menu-item-position (menu-alist)
"Returns the index of the menu alist's selected item."
(position
't menu-alist
:test (lambda (ignore list-item)
(cdr (assoc 'selected list-item)))))
(or (position
't menu-alist
:test (lambda (ignore list-item)
(cdr (assoc 'selected list-item))))
0))