From 8cbd029d6d172967634a6233492afcf11c12016f Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Fri, 9 Jun 2023 18:22:56 -0500 Subject: [PATCH] =?UTF-8?q?Add=20optional=20=E2=80=9CRETURN=E2=80=9D=20pai?= =?UTF-8?q?r=20to=20menu=20alists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- flora-search-aurora.lisp | 8 ++++---- ui.lisp | 32 ++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/flora-search-aurora.lisp b/flora-search-aurora.lisp index 235f0e0..3b2bb59 100644 --- a/flora-search-aurora.lisp +++ b/flora-search-aurora.lisp @@ -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)))) diff --git a/ui.lisp b/ui.lisp index dfaec9f..98ebc9f 100644 --- a/ui.lisp +++ b/ui.lisp @@ -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))