1
0
mirror of https://github.com/br0xen/termbox-screen.git synced 2024-11-22 13:33:14 +00:00

Change events system

This commit is contained in:
Brian Buller 2019-03-05 03:42:22 +00:00
parent baa25fe4cf
commit a1b5bf3c81

View File

@ -5,10 +5,15 @@ import (
"os" "os"
"runtime" "runtime"
"syscall" "syscall"
"time"
termbox "github.com/nsf/termbox-go" termbox "github.com/nsf/termbox-go"
) )
const (
NoRefresh = 0
)
type Screen interface { type Screen interface {
Id() int Id() int
Initialize(Bundle) error Initialize(Bundle) error
@ -25,21 +30,19 @@ type Manager struct {
screens map[int]Screen screens map[int]Screen
displayScreenId int displayScreenId int
events chan termbox.Event events chan termbox.Event
running bool
refreshRate time.Duration
} }
func NewManager() *Manager { func NewManager() *Manager {
m := Manager{ m := Manager{
defaultFg: termbox.ColorWhite, defaultFg: termbox.ColorWhite,
defaultBg: termbox.ColorBlack, defaultBg: termbox.ColorBlack,
events: make(chan termbox.Event), events: make(chan termbox.Event),
screens: make(map[int]Screen), screens: make(map[int]Screen),
refreshRate: NoRefresh,
} }
// Add the default user-input provider
m.AddEventProvider(func(e chan termbox.Event) {
for {
e <- termbox.PollEvent()
}
})
return &m return &m
} }
@ -80,10 +83,6 @@ func (m *Manager) InitializeScreen(id int, b Bundle) error {
return m.screens[id].Initialize(b) return m.screens[id].Initialize(b)
} }
func (m *Manager) AddEventProvider(provider func(chan termbox.Event)) {
go provider(m.events)
}
func (m *Manager) Loop() error { func (m *Manager) Loop() error {
if len(m.screens) == 0 { if len(m.screens) == 0 {
return errors.New("Loop cannot run without screens") return errors.New("Loop cannot run without screens")
@ -91,7 +90,9 @@ func (m *Manager) Loop() error {
if err := termbox.Init(); err != nil { if err := termbox.Init(); err != nil {
return err return err
} }
m.running = true
termbox.SetOutputMode(termbox.Output256) termbox.SetOutputMode(termbox.Output256)
go m.pollUserEvents()
// We always start display the first screen added // We always start display the first screen added
m.layoutAndDrawScreen() m.layoutAndDrawScreen()
for { for {
@ -125,10 +126,39 @@ func (m *Manager) Loop() error {
m.layoutAndDrawScreen() m.layoutAndDrawScreen()
} }
} }
m.running = false
termbox.Close() termbox.Close()
return nil return nil
} }
func (m *Manager) SendNoneEvent() {
m.SendEvent(termbox.Event{Type: termbox.EventNone})
}
func (m *Manager) SendEvent(t termbox.Event) {
m.events <- t
}
func (m *Manager) pollUserEvents() {
for m.running {
m.SendEvent(termbox.PollEvent())
}
}
func (m *Manager) SetRefreshRate(t time.Duration) {
m.refreshRate = t
go m.pollRefreshEvents()
}
func (m *Manager) pollRefreshEvents() {
if m.refreshRate > 0 {
for m.running {
time.Sleep(m.refreshRate)
m.SendNoneEvent()
}
}
}
func (m *Manager) handleKeyEvent(event termbox.Event) int { func (m *Manager) handleKeyEvent(event termbox.Event) int {
return m.screens[m.displayScreenId].HandleKeyEvent(event) return m.screens[m.displayScreenId].HandleKeyEvent(event)
} }