stone/examples/type/main.go

85 lines
1.8 KiB
Go

package main
import "os"
import "fmt"
import "image"
import _ "image/png"
import "git.tebibyte.media/sashakoshka/stone"
import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
var application = &stone.Application { }
var caretX = 0
var caretY = 2
var page = 1
func main () {
application.SetTitle("hellorld")
application.SetSize(32, 28)
iconFile16, err := os.Open("assets/scaffold16.png")
if err != nil { panic(err) }
icon16, _, err := image.Decode(iconFile16)
if err != nil { panic(err) }
iconFile16.Close()
iconFile32, err := os.Open("assets/scaffold32.png")
if err != nil { panic(err) }
icon32, _, err := image.Decode(iconFile32)
if err != nil { panic(err) }
iconFile16.Close()
application.SetIcon([]image.Image { icon16, icon32 })
application.OnStart(redraw)
application.OnPress(onPress)
application.OnResize(redraw)
err = application.Run()
if err != nil { panic(err) }
application.Draw()
}
func redraw () {
application.Clear()
_, height := application.Size()
application.SetDot(0, 0)
fmt.Fprint(application, "type some text below:")
caretX = 0
caretY = 2
application.SetDot(0, height - 1)
fmt.Fprintf(application, "page %d", page)
drawCaret()
}
func drawCaret () {
application.SetRune(caretX, caretY, '+')
application.SetColor(caretX, caretY, stone.ColorDim)
}
func onPress (button stone.Button, modifiers stone.Modifiers) {
width, height := application.Size()
if button == stone.KeyEnter {
application.SetRune(caretX, caretY, 0)
caretX = 0
caretY ++
} else if button.Printable() {
application.SetRune(caretX, caretY, rune(button))
application.SetColor(caretX, caretY, stone.ColorForeground)
caretX ++
if caretX >= width {
caretX = 0
caretY ++
}
}
if caretY >= height - 2 {
page ++
redraw()
}
drawCaret()
application.Draw()
}