Mouse input works

This commit is contained in:
Sasha Koshka 2022-11-06 15:59:06 -05:00
parent f3d0bad959
commit 3a879d9a5f
5 changed files with 37 additions and 10 deletions

View File

@ -97,3 +97,8 @@ func (application *Application) Resized () (resized bool) {
resized = application.backend.Resized()
return
}
func (application *Application) MousePosition () (x, y int) {
x, y = application.backend.MousePosition()
return
}

View File

@ -4,16 +4,17 @@ import "time"
import "errors"
type Backend interface {
Run (callback func (application *Application)) ()
Await (timeout time.Duration) (keepRunning bool)
Poll () (keepRunning bool)
SetTitle (title string)
JustPressed (button Button) (pressed bool)
JustReleased (button Button) (released bool)
Pressed (button Button) (pressed bool)
Repeated (button Button) (repeated bool)
Typed () (text string)
Resized () (resized bool)
Run (callback func (application *Application)) ()
Await (timeout time.Duration) (keepRunning bool)
Poll () (keepRunning bool)
SetTitle (title string)
JustPressed (button Button) (pressed bool)
JustReleased (button Button) (released bool)
Pressed (button Button) (pressed bool)
Repeated (button Button) (repeated bool)
Typed () (text string)
Resized () (resized bool)
MousePosition () (x, y int)
}
type BackendFactory func (application *Application) (backend Backend, err error)

View File

@ -126,6 +126,19 @@ func (backend *Backend) Resized () (resized bool) {
return
}
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
}
func (backend *Backend) draw () {
// didDrawing := false
width, height := backend.application.Size()

View File

@ -65,6 +65,7 @@ func (buffer *Buffer) Size () (width, height int) {
}
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)
@ -115,6 +116,7 @@ type DamageBuffer struct {
}
func (buffer *DamageBuffer) SetSize (width, height int) {
if width < 0 || height < 0 { return }
buffer.Buffer.SetSize(width, height)
buffer.clean = make([]bool, width * height)
}

View File

@ -47,6 +47,12 @@ func run (application *stone.Application) {
application.Dot.X = 0
application.Dot.Y = 2
fmt.Fprintln(application, textBuffer)
}
if application.Pressed(stone.MouseButtonLeft) {
x, y := application.MousePosition()
application.SetRune(x, y, '#')
}
if !application.Await(frameDelay) { break }