stone/application.go

123 lines
3.5 KiB
Go
Raw Normal View History

2022-10-31 13:51:28 -06:00
package stone
import "time"
import "image/color"
2022-11-08 23:01:13 -07:00
// Application represents an application.
2022-10-31 13:51:28 -06:00
type Application struct {
DamageBuffer
title string
backend Backend
config Config
}
2022-11-08 23:01:13 -07:00
// SetSize sets the application's buffer size. This may or may not change the
// size of the window, depending on the backend used.
2022-10-31 13:51:28 -06:00
func (application *Application) SetSize (width, height int) {
application.DamageBuffer.SetSize(width, height)
}
2022-11-08 23:01:13 -07:00
// SetTitle sets the application's title. If in a window, it will appear as the
// window's name.
2022-10-31 13:51:28 -06:00
func (application *Application) SetTitle (title string) {
application.title = title
application.backend.SetTitle(title)
}
2022-11-08 23:01:13 -07:00
// Run initializes the application, and then calls callback. Operations inside
// of callback are allowed to interact with the application. Depending on the
// backend used, this function may bind to the main thread.
2022-11-06 12:47:37 -07:00
func (application *Application) Run (
callback func (application *Application),
) (
err error,
) {
2022-10-31 13:51:28 -06:00
// default values for certain parameters
width, height := application.Size()
if width < 1 { width = 80 }
if height < 1 { height = 20 }
application.DamageBuffer.SetSize(width, height)
// TODO: load these from a file
application.config.colors = [4]color.Color {
2022-11-05 16:43:57 -06:00
color.RGBA { R: 0x2B, G: 0x30, B: 0x3C, A: 0xFF },
color.RGBA { R: 0x4C, G: 0x56, B: 0x6A, A: 0xFF },
color.RGBA { R: 0x2E, G: 0x34, B: 0x40, A: 0xFF },
color.RGBA { R: 0xA8, G: 0x55, B: 0x5D, A: 0xFF },
2022-10-31 13:51:28 -06:00
}
2022-11-05 16:56:56 -06:00
application.config.padding = 4
2022-10-31 13:51:28 -06:00
2022-11-02 13:14:59 -06:00
application.backend, err = instantiateBackend(application)
2022-11-06 12:47:37 -07:00
if err != nil { return }
2022-10-31 13:51:28 -06:00
application.backend.Run(callback)
2022-11-06 12:47:37 -07:00
return
2022-10-31 13:51:28 -06:00
}
2022-11-08 23:01:13 -07:00
// Await blocks until an event is recieved, or until the specified timeout has
// elapsed. If the timeout is zero, it will wait forever. This function returns
// true if the backend is still active, and false if it has closed.
2022-10-31 13:51:28 -06:00
func (application *Application) Await (timeout time.Duration) (keepRunning bool) {
2022-11-02 13:14:59 -06:00
keepRunning = application.backend.Await(timeout)
2022-10-31 13:51:28 -06:00
return
}
2022-11-08 23:01:13 -07:00
// Poll updates the window and checks for new events. This function returns true
// if the backend is still active, and false if it has closed.
2022-10-31 13:51:28 -06:00
func (application *Application) Poll () (keepRunning bool) {
2022-11-02 13:14:59 -06:00
keepRunning = application.backend.Poll()
2022-10-31 13:51:28 -06:00
return
}
2022-11-08 23:01:13 -07:00
// Title returns the application's title.
2022-10-31 13:51:28 -06:00
func (application *Application) Title () (title string) {
title = application.title
return
}
2022-11-08 23:01:13 -07:00
// Config returns a pointer to the application's configuration.
2022-10-31 13:51:28 -06:00
func (application *Application) Config () (config *Config) {
config = &application.config
return
}
2022-11-08 23:01:13 -07:00
// JustPressed returns true if the specified button is pressed, but was not
// pressed the last time events were checked.
func (application *Application) JustPressed (button Button) (pressed bool) {
pressed = application.backend.JustPressed(button)
return
}
2022-11-08 23:01:13 -07:00
// JustReleased returns true if the specified button
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
}
2022-11-02 13:14:59 -06:00
func (application *Application) Resized () (resized bool) {
resized = application.backend.Resized()
return
}
2022-11-06 13:59:06 -07:00
func (application *Application) MousePosition () (x, y int) {
x, y = application.backend.MousePosition()
return
}