stone/application.go

142 lines
3.4 KiB
Go

package stone
import "image"
// Application represents an application.
type Application struct {
DamageBuffer
title string
icons []image.Image
backend Backend
config Config
callbackManager CallbackManager
imageManager ImageManager
}
// Run initializes the application, starts it, and then returns a channel that
// broadcasts events. If no suitable backend can be found, an error is returned.
func (application *Application) Run () (
err error,
) {
// default values for certain parameters
width, height := application.Size()
if width < 1 { width = 80 }
if height < 1 { height = 20 }
application.DamageBuffer.SetSize(width, height)
application.config.load()
application.backend, err = instantiateBackend(application)
if err != nil { return }
application.backend.Run()
return
}
func (application *Application) OnQuit (
onQuit func (),
) {
application.callbackManager.onQuit = onQuit
}
func (application *Application) OnPress (
onPress func (button Button),
) {
application.callbackManager.onPress = onPress
}
func (application *Application) OnRelease (
onRelease func (button Button),
) {
application.callbackManager.onRelease = onRelease
}
func (application *Application) OnResize (
onResize func (),
) {
application.callbackManager.onResize = onResize
}
func (application *Application) OnMouseMove (
onMouseMove func (x, y int),
) {
application.callbackManager.onMouseMove = onMouseMove
}
func (application *Application) OnScroll (
onScroll func (x, y int),
) {
application.callbackManager.onScroll = onScroll
}
func (application *Application) OnStart (
onStart func (),
) {
application.callbackManager.onStart = onStart
}
// Draw "commits" changes made in the buffer to the display.
func (application *Application) Draw () {
application.backend.Draw()
}
// SetTitle sets the application's title. If in a window, it will appear as the
// window's name.
func (application *Application) SetTitle (title string) (err error) {
application.title = title
if application.backend != nil {
err = application.backend.SetTitle(title)
}
return
}
// Title returns the application's title.
func (application *Application) Title () (title string) {
title = application.title
return
}
// SetIcon takes in a list of different sizes of an icon, and sets it as the
// application's icon.
func (application *Application) SetIcon (sizes []image.Image) (err error) {
application.icons = sizes
if application.backend != nil {
err = application.backend.SetIcon(sizes)
}
return
}
// Icon returns all available sizes of the application's icon. If there is no
// icon, nil is returned.
func (application *Application) Icon () (sizes []image.Image) {
sizes = application.icons
return
}
// Config returns a pointer to the application's configuration.
func (application *Application) Config () (config *Config) {
config = &application.config
return
}
// AddImage adds a new image buffer and returns a pointer to it.
func (application *Application) NewImage () (im *ColorImage) {
cellWidth, cellHeight := application.backend.CellMetrics()
im = &ColorImage {
cellWidth: cellWidth,
cellHeight: cellHeight,
}
application.imageManager.Add(im)
return
}
// Remove removes the specified image buffer, if the application has it. If the
// image was found and removed, removed will be true.
func (application *Application) RemoveImage (im *ColorImage) (removed bool) {
removed = application.imageManager.Remove(im)
return
}