summaryrefslogtreecommitdiff
path: root/lisparuga/gui.scm
blob: 9f60526ecda2d42c2440ca168785d887b3672755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; Lisparuga
;;; Copyright © 2020 David Thompson <dthompson2@worcester.edu>
;;;
;;; Lisparuga 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.
;;;
;;; Lisparuga 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 Lisparuga.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; 2D Graphical User Interface
;;
;;; Code:

(define-module (lisparuga gui)
  #:use-module (chickadee math rect)
  #:use-module (chickadee math vector)
  #:use-module (chickadee render color)
  #:use-module (chickadee render font)
  #:use-module (chickadee render shapes)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (lisparuga node)
  #:use-module (lisparuga node-2d)
  #:export (<widget>
            width
            height

            <label-widget>

            <margin-container>
            left
            right
            bottom
            top))


;;;
;;; Base Widget
;;;

(define *draw-bounding-boxes?* #t)
(define %bounding-box-color (make-color 1.0 0.0 1.0 0.2))

(define-class <widget> (<node-2d>)
  (width #:accessor width #:init-keyword #:width #:init-form 0.0)
  (height #:accessor height #:init-keyword #:height #:init-form 0.0)
  (min-width #:accessor min-width #:init-keyword #:min-width #:init-form 0.0)
  (min-height #:accessor min-height #:init-keyword #:min-height #:init-form 0.0)
  (bounding-box #:getter bounding-box #:init-form (make-rect 0.0 0.0 0.0 0.0))
  (dirty-bounding-box? #:accessor dirty-bounding-box? #:init-form #t))

(define-method (dirty! (widget <widget>))
  (set! (dirty-bounding-box? widget) #t)
  (next-method))

(define-method ((setter width) (widget <widget>) w)
  (slot-set! widget 'width (pk 'new-width (max (min-width widget) w)))
  (dirty! widget))

(define-method ((setter height) (widget <widget>) h)
  (slot-set! widget 'height (max (min-height widget) h))
  (dirty! widget))

(define-method (update (widget <widget>) dt)
  (when (dirty-bounding-box? widget)
    (let ((bb (bounding-box widget))
          (w (width widget))
          (h (height widget)))
      (set-rect-width! bb w)
      (set-rect-height! bb h))
    (set! (dirty-bounding-box? widget) #f)))

(define-method (render (widget <widget>) alpha)
  (draw-filled-rect (bounding-box widget) %bounding-box-color
                    #:matrix (world-matrix widget))
  (next-method))


;;;
;;; Text Label
;;;

(define-class <label-widget> (<widget>)
  (text #:accessor text #:init-keyword #:text #:init-form ""))

(define-method ((setter text) (label <label-widget>) new-text)
  (set! (text (& label label)) new-text)
  (next-method))

(define-method (on-boot (label <label-widget>))
  (attach-to label
             (make <label>
               #:name 'label
               #:text (text label))))


;;;
;;; Margin Container
;;;

(define-class <margin-container> (<widget>)
  (left #:accessor left #:init-keyword #:left #:init-form 0.0)
  (right #:accessor right #:init-keyword #:right #:init-form 0.0)
  (bottom #:accessor bottom #:init-keyword #:bottom #:init-form 0.0)
  (top #:accessor top #:init-keyword #:top #:init-form 0.0)
  (needs-resize? #:accessor needs-resize? #:init-form #t))

(define-method (on-attach (container <margin-container>) (widget <widget>))
  (set! (needs-resize? container) #t))

(define-method (on-detach (container <margin-container>) (widget <widget>))
  (set! (needs-resize? container) #t))

(define-method (update (container <margin-container>) dt)
  (when (needs-resize? container)
    (let loop ((c (children container))
               (w 0.0)
               (h 0.0))
      (match c
        (()
         (set! (width container) (pk 'new-width (+ w (left container) (right container))))
         (set! (height container) (+ h (bottom container) (top container)))
         (for-each (lambda (child)
                     (when (is-a? child <widget>)
                       (set! (width child) w)
                       (set! (height child) h)
                       (teleport child (left container) (bottom container))))
                   (children container)))
        ((child . rest)
         (if (is-a? child <widget>)
             (loop rest
                   (max w (min-width child))
                   (max h (min-height child)))
             (loop rest w h)))))
    (set! (needs-resize? container) #f))
  (next-method))