This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
/
scene.go
288 lines (245 loc) · 6.33 KB
/
scene.go
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"wireworld/resources"
"wireworld/sim"
"wireworld/ui"
"wireworld/util"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
// Scene defines a window with loads of drawing and simulation
// manipulation functionality.
type Scene struct {
projection *util.Mat4
window *ui.Window
canvas *ui.Clipboard
panel *ui.InfoPanel
history sim.History
currentTool int
lmbPressed bool
infoVisible bool
}
// CreateScene creates a enw scene.
func CreateScene(c *Config) (*Scene, error) {
var err error
var s Scene
s.window, err = ui.CreateWindow(int(c.Width), int(c.Height), c.Fullscreen)
if err != nil {
s.Release()
return nil, err
}
err = resources.Load()
if err != nil {
s.Release()
return nil, err
}
s.panel = ui.NewInfoPanel()
s.canvas = ui.NewClipboard()
s.currentTool = sim.CellWire
s.lmbPressed = false
s.infoVisible = true
s.window.SetTitle(Version())
s.window.SetKeyCallback(s.keyCallback)
s.window.SetFramebufferSizeCallback(s.resizeCallback)
s.window.SetMouseButtonCallback(s.mouseButtonCallback)
s.window.SetCursorPosCallback(s.mouseMoveCallback)
s.window.SetCharCallback(s.charCallback)
s.window.SetScrollCallback(s.scrollCallback)
// Make sure all components are initialized to the
// correct dimensions.
w, h := s.window.GetFramebufferSize()
s.resizeCallback(nil, w, h)
s.panel.Clear()
return &s, nil
}
func (s *Scene) Release() {
s.panel.Release()
resources.Release()
s.window.Release()
}
// Update updates the scene.
func (s *Scene) Update() bool {
ok := s.window.Update()
sim.Step(false)
s.canvas.SetPanning(s.window.GetKey(glfw.KeySpace) == glfw.Press)
s.updateInfo()
return ok
}
// Draw renders the scene.
func (s *Scene) Draw() {
gl.Clear(gl.COLOR_BUFFER_BIT)
s.canvas.Draw(s.projection)
if s.infoVisible {
s.panel.Draw(s.projection)
}
}
// drawCells draws on the grid. What is being drawn depends on the current mode.
func (s *Scene) drawCells() {
if s.lmbPressed {
x, y := s.canvas.HoverTarget()
sim.Set(x, y, int32(s.currentTool))
}
}
// setTool sets the current drawing tool.
func (s *Scene) setTool(t int) {
s.currentTool = t
}
func (s *Scene) scrollCallback(_ *glfw.Window, x, y float64) {
s.canvas.Scroll(x, y)
}
func (s *Scene) mouseMoveCallback(_ *glfw.Window, x, y float64) {
s.canvas.MouseMove(x, y)
s.drawCells()
}
func (s *Scene) mouseButtonCallback(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
s.canvas.MouseButton(button, action, mod)
s.lmbPressed = (button == glfw.MouseButton1 && action == glfw.Press)
s.drawCells()
}
func (s *Scene) resizeCallback(_ *glfw.Window, w, h int) {
gl.Viewport(0, 0, int32(w), int32(h))
s.projection = util.Mat4Ortho(0, float32(w), 0, float32(h), -1, 1)
s.canvas.Resize(w, h)
s.panel.Resize(0, 0, util.Max(w/5, 280), h)
}
func (s *Scene) charCallback(_ *glfw.Window, char rune) {
}
func (s *Scene) keyCallback(_ *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
switch action {
case glfw.Release:
s.keyRelease(key, scancode, mods)
case glfw.Press:
s.keyPress(key, scancode, mods)
}
}
func (s *Scene) keyRelease(key glfw.Key, scancode int, mods glfw.ModifierKey) {
switch key {
case glfw.KeyLeftShift, glfw.KeyRightShift:
s.canvas.SetAddSelection(false)
}
}
func (s *Scene) keyPress(key glfw.Key, scancode int, mods glfw.ModifierKey) {
switch key {
case glfw.KeyEscape:
s.canvas.SelectionClear()
s.canvas.ClipboardClear()
case glfw.KeyF1:
s.canvas.ToggleGridVisible()
case glfw.KeyF2:
s.canvas.ToggleDrawClipboard()
case glfw.KeyGraveAccent:
s.infoVisible = !s.infoVisible
case glfw.KeyQ:
sim.ToggleRunning()
case glfw.KeyE:
sim.Step(true)
case glfw.KeyT:
sim.Trim()
case glfw.Key1:
s.setTool(sim.CellEmpty)
case glfw.Key2:
s.setTool(sim.CellWire)
case glfw.Key3:
s.setTool(sim.CellHead)
case glfw.Key4:
s.setTool(sim.CellTail)
case glfw.KeyEqual:
sim.ScaleInterval(-1)
case glfw.KeyMinus:
sim.ScaleInterval(+1)
case glfw.KeyLeftShift, glfw.KeyRightShift:
s.canvas.SetAddSelection(true)
case glfw.KeyA:
if mods&glfw.ModControl != 0 {
s.canvas.SelectAll()
}
case glfw.KeyDelete:
s.canvas.SelectionDelete()
case glfw.KeyUp:
s.canvas.SelectionMove(0, -1)
case glfw.KeyDown:
s.canvas.SelectionMove(0, +1)
case glfw.KeyLeft:
s.canvas.SelectionMove(-1, 0)
case glfw.KeyRight:
s.canvas.SelectionMove(+1, 0)
case glfw.KeyC:
if mods&glfw.ModControl != 0 {
s.canvas.ClipboardCopy()
}
case glfw.KeyV:
if mods&glfw.ModControl != 0 {
s.canvas.ClipboardPaste()
} else {
s.canvas.ScrollTo(0, 0)
s.canvas.SetZoom(ui.ZoomDefault)
}
case glfw.KeyX:
if mods&glfw.ModControl != 0 {
s.canvas.ClipboardCut()
}
case glfw.KeyZ:
if mods&glfw.ModControl != 0 && mods&glfw.ModShift == 0 {
s.history.Undo()
} else if mods&glfw.ModControl != 0 && mods&glfw.ModShift != 0 {
s.history.Redo()
}
}
}
// updateInfo recreates the text contents of the debug/info panel.
func (s *Scene) updateInfo() {
if !s.infoVisible {
return
}
line := 1
s.panel.Clear()
p := func(v string, argv ...interface{}) {
s.panel.Print(line, v, argv...)
line++
}
p("Cells: %d, running: %v", sim.CellCount(), sim.Running())
p("Step interval: %s", sim.StepInterval())
p("Current tool: %s", toolName(s.currentTool))
p("")
p("Simulation:")
p(" [q] Start/stop simulation")
p(" [e] Single simulation step")
p(" [+] Double simulation speed")
p(" [-] Halve simulation speed")
p("")
p("Tools:")
p(" [1] Draw Empty cell")
p(" [2] Draw Wire cell")
p(" [3] Draw Electron head")
p(" [4] Draw Electron tail")
p(" [t] Trim empty cells")
p(" [ctrl-a] Select all cells")
p(" [ctrl-x] Cut selection")
p(" [ctrl-c] Copy selection")
p(" [ctrl-v] Paste selection")
p(" [del] Delete selection")
p(" [arrow keys] Move selection")
p("")
p("Misc:")
p(" [~] Show/hide this info panel")
p(" [F1] Toggle grid visibility")
p(" [F2] Toggle clipboard visibility")
p(" [esc] Cancel selection / Clear clipboard")
p(" [lmb] Draw cells")
p(" [rmb] Draw selection")
p(" [wheel] Zoom in/out")
p(" [space+mouse] Pan viewport")
}
// toolName returns a human-readable name for the given cell state.
func toolName(v int) string {
switch v {
case sim.CellWire:
return "Wire"
case sim.CellHead:
return "Electron head"
case sim.CellTail:
return "Electron tail"
default:
return "Empty"
}
}