Skip to content

Instantly share code, notes, and snippets.

@ajstarks
Created November 30, 2023 16:16
Show Gist options
  • Save ajstarks/9224ad09306ecb240c6618bf32da45e6 to your computer and use it in GitHub Desktop.
Save ajstarks/9224ad09306ecb240c6618bf32da45e6 to your computer and use it in GitHub Desktop.
giocanvas client with old event API
// kbpointer processes the keyboard events and pointer events in percent coordinates
func kbpointer(q event.Queue, cfg config) {
width, height := cfg.width, cfg.height
prec := cfg.precision
stepsize := cfg.stepsize
for _, ev := range q.Events(pressed) {
// keyboard events
if k, ok := ev.(key.Event); ok {
switch k.State {
case key.Press:
switch k.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 k.Modifiers {
case 0:
bx += stepsize
case key.ModCtrl:
ex += stepsize
}
case key.NameLeftArrow:
switch k.Modifiers {
case 0:
bx -= stepsize
case key.ModCtrl:
ex -= stepsize
}
case key.NameUpArrow:
switch k.Modifiers {
case 0:
by += stepsize
case key.ModCtrl:
ey += stepsize
}
case key.NameDownArrow:
switch k.Modifiers {
case 0:
by -= stepsize
case key.ModCtrl:
ey -= stepsize
}
case key.NameEscape, "Q":
os.Exit(0)
}
}
}
// pointer events
if p, ok := ev.(pointer.Event); ok {
switch p.Kind {
case pointer.Move:
mouseX, mouseY = pctcoord(p.Position.X, p.Position.Y, width, height)
case pointer.Press:
switch p.Buttons {
case pointer.ButtonPrimary:
bx, by = pctcoord(p.Position.X, p.Position.Y, width, height)
case pointer.ButtonSecondary:
ex, ey = pctcoord(p.Position.X, p.Position.Y, width, height)
case pointer.ButtonTertiary:
deckspec(prec)
}
pressed = true
}
}
}
}
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 system.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 system.FrameEvent:
canvas := giocanvas.NewCanvas(float32(e.Size.X), float32(e.Size.Y), system.FrameEvent{})
key.InputOp{Tag: pressed}.Add(canvas.Context.Ops)
pointer.InputOp{Tag: pressed, Grab: false, Kinds: pointer.Press | pointer.Move}.Add(canvas.Context.Ops)
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.Queue, 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