~eliasnaur/gio-example

4cba22423f26d25f8b5a4300cd56c792b08b131f — Elias Naur 9 months ago 9efa2c4
glfw: demonstrate custom OpenGL rendering and input event handling

Signed-off-by: Elias Naur <mail@eliasnaur.com>
1 files changed, 37 insertions(+), 5 deletions(-)

M glfw/main.go
M glfw/main.go => glfw/main.go +37 -5
@@ 16,6 16,7 @@ package main
import (
	"image"
	"log"
	"math"
	"runtime"
	"time"



@@ 97,6 98,7 @@ func main() {
			},
			Constraints: layout.Exact(sz),
		}
		drawOpenGL()
		draw(gtx, th)
		gpu.Collect(sz, gtx.Ops)
		gpu.Frame()


@@ 105,7 107,31 @@ func main() {
	}
}

var button widget.Clickable
var (
	button widget.Clickable
	green  float64 = 0.2
)

// drawOpenGL demonstrates the direct use of OpenGL commands
// to draw non-Gio content below the Gio UI.
func drawOpenGL() {
	gl.ClearColor(0, float32(green), 0, 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
}

// handleCursorEvent handles cursor events not processed by Gio.
func handleCursorEvent(xpos, ypos float64) {
	log.Printf("mouse cursor: (%f,%f)", xpos, ypos)
}

// handleMouseButtonEvent handles mouse button events not processed by Gio.
func handleMouseButtonEvent(button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
	if action == glfw.Press {
		green += 0.1
		green, _ = math.Frexp(green)
	}
	log.Printf("mouse button: %v action %v mods %v", button, action, mods)
}

func draw(gtx layout.Context, th *material.Theme) layout.Dimensions {
	return layout.Center.Layout(gtx,


@@ 120,13 146,16 @@ func registerCallbacks(window *glfw.Window, q *router.Router) {
	window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) {
		scale, _ := w.GetContentScale()
		lastPos = f32.Point{X: float32(xpos) * scale, Y: float32(ypos) * scale}
		q.Queue(pointer.Event{
		e := pointer.Event{
			Type:     pointer.Move,
			Position: lastPos,
			Source:   pointer.Mouse,
			Time:     time.Since(beginning),
			Buttons:  btns,
		})
		}
		if !q.Queue(e) {
			handleCursorEvent(xpos, ypos)
		}
	})
	window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
		var btn pointer.Buttons


@@ 147,12 176,15 @@ func registerCallbacks(window *glfw.Window, q *router.Router) {
			typ = pointer.Press
			btns |= btn
		}
		q.Queue(pointer.Event{
		e := pointer.Event{
			Type:     typ,
			Source:   pointer.Mouse,
			Time:     time.Since(beginning),
			Position: lastPos,
			Buttons:  btns,
		})
		}
		if !q.Queue(e) {
			handleMouseButtonEvent(button, action, mods)
		}
	})
}