Skip to content

Instantly share code, notes, and snippets.

@ajstarks
Created November 30, 2023 16:15
Show Gist options
  • Save ajstarks/ef633da557c4f90429990d66f0f6c7c6 to your computer and use it in GitHub Desktop.
Save ajstarks/ef633da557c4f90429990d66f0f6c7c6 to your computer and use it in GitHub Desktop.
giocanvas client example with new Gio filter event API:
// kbpointer processes the keyboard events and pointer events in percent coordinates
func kbpointer(q input.Source, cfg config) {
width, height := cfg.width, cfg.height
prec := cfg.precision
stepsize := cfg.stepsize
for {
e, ok := q.Event(
key.Filter{Optional: key.ModCtrl},
pointer.Filter{Kinds: pointer.Press | pointer.Move | pointer.Release},
)
if !ok {
break
}
switch e := e.(type) {
case key.Event: // keyboard events
switch e.State {
case key.Press:
switch e.Name {
case "G":
dogrid = !dogrid
case "A":
shape = "arc"
case "L":
shape = "line"
case "B":
shape = "bezier"
case "C":
shape = "circle"
case "S":
shape = "square"
case "R":
shape = "rect"
case "E":
shape = "ellipse"
case "D":
deckspec(prec)
case key.NameRightArrow:
switch e.Modifiers {
case 0:
bx += stepsize
case key.ModCtrl:
ex += stepsize
}
case key.NameLeftArrow:
switch e.Modifiers {
case 0:
bx -= stepsize
case key.ModCtrl:
ex -= stepsize
}
case key.NameUpArrow:
switch e.Modifiers {
case 0:
by += stepsize
case key.ModCtrl:
ey += stepsize
}
case key.NameDownArrow:
switch e.Modifiers {
case 0:
by -= stepsize
case key.ModCtrl:
ey -= stepsize
}
case key.NameEscape, "Q":
os.Exit(0)
}
}
case pointer.Event: // pointer events
switch e.Kind {
case pointer.Move:
mouseX, mouseY = pctcoord(e.Position.X, e.Position.Y, width, height)
case pointer.Press:
switch e.Buttons {
case pointer.ButtonPrimary:
bx, by = pctcoord(e.Position.X, e.Position.Y, width, height)
case pointer.ButtonSecondary:
ex, ey = pctcoord(e.Position.X, e.Position.Y, width, height)
case pointer.ButtonTertiary:
deckspec(prec)
}
}
}
}
}
func shapesketch(w *app.Window, cfg config) error {
// initial values
bx, by = 25.0, 50.0
ex, ey = 75.0, 50.0
cx, cy = 10, 10
shape = "bezier"
begincolor, endcolor, shapecolor := cfg.begincolor, cfg.endcolor, cfg.shapecolor
// app loop
for {
ev := w.NextEvent()
switch e := ev.(type) {
// return an error on close
case app.DestroyEvent:
return e.Err
// for each frame: register keyboard, pointer press and move events, draw coordinates and
// specified shapes. Track the pointer position for the current point.
case app.FrameEvent:
canvas := giocanvas.NewCanvas(float32(e.Size.X), float32(e.Size.Y), app.FrameEvent{})
canvas.Background(cfg.bgcolor)
grid(canvas, 5, cfg.textcolor)
// draw specified shape
switch shape {
case "line":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, ex, ey, endcolor, cfg)
canvas.Line(bx, by, ex, ey, cfg.linesize, cfg.shapecolor)
case "bezier":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, ex, ey, endcolor, cfg)
textcoord(canvas, cx, cy, shapecolor, cfg)
canvas.QuadStrokedCurve(bx, by, cx, cy, ex, ey, cfg.linesize, cfg.shapecolor)
case "circle":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, cx, cy, shapecolor, cfg)
canvas.Circle(bx, by, dist(bx, by, cx, cy), cfg.shapecolor)
case "square":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, cx, cy, shapecolor, cfg)
canvas.Square(bx, by, (cx-bx)*2, cfg.shapecolor)
case "ellipse":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, cx, cy, shapecolor, cfg)
canvas.Ellipse(bx, by, (cx-bx)*2, (cy-by)*2, cfg.shapecolor)
case "rect":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, cx, cy, shapecolor, cfg)
canvas.CenterRect(bx, by, (cx-bx)*2, (cy-by)*2, cfg.shapecolor)
case "arc":
textcoord(canvas, bx, by, begincolor, cfg)
textcoord(canvas, ex, ey, endcolor, cfg)
textcoord(canvas, cx, cy, shapecolor, cfg)
r, a2, a1 := arcElements()
for t := a1; t < a2; t += math.Pi / 256 {
px, py := canvas.Polar(bx, by, r, float32(t))
canvas.Line(bx, by, px, py, cfg.linesize, cfg.shapecolor)
}
}
kbpointer(e.Source, cfg)
cx, cy = mouseX, mouseY
e.Frame(canvas.Context.Ops)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment