Skip to content

Commit

Permalink
Use ChannelEvents to simplify event processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronriekenberg committed Mar 30, 2024
1 parent 4dfafd5 commit fc1717f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 63 deletions.
111 changes: 50 additions & 61 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,11 @@ func Run() {
)
}

func setPeriodicUpdateTimer(
duration time.Duration,
eventSource view.ScreenEventSource,
) {
go func() {
time.Sleep(duration)

eventSource.PostEvent(tcell.NewEventInterrupt(periodicUpdateInterruptCustomEvent{}))
}()
}

func runEventLoop(
eventSource view.ScreenEventSource,
view view.View,
tetrisModel model.TetrisModel,
) {
setPeriodicUpdateTimer(tetrisModel.PeriodicUpdateDuration(), eventSource)

done := false

quit := func() {
Expand All @@ -62,62 +49,64 @@ func runEventLoop(
}
defer quit()

eventChannel := make(chan tcell.Event)

go eventSource.ChannelEvents(eventChannel, make(chan struct{}))

periodicUpdateChannel := time.After(tetrisModel.PeriodicUpdateDuration())

for !done {
ev := eventSource.PollEvent()
switch ev := ev.(type) {
case *tcell.EventInterrupt:
switch ev.Data().(type) {
case periodicUpdateInterruptCustomEvent:
tetrisModel.PeriodicUpdate()
setPeriodicUpdateTimer(tetrisModel.PeriodicUpdateDuration(), eventSource)

view.Draw()
}
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape:
if !utils.RunningInWASM {
quit()
}
case tcell.KeyLeft:
tetrisModel.MoveCurrentPieceLeft()
view.Draw()
case tcell.KeyRight:
tetrisModel.MoveCurrentPieceRight()
view.Draw()
case tcell.KeyUp:
tetrisModel.RotateCurrentPiece()
view.Draw()
case tcell.KeyDown:
tetrisModel.MoveCurrentPieceDown()
view.Draw()
case tcell.KeyRune:
switch ev.Rune() {
case 'q':
select {
case <-periodicUpdateChannel:
tetrisModel.PeriodicUpdate()
view.Draw()

periodicUpdateChannel = time.After(tetrisModel.PeriodicUpdateDuration())
case ev := <-eventChannel:
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape:
if !utils.RunningInWASM {
quit()
}
case 'r':
tetrisModel.Restart()
case tcell.KeyLeft:
tetrisModel.MoveCurrentPieceLeft()
view.Draw()
case tcell.KeyRight:
tetrisModel.MoveCurrentPieceRight()
view.Draw()
case ' ':
tetrisModel.DropCurrentPiece()
case tcell.KeyUp:
tetrisModel.RotateCurrentPiece()
view.Draw()
case 'v':
view.ToggleShowVersion()
case tcell.KeyDown:
tetrisModel.MoveCurrentPieceDown()
view.Draw()
case tcell.KeyRune:
switch ev.Rune() {
case 'q':
if !utils.RunningInWASM {
quit()
}
case 'r':
tetrisModel.Restart()
view.Draw()
case ' ':
tetrisModel.DropCurrentPiece()
view.Draw()
case 'v':
view.ToggleShowVersion()
}
}
case *tcell.EventMouse:
buttonMask := ev.Buttons()
if (buttonMask & tcell.Button1) != 0 {
x, y := ev.Position()
view.HandleButton1PressEvent(x, y, ev.When())
}
case *tcell.EventResize:
view.HandleResizeEvent()
}
case *tcell.EventMouse:
buttonMask := ev.Buttons()
if (buttonMask & tcell.Button1) != 0 {
x, y := ev.Position()
view.HandleButton1PressEvent(x, y, ev.When())
}
case *tcell.EventResize:
view.HandleResizeEvent()
}
}

}

type periodicUpdateInterruptCustomEvent struct{}
3 changes: 1 addition & 2 deletions view/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
)

type ScreenEventSource interface {
PollEvent() tcell.Event
PostEvent(ev tcell.Event) error
ChannelEvents(ch chan<- tcell.Event, quit <-chan struct{})
Fini()
}

Expand Down

0 comments on commit fc1717f

Please sign in to comment.