stone/backends/pixel/pixel.go

322 lines
9.2 KiB
Go
Raw Normal View History

2022-10-31 13:51:28 -06:00
package pixel
import "time"
import "golang.org/x/image/font"
import "github.com/faiface/pixel"
2022-11-05 16:43:57 -06:00
import "github.com/faiface/pixel/text"
2022-11-05 16:08:00 -06:00
import "github.com/faiface/pixel/imdraw"
2022-10-31 13:51:28 -06:00
import "github.com/faiface/pixel/pixelgl"
import "golang.org/x/image/font/basicfont"
import "git.tebibyte.media/sashakoshka/stone"
2022-11-08 23:01:13 -07:00
// Backend represents an instance of the pixel backend
2022-10-31 13:51:28 -06:00
type Backend struct {
2022-11-05 16:08:00 -06:00
window *pixelgl.Window
boundsDirty bool
2022-11-05 16:56:56 -06:00
windowBounds pixel.Vec
2022-11-05 16:08:00 -06:00
fontFace font.Face
application *stone.Application
config *stone.Config
backgroundStamper *imdraw.IMDraw
2022-11-05 16:43:57 -06:00
fontAtlas *text.Atlas
textDrawer *text.Text
showBounds bool
showCellBounds bool
2022-10-31 13:51:28 -06:00
metrics struct {
cellWidth int
cellHeight int
padding int
paddingX int
paddingY int
descent int
2022-10-31 13:51:28 -06:00
}
}
2022-11-08 23:01:13 -07:00
// Run satisfies the Run method of the Backend interface. Due to the nature of
// pixel, this will forcibly bind to the main thread.
2022-10-31 13:51:28 -06:00
func (backend *Backend) Run (callback func (application *stone.Application)) {
// backend.showBounds = true
2022-11-08 23:01:13 -07:00
// backend.showCellBounds = true
2022-10-31 13:51:28 -06:00
if backend.fontFace == nil {
backend.fontFace = basicfont.Face7x13
}
backend.backgroundStamper = imdraw.New(nil)
backend.fontAtlas = text.NewAtlas(backend.fontFace, text.ASCII)
backend.textDrawer = text.New(pixel.V(0, 0), backend.fontAtlas)
backend.metrics.descent = int(backend.fontAtlas.Descent())
backend.metrics.cellHeight = int(backend.fontAtlas.LineHeight())
2022-10-31 13:51:28 -06:00
// FIXME?: this might not be the best way to get the cell width
faceAdvance, ok := backend.fontFace.GlyphAdvance('M')
if ok {
backend.metrics.cellWidth = faceAdvance.Round()
} else {
backend.metrics.cellWidth = backend.metrics.cellHeight / 2
}
pixelgl.Run (func () {
2022-11-05 16:08:00 -06:00
// construct the window, and all that
2022-10-31 13:51:28 -06:00
var err error
backend.window, err = pixelgl.NewWindow (pixelgl.WindowConfig {
Resizable: true,
Undecorated: true,
VSync: true,
NoIconify: true,
Title: backend.application.Title(),
Bounds: backend.calculateWindowSize(),
})
backend.Poll()
2022-11-08 23:01:13 -07:00
// TODO: this should return the error and not panic
2022-10-31 13:51:28 -06:00
if err != nil { panic(err.Error()) }
callback(backend.application)
})
}
2022-11-08 23:01:13 -07:00
// Await fulfills the Await method of the Backend interface.
2022-10-31 13:51:28 -06:00
func (backend *Backend) Await (timeout time.Duration) (keepRunning bool) {
if backend.window == nil {
panic("call to Backend.Await before window exists")
}
backend.draw()
backend.window.UpdateInputWait(timeout)
backend.processEvents()
keepRunning = !backend.window.Closed()
return
}
2022-11-08 23:01:13 -07:00
// Poll fulfills the Poll method of the Backend interface.
2022-10-31 13:51:28 -06:00
func (backend *Backend) Poll () (keepRunning bool) {
if backend.window == nil {
panic("call to Backend.Poll before window exists")
}
backend.draw()
backend.window.UpdateInput()
backend.processEvents()
keepRunning = !backend.window.Closed()
return
}
2022-11-08 23:01:13 -07:00
// SetTitle fulfills the SetTitle method of the Backend interface.
2022-10-31 13:51:28 -06:00
func (backend *Backend) SetTitle (title string) {
if backend.window != nil {
backend.window.SetTitle(title)
}
}
2022-11-08 23:01:13 -07:00
// JustPressed fulfills the JustPressed method of the Backend interface.
2022-11-02 13:14:59 -06:00
func (backend *Backend) JustPressed (button stone.Button) (pressed bool) {
pressed = backend.window.JustPressed(pixelgl.Button(button))
return
}
2022-11-08 23:01:13 -07:00
// JustReleased fulfills the JustReleased method of the Backend interface.
2022-11-02 13:14:59 -06:00
func (backend *Backend) JustReleased (button stone.Button) (released bool) {
released = backend.window.JustReleased(pixelgl.Button(button))
return
}
2022-11-08 23:01:13 -07:00
// Pressed fulfills the Pressed method of the Backend interface.
2022-11-02 13:14:59 -06:00
func (backend *Backend) Pressed (button stone.Button) (pressed bool) {
pressed = backend.window.Pressed(pixelgl.Button(button))
return
}
2022-11-08 23:01:13 -07:00
// Repeated fulfills the Repeated method of the Backend interface.
2022-11-02 13:14:59 -06:00
func (backend *Backend) Repeated (button stone.Button) (repeated bool) {
repeated = backend.window.Repeated(pixelgl.Button(button))
return
}
2022-11-08 23:01:13 -07:00
// Typed fulfills the Typed method of the Backend interface.
2022-11-02 13:14:59 -06:00
func (backend *Backend) Typed () (text string) {
text = backend.window.Typed()
return
}
2022-11-08 23:01:13 -07:00
// Resized fulfills the Resized method of the Backend interface.
2022-11-02 13:14:59 -06:00
func (backend *Backend) Resized () (resized bool) {
resized = backend.boundsDirty
return
}
2022-11-08 23:01:13 -07:00
// MousePosition fulfills the MousePosition method of the Backend interface.
2022-11-06 13:59:06 -07:00
func (backend *Backend) MousePosition () (x, y int) {
vector := backend.window.MousePosition()
x = int (
(vector.X - float64(backend.metrics.paddingX)) /
float64(backend.metrics.cellWidth))
y = int (
(backend.windowBounds.Y -
vector.Y -
float64(backend.metrics.paddingY)) /
float64(backend.metrics.cellHeight))
return
}
2022-11-08 23:01:13 -07:00
// draw renders updates to the screen.
2022-10-31 13:51:28 -06:00
func (backend *Backend) draw () {
2022-11-02 16:51:33 -06:00
// didDrawing := false
2022-11-05 16:08:00 -06:00
width, height := backend.application.Size()
2022-10-31 13:51:28 -06:00
if backend.boundsDirty {
backend.window.Clear (
backend.config.Color(stone.ColorApplication))
backend.boundsDirty = false
2022-11-02 16:51:33 -06:00
// didDrawing = true
2022-10-31 13:51:28 -06:00
} else {
2022-11-05 16:08:00 -06:00
// clear out dirty cells before drawing them. this is in an else
// block because if the bounds were dirty, we have already
// cleared the entire screen.
backend.backgroundStamper.Clear()
backend.backgroundStamper.Color =
backend.config.Color(stone.ColorApplication)
for x := 0; x < width; x ++ {
for y := 0; y < height; y ++ {
clean := backend.application.Clean(x, y)
if clean { continue }
backend.backgroundStamper.Push (
backend.vectorAtPosition(x, y),
backend.vectorAtPosition(x + 1, y + 1))
2022-11-06 12:44:09 -07:00
backend.backgroundStamper.Rectangle(0)
2022-11-05 16:08:00 -06:00
// didDrawing = true
if backend.showCellBounds {
backend.backgroundStamper.Color =
backend.config.Color(stone.ColorForeground)
backend.backgroundStamper.Push (
backend.vectorAtPosition(x, y),
backend.vectorAtPosition(x + 1, y + 1))
backend.backgroundStamper.Rectangle(1)
backend.backgroundStamper.Color =
backend.config.Color(stone.ColorApplication)
}
2022-11-05 16:08:00 -06:00
}
}
backend.backgroundStamper.Draw(backend.window)
2022-10-31 13:51:28 -06:00
}
2022-11-05 16:43:57 -06:00
backend.textDrawer.Clear()
backend.textDrawer.Color =
backend.config.Color(stone.ColorForeground)
2022-10-31 13:51:28 -06:00
for x := 0; x < width; x ++ {
for y := 0; y < height; y ++ {
clean := backend.application.Clean(x, y)
if clean { continue }
2022-11-05 16:08:00 -06:00
backend.application.MarkClean(x, y)
2022-10-31 13:51:28 -06:00
2022-11-02 16:51:33 -06:00
cell := backend.application.Cell(x, y)
content := cell.Rune()
if content < 32 { continue }
2022-10-31 13:51:28 -06:00
// draw cell
backend.textDrawer.Dot = pixel.V (
float64 (
x * backend.metrics.cellWidth +
backend.metrics.paddingX),
backend.windowBounds.Y - float64 (
(y + 1) * backend.metrics.cellHeight +
backend.metrics.paddingY -
backend.metrics.descent))
2022-11-05 16:43:57 -06:00
backend.textDrawer.WriteRune(content)
backend.textDrawer.Draw(backend.window, pixel.IM)
2022-10-31 13:51:28 -06:00
}
}
// draw a rectangle around the buffer if we are showing bounds
if backend.showBounds {
backend.backgroundStamper.Clear()
backend.backgroundStamper.Color =
backend.config.Color(stone.ColorBackground)
backend.backgroundStamper.Push (
backend.vectorAtPosition(0, 0),
backend.vectorAtPosition(width, height))
backend.backgroundStamper.Rectangle(1)
backend.backgroundStamper.Draw(backend.window)
}
2022-10-31 13:51:28 -06:00
2022-11-02 16:51:33 -06:00
backend.window.SwapBuffers()
2022-10-31 13:51:28 -06:00
}
2022-11-08 23:01:13 -07:00
// processEvents reacts to events recieved from pixel, resizing and
// recalculating things as need be.
2022-10-31 13:51:28 -06:00
func (backend *Backend) processEvents () {
2022-11-02 16:51:33 -06:00
newBounds := backend.window.Bounds().Max
2022-11-05 16:56:56 -06:00
backend.boundsDirty = backend.windowBounds != newBounds
backend.windowBounds = newBounds
2022-10-31 13:51:28 -06:00
2022-11-02 16:51:33 -06:00
if backend.boundsDirty {
// calculate padding
backend.metrics.padding =
backend.config.Padding() *
backend.metrics.cellWidth
deadArea := float64(backend.metrics.padding * 2)
// calculate new width and height for buffer
width := int (
(backend.windowBounds.X - deadArea) /
float64(backend.metrics.cellWidth))
height := int (
(backend.windowBounds.Y - deadArea) /
float64(backend.metrics.cellHeight))
backend.application.SetSize(width, height)
// position buffer in center of screen
frameWidth := width * backend.metrics.cellWidth
frameHeight := height * backend.metrics.cellHeight
backend.metrics.paddingX = (int(backend.windowBounds.X) - frameWidth) / 2
backend.metrics.paddingY = (int(backend.windowBounds.Y) - frameHeight) / 2
2022-11-02 16:51:33 -06:00
}
2022-10-31 13:51:28 -06:00
}
2022-11-08 23:01:13 -07:00
// vectorAtPosition generates a pixel vector at the top left of the specified
// cell.
2022-11-05 16:08:00 -06:00
func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) {
vector = pixel.V (
float64 (
x * backend.metrics.cellWidth +
backend.metrics.paddingX),
2022-11-05 16:56:56 -06:00
backend.windowBounds.Y - float64 (
y * backend.metrics.cellHeight +
backend.metrics.paddingY))
2022-11-05 16:08:00 -06:00
return
}
2022-11-08 23:01:13 -07:00
// calculateWindowSize calculates window bounds based on the internal buffer
// size.
2022-10-31 13:51:28 -06:00
func (backend *Backend) calculateWindowSize () (bounds pixel.Rect) {
width, height := backend.application.Size()
bounds = pixel.R (
0, 0,
2022-11-08 23:01:13 -07:00
float64 (
width * backend.metrics.cellWidth +
backend.metrics.padding * 2),
float64 (
height * backend.metrics.cellHeight +
backend.metrics.padding * 2))
2022-10-31 13:51:28 -06:00
return
}
2022-11-08 23:01:13 -07:00
// factory instantiates a pixel backend.
2022-10-31 13:51:28 -06:00
func factory (application *stone.Application) (output stone.Backend, err error) {
backend := &Backend {
application: application,
config: application.Config(),
}
output = backend
return
}
2022-11-08 23:01:13 -07:00
// init registers this backend when the program starts.
2022-10-31 13:51:28 -06:00
func init () {
stone.RegisterBackend(factory)
}