Windows issues parsing these characters. Bug fix for entering edit mode without setti...
[cowl.git] / examples / 04-matrix.lisp
blob352576abd244fbfd9eca0ba12637b1c100797453
1 (require '#:asdf)
2 (asdf:oos 'asdf:load-op '#:cowl-glfw)
7 (defclass cowl-matrix ()
8 ((container :type cowl:container :accessor widget-of)
9 (matrix :type array
10 :initform (make-array '(4 4) :element-type 'single-float
11 :initial-contents '((1.0 0.0 0.0 0.0)
12 (0.0 1.0 0.0 0.0)
13 (0.0 0.0 1.0 0.0)
14 (0.0 0.0 0.0 1.0)))
15 :accessor matrix-of))
16 (:documentation "This is a graphical representation of a matrix."))
18 (defmethod initialize-instance :after ((cowl-matrix cowl-matrix) &key)
19 (setf (widget-of cowl-matrix)
20 (cowl:make-grid ()
21 ((cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 0 0))
22 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 0 1))
23 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 0 2))
24 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 0 3)))
25 ((cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 1 0))
26 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 1 1))
27 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 1 2))
28 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 1 3)))
29 ((cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 2 0))
30 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 2 1))
31 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 2 2))
32 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 2 3)))
33 ((cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 3 0))
34 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 3 1))
35 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 3 2))
36 (cowl:make-scroll-button () "~4f" (aref (matrix-of cowl-matrix) 3 3))))))
39 (defun multiply-matrix (a b r)
40 "Multiply matrix a by b and put result in r."
41 (loop for row below 4
42 do (loop for col below 4
43 do (setf (aref r row col)
44 (loop for i below 4 summing
45 (* (aref a row i)
46 (aref b i col)))))))
49 (defun main ()
50 (glfw:do-window (:title "Cowl Matrix Example" :width 800 :height 600)
51 ((setf cowl:*root-widget*
52 ;; here we have our bindings to the matrices so we can close over them
53 (let (ma mb mr)
54 (cowl:make-vbox (:width 800 :height 600
55 :row-heights '((:weight . 1.0)
56 (:expand . 10)
57 (:expand . 10)
58 (:expand . 10)
59 (:weight . 1.0)))
61 ;; the body of this macro is a list of widgets for the wrapper
63 nil ;; nil in weighted spaces will expand as necessary
65 (cowl:make-label "Matrix multiplication example")
67 (cowl:make-hbox (:width 800
68 :column-widths '((:weight . 1.0)
69 (:expand . 10)
70 (:weight . 1.0)
71 (:expand . 10)
72 (:weight . 1.0)
73 (:expand . 10)
74 (:weight . 1.0)
75 (:expand . 10)
76 (:weight . 1.0)
77 (:expand . 10)
78 (:weight . 1.0)))
79 nil
80 (widget-of (setf ma (make-instance 'cowl-matrix)))
81 nil
82 (cowl:make-label "x")
83 nil
84 (widget-of (setf mb (make-instance 'cowl-matrix)))
85 nil
86 (cowl:make-label "=")
87 nil
88 (widget-of (setf mr (make-instance 'cowl-matrix)))
89 nil)
91 (cowl:make-button () "Calculate"
92 (multiply-matrix (matrix-of ma)
93 (matrix-of mb)
94 (matrix-of mr))
95 (cowl:update-text (widget-of mr)))
98 (cowl:make-button () "Quit"
99 (return-from main))
101 nil)))
103 (cowl-glfw:setup-input-callbacks))
105 ;; Main loop the same as before
106 (gl:clear gl:+color-buffer-bit+)
107 (cowl:layout-root)
108 (cowl:draw-root)
109 (sleep 0.04)))
111 (main)