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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
package win
import (
"image"
"image/draw"
"runtime"
"time"
"unsafe"
"github.com/faiface/gui"
"github.com/faiface/mainthread"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
// Option is a functional option to the window constructor New.
type Option func(*options)
type options struct {
title string
width, height int
resizable bool
}
// Title option sets the title (caption) of the window.
func Title(title string) Option {
return func(o *options) {
o.title = title
}
}
// Size option sets the width and height of the window.
func Size(width, height int) Option {
return func(o *options) {
o.width = width
o.height = height
}
}
// Resizable option makes the window resizable by the user.
func Resizable() Option {
return func(o *options) {
o.resizable = true
}
}
// New creates a new window with all the supplied options.
//
// The default title is empty and the default size is 640x480.
func New(opts ...Option) (*Win, error) {
o := options{
title: "",
width: 640,
height: 480,
resizable: false,
}
for _, opt := range opts {
opt(&o)
}
eventsOut, eventsIn := gui.MakeEventsChan()
w := &Win{
eventsOut: eventsOut,
eventsIn: eventsIn,
draw: make(chan func(draw.Image) image.Rectangle),
newSize: make(chan image.Rectangle),
finish: make(chan struct{}),
}
var err error
mainthread.Call(func() {
w.w, err = makeGLFWWin(&o)
})
if err != nil {
return nil, err
}
mainthread.Call(func() {
// hiDPI hack
width, _ := w.w.GetFramebufferSize()
w.ratio = width / o.width
if w.ratio != 1 {
o.width /= w.ratio
o.height /= w.ratio
}
w.w.Destroy()
w.w, err = makeGLFWWin(&o)
})
if err != nil {
return nil, err
}
bounds := image.Rect(0, 0, o.width, o.height)
w.img = image.NewRGBA(bounds)
go func() {
runtime.LockOSThread()
w.openGLThread()
}()
mainthread.CallNonBlock(w.eventThread)
return w, nil
}
func makeGLFWWin(o *options) (*glfw.Window, error) {
err := glfw.Init()
if err != nil {
return nil, err
}
glfw.WindowHint(glfw.DoubleBuffer, glfw.False)
if o.resizable {
glfw.WindowHint(glfw.Resizable, glfw.True)
} else {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
w, err := glfw.CreateWindow(o.width, o.height, o.title, nil, nil)
if err != nil {
return nil, err
}
return w, nil
}
// Win is an Env that handles an actual graphical window.
//
// It receives its events from the OS and it draws to the surface of the window.
//
// Warning: only one window can be open at a time. This will be fixed.
//
// Here are all kinds of events that a window can produce, along with descriptions.
// Things enclosed in <> are values that are filled in.
//
// resize/<w>/<h> Window resized to w x h.
// wi/close Window close button pressed.
// mo/move/<x>/<y> Mouse moved to (x, y).
// mo/down/<x>/<y>/<button> A mouse button pressed on (x, y).
// mo/up/<x>/<y>/<button> A mouse button released on (x, y).
// kb/type/<code> A unicode character typed on the keyboard.
// kb/down/<key> A key on the keyboard pressed.
// kb/up/<key> A key on the keyboard released.
// kb/repeat/<key> A key on the keyboard repeated (happens when held).
//
// <w>, <h>, <x>, <y>, and <code> are numbers (%d).
// <button> is one of:
// left right middle
// <key> is one of:
// left right up down escape space backspace delete enter
// tab home end pageup pagedown shift ctrl alt
type Win struct {
eventsOut <-chan gui.Event
eventsIn chan<- gui.Event
draw chan func(draw.Image) image.Rectangle
newSize chan image.Rectangle
finish chan struct{}
w *glfw.Window
img *image.RGBA
ratio int
}
// Events returns the events channel of the window.
func (w *Win) Events() <-chan gui.Event { return w.eventsOut }
// Draw returns the draw channel of the window.
func (w *Win) Draw() chan<- func(draw.Image) image.Rectangle { return w.draw }
var buttonNames = map[glfw.MouseButton]string{
glfw.MouseButtonLeft: "left",
glfw.MouseButtonRight: "right",
glfw.MouseButtonMiddle: "middle",
}
var keyNames = map[glfw.Key]string{
glfw.KeyLeft: "left",
glfw.KeyRight: "right",
glfw.KeyUp: "up",
glfw.KeyDown: "down",
glfw.KeyEscape: "escape",
glfw.KeySpace: "space",
glfw.KeyBackspace: "backspace",
glfw.KeyDelete: "delete",
glfw.KeyEnter: "enter",
glfw.KeyTab: "tab",
glfw.KeyHome: "home",
glfw.KeyEnd: "end",
glfw.KeyPageUp: "pageup",
glfw.KeyPageDown: "pagedown",
glfw.KeyLeftShift: "shift",
glfw.KeyRightShift: "shift",
glfw.KeyLeftControl: "ctrl",
glfw.KeyRightControl: "ctrl",
glfw.KeyLeftAlt: "alt",
glfw.KeyRightAlt: "alt",
}
func (w *Win) eventThread() {
var moX, moY int
w.w.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
moX, moY = int(x), int(y)
w.eventsIn <- gui.Eventf("mo/move/%d/%d", moX*w.ratio, moY*w.ratio)
})
w.w.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
b, ok := buttonNames[button]
if !ok {
return
}
switch action {
case glfw.Press:
w.eventsIn <- gui.Eventf("mo/down/%d/%d/%s", moX*w.ratio, moY*w.ratio, b)
case glfw.Release:
w.eventsIn <- gui.Eventf("mo/up/%d/%d/%s", moX*w.ratio, moY*w.ratio, b)
}
})
w.w.SetScrollCallback(func(_ *glfw.Window, xoff, yoff float64) {
w.eventsIn <- gui.Eventf("mo/scroll/%d/%d", int(yoff), int(xoff))
})
w.w.SetCharCallback(func(_ *glfw.Window, r rune) {
w.eventsIn <- gui.Eventf("kb/type/%d", r)
})
w.w.SetKeyCallback(func(_ *glfw.Window, key glfw.Key, _ int, action glfw.Action, _ glfw.ModifierKey) {
k, ok := keyNames[key]
if !ok {
return
}
switch action {
case glfw.Press:
w.eventsIn <- gui.Eventf("kb/down/%s", k)
case glfw.Release:
w.eventsIn <- gui.Eventf("kb/up/%s", k)
case glfw.Repeat:
w.eventsIn <- gui.Eventf("kb/repeat/%s", k)
}
})
w.w.SetFramebufferSizeCallback(func(_ *glfw.Window, width, height int) {
r := image.Rect(0, 0, width, height)
w.newSize <- r
w.eventsIn <- gui.Eventf("resize/%d/%d/%d/%d", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
})
w.w.SetCloseCallback(func(_ *glfw.Window) {
w.eventsIn <- gui.Eventf("wi/close")
})
r := w.img.Bounds()
w.eventsIn <- gui.Eventf("resize/%d/%d/%d/%d", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
for {
select {
case <-w.finish:
close(w.eventsIn)
w.w.Destroy()
return
default:
glfw.WaitEventsTimeout(1.0 / 30)
}
}
}
func (w *Win) openGLThread() {
w.w.MakeContextCurrent()
gl.Init()
w.openGLFlush(w.img.Bounds())
loop:
for {
var totalR image.Rectangle
select {
case r := <-w.newSize:
img := image.NewRGBA(r)
draw.Draw(img, w.img.Bounds(), w.img, w.img.Bounds().Min, draw.Src)
w.img = img
totalR = totalR.Union(r)
case d, ok := <-w.draw:
if !ok {
close(w.finish)
return
}
r := d(w.img)
totalR = totalR.Union(r)
}
for {
select {
case <-time.After(time.Second / 960):
w.openGLFlush(totalR)
totalR = image.ZR
continue loop
case r := <-w.newSize:
img := image.NewRGBA(r)
draw.Draw(img, w.img.Bounds(), w.img, w.img.Bounds().Min, draw.Src)
w.img = img
totalR = totalR.Union(r)
case d, ok := <-w.draw:
if !ok {
close(w.finish)
return
}
r := d(w.img)
totalR = totalR.Union(r)
}
}
}
}
func (w *Win) openGLFlush(r image.Rectangle) {
bounds := w.img.Bounds()
r = r.Intersect(bounds)
if r.Empty() {
return
}
tmp := image.NewRGBA(r)
draw.Draw(tmp, r, w.img, r.Min, draw.Src)
gl.DrawBuffer(gl.FRONT)
gl.Viewport(
int32(bounds.Min.X),
int32(bounds.Min.Y),
int32(bounds.Dx()),
int32(bounds.Dy()),
)
gl.RasterPos2d(
-1+2*float64(r.Min.X)/float64(bounds.Dx()),
+1-2*float64(r.Min.Y)/float64(bounds.Dy()),
)
gl.PixelZoom(1, -1)
gl.DrawPixels(
int32(r.Dx()),
int32(r.Dy()),
gl.RGBA,
gl.UNSIGNED_BYTE,
unsafe.Pointer(&tmp.Pix[0]),
)
gl.Flush()
}
|