menu.lisp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ;;;; Copyright ยฉ 2023, Jaidyn Ann <jadedctrl@posteo.at>
  2. ;;;;
  3. ;;;; This program is free software: you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU General Public License as
  5. ;;;; published by the Free Software Foundation, either version 3 of
  6. ;;;; the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This program is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;;;; GNU General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU General Public License
  14. ;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;;; FLORA-SEARCH-AURORA.MENU ๐Ÿ“‹
  16. ;;;; Generic menu-making, displaying, and management.
  17. ;;;; Let's get to it, we're on a deadline!
  18. (in-package :flora-search-aurora.menu)
  19. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  20. ;;; Menu loops
  21. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  22. (defun make-menu-function (menu-list)
  23. "Return a state-function for the gameโ€™s main menu, for use with STATE-LOOP."
  24. (lambda (matrix)
  25. (๐Ÿ“‹:menu-state matrix menu-list)))
  26. (defun menu-state (matrix menu-plist)
  27. "Render a menu in menu-plist format to the given matrix, and process user-input.
  28. A state-function for use with the #'state-loop."
  29. (sleep .02)
  30. (menu-state-draw matrix menu-plist)
  31. (menu-state-update menu-plist))
  32. (defun menu-state-draw (matrix menu-plist)
  33. "Render a menu in menu-plist format to the given matrix.
  34. A core part of #'menu-state."
  35. (render-menu-items matrix menu-plist 0 0))
  36. (defun menu-state-update (menu-plist)
  37. "Update a menu โ€” that is, take user input and modify the menuโ€™s plist appropriately.
  38. A core part of #'menu-state."
  39. (progress-menu-items menu-plist)
  40. (process-menu-input menu-plist))
  41. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  42. ;;; Menu display
  43. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  44. (defun render-menu-item
  45. (matrix text x y &key (width (+ (length text) 2)) (height 3) (selection 0) (selected nil))
  46. "Render a โ€œmenu-itemโ€ โ€” that is, text surrounded by a box with an optional
  47. 'selected' form. If selected is a non-zero number below 100, then that percent
  48. of the box will be displayed as selected/highlighted. This percent is from
  49. left-to-right, unless negative โ€” in which case, right-to-left."
  50. (โœŽ:render-string matrix text (list :x (+ x 1) :y (+ 1 y)) :width width)
  51. ;; Render the normal top and bottom bars.
  52. (dotimes (i width)
  53. (setf (aref matrix y (+ x i)) #\-)
  54. (setf (aref matrix (+ y (- height 1)) (+ x i)) #\-))
  55. ;; Render the weird โ€œselectedโ€ top and bottom bars. A menu item might be
  56. ;; only partially-selectedโ€ฆ
  57. (if (and selection
  58. (not (eq selection 0)))
  59. (let* ((bar-width
  60. (โ€ฆ:at-most width (ceiling (* width (* (abs selection)
  61. .01)))))
  62. (bar-start (if (> 0 selection) (- width bar-width) 0)))
  63. (dotimes (i bar-width)
  64. (setf (aref matrix y (+ x bar-start i)) #\=)
  65. (setf (aref matrix (+ y (- height 1)) (+ x bar-start i)) #\=))))
  66. ;; Render the horizontal โ€œearmuffsโ€ for helping the selected item stand out.
  67. (when selected
  68. (dotimes (i (- height 2))
  69. (setf (aref matrix (+ y i 1) x) #\|)
  70. (setf (aref matrix (+ y i 1) (+ x width -1)) #\|))
  71. matrix))
  72. (defun render-menu-items (matrix items x y &key (max-item-width 12) (height 3))
  73. "Render several menu items to the matrix, starting at the given x/y coordinates,
  74. maximum width for any given item, and the height of all items.
  75. The item list should be an plist of the following format:
  76. (((LABEL :en โ€œBIRDโ€ :eo โ€œBIRDOโ€)(SELECTED . T)(SELECTION . 100))
  77. ((LABEL :en โ€œBARโ€ :eo โ€œBAROโ€)(SELECTION . -20)) โ‹ฏ)"
  78. (let ((row-xโ€™es '()))
  79. (mapcar
  80. (lambda (item)
  81. (let* ((label (โ€ฆ:getf-lang item))
  82. (selection (or (getf item :selection)
  83. 0))
  84. (width (โ€ฆ:at-most max-item-width
  85. (+ (length label) 2)))
  86. (row (or (getf item :row) 0))
  87. (item-x (or (getf row-xโ€™es row) x))
  88. (item-y (+ y (* row height))))
  89. (render-menu-item matrix label
  90. item-x item-y
  91. :width width
  92. :height height
  93. :selection selection
  94. :selected (getf item :selected))
  95. (setf (getf row-xโ€™es row) (+ item-x width 1))))
  96. items))
  97. matrix)
  98. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  99. ;;; Menu logic
  100. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  101. (defun progress-menu-items (menu-plist)
  102. "Given an property list of menu-items, decrement or increment each
  103. item's โ€œselected-percentageโ€, so that they converge at the right percent.
  104. That is, 0 for non-selected items and 100 for selected items."
  105. (mapcar
  106. (lambda (item)
  107. (let* ((selection (or (getf item :selection) 0))
  108. (selectedp (getf item :selected)))
  109. (if selection
  110. (setf (getf item :selection)
  111. (gravitate-toward
  112. (cond ((and selectedp (< selection 0) 0)
  113. -100)
  114. (selectedp 100)
  115. ('t 0))
  116. selection 10)))))
  117. menu-plist)
  118. menu-plist)
  119. (defun process-menu-input (menu-plist)
  120. "Get and process any keyboard input, modifying the menu plist as necessary."
  121. (if (listen)
  122. (let* ((input (โŒจ:read-gamefied-char-plist))
  123. (selected-item (nth (selected-menu-item-position menu-plist)
  124. menu-plist))
  125. (func (getf selected-item :function))
  126. (exec (getf selected-item :exec)))
  127. (case (getf input :semantic)
  128. ('โŒจ:โ†’ (progn (select-right-menu-item menu-plist)
  129. 't))
  130. ('โŒจ:โ† (progn (select-left-menu-item menu-plist)
  131. 't))
  132. ('โŒจ:โ†‘ (progn (select-up-menu-item menu-plist)
  133. 't))
  134. ('โŒจ:โ†“ (progn (select-down-menu-item menu-plist)
  135. 't))
  136. ('โŒจ:โ†ฐ (progn (select-up-menu-item menu-plist)
  137. (select-left-menu-item menu-plist)
  138. 't))
  139. ('โŒจ:โ†ฑ (progn (select-up-menu-item menu-plist)
  140. (select-right-menu-item menu-plist)
  141. 't))
  142. ('โŒจ:โ†ฒ (progn (select-down-menu-item menu-plist)
  143. (select-left-menu-item menu-plist)
  144. 't))
  145. ('โŒจ:โ†ณ (progn (select-down-menu-item menu-plist)
  146. (select-right-menu-item menu-plist)
  147. 't))
  148. ;; ('โŒจ:โŽ
  149. ;; (list :drop 1))
  150. ('โŒจ:๐Ÿ†—
  151. (if (getf selected-item :exec)
  152. (apply (getf selected-item :exec) '())
  153. selected-item))))
  154. 't))
  155. (defun select-menu-item (menu-plist position)
  156. "Given a menu property list, select the menu-item at the given position."
  157. (let ((old-position (selected-menu-item-position menu-plist)))
  158. ;; The โ€œpolarityโ€ (direction of selection) depends on the relative
  159. ;; direction of the previous selection.
  160. (setf (getf (nth position menu-plist) :selection)
  161. (if (< old-position position) 10 -10))
  162. (setf (getf (nth position menu-plist) :selected) 't)
  163. ;; Likewise for the previously-selected item.
  164. (setf (getf (nth old-position menu-plist) :selection)
  165. (if (< old-position position) -90 90))
  166. (setf (getf (nth old-position menu-plist) :selected) nil))
  167. menu-plist)
  168. (defun select-next-item-of-row (menu-plist row)
  169. "Given a specific ROW, select the next item following the currently-selected
  170. on that same ROW."
  171. (let* ((selected-n (selected-menu-item-position menu-plist)))
  172. (loop for i from (1+ selected-n) upto (1- (length menu-plist))
  173. do (when (eq (or (getf (nth i menu-plist) :row) 0) row)
  174. (return (select-menu-item menu-plist i))))))
  175. (defun select-previous-item-of-row (menu-plist row)
  176. "Given a specific ROW, select the next item preceding the currently-selected
  177. on that same ROW."
  178. (when (>= row 0)
  179. (let* ((selected-n (selected-menu-item-position menu-plist)))
  180. (loop for i from (1- selected-n) downto 0
  181. do (when (eq (or (getf (nth i menu-plist) :row) 0) row)
  182. (return (select-menu-item menu-plist i)))))))
  183. (defun select-right-menu-item (menu-plist)
  184. "Select the item to the right of the currenty-selected item."
  185. (select-next-item-of-row
  186. menu-plist
  187. (or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
  188. 0)))
  189. (defun select-left-menu-item (menu-plist)
  190. "Select the item to the right of the currenty-selected item."
  191. (select-previous-item-of-row
  192. menu-plist
  193. (or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
  194. 0)))
  195. (defun select-up-menu-item (menu-plist)
  196. "Select the next item above the currently-selected item."
  197. (select-previous-item-of-row
  198. menu-plist
  199. (1- (or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
  200. 1))))
  201. (defun select-down-menu-item (menu-plist)
  202. "Select the next item to below the currently-selected item."
  203. (select-next-item-of-row
  204. menu-plist
  205. (1+ (or (getf (nth (selected-menu-item-position menu-plist) menu-plist) :row)
  206. 0))))
  207. (defun selected-menu-item-position (menu-plist)
  208. "Returns the index of the menu plist's selected item."
  209. (or (position
  210. 't menu-plist
  211. :test (lambda (ignore list-item)
  212. (getf list-item :selected)))
  213. 0))
  214. (defun selected-menu-item (menu-plist)
  215. (nth (selected-menu-item-position menu-plist)
  216. menu-plist))
  217. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  218. ;;; Misc. utils
  219. ;;; โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
  220. (defun gravitate-toward (goal num delta)
  221. "Either add to a number, or subtract from it; whichever brings it closer to zero.
  222. In addition, the resultant value shall not โ€œpassโ€ zero."
  223. (cond
  224. ((< num goal)
  225. (โ€ฆ:at-most goal (+ num delta)))
  226. ((> num goal)
  227. (โ€ฆ:at-least goal (- num delta)))
  228. ('t
  229. goal)))
  230. ;;"---{============= -------------------"
  231. ;; | Kill your mom | Give into despair
  232. ;; ---{============= -------------------