-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.clj
184 lines (147 loc) · 4.9 KB
/
gui.clj
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(ns projection.gui
(:require [seesaw [core :as sc]
[graphics :as sg]]
[projection [core :as core]
[digits :as digits]])
(:import [java.awt.event MouseEvent KeyEvent]))
#_(def objects [{:points [[5 1 -1]
[5 1 1]
[5 -1 1]
[5 -1 -1]
[3 0 0]]
:faces [[0 1 2 3 0]
[0 1 4 0]
[1 4 2 1]
[2 3 4 2]
[0 4 3 0]]}])
(def objects (atom []))
(def width 750)
(def height 750)
(def angle-sp 0.003)
(def speed 0.1)
(def radius 2)
(def config (atom
{:VUP [0 0 1]
:VPN [1 0 0]
:VPN-y [0 1 0]
:VPN-z [0 0 1]
:u [-0.5 0.5]
:v [-0.5 0.5]
:COP [0 0 0]
:TRG [2 0 0]
:focus 1
:width width
:height height}))
(def cursor (let [img (java.awt.image.BufferedImage. 16, 16, java.awt.image.BufferedImage/TYPE_INT_ARGB)]
(.createCustomCursor (java.awt.Toolkit/getDefaultToolkit) img (java.awt.Point. 0 0) "blank cursor")))
(declare frame)
(def prev-pos (atom nil))
(defn rotate [crds]
(let [old @prev-pos]
(reset! prev-pos crds)
(when-not (nil? old)
(let [[dx dy] (map - crds old)
a-z (* (- angle-sp) dx)
a-y (* (- angle-sp) dy)]
(swap! config core/rotate a-y a-z)))))
(def styles {:point (sg/style :stroke 1 :background "red")
:line (sg/style :stroke 1 :foreground "blue")
:border (sg/style :stroke 3 :foreground "black")})
(defn draw-points [g points]
(doseq [[x y] (remove nil? points)]
(sg/draw g
(sg/circle x y radius)
(styles :point))))
(defn draw-face [g points face]
(doseq [[a b] (partition 2 1 face)]
(when (every? #(not (nil? (points %))) [a b])
(let [[x1 y1] (points a)
[x2 y2] (points b)]
(sg/draw g
(sg/line x1 y1 x2 y2)
(styles :line))))))
(defn draw-object [g {:keys [points faces]}]
(let [transformed (mapv #(core/transform % @config) points)]
(draw-points g transformed)
(doseq [face faces] (draw-face g transformed face))))
(defn draw-border [g]
(sg/draw g
(sg/rect 2 2 (- width 3) (- height 3))
(styles :border)))
(defn draw [c g]
(doseq [object @objects]
(draw-object g object))
(draw-border g))
(defn left-button [e]
(= (.getButton e) (MouseEvent/BUTTON1)))
(defn right-button [e]
(= (.getButton e) (MouseEvent/BUTTON3)))
(defn recalculate []
(swap! config #(-> % core/calc-parameters core/calc-matrix)))
(defn mouse-dragged [e]
(rotate [(.getX e) (.getY e)])
(recalculate)
(.repaint (sc/select frame [:#canvas])))
(defn mouse-wheel [e]
(let [dist (* (.getWheelRotation e) speed -1)
move (fn [{:keys [COP VPN] :as config}]
(assoc config
:COP (mapv + COP (core/mult dist VPN))))]
(swap! config move))
(recalculate)
(.repaint (sc/select frame [:#canvas])))
(defn mouse-pressed [e]
(reset! prev-pos [(.getX e) (.getY e)]))
(defn move [{:keys [COP] :as config} dist dir]
(assoc config
:COP (mapv + COP (core/mult dist (config dir)))))
(def keymap
{KeyEvent/VK_W [1 :VPN]
KeyEvent/VK_S [-1 :VPN]
KeyEvent/VK_A [-1 :VPN-y]
KeyEvent/VK_D [1 :VPN-y]
KeyEvent/VK_Q [1 :VPN-z]
KeyEvent/VK_E [-1 :VPN-z]})
(defn key-pressed [e]
(when (contains? keymap (.getKeyCode e))
(let [[sign dir] (keymap (.getKeyCode e))
mult (if (.isShiftDown e) 10 1)]
(swap! config move (* sign speed mult) dir)
(recalculate)
(.repaint (sc/select frame [:#canvas])))))
(defn canvas []
(let [canvas (sc/canvas :size [width :by height]
:paint draw
:id :canvas
:listen [:mouse-dragged mouse-dragged
:mouse-pressed mouse-pressed
:key-pressed key-pressed
:mouse-wheel-moved mouse-wheel])]
(.setCursor canvas cursor)
canvas))
(defn create-frame []
(sc/frame :title "Hello"
:content (canvas)
:on-close :dispose))
(defn run-timer []
(let [listener (proxy [java.awt.event.ActionListener] []
(actionPerformed [e]
(reset! objects (digits/get-objects (.getSeconds (java.util.Date.))))
(.repaint (sc/select frame [:#canvas]))))]
(def timer (javax.swing.Timer. 1000 listener))
(.start timer)))
(defn start []
(sc/invoke-later
(-> frame
sc/pack!
sc/show!)))
(defn restart []
(def frame (create-frame))
(.setFocusable (sc/select frame [:#canvas]) true)
(run-timer)
(recalculate)
(start))
(defn stop-timer []
(.stop timer))
(defn -main [& args]
(start))