Merge pull request '(Mostly) under the hoood improvements' (#3) from fix-x-concurrency into main

Reviewed-on: #3
This commit is contained in:
Sasha Koshka 2022-11-17 18:33:36 +00:00
commit 19b744250f
12 changed files with 396 additions and 322 deletions

View File

@ -10,12 +10,12 @@ type Application struct {
icons []image.Image
backend Backend
config Config
callbackManager CallbackManager
}
// 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 () (
channel chan(Event),
err error,
) {
// default values for certain parameters
@ -29,12 +29,46 @@ func (application *Application) Run () (
application.backend, err = instantiateBackend(application)
if err != nil { return }
channel = make(chan(Event))
go application.backend.Run(channel)
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) OnStart (
onStart func (),
) {
application.callbackManager.onStart = onStart
}
// Draw "commits" changes made in the buffer to the display.
func (application *Application) Draw () {
application.backend.Draw()

View File

@ -4,13 +4,19 @@ import "image"
import "errors"
type Backend interface {
Run (channel chan(Event))
Run ()
SetTitle (title string) (err error)
SetIcon (icons []image.Image) (err error)
Draw ()
}
type BackendFactory func (application *Application) (backend Backend, err error)
type BackendFactory func (
application *Application,
callbackManager *CallbackManager,
) (
backend Backend,
err error,
)
var factories []BackendFactory
@ -21,7 +27,7 @@ func RegisterBackend (factory BackendFactory) {
func instantiateBackend (application *Application) (backend Backend, err error) {
// find a suitable backend
for _, factory := range factories {
backend, err = factory(application)
backend, err = factory(application, &application.callbackManager)
if err == nil && backend != nil { return }
}

View File

@ -3,55 +3,113 @@ package x
import "image"
import "image/draw"
import "golang.org/x/image/math/fixed"
import "github.com/jezek/xgbutil/xgraphics"
import "git.tebibyte.media/sashakoshka/stone"
func (backend *Backend) Draw () {
backend.drawLock.Lock()
defer backend.drawLock.Unlock()
backend.lock.Lock()
defer backend.lock.Unlock()
boundsChanged :=
backend.memory.windowWidth != backend.metrics.windowWidth ||
backend.memory.windowHeight != backend.metrics.windowHeight
backend.memory.windowWidth = backend.metrics.windowWidth
backend.memory.windowHeight = backend.metrics.windowHeight
if boundsChanged {
if backend.windowBoundsClean {
backend.canvas.XPaintRects (
backend.window.Id,
backend.drawCells(false)...)
} else {
backend.reallocateCanvas()
backend.drawCells(true)
backend.canvas.XDraw()
backend.canvas.XPaint(backend.window.Id)
} else {
backend.updateWindowAreas(backend.drawCells(false)...)
backend.windowBoundsClean = true
}
}
func (backend *Backend) updateWindowAreas (areas ...image.Rectangle) {
backend.canvas.XPaintRects(backend.window.Id, areas...)
func (backend *Backend) reallocateCanvas () {
if backend.canvas != nil {
backend.canvas.Destroy()
}
backend.canvas = xgraphics.New (
backend.connection,
image.Rect (
0, 0,
backend.metrics.windowWidth,
backend.metrics.windowHeight))
backend.canvas.For (func (x, y int) xgraphics.BGRA {
return backend.colors[stone.ColorBackground]
})
backend.canvas.XSurfaceSet(backend.window.Id)
}
func (backend *Backend) drawRune (x, y int, character rune, runeColor stone.Color) {
func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
width, height := backend.application.Size()
for y := 0; y < height; y ++ {
for x := 0; x < width; x ++ {
if !forceRedraw && backend.application.Clean(x, y) { continue }
cell := backend.application.GetForRendering(x, y)
content := cell.Rune()
if forceRedraw && content < 32 { continue }
areas = append(areas, backend.boundsOfCell(x, y))
backend.drawRune(x, y, content, cell.Color(), !forceRedraw)
}}
if backend.drawBufferBounds && forceRedraw {
strokeRectangle (
&image.Uniform {
C: backend.config.Color(stone.ColorForeground),
},
backend.canvas,
image.Rectangle {
Min: backend.originOfCell(0, 0),
Max: backend.originOfCell(width, height),
})
}
return
}
func (backend *Backend) drawRune (
x, y int,
character rune,
runeColor stone.Color,
drawBackground bool,
) {
// TODO: cache these draws as non-transparent buffers with the
// application background color as the background. that way, we won't
// need to redraw the characters *or* composite them.
fillRectangle (
&image.Uniform {
C: backend.config.Color(stone.ColorBackground),
},
backend.canvas,
backend.boundsOfCell(x, y))
if drawBackground {
fillRectangle (
&image.Uniform {
C: backend.config.Color(stone.ColorBackground),
},
backend.canvas,
backend.boundsOfCell(x, y))
}
if character < 32 { return }
origin := backend.originOfCell(x, y + 1)
destinationRectangle, mask, maskPoint, _, _ := backend.font.face.Glyph (
destinationRectangle, mask, maskPoint, _, ok := backend.font.face.Glyph (
fixed.Point26_6 {
X: fixed.I(origin.X),
Y: fixed.I(origin.Y - backend.metrics.descent),
},
character)
if !ok {
println("warning")
strokeRectangle (
&image.Uniform {
C: backend.config.Color(stone.ColorForeground),
},
backend.canvas,
backend.boundsOfCell(x, y))
return
}
if backend.drawCellBounds {
strokeRectangle (
&image.Uniform {
@ -73,36 +131,6 @@ func (backend *Backend) drawRune (x, y int, character rune, runeColor stone.Colo
draw.Over)
}
func (backend *Backend) drawCells (forceRedraw bool) (areas []image.Rectangle) {
width, height := backend.application.Size()
for y := 0; y < height; y ++ {
for x := 0; x < width; x ++ {
if !forceRedraw && backend.application.Clean(x, y) { continue }
backend.application.MarkClean(x, y)
cell := backend.application.Cell(x, y)
content := cell.Rune()
if forceRedraw && content < 32 { continue }
areas = append(areas, backend.boundsOfCell(x, y))
backend.drawRune(x, y, content, cell.Color())
}}
if backend.drawBufferBounds && forceRedraw {
strokeRectangle (
&image.Uniform {
C: backend.config.Color(stone.ColorForeground),
},
backend.canvas,
image.Rectangle {
Min: backend.originOfCell(0, 0),
Max: backend.originOfCell(width, height),
})
}
return
}
func fillRectangle (
source image.Image,
destination draw.Image,

View File

@ -8,17 +8,19 @@ import "github.com/jezek/xgbutil/xevent"
import "git.tebibyte.media/sashakoshka/stone"
func (backend *Backend) Run (channel chan(stone.Event)) {
backend.channel = channel
func (backend *Backend) Run () {
backend.callbackManager.RunStart()
backend.Draw()
xevent.Main(backend.connection)
backend.shutDown()
backend.callbackManager.RunQuit()
}
func (backend *Backend) handleConfigureNotify (
connection *xgbutil.XUtil,
event xevent.ConfigureNotifyEvent,
) {
backend.lock.Lock()
configureEvent := *event.ConfigureNotifyEvent
newWidth := int(configureEvent.Width)
@ -33,23 +35,31 @@ func (backend *Backend) handleConfigureNotify (
configureEvent =
backend.compressConfigureNotify(configureEvent)
// we should not resize the canvas while drawing is taking place
backend.drawLock.Lock()
defer backend.drawLock.Unlock()
// resize buffer
width, height := backend.calculateBufferSize()
backend.application.SetSize(width, height)
// position buffer in the center of the screen
frameWidth := width * backend.metrics.cellWidth
frameHeight := height * backend.metrics.cellHeight
backend.metrics.paddingX =
(backend.metrics.windowWidth - frameWidth) / 2
backend.metrics.paddingY =
(backend.metrics.windowHeight - frameHeight) / 2
backend.channel <- stone.EventResize { }
if backend.config.Center() {
// position buffer in the center of the screen
frameWidth := width * backend.metrics.cellWidth
frameHeight := height * backend.metrics.cellHeight
backend.metrics.paddingX =
(backend.metrics.windowWidth - frameWidth) / 2
backend.metrics.paddingY =
(backend.metrics.windowHeight - frameHeight) / 2
} else {
backend.metrics.paddingX = backend.metrics.padding
backend.metrics.paddingY = backend.metrics.padding
}
backend.windowBoundsClean = false
}
backend.lock.Unlock()
if sizeChanged {
backend.callbackManager.RunResize()
backend.Draw()
}
}
@ -58,9 +68,7 @@ func (backend *Backend) handleButtonPress (
event xevent.ButtonPressEvent,
) {
buttonEvent := *event.ButtonPressEvent
backend.channel <- stone.EventPress {
Button: stone.Button(buttonEvent.Detail + 127),
}
backend.callbackManager.RunPress(stone.Button(buttonEvent.Detail + 127))
}
func (backend *Backend) handleButtonRelease (
@ -68,9 +76,7 @@ func (backend *Backend) handleButtonRelease (
event xevent.ButtonReleaseEvent,
) {
buttonEvent := *event.ButtonReleaseEvent
backend.channel <- stone.EventRelease {
Button: stone.Button(buttonEvent.Detail + 127),
}
backend.callbackManager.RunRelease(stone.Button(buttonEvent.Detail + 127))
}
func (backend *Backend) handleKeyPress (
@ -79,7 +85,7 @@ func (backend *Backend) handleKeyPress (
) {
keyEvent := *event.KeyPressEvent
button := backend.keycodeToButton(keyEvent.Detail, keyEvent.State)
backend.channel <- stone.EventPress { Button: button }
backend.callbackManager.RunPress(button)
}
func (backend *Backend) handleKeyRelease (
@ -88,7 +94,7 @@ func (backend *Backend) handleKeyRelease (
) {
keyEvent := *event.KeyReleaseEvent
button := backend.keycodeToButton(keyEvent.Detail, keyEvent.State)
backend.channel <- stone.EventRelease { Button: button }
backend.callbackManager.RunRelease(button)
}
func (backend *Backend) handleMotionNotify (
@ -100,10 +106,7 @@ func (backend *Backend) handleMotionNotify (
X: int(motionEvent.EventX),
Y: int(motionEvent.EventY),
})
backend.channel <- stone.EventMouseMove {
X: x,
Y: y,
}
backend.callbackManager.RunMouseMove(x, y)
}
func (backend *Backend) compressConfigureNotify (
@ -129,7 +132,3 @@ func (backend *Backend) compressConfigureNotify (
return
}
func (backend *Backend) shutDown () {
backend.channel <- stone.EventQuit { }
}

View File

@ -18,10 +18,17 @@ import "git.tebibyte.media/sashakoshka/stone"
import "github.com/flopp/go-findfont"
// factory instantiates an X backend.
func factory (application *stone.Application) (output stone.Backend, err error) {
func factory (
application *stone.Application,
callbackManager *stone.CallbackManager,
) (
output stone.Backend,
err error,
) {
backend := &Backend {
application: application,
config: application.Config(),
application: application,
config: application.Config(),
callbackManager: callbackManager,
}
// load font
@ -76,8 +83,6 @@ func factory (application *stone.Application) (output stone.Backend, err error)
backend.metrics.windowWidth, backend.metrics.windowHeight,
0)
backend.window.Map()
// TODO: also listen to mouse movement (compressed) and mouse and
// keyboard buttons (uncompressed)
err = backend.window.Listen (
xproto.EventMaskStructureNotify,
xproto.EventMaskPointerMotion,
@ -110,7 +115,7 @@ func factory (application *stone.Application) (output stone.Backend, err error)
// attatch graceful close handler
backend.window.WMGracefulClose (func (window *xwindow.Window) {
backend.window.Destroy()
backend.shutDown()
xevent.Quit(backend.connection)
})
// attatch event handlers

View File

@ -14,17 +14,17 @@ import "github.com/jezek/xgbutil/xgraphics"
import "git.tebibyte.media/sashakoshka/stone"
type Backend struct {
application *stone.Application
config *stone.Config
connection *xgbutil.XUtil
window *xwindow.Window
canvas *xgraphics.Image
channel chan(stone.Event)
application *stone.Application
config *stone.Config
callbackManager *stone.CallbackManager
connection *xgbutil.XUtil
window *xwindow.Window
canvas *xgraphics.Image
drawCellBounds bool
drawBufferBounds bool
drawLock sync.Mutex
lock sync.Mutex
font struct {
face font.Face
@ -43,10 +43,7 @@ type Backend struct {
descent int
}
memory struct {
windowWidth int
windowHeight int
}
windowBoundsClean bool
}
func (backend *Backend) SetTitle (title string) (err error) {
@ -114,23 +111,6 @@ func (backend *Backend) calculateBufferSize () (width, height int) {
return
}
func (backend *Backend) reallocateCanvas () {
if backend.canvas != nil {
backend.canvas.Destroy()
}
backend.canvas = xgraphics.New (
backend.connection,
image.Rect (
0, 0,
backend.metrics.windowWidth,
backend.metrics.windowHeight))
backend.canvas.For (func (x, y int) xgraphics.BGRA {
return backend.colors[stone.ColorBackground]
})
backend.canvas.XSurfaceSet(backend.window.Id)
}
func (backend *Backend) cellAt (onScreen image.Point) (x, y int) {
x = (onScreen.X - backend.metrics.paddingX) / backend.metrics.cellWidth
y = (onScreen.Y - backend.metrics.paddingY) / backend.metrics.cellHeight

206
buffer.go
View File

@ -1,5 +1,7 @@
package stone
import "sync"
// Color represents all the different colors a cell can be.
type Color uint8
@ -22,6 +24,8 @@ const (
StyleNormal Style = iota
StyleBold Style = iota >> 1
StyleItalic
StyleUnderline
StyleHighlight
StyleBoldItalic Style = StyleBold | StyleItalic
)
@ -51,18 +55,38 @@ func (cell Cell) Rune () (content rune) {
return
}
// Buffer is a basic grid of cells.
type Buffer struct {
content []Cell
width int
height int
Dot struct {
X int
Y int
}
// Buffer represents a two dimensional text buffer.
type Buffer interface {
Size () (with, height int)
Cell (x, y int) (cell Cell)
SetColor (x, y int, color Color)
SetSize (with, height int)
SetStyle (x, y int, style Style)
SetRune (x, y int, content rune)
Clear ()
}
func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) {
// DamageBuffer is a two dimensional text buffer that stores a grid of cells, as
// well as information stating whether each cell is clean or dirty. Cells are
// dirty by default, are only clean when marked as clean, and become dirty again
// when they are altered in some way.
type DamageBuffer struct {
content []Cell
onScreen []Cell
width int
height int
dot struct {
x int
y int
}
// This should be write locked when resizing the buffer, and read locked
// when writing to cells or reading information about the buffer.
lock sync.RWMutex
}
func (buffer *DamageBuffer) isOutOfBounds (x, y int) (outOfBounds bool) {
outOfBounds =
x < 0 ||
y < 0 ||
@ -72,122 +96,105 @@ func (buffer *Buffer) isOutOfBounds (x, y int) (outOfBounds bool) {
}
// Size returns the width and height of the buffer.
func (buffer *Buffer) Size () (width, height int) {
func (buffer *DamageBuffer) Size () (width, height int) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
width = buffer.width
height = buffer.height
return
}
// SetSize sets the width and height of the buffer. This clears all data in the
// buffer. If the width or height is negative, this method does nothing.
func (buffer *Buffer) SetSize (width, height int) {
if width < 0 || height < 0 { return }
buffer.width = width
buffer.height = height
buffer.content = make([]Cell, width * height)
for index := 0; index < len(buffer.content); index ++ {
buffer.content[index].color = ColorForeground
}
// SetDot sets the buffer's text insertion position relative to the buffer
// origin point (0, 0).
func (buffer *DamageBuffer) SetDot (x, y int) {
buffer.dot.x = x
buffer.dot.y = y
}
// Cell returns the cell at the specified x and y coordinates. If the
// coordinates are out of bounds, this method will return a blank cell.
func (buffer *Buffer) Cell (x, y int) (cell Cell) {
func (buffer *DamageBuffer) Cell (x, y int) (cell Cell) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
if buffer.isOutOfBounds(x, y) { return }
cell = buffer.content[x + y * buffer.width]
return
}
// SetColor sets the color of the cell at the specified x and y coordinates.
func (buffer *Buffer) SetColor (x, y int, color Color) {
func (buffer *DamageBuffer) SetColor (x, y int, color Color) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
if buffer.isOutOfBounds(x, y) { return }
buffer.content[x + y * buffer.width].color = color
}
// SetSize sets the width and height of the buffer. This clears all data in the
// buffer. If the width or height is negative, this method does nothing.
func (buffer *DamageBuffer) SetSize (width, height int) {
buffer.lock.Lock()
defer buffer.lock.Unlock()
if width < 0 || height < 0 { return }
buffer.width = width
buffer.height = height
buffer.content = make([]Cell, width * height)
buffer.onScreen = make([]Cell, width * height)
for index := 0; index < len(buffer.content); index ++ {
buffer.content[index].color = ColorForeground
}
}
// SetStyle sets the style of the cell at the specified x and y coordinates.
func (buffer *Buffer) SetStyle (x, y int, style Style) {
func (buffer *DamageBuffer) SetStyle (x, y int, style Style) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
if buffer.isOutOfBounds(x, y) { return }
buffer.content[x + y * buffer.width].style = style
}
// SetRune sets the rune of the cell at the specified x and y coordinates.
func (buffer *Buffer) SetRune (x, y int, content rune) {
func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
buffer.setRune(x, y, content)
}
// Clear resets the entire buffer.
func (buffer *DamageBuffer) Clear () {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
for index := 0; index < len(buffer.content); index ++ {
buffer.content[index] = Cell {
color: ColorForeground,
}
}
}
func (buffer *DamageBuffer) setRune (x, y int, content rune) {
if buffer.isOutOfBounds(x, y) { return }
buffer.content[x + y * buffer.width].content = content
}
// Write writes data stored in a byte slice to the buffer at the current dot
// position. This makes Buffer an io.Writer.
func (buffer *Buffer) Write (bytes []byte) (bytesWritten int, err error) {
text := string(bytes)
bytesWritten = len(bytes)
for _, character := range text {
buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character)
buffer.Dot.X ++
if buffer.Dot.X > buffer.width { break }
}
return
}
// ResetDot is a convenience method to reset the dot to the buffer origin point
// (0, 0).
func (buffer *Buffer) ResetDot () {
buffer.Dot.X = 0
buffer.Dot.Y = 0
}
// DamageBuffer is a special buffer that keeps track of damage information.
// Cells are dirty by default, are only clean when marked as clean, and become
// dirty again when they are altered in some way.
type DamageBuffer struct {
Buffer
clean []bool
}
// SetSize sets the width and height of the buffer. This clears all data in the
// buffer. If the width or height is negative, this method does nothing.
func (buffer *DamageBuffer) SetSize (width, height int) {
if width < 0 || height < 0 { return }
buffer.Buffer.SetSize(width, height)
buffer.clean = make([]bool, width * height)
}
// SetColor sets the color of the cell at the specified x and y coordinates.
func (buffer *DamageBuffer) SetColor (x, y int, color Color) {
if buffer.isOutOfBounds(x, y) { return }
index := x + y * buffer.width
buffer.clean[index] = buffer.content[index].color == color
buffer.Buffer.SetColor(x, y, color)
}
// SetStyle sets the style of the cell at the specified x and y coordinates.
func (buffer *DamageBuffer) SetStyle (x, y int, style Style) {
if buffer.isOutOfBounds(x, y) { return }
index := x + y * buffer.width
buffer.clean[index] = buffer.content[index].style == style
buffer.Buffer.SetStyle(x, y, style)
}
// SetRune sets the rune of the cell at the specified x and y coordinates.
func (buffer *DamageBuffer) SetRune (x, y int, content rune) {
if buffer.isOutOfBounds(x, y) { return }
index := x + y * buffer.width
buffer.clean[index] = buffer.content[index].content == content
buffer.Buffer.SetRune(x, y, content)
}
// Write writes data stored in a byte slice to the buffer at the current dot
// position. This makes DamageBuffer an io.Writer.
func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
text := string(bytes)
bytesWritten = len(bytes)
for _, character := range text {
buffer.SetRune(buffer.Dot.X, buffer.Dot.Y, character)
buffer.Dot.X ++
if buffer.Dot.X > buffer.width { break }
buffer.setRune(buffer.dot.x, buffer.dot.y, character)
buffer.dot.x ++
if buffer.dot.x > buffer.width { break }
}
return
@ -196,13 +203,24 @@ func (buffer *DamageBuffer) Write (bytes []byte) (bytesWritten int, err error) {
// Clean returns whether or not the cell at the specified x and y coordinates is
// clean.
func (buffer *DamageBuffer) Clean (x, y int) (clean bool) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
if buffer.isOutOfBounds(x, y) { return }
clean = buffer.clean[x + y * buffer.width]
index := x + y * buffer.width
clean = buffer.content[index] == buffer.onScreen[index]
return
}
// MarkClean marks the cell at the specified x and y coordinates as clean.
func (buffer *DamageBuffer) MarkClean (x, y int) {
// GetForRendering returns the cell at the specified x and y coordinates and
// marks it as clean.
func (buffer *DamageBuffer) GetForRendering (x, y int) (cell Cell) {
buffer.lock.RLock()
defer buffer.lock.RUnlock()
if buffer.isOutOfBounds(x, y) { return }
buffer.clean[x + y * buffer.width] = true
index := x + y * buffer.width
buffer.onScreen[index] = buffer.content[index]
cell = buffer.content[index]
return
}

View File

@ -12,6 +12,7 @@ import "path/filepath"
type Config struct {
colors [8]color.Color
padding int
center bool
fontSize int
fontName string
}
@ -29,6 +30,13 @@ func (config *Config) Padding () (padding int) {
return
}
// Center returns whether the buffer should be displayed in the center of the
// window like in kitty, or aligned to one corner like in gnome-terminal.
func (config *Config) Center () (center bool) {
center = config.center
return
}
// FontSize specifies how big the font should be.
func (config *Config) FontSize () (fontSize int) {
fontSize = config.fontSize
@ -42,7 +50,6 @@ func (config *Config) FontName () (fontName string) {
}
func (config *Config) load () {
// TODO: load these from a file
config.colors = [8]color.Color {
// background
color.RGBA { R: 0, G: 0, B: 0, A: 0 },
@ -57,9 +64,9 @@ func (config *Config) load () {
// green
color.RGBA { R: 0x00, G: 0xFF, B: 0x00, A: 0xFF },
// blue
color.RGBA { R: 0x00, G: 0x00, B: 0xFF, A: 0xFF },
color.RGBA { R: 0x00, G: 0x80, B: 0xFF, A: 0xFF },
// purple
color.RGBA { R: 0x80, G: 0x00, B: 0xFF, A: 0xFF },
color.RGBA { R: 0x80, G: 0x40, B: 0xFF, A: 0xFF },
}
config.fontName = ""
config.fontSize = 11
@ -99,8 +106,14 @@ func (config *Config) loadFile (path string) {
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
var valueInt int
var valueColor color.Color
var valueBoolean bool
if value == "true" {
valueBoolean = true
}
if value[0] == '#' {
if len(value) != 7 {
@ -135,6 +148,8 @@ func (config *Config) loadFile (path string) {
config.fontSize = valueInt
case "padding":
config.padding = valueInt
case "center":
config.center = valueBoolean
case "colorBackground":
config.colors[ColorBackground] = valueColor
case "colorForeground":

View File

@ -1,27 +1,40 @@
package stone
// Event can be any event.
type Event interface { }
// EventQuit is sent when the backend shuts down due to a window close, error,
// or something else.
type EventQuit struct { }
// EventPress is sent when a button is pressed, or a key repeat event is
// triggered.
type EventPress struct { Button }
// Release is sent when a button is released.
type EventRelease struct { Button }
// Resize is sent when the application window is resized by the user. This event
// must be handled, as it implies that the buffer has been resized and therefore
// cleared. Application.Draw() must be called after this event is recieved.
type EventResize struct { }
// EventMouseMove is sent when the mouse changes position. It contains the X and
// Y position of the mouse.
type EventMouseMove struct {
X int
Y int
type CallbackManager struct {
onQuit func ()
onPress func (button Button)
onRelease func (button Button)
onResize func ()
onMouseMove func (x, y int)
onStart func ()
}
func (manager *CallbackManager) RunQuit () {
if manager.onQuit == nil { return }
manager.onQuit()
}
func (manager *CallbackManager) RunPress (button Button) {
if manager.onPress == nil { return }
manager.onPress(button)
}
func (manager *CallbackManager) RunRelease (button Button) {
if manager.onRelease == nil { return }
manager.onRelease(button)
}
func (manager *CallbackManager) RunResize () {
if manager.onResize == nil { return }
manager.onResize()
}
func (manager *CallbackManager) RunMouseMove (x, y int) {
if manager.onMouseMove == nil { return }
manager.onMouseMove(x, y)
}
func (manager *CallbackManager) RunStart () {
if manager.onStart == nil { return }
manager.onStart()
}

View File

@ -10,7 +10,7 @@ import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
var application = &stone.Application { }
func main () {
application.SetTitle("hellorld")
application.SetTitle("color demo")
application.SetSize(12, 7)
iconFile16, err := os.Open("assets/scaffold16.png")
@ -26,29 +26,26 @@ func main () {
application.SetIcon([]image.Image { icon16, icon32 })
channel, err := application.Run()
if err != nil { panic(err) }
application.OnStart(onStart)
application.OnResize(onResize)
err = application.Run()
if err != nil { panic(err) }
}
func onStart () {
redraw()
}
for {
event := <- channel
switch event.(type) {
case stone.EventQuit:
os.Exit(0)
case stone.EventResize:
redraw()
}
}
func onResize () {
redraw()
}
func redraw () {
text := "RAINBOW :D"
width, height := application.Size()
application.Dot.X = (width - len(text)) / 2
application.Dot.Y = height / 2
application.SetDot((width - len(text)) / 2, height / 2)
fmt.Fprintln(application, text)
application.SetColor(0, 0, stone.ColorYellow)
@ -70,6 +67,4 @@ func redraw () {
application.SetRune(x, height - 1, '=')
application.SetColor(x, height - 1, stone.ColorRed)
}
application.Draw()
}

View File

@ -10,7 +10,7 @@ var application = &stone.Application { }
var mousePressed bool
func main () {
application.SetTitle("hellorld")
application.SetTitle("drawing canvas")
application.SetSize(32, 16)
iconFile16, err := os.Open("assets/scaffold16.png")
@ -26,42 +26,32 @@ func main () {
application.SetIcon([]image.Image { icon16, icon32 })
channel, err := application.Run()
if err != nil { panic(err) }
application.OnPress(onPress)
application.OnRelease(onRelease)
application.OnMouseMove(onMouseMove)
application.Draw()
err = application.Run()
if err != nil { panic(err) }
}
for {
event := <- channel
switch event.(type) {
case stone.EventQuit:
os.Exit(0)
case stone.EventPress:
button := event.(stone.EventPress).Button
if button == stone.MouseButtonLeft {
mousePressed = true
application.SetRune(0, 0, '+')
application.Draw()
}
case stone.EventRelease:
button := event.(stone.EventRelease).Button
if button == stone.MouseButtonLeft {
mousePressed = false
application.SetRune(0, 0, 0)
application.Draw()
}
case stone.EventMouseMove:
event := event.(stone.EventMouseMove)
if mousePressed {
application.SetRune(event.X, event.Y, '#')
application.Draw()
}
case stone.EventResize:
application.Draw()
}
func onPress (button stone.Button) {
if button == stone.MouseButtonLeft {
mousePressed = true
application.SetRune(0, 0, '+')
application.Draw()
}
}
func onRelease (button stone.Button) {
if button == stone.MouseButtonLeft {
mousePressed = false
application.SetRune(0, 0, 0)
application.Draw()
}
}
func onMouseMove (x, y int) { if mousePressed {
application.SetRune(x, y, '#')
application.Draw()
}
}

View File

@ -10,7 +10,6 @@ import _ "git.tebibyte.media/sashakoshka/stone/backends/x"
var application = &stone.Application { }
var currentTime = time.Time { }
var tickPing = make(chan(struct { }))
func main () {
application.SetTitle("hellorld")
@ -29,33 +28,26 @@ func main () {
application.SetIcon([]image.Image { icon16, icon32 })
channel, err := application.Run()
if err != nil { panic(err) }
application.OnStart(onStart)
application.OnResize(onResize)
err = application.Run()
if err != nil { panic(err) }
}
func onStart () {
redraw()
go tick()
}
for {
select {
case <- tickPing:
redraw()
case event := <- channel:
switch event.(type) {
case stone.EventQuit:
os.Exit(0)
case stone.EventResize:
redraw()
}
}
}
func onResize () {
redraw()
}
func redraw () {
currentTime = time.Now()
application.ResetDot()
application.SetDot(0, 0)
fmt.Fprintln(application, "hellorld!")
hour := currentTime.Hour()
@ -70,13 +62,12 @@ func redraw () {
application.SetRune(5, 1, ':')
application.SetRune(6, 1, rune(second / 10 + 48))
application.SetRune(7, 1, rune(second % 10 + 48))
application.Draw()
}
func tick () {
for {
tickPing <- struct { } { }
redraw()
application.Draw()
time.Sleep(time.Second)
}
}