Pass keyboard events through application

This commit is contained in:
Sasha Koshka 2022-11-06 15:12:44 -05:00
parent f6b8d9903b
commit ff7240d553
3 changed files with 43 additions and 7 deletions

View File

@ -13,7 +13,6 @@ type Application struct {
func (application *Application) SetSize (width, height int) { func (application *Application) SetSize (width, height int) {
application.DamageBuffer.SetSize(width, height) application.DamageBuffer.SetSize(width, height)
// application.updateWindowSize()
} }
func (application *Application) SetTitle (title string) { func (application *Application) SetTitle (title string) {
@ -69,6 +68,31 @@ func (application *Application) Config () (config *Config) {
return return
} }
func (application *Application) JustPressed (button Button) (pressed bool) {
pressed = application.backend.JustPressed(button)
return
}
func (application *Application) JustReleased (button Button) (released bool) {
released = application.backend.JustReleased(button)
return
}
func (application *Application) Pressed (button Button) (pressed bool) {
pressed = application.backend.Pressed(button)
return
}
func (application *Application) Repeated (button Button) (repeated bool) {
repeated = application.backend.Repeated(button)
return
}
func (application *Application) Typed () (text string) {
text = application.backend.Typed()
return
}
func (application *Application) Resized () (resized bool) { func (application *Application) Resized () (resized bool) {
resized = application.backend.Resized() resized = application.backend.Resized()
return return

View File

@ -85,9 +85,9 @@ func (buffer *Buffer) Write (bytes []byte) (bytesWritten int, err error) {
for _, character := range text { for _, character := range text {
buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character) buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character)
buffer.Dot.X ++ buffer.Dot.X ++
if buffer.Dot.X > buffer.width { if buffer.Dot.X > buffer.width || character == '\n' {
buffer.Dot.X = 0 buffer.Dot.X = 0
buffer.Dot.Y -- buffer.Dot.Y ++
} }
} }
@ -131,9 +131,9 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
for _, character := range text { for _, character := range text {
buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character) buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character)
buffer.Dot.X ++ buffer.Dot.X ++
if buffer.Dot.X > buffer.width { if buffer.Dot.X > buffer.width || character == '\n' {
buffer.Dot.X = 0 buffer.Dot.X = 0
buffer.Dot.Y -- buffer.Dot.Y ++
} }
} }

View File

@ -14,9 +14,18 @@ func main () {
func run (application *stone.Application) { func run (application *stone.Application) {
currentTime := time.Time { } currentTime := time.Time { }
frameDelay := time.Second / 2 frameDelay := time.Second / 2
textBuffer := ""
for { for {
if application.Resized() || time.Since(currentTime) > frameDelay { typed := application.Typed()
textBuffer += typed
shouldRender :=
application.Resized() ||
time.Since(currentTime) > frameDelay ||
len(typed) > 0
if shouldRender {
currentTime = time.Now() currentTime = time.Now()
application.ResetDot() application.ResetDot()
@ -35,6 +44,9 @@ func run (application *stone.Application) {
application.SetRune(6, 1, rune(second / 10 + 48)) application.SetRune(6, 1, rune(second / 10 + 48))
application.SetRune(7, 1, rune(second % 10 + 48)) application.SetRune(7, 1, rune(second % 10 + 48))
application.Dot.X = 0
application.Dot.Y = 2
fmt.Fprintln(application, textBuffer)
} }
if !application.Await(frameDelay) { break } if !application.Await(frameDelay) { break }