Refactoring menu drawing

This commit is contained in:
Amy Grinn 2024-12-20 08:50:40 -05:00
parent 89fa09c387
commit 4a46d3d1d1
No known key found for this signature in database
GPG key ID: 6B558BED1DCF3192

View file

@ -326,6 +326,7 @@
'menu menu:level-select) 'menu menu:level-select)
(make-menu-item "Credits" 'credits)))) (make-menu-item "Credits" 'credits))))
;; -1 for the index means 'Back' will be indicated first
(define *menu* (make-menu-state menu:main -1 0 '())) (define *menu* (make-menu-state menu:main -1 0 '()))
(define (current-menu) (define (current-menu)
@ -750,45 +751,58 @@
(height (+ 2 ;; Menu title + back/ellipses (height (+ 2 ;; Menu title + back/ellipses
(* 2 padding-y) ;; Padding (* 2 padding-y) ;; Padding
(if (> num-items menu:max-items) (if (> num-items menu:max-items)
(1+ menu:max-items) (1+ menu:max-items) ;; bottom ellipses
num-items))) num-items)))
(r-start (- -2 padding-y)) (y-start (- (vec2-y center) (* tile-height
(r-end (- (+ r-start height) padding-y)) (floor (/ height 2)))))
(gutter-x (- (vec2-x center) (* tile-width (1- (floor (/ width 2)))))) (x-start (- (vec2-x center) (* tile-width
(text-x (+ tile-width gutter-x))) (floor (/ width 2))))))
(let row ((r r-start)
(y (- (vec2-y center) (* tile-height (floor (/ height 2)))))) ;; Draw menu background
;; Draw menu background (let ((w (* tile-width width))
(when (= r r-start) (h (* tile-height height)))
(let ((x (- (vec2-x center) (* tile-width (floor (/ width 2))))) (set-fill-color! context "#000")
(w (* tile-width width)) (fill-rect context x-start y-start w h)
(h (* tile-height height))) (set-stroke-color! context "blue")
(set-fill-color! context "#000") (stroke-rect context x-start y-start w h))
(fill-rect context x y w h)
(set-stroke-color! context "blue") ;; Draw menu text
(stroke-rect context x y w h))) (set-font! context "normal 16px monogram")
;; Draw menu text (set-fill-color! context "#fff")
(let ((r-menu-index (- (+ r (* (current-menu-page) menu:max-items)) (current-menu-page)))) (let* (;; The first menu item is at index 0. The 'Back' or ellipses are
(set-font! context "normal 16px monogram") ;; directly above the first menu item at index -1. The menu title
(set-fill-color! context "#fff") ;; is above the 'Back' button and separated by the padding-y
(r-start (- -2 padding-y))
;; end of text
(r-end (- (+ r-start height) padding-y))
;; r will not take into account which page you are on
(r-page-offset (- (* (current-menu-page) menu:max-items) (current-menu-page)))
(gutter-x (+ tile-width x-start))
(text-x (+ tile-width gutter-x)))
(do ((r r-start (1+ r)) (y y-start (+ tile-height y)))
((or (>= r r-end) (>= (+ r r-page-offset) num-items)))
;; Draw menu title
(when (= r r-start) (when (= r r-start)
(set-text-align! context "center") (set-text-align! context "center")
(fill-text context (menu-name (current-menu)) (vec2-x center) (+ y text-offset-y))) (fill-text context (menu-name (current-menu))
(vec2-x center) (+ y text-offset-y)))
(set-text-align! context "left") (set-text-align! context "left")
(when (= r-menu-index (current-menu-index)) ;; indicator
(when (= (+ r r-page-offset) (current-menu-index))
(fill-text context "▸" gutter-x (+ y text-offset-y))) (fill-text context "▸" gutter-x (+ y text-offset-y)))
;; Menu items
(when (>= r -1) (when (>= r -1)
(fill-text context (fill-text context
(cond (cond
((= r-menu-index -1) "Back") ((= (+ r r-page-offset) -1) "Back")
((or (= r -1) (and (= r (1- r-end)) (< r-menu-index (1- num-items)))) ((or (= r -1) (and (= r (1- r-end))
(< (+ r r-page-offset) (1- num-items))))
"...") "...")
(else (else
(menu-item-name (vector-ref (menu-items (current-menu)) r-menu-index)))) (menu-item-name
text-x (+ y text-offset-y))) (vector-ref (menu-items (current-menu))
;; Draw next row (+ r r-page-offset)))))
(when (and (< (1+ r) r-end) (< (1+ r-menu-index) num-items)) text-x (+ y text-offset-y)))))))
(row (1+ r) (+ y tile-height)))))))
(define (draw-level) (define (draw-level)
(draw-background) (draw-background)