2022-10-31 13:51:28 -06:00
|
|
|
package stone
|
|
|
|
|
2022-11-10 00:02:08 -07:00
|
|
|
import "image"
|
2022-10-31 13:51:28 -06:00
|
|
|
|
2022-11-08 23:01:13 -07:00
|
|
|
// Application represents an application.
|
2022-10-31 13:51:28 -06:00
|
|
|
type Application struct {
|
2022-11-16 19:20:48 -07:00
|
|
|
DamageBuffer
|
2022-10-31 13:51:28 -06:00
|
|
|
|
|
|
|
title string
|
2022-11-10 00:02:08 -07:00
|
|
|
icons []image.Image
|
2022-10-31 13:51:28 -06:00
|
|
|
backend Backend
|
|
|
|
config Config
|
2022-11-15 22:29:23 -07:00
|
|
|
callbackManager CallbackManager
|
2022-10-31 13:51:28 -06:00
|
|
|
}
|
|
|
|
|
2022-11-08 23:18:56 -07:00
|
|
|
// 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.
|
2022-11-09 13:52:49 -07:00
|
|
|
func (application *Application) Run () (
|
2022-11-08 23:18:56 -07:00
|
|
|
err error,
|
2022-11-06 12:47:37 -07:00
|
|
|
) {
|
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 }
|
2022-11-16 19:20:48 -07:00
|
|
|
application.DamageBuffer.SetSize(width, height)
|
2022-10-31 13:51:28 -06:00
|
|
|
|
2022-11-15 11:43:21 -07:00
|
|
|
application.config.load()
|
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-11-09 13:52:49 -07:00
|
|
|
|
2022-11-15 22:29:23 -07:00
|
|
|
application.backend.Run()
|
2022-11-06 12:47:37 -07:00
|
|
|
return
|
2022-10-31 13:51:28 -06:00
|
|
|
}
|
2022-11-09 16:53:14 -07:00
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnQuit registers an event handler to be called just before the application
|
|
|
|
// quits. This can happen when the user closes the application, or the backend
|
|
|
|
// experiences an unrecoverable error.
|
2022-11-15 22:29:23 -07:00
|
|
|
func (application *Application) OnQuit (
|
|
|
|
onQuit func (),
|
|
|
|
) {
|
|
|
|
application.callbackManager.onQuit = onQuit
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnPress registers an event handler to be called when a key or mouse button
|
|
|
|
// is pressed.
|
2022-11-15 22:29:23 -07:00
|
|
|
func (application *Application) OnPress (
|
2022-11-21 22:21:35 -07:00
|
|
|
onPress func (button Button, modifiers Modifiers),
|
2022-11-15 22:29:23 -07:00
|
|
|
) {
|
|
|
|
application.callbackManager.onPress = onPress
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnPress registers an event handler to be called when a key or mouse button
|
|
|
|
// is released.
|
2022-11-15 22:29:23 -07:00
|
|
|
func (application *Application) OnRelease (
|
|
|
|
onRelease func (button Button),
|
|
|
|
) {
|
|
|
|
application.callbackManager.onRelease = onRelease
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnResize registers an event handler to be called when the application window
|
|
|
|
// is resized. After the event handler is called, any updates it makes will
|
|
|
|
// automatically be pushed to the screen.
|
2022-11-15 22:29:23 -07:00
|
|
|
func (application *Application) OnResize (
|
|
|
|
onResize func (),
|
|
|
|
) {
|
|
|
|
application.callbackManager.onResize = onResize
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnMouseMove registers an event handler to be called when mouse motion is
|
|
|
|
// detected. The coordinates of the cell that the mouse now hovers over are
|
|
|
|
// given as input.
|
2022-11-15 22:29:23 -07:00
|
|
|
func (application *Application) OnMouseMove (
|
|
|
|
onMouseMove func (x, y int),
|
|
|
|
) {
|
|
|
|
application.callbackManager.onMouseMove = onMouseMove
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnScroll registers an event handler to be called when the user uses the mouse
|
|
|
|
// scroll wheel. Horizontal and vertical amounts are given as input.
|
2022-11-17 17:11:49 -07:00
|
|
|
func (application *Application) OnScroll (
|
|
|
|
onScroll func (x, y int),
|
|
|
|
) {
|
|
|
|
application.callbackManager.onScroll = onScroll
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// OnStart registers an event handler to be called once when the application
|
|
|
|
// starts, right before the first time updates are pushed to the screen.
|
|
|
|
// Anything done in here will be the first thing to appear on screen.
|
2022-11-15 22:29:23 -07:00
|
|
|
func (application *Application) OnStart (
|
|
|
|
onStart func (),
|
|
|
|
) {
|
|
|
|
application.callbackManager.onStart = onStart
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:30:59 -07:00
|
|
|
// Draw "commits" changes made in the buffer to the display.
|
|
|
|
func (application *Application) Draw () {
|
|
|
|
application.backend.Draw()
|
|
|
|
}
|
|
|
|
|
2022-11-10 00:02:08 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:22:01 -07:00
|
|
|
// Title returns the application's title.
|
2022-11-10 00:02:08 -07:00
|
|
|
func (application *Application) Title () (title string) {
|
|
|
|
title = application.title
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:22:01 -07:00
|
|
|
// SetIcon takes in a list of different sizes of an icon, and sets it as the
|
|
|
|
// application's icon.
|
2022-11-10 00:02:08 -07:00
|
|
|
func (application *Application) SetIcon (sizes []image.Image) (err error) {
|
|
|
|
application.icons = sizes
|
|
|
|
if application.backend != nil {
|
|
|
|
err = application.backend.SetIcon(sizes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:22:01 -07:00
|
|
|
// Icon returns all available sizes of the application's icon. If there is no
|
|
|
|
// icon, nil is returned.
|
2022-11-10 00:02:08 -07:00
|
|
|
func (application *Application) Icon () (sizes []image.Image) {
|
|
|
|
sizes = application.icons
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:53:14 -07:00
|
|
|
// Config returns a pointer to the application's configuration.
|
|
|
|
func (application *Application) Config () (config *Config) {
|
|
|
|
config = &application.config
|
|
|
|
return
|
|
|
|
}
|