2022-10-31 13:51:28 -06:00
|
|
|
package stone
|
|
|
|
|
2022-11-08 23:13:44 -07:00
|
|
|
import "image"
|
2022-10-31 13:51:28 -06:00
|
|
|
import "errors"
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// Backend represents a backend for stone. Backends can be registered for use
|
|
|
|
// with the RegisterBackend() function. All of the below methods MUST be thread
|
|
|
|
// safe!
|
2022-10-31 13:51:28 -06:00
|
|
|
type Backend interface {
|
2022-11-19 16:00:47 -07:00
|
|
|
// Run is the backend's event loop. It must cleanly exit when the user
|
|
|
|
// closes the window, but not before calling the OnQuit event. Run
|
|
|
|
// must call event handlers within its own event loop in a
|
|
|
|
// non-concurrent fashion.
|
|
|
|
//
|
|
|
|
// The OnStart event handler must run after the backend has been fully
|
|
|
|
// initialized, and right before updates are first pushed to the screen.
|
|
|
|
// Whatever the application draws from within this event handler must be
|
|
|
|
// the first thing that appears on-screen.
|
|
|
|
//
|
2022-11-21 21:43:22 -07:00
|
|
|
// The OnResize event handler must run whenever the window is resized.
|
2022-11-19 16:00:47 -07:00
|
|
|
// The backend must push updates to the screen after OnResize has been
|
|
|
|
// run.
|
|
|
|
//
|
|
|
|
// The backend must not push updates to the screen in any other case,
|
|
|
|
// except when its Draw() method is specifically called.
|
|
|
|
//
|
|
|
|
// The OnPress, OnRelease, OnMouseMove, and OnMouseScroll events are to
|
|
|
|
// be called when such events happen. It is reccommended to compress
|
|
|
|
// resize, mouse move, and mouse scroll events whenever possible to
|
|
|
|
// reduce the likelihood of event buildup.
|
|
|
|
Run ()
|
|
|
|
|
|
|
|
// SetTitle sets the application title. This will most often be the
|
|
|
|
// window title. This method may not always produce an effect, depending
|
|
|
|
// on the backend.
|
2022-11-10 00:02:08 -07:00
|
|
|
SetTitle (title string) (err error)
|
2022-11-19 16:00:47 -07:00
|
|
|
|
|
|
|
// SetIcon takes in a set of images of different sizes and sets the
|
|
|
|
// window's icon to them. This method may not always produce an effect,
|
|
|
|
// depending on the backend.
|
|
|
|
SetIcon (icons []image.Image) (err error)
|
|
|
|
|
|
|
|
// Draw pushes all updates made to the application's buffer to the
|
|
|
|
// screen.
|
|
|
|
Draw ()
|
2022-10-31 13:51:28 -06:00
|
|
|
}
|
|
|
|
|
2022-11-21 21:43:22 -07:00
|
|
|
// BackendFactory must completely initialize a backend, and return it. If
|
|
|
|
// anything goes wrong, it must stop, clean up any resources and return an
|
2022-11-19 16:00:47 -07:00
|
|
|
// error so another backend can be chosen.
|
2022-11-15 22:29:23 -07:00
|
|
|
type BackendFactory func (
|
|
|
|
application *Application,
|
|
|
|
callbackManager *CallbackManager,
|
|
|
|
) (
|
|
|
|
backend Backend,
|
|
|
|
err error,
|
|
|
|
)
|
2022-10-31 13:51:28 -06:00
|
|
|
|
|
|
|
var factories []BackendFactory
|
|
|
|
|
2022-11-19 16:00:47 -07:00
|
|
|
// RegisterBackend registers a backend factory.
|
2022-10-31 13:51:28 -06:00
|
|
|
func RegisterBackend (factory BackendFactory) {
|
|
|
|
factories = append(factories, factory)
|
|
|
|
}
|
|
|
|
|
|
|
|
func instantiateBackend (application *Application) (backend Backend, err error) {
|
|
|
|
// find a suitable backend
|
|
|
|
for _, factory := range factories {
|
2022-11-15 22:29:23 -07:00
|
|
|
backend, err = factory(application, &application.callbackManager)
|
2022-10-31 13:51:28 -06:00
|
|
|
if err == nil && backend != nil { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
// if none were found, but there was no error produced, produce an
|
|
|
|
// error
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("no available backends")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|