-
Notifications
You must be signed in to change notification settings - Fork 15
Implement keyboard API #53
Conversation
Codecov Report
@@ Coverage Diff @@
## main #53 +/- ##
==========================================
- Coverage 80.62% 75.84% -4.79%
==========================================
Files 9 10 +1
Lines 609 654 +45
==========================================
+ Hits 491 496 +5
- Misses 96 133 +37
- Partials 22 25 +3
Continue to review full report at Codecov.
|
|
thanks for the PR. I like you I think I'd rather have only one package p5
type Keyboard struct {
down map[string]struct{}
release map[key.Event]struct{}
last string
Pressed KeyCallback
Typed KeyCallback
Released KeyCallback
}
func WithKeyCallback(cbk Keyboard) Option {
return func(p *Proc) { p.Key = cbk }
}and: package main
func main() {
p5.Run(setup, draw, p5.WithKeyCallback(p5.Keyboard{
Pressed: keyPressed,
...
})
}what do you think? |
|
not necessarily. func Run(setup, draw Func, opts ...Option) {
...
for _, opt := range opts {
opt(gproc)
}
if gproc.Key.Pressed == nil {
gproc.Key.Pressed = func() {}
}
...
}
I was indeed using notation from my previous PR :)
this blog entry is a bit old :)
you're right. (I am sorry I'll have to disappear for a week w/o much internet connectivity and leave this PR unreviewed for the time being. I'll review it further coming back from holidays) |
|
Okay, I replaced func main() {
p5.Run(setup, draw, p5.WithKeyCallback(p5.KeyCb{
Pressed: keyPressed,
Typed: keyTyped,
}))
...Is it fine? |
|
@sbinet Do you have time to look at it? |
sbinet
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi,
apologies for the belated answer.
please find my comments inlined.
do you have an idea on a testing strategy for this new feature?
(something testable through the usual go test scaffolding)
also: could you rebase off the latest main?
thanks.
| @@ -0,0 +1,33 @@ | |||
| // Copyright ©2020 The go-p5 Authors. All rights reserved. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // Copyright ©2020 The go-p5 Authors. All rights reserved. | |
| // Copyright ©2021 The go-p5 Authors. All rights reserved. |
| if e.State == key.Press { | ||
| p.onKeyPressed(e) | ||
| } else if e.State == key.Release { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if e.State == key.Press { | |
| p.onKeyPressed(e) | |
| } else if e.State == key.Release { | |
| switch e.State { | |
| case key.Press: | |
| p.onKeyPressed(e) | |
| case key.Release: |
|
|
||
| import "gioui.org/io/key" | ||
|
|
||
| var Keyboard struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps move this as an anonymous field of the global Event variable, much like we did for Mouse ?
Line 9 in 6b7ccd9
| Mouse struct { |
ie:
var Event struct {
Mouse struct { ... }
Key struct { ... }
}| KeyPressed KeyEventFunc | ||
| KeyTyped KeyEventFunc | ||
| KeyReleased KeyEventFunc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of "exploding" the KeyCb fields into 3 different fields in Proc, perhaps just add a Key KeyCb field to Proc ?
| // KeyEventFunc is the type of key functions users provide to p5. | ||
| type KeyEventFunc func(key.Event) | ||
|
|
||
| type KeyCb struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this new type probably needs a bit of documentation.
also, as we spelled out the func-option WithKeyCallback, what about renaming this type KeyCallback ?
(alternatively, we could go with WithKeyCbk / KeyCbk)
the meaning of (and their semantical nuance) the Pressed and Typed probably need to be documented and/or explained.
this can be done as part of the documentation of each of these fields.
| gproc.DrawImage(img, x, y) | ||
| } | ||
|
|
||
| // KeyIsDown checks if given key is already pressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // KeyIsDown checks if given key is already pressed. | |
| // KeyIsDown checks whether a given key is already pressed. |
| } | ||
|
|
||
| // KeyIsDown checks if given key is already pressed. | ||
| func KeyIsDown(code string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| func KeyIsDown(code string) bool { | |
| func KeyIsDown(name string) bool { |
| // KeyIsDown checks if given key is already pressed. | ||
| func (p *Proc) KeyIsDown(code string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // KeyIsDown checks if given key is already pressed. | |
| func (p *Proc) KeyIsDown(code string) bool { | |
| // KeyIsDown checks whether a given key is already pressed. | |
| func (p *Proc) KeyIsDown(name string) bool { |
| if _, ok := Keyboard.downKeys[code]; ok { | ||
| return true | ||
| } | ||
| return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if _, ok := Keyboard.downKeys[code]; ok { | |
| return true | |
| } | |
| return false | |
| _, ok := Keyboard.downKeys[code] | |
| return ok |
|
ping? |
|
Sorry, I am quite busy nowadays and I am no longer interested in using go-p5. |
Base API
Functions:
p5.KeyIsDown(code string) bool- checks if given key is already pressedVariables:
p5.KeyIsPressed bool- whether any key is pressed nowp5.Key string- name of last pressed/typed keyCallbacks
Callbacks can be set with following functions:
p5.SetKeyPressedCallback(f KeyEventFunc)p5.SetKeyTypedCallback(f KeyEventFunc)p5.SetKeyReleasedCallback(f KeyEventFunc)Caveats:
@sbinet I am not sure if the code is safe, so please take a look. I used your examples key-snake and key-pressed from #52. Feel free to suggest changes, this is just proposal 😀