2023-06-04 12:52:18 -05:00
|
|
|
|
;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This program is free software: you can redistribute it and/or
|
|
|
|
|
;;;; modify it under the terms of the GNU General Public License as
|
|
|
|
|
;;;; published by the Free Software Foundation, either version 3 of
|
|
|
|
|
;;;; the License, or (at your option) any later version.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;;; GNU General Public License for more details.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;;; FLORA-SEARCH-AURORA.UI
|
|
|
|
|
;;;; Generic menu-making, displaying, and management.
|
|
|
|
|
;;;; Let's get to it, we're on a deadline!
|
|
|
|
|
|
2023-07-02 12:05:29 -05:00
|
|
|
|
(in-package :flora-search-aurora.menu)
|
2023-06-04 12:52:18 -05:00
|
|
|
|
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Menu loops
|
2023-06-23 12:41:47 -05:00
|
|
|
|
;;; ——————————————————————————————————
|
|
|
|
|
(defun make-menu-state (menu-list)
|
|
|
|
|
"Return a state-function for the game’s main menu, for use with STATE-LOOP."
|
|
|
|
|
(lambda (matrix)
|
|
|
|
|
(📋:menu-state matrix menu-list)))
|
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun menu-state (matrix menu-plist)
|
|
|
|
|
"Render a menu in menu-plist format to the given matrix, and process user-input.
|
2023-06-09 16:14:46 -05:00
|
|
|
|
A state-function for use with the #'state-loop."
|
2023-06-08 21:57:51 -05:00
|
|
|
|
(sleep .02)
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(menu-state-draw matrix menu-plist)
|
|
|
|
|
(menu-state-update menu-plist))
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun menu-state-draw (matrix menu-plist)
|
|
|
|
|
"Render a menu in menu-plist format to the given matrix.
|
2023-06-09 16:14:46 -05:00
|
|
|
|
A core part of #'menu-state."
|
2023-07-02 18:31:35 -05:00
|
|
|
|
(render-menu-items matrix menu-plist 0 0))
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun menu-state-update (menu-plist)
|
|
|
|
|
"Update a menu — that is, take user input and modify the menu’s plist appropriately.
|
2023-06-09 16:14:46 -05:00
|
|
|
|
A core part of #'menu-state."
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(progress-menu-items menu-plist)
|
|
|
|
|
(process-menu-input menu-plist))
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Menu display
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(defun render-menu-item
|
2023-06-07 19:02:11 -05:00
|
|
|
|
(matrix text x y &key (width (+ (length text) 2)) (height 3) (selection 0) (selected nil))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
"Render a “menu-item” — that is, text surrounded by a box with an optional
|
2023-06-06 00:07:47 -05:00
|
|
|
|
'selected' form. If selected is a non-zero number below 100, then that percent
|
|
|
|
|
of the box will be displayed as selected/highlighted. This percent is from
|
|
|
|
|
left-to-right, unless negative — in which case, right-to-left."
|
2023-07-02 19:34:29 -05:00
|
|
|
|
(✎:render-string matrix text (list :x (+ x 1) :y (+ 1 y)) :width width)
|
2023-06-06 00:07:47 -05:00
|
|
|
|
;; Render the normal top and bottom bars.
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(dotimes (i width)
|
2023-06-06 00:07:47 -05:00
|
|
|
|
(setf (aref matrix y (+ x i)) #\-)
|
|
|
|
|
(setf (aref matrix (+ y (- height 1)) (+ x i)) #\-))
|
|
|
|
|
|
|
|
|
|
;; Render the weird “selected” top and bottom bars. A menu item might be
|
|
|
|
|
;; only partially-selected…
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(if (and selection
|
|
|
|
|
(not (eq selection 0)))
|
2023-06-06 00:07:47 -05:00
|
|
|
|
(let* ((bar-width
|
2023-06-20 20:04:13 -05:00
|
|
|
|
(…:at-most width (ceiling (* width (* (abs selection)
|
|
|
|
|
.01)))))
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(bar-start (if (> 0 selection) (- width bar-width) 0)))
|
2023-06-06 00:07:47 -05:00
|
|
|
|
(dotimes (i bar-width)
|
|
|
|
|
(setf (aref matrix y (+ x bar-start i)) #\=)
|
|
|
|
|
(setf (aref matrix (+ y (- height 1)) (+ x bar-start i)) #\=))))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
|
|
|
|
|
;; Render the horizontal “earmuffs” for helping the selected item stand out.
|
2023-06-07 19:02:11 -05:00
|
|
|
|
(when selected
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(dotimes (i (- height 2))
|
2023-06-07 19:02:11 -05:00
|
|
|
|
(setf (aref matrix (+ y i 1) x) #\|)
|
|
|
|
|
(setf (aref matrix (+ y i 1) (+ x width -1)) #\|))
|
2023-06-06 00:07:47 -05:00
|
|
|
|
matrix))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 18:31:35 -05:00
|
|
|
|
(defun render-menu-items (matrix items x y &key (max-item-width 12) (height 3))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
"Render several menu items to the matrix, starting at the given x/y coordinates,
|
2023-06-06 23:27:30 -05:00
|
|
|
|
maximum width for any given item, and the height of all items.
|
2023-07-02 12:40:47 -05:00
|
|
|
|
The item list should be an plist of the following format:
|
2023-06-23 12:54:29 -05:00
|
|
|
|
(((LABEL :en “BIRD” :eo “BIRDO”)(SELECTED . T)(SELECTION . 100))
|
|
|
|
|
((LABEL :en “BAR” :eo “BARO”)(SELECTION . -20)) ⋯)"
|
2023-07-02 18:31:35 -05:00
|
|
|
|
(let ((row-x’es '()))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(mapcar
|
|
|
|
|
(lambda (item)
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(let* ((label (…:getf-lang item))
|
|
|
|
|
(selection (or (getf item :selection)
|
2023-06-06 23:27:30 -05:00
|
|
|
|
0))
|
2023-06-20 20:04:13 -05:00
|
|
|
|
(width (…:at-most max-item-width
|
2023-07-02 18:31:35 -05:00
|
|
|
|
(+ (length label) 2)))
|
|
|
|
|
(row (or (getf item :row) 0))
|
|
|
|
|
(item-x (or (getf row-x’es row) x))
|
|
|
|
|
(item-y (+ y (* row height))))
|
|
|
|
|
(render-menu-item matrix label
|
|
|
|
|
item-x item-y
|
2023-06-06 00:07:47 -05:00
|
|
|
|
:width width
|
|
|
|
|
:height height
|
2023-06-07 19:02:11 -05:00
|
|
|
|
:selection selection
|
2023-07-02 12:40:47 -05:00
|
|
|
|
:selected (getf item :selected))
|
2023-07-02 18:31:35 -05:00
|
|
|
|
(setf (getf row-x’es row) (+ item-x width 1))))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
items))
|
|
|
|
|
matrix)
|
|
|
|
|
|
|
|
|
|
|
2023-06-06 23:27:30 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Menu logic
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun progress-menu-items (menu-plist)
|
|
|
|
|
"Given an property list of menu-items, decrement or increment each
|
2023-06-06 23:27:30 -05:00
|
|
|
|
item's “selected-percentage”, so that they converge at the right percent.
|
|
|
|
|
That is, 0 for non-selected items and 100 for selected items."
|
|
|
|
|
(mapcar
|
|
|
|
|
(lambda (item)
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(let* ((selection (or (getf item :selection) 0))
|
|
|
|
|
(selectedp (getf item :selected)))
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(if selection
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(setf (getf item :selection)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
(gravitate-toward
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(cond ((and selectedp (< selection 0) 0)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
-100)
|
|
|
|
|
(selectedp 100)
|
|
|
|
|
('t 0))
|
2023-07-02 12:40:47 -05:00
|
|
|
|
selection 10)))))
|
|
|
|
|
menu-plist)
|
|
|
|
|
menu-plist)
|
2023-06-06 23:27:30 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun process-menu-input (menu-plist)
|
|
|
|
|
"Get and process any keyboard input, modifying the menu plist as necessary."
|
2023-06-08 21:57:51 -05:00
|
|
|
|
(if (listen)
|
2023-06-27 11:54:52 -05:00
|
|
|
|
(let* ((input (⌨:read-gamefied-char-plist))
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(selected-item (nth (selected-menu-item-position menu-plist)
|
|
|
|
|
menu-plist))
|
|
|
|
|
(func (getf selected-item :function))
|
|
|
|
|
(return-val-p (member :return selected-item))
|
|
|
|
|
(return-val (getf selected-item :return)))
|
2023-06-27 11:54:52 -05:00
|
|
|
|
(case (getf input :semantic)
|
2023-07-02 12:40:47 -05:00
|
|
|
|
('⌨:→ (progn (select-right-menu-item menu-plist)
|
2023-06-27 11:54:52 -05:00
|
|
|
|
't))
|
2023-07-02 12:40:47 -05:00
|
|
|
|
('⌨:← (progn (select-left-menu-item menu-plist)
|
2023-06-27 11:54:52 -05:00
|
|
|
|
't))
|
2023-07-02 19:34:29 -05:00
|
|
|
|
('⌨:↑ (progn (select-up-menu-item menu-plist)
|
|
|
|
|
't))
|
|
|
|
|
('⌨:↓ (progn (select-down-menu-item menu-plist)
|
|
|
|
|
't))
|
|
|
|
|
('⌨:↰ (progn (select-up-menu-item menu-plist)
|
|
|
|
|
(select-left-menu-item menu-plist)
|
|
|
|
|
't))
|
|
|
|
|
('⌨:↱ (progn (select-up-menu-item menu-plist)
|
|
|
|
|
(select-right-menu-item menu-plist)
|
|
|
|
|
't))
|
|
|
|
|
('⌨:↲ (progn (select-down-menu-item menu-plist)
|
|
|
|
|
(select-left-menu-item menu-plist)
|
|
|
|
|
't))
|
|
|
|
|
('⌨:↳ (progn (select-down-menu-item menu-plist)
|
|
|
|
|
(select-right-menu-item menu-plist)
|
|
|
|
|
't))
|
2023-06-27 11:54:52 -05:00
|
|
|
|
('⌨:❎
|
|
|
|
|
nil)
|
|
|
|
|
('⌨:🆗
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(cond ((and func return-val-p)
|
2023-06-09 18:22:56 -05:00
|
|
|
|
(apply func '())
|
2023-07-02 12:40:47 -05:00
|
|
|
|
return-val)
|
2023-06-09 18:22:56 -05:00
|
|
|
|
(func
|
|
|
|
|
(apply func '()))
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(return-val-p
|
|
|
|
|
return-val)
|
2023-06-09 18:22:56 -05:00
|
|
|
|
('t
|
|
|
|
|
't)))
|
|
|
|
|
(otherwise 't)))
|
2023-06-08 21:57:51 -05:00
|
|
|
|
't))
|
2023-06-07 19:02:11 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun select-menu-item (menu-plist position)
|
|
|
|
|
"Given a menu property list, select the menu-item at the given position."
|
|
|
|
|
(let ((old-position (selected-menu-item-position menu-plist)))
|
2023-06-07 19:02:11 -05:00
|
|
|
|
;; The “polarity” (direction of selection) depends on the relative
|
|
|
|
|
;; direction of the previous selection.
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(setf (getf (nth position menu-plist) :selection)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
(if (< old-position position) 10 -10))
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(setf (getf (nth position menu-plist) :selected) 't)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
;; Likewise for the previously-selected item.
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(setf (getf (nth old-position menu-plist) :selection)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
(if (< old-position position) -90 90))
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(setf (getf (nth old-position menu-plist) :selected) nil))
|
|
|
|
|
menu-plist)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 19:34:29 -05:00
|
|
|
|
(defun select-next-item-of-row (menu-plist row)
|
|
|
|
|
"Given a specific ROW, select the next item following the currently-selected
|
|
|
|
|
on that same ROW."
|
|
|
|
|
(let* ((selected-n (selected-menu-item-position menu-plist)))
|
|
|
|
|
(loop for i from (1+ selected-n) upto (length menu-plist)
|
|
|
|
|
do (when (eq (or (getf (nth i menu-plist) :row) 0) row)
|
|
|
|
|
(return (select-menu-item menu-plist i))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun select-previous-item-of-row (menu-plist row)
|
|
|
|
|
"Given a specific ROW, select the next item preceding the currently-selected
|
|
|
|
|
on that same ROW."
|
|
|
|
|
(when (>= row 0)
|
|
|
|
|
(let* ((selected-n (selected-menu-item-position menu-plist)))
|
|
|
|
|
(loop for i from (1- selected-n) downto 0
|
|
|
|
|
do (when (eq (or (getf (nth i menu-plist) :row) 0) row)
|
|
|
|
|
(return (select-menu-item menu-plist i)))))))
|
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun select-right-menu-item (menu-plist)
|
2023-06-07 19:02:11 -05:00
|
|
|
|
"Select the item to the right of the currenty-selected item."
|
2023-07-02 19:34:29 -05:00
|
|
|
|
(select-next-item-of-row
|
|
|
|
|
menu-plist
|
|
|
|
|
(or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
|
|
|
|
|
0)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun select-left-menu-item (menu-plist)
|
|
|
|
|
"Select the item to the right of the currenty-selected item."
|
|
|
|
|
(select-previous-item-of-row
|
|
|
|
|
menu-plist
|
|
|
|
|
(or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
|
|
|
|
|
0)))
|
|
|
|
|
|
2023-06-07 19:02:11 -05:00
|
|
|
|
|
2023-07-02 19:34:29 -05:00
|
|
|
|
(defun select-up-menu-item (menu-plist)
|
|
|
|
|
"Select the next item above the currently-selected item."
|
|
|
|
|
(select-previous-item-of-row
|
|
|
|
|
menu-plist
|
|
|
|
|
(1- (or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
|
|
|
|
|
1))))
|
2023-06-07 19:02:11 -05:00
|
|
|
|
|
2023-07-02 19:34:29 -05:00
|
|
|
|
(defun select-down-menu-item (menu-plist)
|
|
|
|
|
"Select the next item to below the currently-selected item."
|
|
|
|
|
(select-next-item-of-row
|
|
|
|
|
menu-plist
|
|
|
|
|
(1+ (or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
|
|
|
|
|
0))))
|
2023-06-07 19:02:11 -05:00
|
|
|
|
|
|
|
|
|
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(defun selected-menu-item-position (menu-plist)
|
|
|
|
|
"Returns the index of the menu plist's selected item."
|
2023-06-09 18:22:56 -05:00
|
|
|
|
(or (position
|
2023-07-02 12:40:47 -05:00
|
|
|
|
't menu-plist
|
2023-06-09 18:22:56 -05:00
|
|
|
|
:test (lambda (ignore list-item)
|
2023-07-02 12:40:47 -05:00
|
|
|
|
(getf list-item :selected)))
|
2023-06-09 18:22:56 -05:00
|
|
|
|
0))
|
2023-06-07 19:02:11 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Misc. utils
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(defun gravitate-toward (goal num delta)
|
2023-06-06 15:59:31 -05:00
|
|
|
|
"Either add to a number, or subtract from it; whichever brings it closer to zero.
|
|
|
|
|
In addition, the resultant value shall not “pass” zero."
|
|
|
|
|
(cond
|
2023-06-06 23:27:30 -05:00
|
|
|
|
((< num goal)
|
2023-06-20 20:04:13 -05:00
|
|
|
|
(…:at-most goal (+ num delta)))
|
2023-06-06 23:27:30 -05:00
|
|
|
|
((> num goal)
|
2023-06-20 20:04:13 -05:00
|
|
|
|
(…:at-least goal (- num delta)))
|
2023-06-06 15:59:31 -05:00
|
|
|
|
('t
|
2023-06-06 23:27:30 -05:00
|
|
|
|
goal)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;"---{============= -------------------"
|
|
|
|
|
;; | Kill your mom | Give into despair
|
|
|
|
|
;; ---{============= -------------------
|