Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package p5

import "gioui.org/io/key"

// Event is the current event pushed from the system.
var Event struct {
Mouse struct {
Expand All @@ -18,6 +20,11 @@ var Event struct {
}
Buttons Buttons
}

Key struct {
Cur key.Event
Prev key.Event
}
}

// Buttons is a set of mouse buttons.
Expand Down
48 changes: 48 additions & 0 deletions example/key-pressed/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright ©2021 The go-p5 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"fmt"
"image/color"

"gioui.org/io/key"
"github.com/go-p5/p5"
)

func main() {
p5.Run(setup, draw, p5.WithKeyCallback(kcbk))
}

func setup() {
p5.Canvas(400, 400)
p5.Background(color.Gray{Y: 220})
}

func draw() {
switch p5.Event.Key.Cur.State {
case key.Press:
p5.Stroke(color.Black)
p5.Fill(color.RGBA{R: 255, A: 255})
default:
p5.Stroke(nil)
p5.Fill(color.Transparent)
}
p5.Ellipse(
200, 200,
100, 100,
)

p5.TextSize(24)
p5.Text(fmt.Sprintf("count=%d", cnt), 10, 390)
}

var cnt int

func kcbk() {
if p5.Event.Key.Cur.State == key.Press && p5.Event.Key.Prev.State != key.Press {
cnt++
}
}
78 changes: 78 additions & 0 deletions example/key-snake/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright ©2021 The go-p5 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"image/color"

"gioui.org/io/key"
"github.com/go-p5/p5"
)

func main() {
p5.Run(setup, draw, p5.WithKeyCallback(kcbk))
}

const (
width = 400
height = 400
)

func setup() {
p5.Canvas(width, height)
p5.Stroke(nil)
p5.Background(color.Gray{Y: 220})
}

var (
xs = make([]float64, 50)
ys = make([]float64, 50)

px, py float64
)

func draw() {
for i := 1; i < len(xs); i++ {
xs[i-1] = xs[i]
ys[i-1] = ys[i]
}

xs[len(xs)-1] = px
ys[len(xs)-1] = py

for i := range xs {
p5.Fill(color.RGBA{R: 255, A: uint8(i * 5)})
p5.Ellipse(xs[i], ys[i], float64(i), float64(i))
}
}

func kcbk() {
cur := p5.Event.Key.Cur
if cur.State != key.Press {
return
}
switch cur.Name {
case key.NameLeftArrow:
px -= 5
case key.NameRightArrow:
px += 5
case key.NameUpArrow:
py -= 5
case key.NameDownArrow:
py += 5
}
px = clip(px, 0, width)
py = clip(py, 0, height)
}

func clip(x, min, max float64) float64 {
switch {
case x < min:
x = min
case x > max:
x = max
}
return x
}
26 changes: 25 additions & 1 deletion p5.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,35 @@ var (

// Run executes the user functions setup and draw.
// Run never exits.
func Run(setup, draw Func) {
func Run(setup, draw Func, opts ...Option) {
gproc.Setup = setup
gproc.Draw = draw

for _, opt := range opts {
opt(gproc)
}

gproc.Run()
}

// Func is the type of functions users provide to p5.
type Func func()

// Option customizes further a p5 processor.
type Option func(p *Proc)

// WithKeyCallback binds the given function to the p5 processor's
// key callback.
func WithKeyCallback(f Func) Option {
return func(p *Proc) {
p.Key = f
}
}

// WithMouseCallback binds the given function to the p5 processor's
// mouse callback.
func WithMouseCallback(f Func) Option {
return func(p *Proc) {
p.Mouse = f
}
}
8 changes: 8 additions & 0 deletions proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Proc struct {
Setup Func
Draw Func
Mouse Func
Key Func

ctl struct {
FrameRate time.Duration
Expand Down Expand Up @@ -238,6 +239,10 @@ func (p *Proc) run() error {
return e.Err

case key.Event:
Event.Key.Prev = Event.Key.Cur
Event.Key.Cur = e
p.Key()

switch e.Name {
case key.NameEscape:
w.Close()
Expand Down Expand Up @@ -282,6 +287,9 @@ func (p *Proc) setupUserFuncs() {
if p.Mouse == nil {
p.Mouse = func() {}
}
if p.Key == nil {
p.Key = func() {}
}
}

func (p *Proc) draw(e system.FrameEvent) {
Expand Down