Added some documentation
This commit is contained in:
parent
509c6f0bc6
commit
7a6be48cb8
@ -3,6 +3,7 @@ package stone
|
||||
import "time"
|
||||
import "image/color"
|
||||
|
||||
// Application represents an application.
|
||||
type Application struct {
|
||||
DamageBuffer
|
||||
|
||||
@ -11,15 +12,22 @@ type Application struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
// SetSize sets the application's buffer size. This may or may not change the
|
||||
// size of the window, depending on the backend used.
|
||||
func (application *Application) SetSize (width, height int) {
|
||||
application.DamageBuffer.SetSize(width, height)
|
||||
}
|
||||
|
||||
// SetTitle sets the application's title. If in a window, it will appear as the
|
||||
// window's name.
|
||||
func (application *Application) SetTitle (title string) {
|
||||
application.title = title
|
||||
application.backend.SetTitle(title)
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (application *Application) Run (
|
||||
callback func (application *Application),
|
||||
) (
|
||||
@ -48,31 +56,41 @@ func (application *Application) Run (
|
||||
return
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (application *Application) Await (timeout time.Duration) (keepRunning bool) {
|
||||
keepRunning = application.backend.Await(timeout)
|
||||
return
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (application *Application) Poll () (keepRunning bool) {
|
||||
keepRunning = application.backend.Poll()
|
||||
return
|
||||
}
|
||||
|
||||
// Title returns the application's title.
|
||||
func (application *Application) Title () (title string) {
|
||||
title = application.title
|
||||
return
|
||||
}
|
||||
|
||||
// Config returns a pointer to the application's configuration.
|
||||
func (application *Application) Config () (config *Config) {
|
||||
config = &application.config
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// JustReleased returns true if the specified button
|
||||
func (application *Application) JustReleased (button Button) (released bool) {
|
||||
released = application.backend.JustReleased(button)
|
||||
return
|
||||
|
@ -9,6 +9,7 @@ import "github.com/faiface/pixel/pixelgl"
|
||||
import "golang.org/x/image/font/basicfont"
|
||||
import "git.tebibyte.media/sashakoshka/stone"
|
||||
|
||||
// Backend represents an instance of the pixel backend
|
||||
type Backend struct {
|
||||
window *pixelgl.Window
|
||||
boundsDirty bool
|
||||
@ -32,9 +33,11 @@ type Backend struct {
|
||||
}
|
||||
}
|
||||
|
||||
// Run satisfies the Run method of the Backend interface. Due to the nature of
|
||||
// pixel, this will forcibly bind to the main thread.
|
||||
func (backend *Backend) Run (callback func (application *stone.Application)) {
|
||||
// backend.showBounds = true
|
||||
backend.showCellBounds = true
|
||||
// backend.showCellBounds = true
|
||||
|
||||
if backend.fontFace == nil {
|
||||
backend.fontFace = basicfont.Face7x13
|
||||
@ -66,12 +69,14 @@ func (backend *Backend) Run (callback func (application *stone.Application)) {
|
||||
Bounds: backend.calculateWindowSize(),
|
||||
})
|
||||
backend.Poll()
|
||||
|
||||
|
||||
// TODO: this should return the error and not panic
|
||||
if err != nil { panic(err.Error()) }
|
||||
callback(backend.application)
|
||||
})
|
||||
}
|
||||
|
||||
// Await fulfills the Await method of the Backend interface.
|
||||
func (backend *Backend) Await (timeout time.Duration) (keepRunning bool) {
|
||||
if backend.window == nil {
|
||||
panic("call to Backend.Await before window exists")
|
||||
@ -84,6 +89,7 @@ func (backend *Backend) Await (timeout time.Duration) (keepRunning bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Poll fulfills the Poll method of the Backend interface.
|
||||
func (backend *Backend) Poll () (keepRunning bool) {
|
||||
if backend.window == nil {
|
||||
panic("call to Backend.Poll before window exists")
|
||||
@ -96,42 +102,50 @@ func (backend *Backend) Poll () (keepRunning bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// SetTitle fulfills the SetTitle method of the Backend interface.
|
||||
func (backend *Backend) SetTitle (title string) {
|
||||
if backend.window != nil {
|
||||
backend.window.SetTitle(title)
|
||||
}
|
||||
}
|
||||
|
||||
// JustPressed fulfills the JustPressed method of the Backend interface.
|
||||
func (backend *Backend) JustPressed (button stone.Button) (pressed bool) {
|
||||
pressed = backend.window.JustPressed(pixelgl.Button(button))
|
||||
return
|
||||
}
|
||||
|
||||
// JustReleased fulfills the JustReleased method of the Backend interface.
|
||||
func (backend *Backend) JustReleased (button stone.Button) (released bool) {
|
||||
released = backend.window.JustReleased(pixelgl.Button(button))
|
||||
return
|
||||
}
|
||||
|
||||
// Pressed fulfills the Pressed method of the Backend interface.
|
||||
func (backend *Backend) Pressed (button stone.Button) (pressed bool) {
|
||||
pressed = backend.window.Pressed(pixelgl.Button(button))
|
||||
return
|
||||
}
|
||||
|
||||
// Repeated fulfills the Repeated method of the Backend interface.
|
||||
func (backend *Backend) Repeated (button stone.Button) (repeated bool) {
|
||||
repeated = backend.window.Repeated(pixelgl.Button(button))
|
||||
return
|
||||
}
|
||||
|
||||
// Typed fulfills the Typed method of the Backend interface.
|
||||
func (backend *Backend) Typed () (text string) {
|
||||
text = backend.window.Typed()
|
||||
return
|
||||
}
|
||||
|
||||
// Resized fulfills the Resized method of the Backend interface.
|
||||
func (backend *Backend) Resized () (resized bool) {
|
||||
resized = backend.boundsDirty
|
||||
return
|
||||
}
|
||||
|
||||
// MousePosition fulfills the MousePosition method of the Backend interface.
|
||||
func (backend *Backend) MousePosition () (x, y int) {
|
||||
vector := backend.window.MousePosition()
|
||||
x = int (
|
||||
@ -145,6 +159,7 @@ func (backend *Backend) MousePosition () (x, y int) {
|
||||
return
|
||||
}
|
||||
|
||||
// draw renders updates to the screen.
|
||||
func (backend *Backend) draw () {
|
||||
// didDrawing := false
|
||||
width, height := backend.application.Size()
|
||||
@ -231,6 +246,8 @@ func (backend *Backend) draw () {
|
||||
backend.window.SwapBuffers()
|
||||
}
|
||||
|
||||
// processEvents reacts to events recieved from pixel, resizing and
|
||||
// recalculating things as need be.
|
||||
func (backend *Backend) processEvents () {
|
||||
newBounds := backend.window.Bounds().Max
|
||||
backend.boundsDirty = backend.windowBounds != newBounds
|
||||
@ -260,6 +277,8 @@ func (backend *Backend) processEvents () {
|
||||
}
|
||||
}
|
||||
|
||||
// vectorAtPosition generates a pixel vector at the top left of the specified
|
||||
// cell.
|
||||
func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) {
|
||||
vector = pixel.V (
|
||||
float64 (
|
||||
@ -271,15 +290,22 @@ func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) {
|
||||
return
|
||||
}
|
||||
|
||||
// calculateWindowSize calculates window bounds based on the internal buffer
|
||||
// size.
|
||||
func (backend *Backend) calculateWindowSize () (bounds pixel.Rect) {
|
||||
width, height := backend.application.Size()
|
||||
bounds = pixel.R (
|
||||
0, 0,
|
||||
float64(width * backend.metrics.cellWidth),
|
||||
float64(height * backend.metrics.cellHeight))
|
||||
float64 (
|
||||
width * backend.metrics.cellWidth +
|
||||
backend.metrics.padding * 2),
|
||||
float64 (
|
||||
height * backend.metrics.cellHeight +
|
||||
backend.metrics.padding * 2))
|
||||
return
|
||||
}
|
||||
|
||||
// factory instantiates a pixel backend.
|
||||
func factory (application *stone.Application) (output stone.Backend, err error) {
|
||||
backend := &Backend {
|
||||
application: application,
|
||||
@ -289,6 +315,7 @@ func factory (application *stone.Application) (output stone.Backend, err error)
|
||||
return
|
||||
}
|
||||
|
||||
// init registers this backend when the program starts.
|
||||
func init () {
|
||||
stone.RegisterBackend(factory)
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package stone
|
||||
|
||||
import "image/color"
|
||||
|
||||
// Config stores configuration parameters. Backends only should honor parameters
|
||||
// that they can support.
|
||||
type Config struct {
|
||||
colors [4]color.Color
|
||||
padding int
|
||||
@ -9,21 +11,26 @@ type Config struct {
|
||||
fontName string
|
||||
}
|
||||
|
||||
// Color returns the color value at the specified index.
|
||||
func (config *Config) Color (index Color) (value color.Color) {
|
||||
value = config.colors[index]
|
||||
return
|
||||
}
|
||||
|
||||
// Padding specifies how many cell's worth of padding should be on all sides of
|
||||
// the buffer.
|
||||
func (config *Config) Padding () (padding int) {
|
||||
padding = config.padding
|
||||
return
|
||||
}
|
||||
|
||||
// FontSize specifies how big the font should be.
|
||||
func (config *Config) FontSize () (fontSize int) {
|
||||
fontSize = config.fontSize
|
||||
return
|
||||
}
|
||||
|
||||
// FontName specifies the name of the font to use.
|
||||
func (config *Config) FontName () (fontName string) {
|
||||
fontName = config.fontName
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user