aboutsummaryrefslogtreecommitdiffstats
path: root/win/win.go
blob: 772ee8e48fee197114b67b04c6cbb9061ccb4ecd (plain) (blame)
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
package win

import (
	"image"
	"image/draw"
	"runtime"

	"github.com/faiface/gui/event"
	"github.com/faiface/mainthread"
	"github.com/go-gl/gl/v2.1/gl"
	"github.com/go-gl/glfw/v3.2/glfw"
)

type Option func(*options)

type options struct {
	title         string
	width, height int
	resizable     bool
}

func Title(title string) Option {
	return func(o *options) {
		o.title = title
	}
}

func Size(width, height int) Option {
	return func(o *options) {
		o.width = width
		o.height = height
	}
}

func Resizable() Option {
	return func(o *options) {
		o.resizable = true
	}
}

func New(opts ...Option) (*Win, error) {
	o := options{
		title:     "",
		width:     640,
		height:    480,
		resizable: false,
	}
	for _, opt := range opts {
		opt(&o)
	}

	w := &Win{
		events: make(chan struct {
			event  string
			result chan<- bool
		}),
		flushes: make(chan struct {
			r       image.Rectangle
			flushed chan<- struct{}
		}),
	}

	var err error
	mainthread.Call(func() {
		w.w, err = makeGLFWWin(&o)
	})
	if err != nil {
		return nil, err
	}

	var (
		cancelOpenGLThread  = make(chan chan struct{})
		cancelEventDispatch = make(chan chan struct{})
		cancelEventThread   = make(chan chan struct{})
	)

	w.cancels = []chan<- chan struct{}{
		cancelEventThread,
		cancelEventDispatch,
		cancelOpenGLThread,
	}

	go func() {
		runtime.LockOSThread()
		openGLThread(w, cancelOpenGLThread, w.flushes)
	}()

	w.resize(o.width, o.height)

	go eventDispatch(w, cancelEventDispatch, w.events)
	mainthread.CallNonBlock(func() {
		eventThread(w, cancelEventThread)
	})

	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
}

type Win struct {
	dispatch event.Dispatch
	w        *glfw.Window
	rgba     *image.RGBA
	events   chan struct {
		event  string
		result chan<- bool
	}
	flushes chan struct {
		r       image.Rectangle
		flushed chan<- struct{}
	}
	cancels []chan<- chan struct{}
}

func (w *Win) Event(pattern string, handler func(evt string) bool) {
	w.dispatch.Event(pattern, handler)
}

func (w *Win) Happen(evt string) bool {
	result := make(chan bool)
	w.events <- struct {
		event  string
		result chan<- bool
	}{evt, result}
	return <-result
}

func (w *Win) Close() error {
	go func() {
		for _, cancel := range w.cancels {
			confirm := make(chan struct{})
			cancel <- confirm
			<-confirm
		}
	}()
	return nil
}

func (w *Win) Image() *image.RGBA {
	return w.rgba
}

func (w *Win) Flush(r image.Rectangle) {
	flushed := make(chan struct{})
	w.flushes <- struct {
		r       image.Rectangle
		flushed chan<- struct{}
	}{r, flushed}
	<-flushed
}

func (w *Win) resize(width, height int) {
	bounds := image.Rect(0, 0, width, height)
	rgba := image.NewRGBA(bounds)
	if w.rgba != nil {
		draw.Draw(rgba, w.rgba.Bounds(), w.rgba, w.rgba.Bounds().Min, draw.Src)
	}
	w.rgba = rgba
	w.Flush(bounds)
}

func eventDispatch(w *Win, cancel <-chan chan struct{}, events chan struct {
	event  string
	result chan<- bool
}) {
loop:
	for {
		select {
		case evt := <-events:
			evt.result <- w.dispatch.Happen(evt.event)
		case confirm := <-cancel:
			close(confirm)
			break loop
		}
	}
}

func eventThread(w *Win, cancel <-chan chan struct{}) {
	var moX, moY int

	w.w.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
		switch action {
		case glfw.Press:
			w.Happen(event.Sprint("mo", "down", moX, moY))
		case glfw.Release:
			w.Happen(event.Sprint("mo", "up", moX, moY))
		}
	})

	w.w.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
		moX, moY = int(x), int(y)
		w.Happen(event.Sprint("mo", "move", moX, moY))
	})

	w.w.SetCharCallback(func(_ *glfw.Window, r rune) {
		w.Happen(event.Sprint("kb", "type", r))
	})

	w.w.SetSizeCallback(func(_ *glfw.Window, width, height int) {
		w.resize(width, height)
		w.Happen(event.Sprint("resize", 0, 0, width, height))
	})

	w.w.SetCloseCallback(func(_ *glfw.Window) {
		w.Happen(event.Sprint("wi", "close"))
	})

loop:
	for {
		select {
		default:
			glfw.WaitEvents()
		case confirm := <-cancel:
			close(confirm)
			break loop
		}
	}
}

func openGLThread(w *Win, cancel <-chan chan struct{}, flushes <-chan struct {
	r       image.Rectangle
	flushed chan<- struct{}
}) {
	w.w.MakeContextCurrent()
	gl.Init()

loop:
	for {
		select {
		case flush := <-w.flushes:
			r := flush.r

			bounds := w.rgba.Bounds()
			r = bounds.Intersect(r)
			if r.Empty() {
				return
			}

			tmp := image.NewRGBA(r)
			draw.Draw(tmp, r, w.rgba, 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,
				gl.Ptr(tmp.Pix),
			)
			gl.Flush()

			close(flush.flushed)
		case confirm := <-cancel:
			w.w.Destroy()
			close(confirm)
			break loop
		}
	}
}