Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event: overhaul api #196

Merged
merged 48 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
9410fc5
event: overhaul api
200sc Mar 12, 2022
71c28e1
oak: use new event paradigms in key, mouse, joystick, and oak
200sc Mar 19, 2022
1279c49
oak: go mod tidy
200sc Mar 19, 2022
872db9b
Merge remote-tracking branch 'origin' into feature/v4-events
200sc Mar 19, 2022
94f50c9
oak: rename CID to CallerID globally, fix render, collision for new e…
200sc Mar 19, 2022
2708f5c
audio/pcm: drop writer js
200sc Mar 19, 2022
7eefe21
event: move two three event type system:
200sc Mar 22, 2022
64f2052
scene: remove loop
200sc Mar 22, 2022
f5d5052
entities: convert to event v4
200sc Mar 22, 2022
e5facb6
event: add unbind all helper, fix ticker and bindable list logic
200sc Mar 22, 2022
38bef9d
examples: Update a few examples without dependencies to have the new …
Implausiblyfun Mar 26, 2022
480e791
event: Update Bindable signature for better error messages
Implausiblyfun Mar 26, 2022
02e311e
oak: fix main package tests for event package changes
200sc Mar 26, 2022
be4ed76
Merge branch 'feature/v4-events' of github.com:oakmound/oak into feat…
200sc Mar 26, 2022
7d75b18
collision: update tests for new event api
200sc Mar 26, 2022
82ac457
scene: update tests for new event api
200sc Mar 26, 2022
c973759
render: correct tests for new event api
200sc Mar 26, 2022
daa0ded
mouse: correct tests for new event api
200sc Mar 26, 2022
f983f31
scene: Embed Eventhandler to allow for some embedded calls
Implausiblyfun Mar 26, 2022
6602149
event: rename CallerMap.Reset to Clear, enabling embedding via scene.…
200sc Mar 26, 2022
7f35da3
Merge pull request #197 from oakmound/poc/v4-eventhandler-rename
200sc Mar 28, 2022
747702d
entities/x: adjust to compile with event api v4
200sc Mar 28, 2022
d8f60a0
event: adjust docs
200sc Mar 29, 2022
382dded
event: add tests for trigger
200sc Mar 30, 2022
91ef924
event: add most tests, further trim down API, ensure concurrent-safe use
200sc Mar 31, 2022
ea990b1
event: restore 100% test coverage
200sc Mar 31, 2022
9d98d49
render/particle: Source CallerID ordering fix + Start adding non-defa…
Implausiblyfun Apr 2, 2022
d0fba73
key: Pull in more mobile key info into oak for cleaner calling signat…
Implausiblyfun Apr 2, 2022
494c9e1
debugtools\inputviz: Updates for the new key and cid changes
Implausiblyfun Apr 2, 2022
780a1fb
entities: Moving works as expected post event refactor
Implausiblyfun Apr 2, 2022
133cfba
examples: Fixes for CID changes and key changes
Implausiblyfun Apr 2, 2022
710c770
oak: drop background smear todo, correct key usage docs
200sc Apr 2, 2022
3ea313c
entities: Fixes new solids that dont have a cid passed in sharing col…
Implausiblyfun Apr 2, 2022
ac0771a
examples: Update more examples to the new event paradigm
Implausiblyfun Apr 2, 2022
9e93421
key: use Code types everywhere
200sc Apr 2, 2022
dbeccbe
Merge branch 'feature/v4-eventskeys+examples' of github.com:oakmound/…
200sc Apr 2, 2022
9ed6791
examples/titlescreen-demo: use new key / events
200sc Apr 2, 2022
24d9dd1
examples\platformer: Change for strs
Implausiblyfun Apr 2, 2022
435da4e
examples\sprite: str changes
Implausiblyfun Apr 2, 2022
d9b3e4b
examples/top-down-shooter-tutorial: finish fixing for new events and …
200sc Apr 2, 2022
112a5a7
Merge branch 'feature/v4-events-tests' into feature/v4-eventskeys+exa…
200sc Apr 2, 2022
862f50f
Merge branch 'feature/v4-eventskeys+examples' of github.com:oakmound/…
200sc Apr 2, 2022
4dfa4ce
examples/slides: Update for new event format
Implausiblyfun Apr 2, 2022
8ed5338
.github: Update workflows for go 1.18
Implausiblyfun Apr 2, 2022
e27b187
Merge pull request #199 from oakmound/feature/v4-eventskeys+examples
Implausiblyfun Apr 2, 2022
8c9ca67
examples: remove event.NoPayload usages
200sc Apr 2, 2022
8dd2b59
event: add SetCID to Caller, stop returning CIDs from Register
200sc Apr 2, 2022
419b016
Revert "event: add SetCID to Caller, stop returning CIDs from Register"
200sc Apr 2, 2022
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

