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() resized = application.backend.Resized()
return return
} }
func (application *Application) MousePosition () (x, y int) {
x, y = application.backend.MousePosition()
return
}

View File

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

View File

@ -126,6 +126,19 @@ func (backend *Backend) Resized () (resized bool) {
return 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 () { func (backend *Backend) draw () {
// didDrawing := false // didDrawing := false
width, height := backend.application.Size() 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) { func (buffer *Buffer) SetSize (width, height int) {
if width < 0 || height < 0 { return }
buffer.width = width buffer.width = width
buffer.height = height buffer.height = height
buffer.content = make([]Cell, width * height) buffer.content = make([]Cell, width * height)
@ -115,6 +116,7 @@ type DamageBuffer struct {
} }
func (buffer *DamageBuffer) SetSize (width, height int) { func (buffer *DamageBuffer) SetSize (width, height int) {
if width < 0 || height < 0 { return }
buffer.Buffer.SetSize(width, height) buffer.Buffer.SetSize(width, height)
buffer.clean = make([]bool, 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.X = 0
application.Dot.Y = 2 application.Dot.Y = 2
fmt.Fprintln(application, textBuffer) fmt.Fprintln(application, textBuffer)
}
if application.Pressed(stone.MouseButtonLeft) {
x, y := application.MousePosition()
application.SetRune(x, y, '#')
} }
if !application.Await(frameDelay) { break } if !application.Await(frameDelay) { break }