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!
|
|
|
|
|
|
|
|
|
|
(defpackage :flora-search-aurora.ui
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(:use :cl :flora-search-aurora.display)
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(:export #:ui-loop #:render-menu-strip :selection :selected))
|
2023-06-04 12:52:18 -05:00
|
|
|
|
|
|
|
|
|
(in-package :flora-search-aurora.ui)
|
|
|
|
|
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Menu loops
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
(defun ui-loop (last-matrix menu-alist)
|
|
|
|
|
"The state loop to be used for displaying/processing/input-managing
|
|
|
|
|
with menus."
|
|
|
|
|
(let* ((matrix (make-screen-matrix))
|
|
|
|
|
(new-menu (ui-update matrix menu-alist)))
|
|
|
|
|
(ui-draw matrix last-matrix)
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(sleep .2)
|
2023-06-06 15:59:31 -05:00
|
|
|
|
(ui-loop matrix new-menu)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun ui-draw (matrix last-matrix)
|
|
|
|
|
"The draw loop for menus."
|
|
|
|
|
(print-screen-matrix (matrix-delta last-matrix matrix))
|
|
|
|
|
(finish-output))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun ui-update (matrix menu-alist)
|
|
|
|
|
"The update loop for menus. It processes all input, state, etc, and
|
|
|
|
|
returns the new state of the menu."
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(let ((new-menu (progress-menu-items menu-alist)))
|
|
|
|
|
(render-menu-strip matrix new-menu 0 0)
|
|
|
|
|
new-menu))
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Menu display
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-04 12:52:18 -05:00
|
|
|
|
(defun render-line (matrix text x y)
|
|
|
|
|
"Apply a one-line string to the matrix at the given coordinates."
|
|
|
|
|
(if (and (stringp text)
|
|
|
|
|
(> (length text) 0))
|
|
|
|
|
(progn
|
|
|
|
|
(setf (aref matrix y x)
|
|
|
|
|
(char text 0))
|
|
|
|
|
(render-line matrix (subseq text 1)
|
|
|
|
|
(+ x 1) y))
|
|
|
|
|
matrix))
|
|
|
|
|
|
|
|
|
|
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(defun render-menu-item
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(matrix text x y &key (width (+ (length text) 2)) (height 3) (selection 100))
|
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-06-05 23:28:03 -05:00
|
|
|
|
(render-string matrix text (+ x 1) (+ 1 y)
|
|
|
|
|
:max-column (- (+ x width) 1)
|
|
|
|
|
:max-row (- (+ y height) 2))
|
|
|
|
|
|
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-06 23:27:30 -05:00
|
|
|
|
(at-most width (ceiling (* width (* (abs selection)
|
2023-06-06 00:07:47 -05:00
|
|
|
|
.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-06 23:27:30 -05:00
|
|
|
|
(when (and selection
|
|
|
|
|
(not (eq selection 0)))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(dotimes (i (- height 2))
|
|
|
|
|
(setf (aref matrix (+ y i 1) x) #\|)
|
2023-06-06 00:07:47 -05:00
|
|
|
|
(setf (aref matrix (+ y i 1) (+ x width -1)) #\|))
|
|
|
|
|
matrix))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-06 15:59:31 -05:00
|
|
|
|
(defun render-menu-strip (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.
|
|
|
|
|
The item list should be an alist of the following format:
|
|
|
|
|
((“LABEL” ((SELECTED . 'T)(SELECTION . 100))) (“LABEL-2” (SELECTION . -20)) ⋯)"
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(let ((x x))
|
|
|
|
|
(mapcar
|
|
|
|
|
(lambda (item)
|
2023-06-06 15:59:31 -05:00
|
|
|
|
(let* ((label (car item))
|
2023-06-06 23:27:30 -05:00
|
|
|
|
(selection (or (cdr (assoc 'selection (cdr item)))
|
|
|
|
|
0))
|
2023-06-06 15:59:31 -05:00
|
|
|
|
(width (at-most max-item-width
|
|
|
|
|
(+ (length label) 2))))
|
|
|
|
|
(render-menu-item matrix label x y
|
2023-06-06 00:07:47 -05:00
|
|
|
|
:width width
|
|
|
|
|
:height height
|
2023-06-06 23:27:30 -05:00
|
|
|
|
:selection selection)
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(setf x (+ x width 1))))
|
|
|
|
|
items))
|
|
|
|
|
matrix)
|
|
|
|
|
|
|
|
|
|
|
2023-06-04 12:52:18 -05:00
|
|
|
|
(defun render-string (matrix text x y &key (max-column 72) (max-row 20))
|
|
|
|
|
"Render the given string to the matrix of characters, character-by-character.
|
|
|
|
|
Will line-break or truncate as appropriate and necessary to not exceed the
|
|
|
|
|
positional arguments nor the dimensions of the matrix."
|
|
|
|
|
(let* ((dimensions (array-dimensions matrix))
|
|
|
|
|
(max-column (at-most (cadr dimensions) max-column))
|
|
|
|
|
(max-row (at-most (car dimensions) max-row))
|
|
|
|
|
(substrings (split-string-by-length text (- max-column x)))
|
|
|
|
|
(row 0))
|
2023-06-05 23:28:03 -05:00
|
|
|
|
(loop while (and (<= (+ y row) max-row)
|
2023-06-04 12:52:18 -05:00
|
|
|
|
substrings)
|
|
|
|
|
do
|
|
|
|
|
(render-line matrix (pop substrings)
|
|
|
|
|
x (+ y row))
|
|
|
|
|
(incf row)))
|
|
|
|
|
matrix)
|
|
|
|
|
|
|
|
|
|
|
2023-06-06 23:27:30 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Menu logic
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
(defun progress-menu-items (menu-alist)
|
|
|
|
|
"Given an associative 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 (assoc 'selection (cdr item)))
|
|
|
|
|
(selectedp (assoc 'selected (cdr item))))
|
|
|
|
|
(if selection
|
|
|
|
|
(setf (cdr selection)
|
|
|
|
|
(gravitate-toward (if selectedp 100 0)
|
|
|
|
|
(cdr selection) 10)))))
|
|
|
|
|
menu-alist)
|
|
|
|
|
menu-alist)
|
|
|
|
|
|
|
|
|
|
|
2023-06-06 15:59:31 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Misc. utils
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-04 12:52:18 -05:00
|
|
|
|
(defun split-string-by-length (string line-length &key (substrings '()))
|
|
|
|
|
"Given a string, split it into a list of substrings all with lengths
|
|
|
|
|
equal or lower to the given length."
|
|
|
|
|
(if (> (length string) line-length)
|
|
|
|
|
(split-string-by-length
|
|
|
|
|
(subseq string line-length)
|
|
|
|
|
line-length
|
|
|
|
|
:substrings (append substrings
|
|
|
|
|
`(,(subseq string 0 line-length))))
|
|
|
|
|
(append substrings `(,string))))
|
|
|
|
|
|
|
|
|
|
|
2023-06-06 15:59:31 -05:00
|
|
|
|
(defun at-most (maximum num)
|
|
|
|
|
"This function returns at most every hope and dream you've ever had, and at
|
|
|
|
|
minimum returns your more pitiful of moments."
|
|
|
|
|
(if (> num maximum)
|
|
|
|
|
maximum
|
|
|
|
|
num))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun at-least (minimum num)
|
|
|
|
|
"This function returns at least every hope and dream you've ever had, and at
|
|
|
|
|
maximum returns your more pitiful of moments."
|
|
|
|
|
(if (< num minimum)
|
|
|
|
|
minimum
|
|
|
|
|
num))
|
2023-06-04 12:52:18 -05:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
(at-most goal (+ num delta)))
|
|
|
|
|
((> num goal)
|
|
|
|
|
(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
|
|
|
|
|
;; ---{============= -------------------
|
|
|
|
|
|