diff options
| -rw-r--r-- | win/events.go | 27 | ||||
| -rw-r--r-- | win/win.go | 14 |
2 files changed, 19 insertions, 22 deletions
diff --git a/win/events.go b/win/events.go index e7dbc31..7681e2c 100644 --- a/win/events.go +++ b/win/events.go @@ -8,47 +8,44 @@ import ( "github.com/go-gl/glfw/v3.2/glfw" ) -func (w *Win) setUpEvents(events chan<- string) { +func (w *Win) setUpMainthreadEvents() { var moX, moY int w.w.SetMouseButtonCallback(func(_ *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) { switch action { case glfw.Press: - sendEvent(events, "mo", "down", moX, moY) + w.mainthreadEvent("mo", "down", moX, moY) case glfw.Release: - sendEvent(events, "mo", "up", moX, moY) + w.mainthreadEvent("mo", "up", moX, moY) } }) w.w.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) { moX, moY = int(x), int(y) - sendEvent(events, "mo", "move", moX, moY) + w.mainthreadEvent("mo", "move", moX, moY) }) w.w.SetCharCallback(func(_ *glfw.Window, r rune) { - sendEvent(events, "kb", "type", r) + w.mainthreadEvent("kb", "type", r) }) w.w.SetSizeCallback(func(_ *glfw.Window, width, height int) { w.resize(width, height) - sendEvent(events, "wi", "resize", width, height) + w.mainthreadEvent("wi", "resize", width, height) }) w.w.SetCloseCallback(func(_ *glfw.Window) { - sendEvent(events, "wi", "close") + w.mainthreadEvent("wi", "close") }) } -func sendEvent(events chan<- string, a ...interface{}) { - go func() { - events <- mkEvent(a...) - }() -} - -func mkEvent(a ...interface{}) string { +func (w *Win) mainthreadEvent(a ...interface{}) { s := make([]string, len(a)) for i := range s { s[i] = fmt.Sprint(a[i]) } - return strings.Join(s, event.Sep) + event := strings.Join(s, event.Sep) + go func() { + w.mainthreadEvents <- event + }() } @@ -61,16 +61,16 @@ func New(opts ...Option) (*Win, error) { return nil, err } - events := make(chan string) + w.mainthreadEvents = make(chan string) mainthread.Call(func() { w.resize(o.width, o.height) - w.setUpEvents(events) + w.setUpMainthreadEvents() }) go func() { for { select { - case event := <-events: + case event := <-w.mainthreadEvents: w.Dispatch.Happen(event) case <-w.closed: return @@ -114,9 +114,10 @@ func makeGLFWWin(o *options) (*glfw.Window, error) { type Win struct { event.Dispatch - w *glfw.Window - rgba *image.RGBA - closed chan struct{} + w *glfw.Window + rgba *image.RGBA + mainthreadEvents chan string + closed chan struct{} } func (w *Win) Close() error { @@ -130,7 +131,6 @@ func (w *Win) Image() *image.RGBA { var curWin *Win = nil func (w *Win) Flush(r image.Rectangle) { - w.Dispatch.Happen(mkEvent("wi", "flush", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)) mainthread.Call(func() { w.flush(r) }) |