123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- ;;;; 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.MENU ๐
- ;;;; Generic menu-making, displaying, and management.
- ;;;; Let's get to it, we're on a deadline!
- (in-package :flora-search-aurora.menu)
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- ;;; Menu loops
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- (defun make-menu-function (menu-list)
- "Return a state-function for the gameโs main menu, for use with STATE-LOOP."
- (lambda (matrix)
- (๐:menu-state matrix menu-list)))
- (defun menu-state (matrix menu-plist)
- "Render a menu in menu-plist format to the given matrix, and process user-input.
- A state-function for use with the #'state-loop."
- (sleep .02)
- (menu-state-draw matrix menu-plist)
- (menu-state-update menu-plist))
- (defun menu-state-draw (matrix menu-plist)
- "Render a menu in menu-plist format to the given matrix.
- A core part of #'menu-state."
- (render-menu-items matrix menu-plist 0 0))
- (defun menu-state-update (menu-plist)
- "Update a menu โ that is, take user input and modify the menuโs plist appropriately.
- A core part of #'menu-state."
- (progress-menu-items menu-plist)
- (process-menu-input menu-plist))
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- ;;; Menu display
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- (defun render-menu-item
- (matrix text x y &key (width (+ (length text) 2)) (height 3) (selection 0) (selected nil))
- "Render a โmenu-itemโ โ that is, text surrounded by a box with an optional
- '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."
- (โ:render-string matrix text (list :x (+ x 1) :y (+ 1 y)) :width width)
- ;; Render the normal top and bottom bars.
- (dotimes (i width)
- (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โฆ
- (if (and selection
- (not (eq selection 0)))
- (let* ((bar-width
- (โฆ:at-most width (ceiling (* width (* (abs selection)
- .01)))))
- (bar-start (if (> 0 selection) (- width bar-width) 0)))
- (dotimes (i bar-width)
- (setf (aref matrix y (+ x bar-start i)) #\=)
- (setf (aref matrix (+ y (- height 1)) (+ x bar-start i)) #\=))))
- ;; Render the horizontal โearmuffsโ for helping the selected item stand out.
- (when selected
- (dotimes (i (- height 2))
- (setf (aref matrix (+ y i 1) x) #\|)
- (setf (aref matrix (+ y i 1) (+ x width -1)) #\|))
- matrix))
- (defun render-menu-items (matrix items x y &key (max-item-width 12) (height 3))
- "Render several menu items to the matrix, starting at the given x/y coordinates,
- maximum width for any given item, and the height of all items.
- The item list should be an plist of the following format:
- (((LABEL :en โBIRDโ :eo โBIRDOโ)(SELECTED . T)(SELECTION . 100))
- ((LABEL :en โBARโ :eo โBAROโ)(SELECTION . -20)) โฏ)"
- (let ((row-xโes '()))
- (mapcar
- (lambda (item)
- (let* ((label (โฆ:getf-lang item))
- (selection (or (getf item :selection)
- 0))
- (width (โฆ:at-most max-item-width
- (+ (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
- :width width
- :height height
- :selection selection
- :selected (getf item :selected))
- (setf (getf row-xโes row) (+ item-x width 1))))
- items))
- matrix)
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- ;;; Menu logic
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- (defun progress-menu-items (menu-plist)
- "Given an property list of menu-items, decrement or increment each
- 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)
- (let* ((selection (or (getf item :selection) 0))
- (selectedp (getf item :selected)))
- (if selection
- (setf (getf item :selection)
- (gravitate-toward
- (cond ((and selectedp (< selection 0) 0)
- -100)
- (selectedp 100)
- ('t 0))
- selection 10)))))
- menu-plist)
- menu-plist)
- (defun process-menu-input (menu-plist)
- "Get and process any keyboard input, modifying the menu plist as necessary."
- (if (listen)
- (let* ((input (โจ:read-gamefied-char-plist))
- (selected-item (nth (selected-menu-item-position menu-plist)
- menu-plist))
- (func (getf selected-item :function))
- (exec (getf selected-item :exec)))
- (case (getf input :semantic)
- ('โจ:โ (progn (select-right-menu-item menu-plist)
- 't))
- ('โจ:โ (progn (select-left-menu-item menu-plist)
- 't))
- ('โจ:โ (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))
- ;; ('โจ:โ
- ;; (list :drop 1))
- ('โจ:๐
- (if (getf selected-item :exec)
- (apply (getf selected-item :exec) '())
- selected-item))))
- 't))
- (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)))
- ;; The โpolarityโ (direction of selection) depends on the relative
- ;; direction of the previous selection.
- (setf (getf (nth position menu-plist) :selection)
- (if (< old-position position) 10 -10))
- (setf (getf (nth position menu-plist) :selected) 't)
- ;; Likewise for the previously-selected item.
- (setf (getf (nth old-position menu-plist) :selection)
- (if (< old-position position) -90 90))
- (setf (getf (nth old-position menu-plist) :selected) nil))
- menu-plist)
- (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 (1- (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)))))))
- (defun select-right-menu-item (menu-plist)
- "Select the item to the right of the currenty-selected item."
- (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)))
- (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))))
- (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))))
- (defun selected-menu-item-position (menu-plist)
- "Returns the index of the menu plist's selected item."
- (or (position
- 't menu-plist
- :test (lambda (ignore list-item)
- (getf list-item :selected)))
- 0))
- (defun selected-menu-item (menu-plist)
- (nth (selected-menu-item-position menu-plist)
- menu-plist))
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- ;;; Misc. utils
- ;;; โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- (defun gravitate-toward (goal num delta)
- "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
- ((< num goal)
- (โฆ:at-most goal (+ num delta)))
- ((> num goal)
- (โฆ:at-least goal (- num delta)))
- ('t
- goal)))
- ;;"---{============= -------------------"
- ;; | Kill your mom | Give into despair
- ;; ---{============= -------------------
|