coverage.txt
coverage.txt

# Workspace configuration
.vscode
34 changes: 16 additions & 18 deletions collision/attachSpace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,36 @@ type AttachSpace struct {
aSpace **Space
tree *Tree
offX, offY float64
binding event.Binding
}

func (as *AttachSpace) getAttachSpace() *AttachSpace {
return as
}

func (as *AttachSpace) CID() event.CallerID {
return (*as.aSpace).CID
}

var _ attachSpace = &AttachSpace{}

type attachSpace interface {
event.Caller
getAttachSpace() *AttachSpace
}

// Attach attaches v to the given space with optional x,y offsets. See AttachSpace.
func Attach(v physics.Vector, s *Space, tree *Tree, offsets ...float64) error {
if t, ok := s.CID.E().(attachSpace); ok {
en := event.DefaultCallerMap.GetEntity(s.CID)
if t, ok := en.(attachSpace); ok {
as := t.getAttachSpace()
as.aSpace = &s
as.follow = v
as.tree = tree
if as.tree == nil {
as.tree = DefaultTree
}
s.CID.Bind(event.Enter, attachSpaceEnter)
as.binding = event.Bind(event.DefaultBus, event.Enter, t, attachSpaceEnter)
if len(offsets) > 0 {
as.offX = offsets[0]
if len(offsets) > 1 {
Expand All @@ -54,30 +63,19 @@ func Attach(v physics.Vector, s *Space, tree *Tree, offsets ...float64) error {
// Detach removes the attachSpaceEnter binding from an entity composed with
// AttachSpace
func Detach(s *Space) error {
en := s.CID.E()
if _, ok := en.(attachSpace); ok {
event.UnbindBindable(
event.UnbindOption{
Event: event.Event{
Name: event.Enter,
CallerID: s.CID,
},
Fn: attachSpaceEnter,
},
)
en := event.DefaultCallerMap.GetEntity(s.CID)
if as, ok := en.(attachSpace); ok {
as.getAttachSpace().binding.Unbind()
return nil
}
return errors.New("this space's entity is not composed of AttachSpace")
}

func attachSpaceEnter(id event.CID, _ interface{}) int {
as := id.E().(attachSpace).getAttachSpace()
func attachSpaceEnter(asIface attachSpace, _ event.EnterPayload) event.Response {
as := asIface.(attachSpace).getAttachSpace()
x, y := as.follow.X()+as.offX, as.follow.Y()+as.offY
if x != (*as.aSpace).X() ||
y != (*as.aSpace).Y() {

// If this was a nil pointer it would have already crashed but as of release 2.2.0
// this could error from the space to delete not existing in the rtree.
as.tree.UpdateSpace(x, y, (*as.aSpace).GetW(), (*as.aSpace).GetH(), *as.aSpace)
}
return 0
Expand Down
2 changes: 1 addition & 1 deletion collision/attachSpace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type aspace struct {
AttachSpace
}

func (as *aspace) Init() event.CID {
func (as *aspace) Init() event.CallerID {
return event.NextID(as)
}

Expand Down
2 changes: 1 addition & 1 deletion collision/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Without(tossFn func(*Space) bool) Filter {
}

// WithoutCIDs will return no spaces with a CID in the input
func WithoutCIDs(cids ...event.CID) Filter {
func WithoutCIDs(cids ...event.CallerID) Filter {
return Without(func(s *Space) bool {
for _, c := range cids {
if s.CID == c {
Expand Down
51 changes: 24 additions & 27 deletions collision/onCollision.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,43 @@ func PhaseCollisionWithBus(s *Space, tree *Tree, bus event.Handler, entities *ev
if oc.tree == nil {
oc.tree = DefaultTree
}
bus.Bind(event.Enter, s.CID, phaseCollisionEnter(entities))
bus.UnsafeBind(event.Enter.UnsafeEventID, s.CID, phaseCollisionEnter)
return nil
}
return errors.New("This space's entity does not implement collisionPhase")
}

// CollisionStart/Stop: when a PhaseCollision entity starts/stops touching some label.
// Payload: (Label) the label the entity has started/stopped touching
const (
Start = "CollisionStart"
Stop = "CollisionStop"
var (
Start = event.RegisterEvent[Label]()
Stop = event.RegisterEvent[Label]()
)

func phaseCollisionEnter(entities *event.CallerMap) func(id event.CID, nothing interface{}) int {
return func(id event.CID, nothing interface{}) int {
e := entities.GetEntity(id).(collisionPhase)
oc := e.getCollisionPhase()
func phaseCollisionEnter(id event.CallerID, handler event.Handler, _ interface{}) event.Response {
e := handler.GetCallerMap().GetEntity(id).(collisionPhase)
oc := e.getCollisionPhase()

// check hits
hits := oc.tree.Hits(oc.OnCollisionS)
newTouching := map[Label]bool{}
// check hits
hits := oc.tree.Hits(oc.OnCollisionS)
newTouching := map[Label]bool{}

// if any are new, trigger on collision
for _, h := range hits {
l := h.Label
if _, ok := oc.Touching[l]; !ok {
id.TriggerBus(Start, l, oc.bus)
}
newTouching[l] = true
// if any are new, trigger on collision
for _, h := range hits {
l := h.Label
if _, ok := oc.Touching[l]; !ok {
event.TriggerForCallerOn(oc.bus, id, Start, l)
}
newTouching[l] = true
}

// if we lost any, trigger off collision
for l := range oc.Touching {
if _, ok := newTouching[l]; !ok {
id.TriggerBus(Stop, l, oc.bus)
}
// if we lost any, trigger off collision
for l := range oc.Touching {
if _, ok := newTouching[l]; !ok {
event.TriggerForCallerOn(handler, id, Stop, l)
}
}

oc.Touching = newTouching
oc.Touching = newTouching

return 0
}
return 0
}
6 changes: 3 additions & 3 deletions collision/onCollision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type cphase struct {
callers *event.CallerMap
}

func (cp *cphase) Init() event.CID {
func (cp *cphase) Init() event.CallerID {
return cp.callers.NextID(cp)
}

Expand All @@ -37,11 +37,11 @@ func TestCollisionPhase(t *testing.T) {
t.Fatalf("phase collision failed: %v", err)
}
var active bool
bus.Bind("CollisionStart", cid, func(event.CID, interface{}) int {
bus.Bind("CollisionStart", cid, func(event.CallerID, interface{}) int {
active = true
return 0
})
bus.Bind("CollisionStop", cid, func(event.CID, interface{}) int {
bus.Bind("CollisionStop", cid, func(event.CallerID, interface{}) int {
active = false
return 0
})
Expand Down
4 changes: 2 additions & 2 deletions collision/ray/castFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func IgnoreLabels(ls ...collision.Label) CastOption {
}

// AcceptIDs is equivalent to AcceptLabels, but for CIDs.
func AcceptIDs(ids ...event.CID) CastOption {
func AcceptIDs(ids ...event.CallerID) CastOption {
return AddFilter(func(s *collision.Space) bool {
for _, id := range ids {
if s.CID == id {
Expand All @@ -58,7 +58,7 @@ func AcceptIDs(ids ...event.CID) CastOption {
}

// IgnoreIDs is equivalent to IgnoreLabels, but for CIDs.
func IgnoreIDs(ids ...event.CID) CastOption {
func IgnoreIDs(ids ...event.CallerID) CastOption {
return AddFilter(func(s *collision.Space) bool {
for _, id := range ids {
if s.CID == id {
Expand Down
2 changes: 1 addition & 1 deletion collision/ray/castLimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func StopAtLabel(ls ...collision.Label) CastOption {

// StopAtID will cause a caster to cease casting as soon as it
// hits one of the input CIDs.
func StopAtID(ids ...event.CID) CastOption {
func StopAtID(ids ...event.CallerID) CastOption {
return AddLimit(func(ps []collision.Point) bool {
z := ps[len(ps)-1].Zone
for _, id := range ids {
Expand Down
10 changes: 5 additions & 5 deletions collision/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Space struct {
Label Label
// A CID can be used to get the exact
// entity which this rectangle belongs to.
CID event.CID
CID event.CallerID
// Type represents which ID space the above ID
// corresponds to.
Type int
Expand Down Expand Up @@ -197,7 +197,7 @@ func NewUnassignedSpace(x, y, w, h float64) *Space {
}

// NewSpace returns a space with an associated caller id
func NewSpace(x, y, w, h float64, cID event.CID) *Space {
func NewSpace(x, y, w, h float64, cID event.CallerID) *Space {
return NewFullSpace(x, y, w, h, NilLabel, cID)
}

Expand All @@ -212,7 +212,7 @@ func NewLabeledSpace(x, y, w, h float64, l Label) *Space {
}

// NewFullSpace returns a space with both a label and a caller id
func NewFullSpace(x, y, w, h float64, l Label, cID event.CID) *Space {
func NewFullSpace(x, y, w, h float64, l Label, cID event.CallerID) *Space {
rect := NewRect(x, y, w, h)
return &Space{
rect,
Expand All @@ -223,12 +223,12 @@ func NewFullSpace(x, y, w, h float64, l Label, cID event.CID) *Space {
}

// NewRect2Space returns a space with an associated caller id from a rect2
func NewRect2Space(rect floatgeom.Rect2, cID event.CID) *Space {
func NewRect2Space(rect floatgeom.Rect2, cID event.CallerID) *Space {
return NewSpace(rect.Min.X(), rect.Min.Y(), rect.W(), rect.H(), cID)
}

// NewRectSpace creates a colliison space with the specified 3D rectangle
func NewRectSpace(rect floatgeom.Rect3, l Label, cID event.CID) *Space {
func NewRectSpace(rect floatgeom.Rect3, l Label, cID event.CallerID) *Space {
return &Space{
rect,
l,
Expand Down
5 changes: 0 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Config struct {
IdleDrawFrameRate int `json:"idleDrawFrameRate"`
Language string `json:"language"`
Title string `json:"title"`
EventRefreshRate Duration `json:"refreshRate"`
BatchLoad bool `json:"batchLoad"`
GestureSupport bool `json:"gestureSupport"`
LoadBuiltinCommands bool `json:"loadBuiltinCommands"`
Expand Down Expand Up @@ -98,7 +97,6 @@ func (c Config) setDefaults() Config {
c.IdleDrawFrameRate = 60
c.Language = "English"
c.Title = "Oak Window"
c.EventRefreshRate = Duration(50 * time.Millisecond)
return c
}

Expand Down Expand Up @@ -223,9 +221,6 @@ func (c Config) overwriteFrom(c2 Config) Config {
if c2.Title != "" {
c.Title = c2.Title
}
if c2.EventRefreshRate != 0 {
c.EventRefreshRate = c2.EventRefreshRate
}
// Booleans can be directly overwritten-- all booleans in a Config
// default to false, if they were unset they will stay false.
c.BatchLoad = c2.BatchLoad
Expand Down
17 changes: 9 additions & 8 deletions debugstream/scopeHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ const explainMouseDetails = "the mext mouse click on the given window will print

func mouseCommands(w window.Window) func([]string) string {
return func(tokenString []string) string {
w.EventHandler().GlobalBind("MouseRelease", mouseDetails(w))
event.GlobalBind(w.EventHandler(), mouse.Release, mouseDetails(w))
return ""
}
}

func mouseDetails(w window.Window) func(event.CID, interface{}) int {
return func(nothing event.CID, mevent interface{}) int {
me := mevent.(mouse.Event)
func mouseDetails(w window.Window) func(*mouse.Event) event.Response {
return func(me *mouse.Event) event.Response {
viewPos := w.Viewport()
x := int(me.X()) + viewPos[0]
y := int(me.Y()) + viewPos[1]
Expand All @@ -86,17 +85,19 @@ func mouseDetails(w window.Window) func(event.CID, interface{}) int {
if len(results) == 0 {
results = mouse.Hits(loc)
}
cm := w.GetCallerMap()

if len(results) > 0 {
i := int(results[0].CID)
if i > 0 && event.HasEntity(event.CID(i)) {
e := event.GetEntity(event.CID(i))
i := results[0].CID
if i > 0 && cm.HasEntity(i) {
e := cm.GetEntity(i)
fmt.Printf("%+v\n", e)
} else {
fmt.Println("No entity ", i)
}
}

return event.UnbindSingle
return event.UnbindThis
}
}

Expand Down
Loading