From 3a879d9a5f407a26328ee99054e2bbd28cfb62f5 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 6 Nov 2022 15:59:06 -0500 Subject: [PATCH] Mouse input works --- application.go | 5 +++++ backend.go | 21 +++++++++++---------- backends/pixel/pixel.go | 13 +++++++++++++ buffer.go | 2 ++ examples/hello/main.go | 6 ++++++ 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/application.go b/application.go index a1dca70..17a1bd2 100644 --- a/application.go +++ b/application.go @@ -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 +} diff --git a/backend.go b/backend.go index 05832e5..39561e3 100644 --- a/backend.go +++ b/backend.go @@ -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) diff --git a/backends/pixel/pixel.go b/backends/pixel/pixel.go index 8209b56..2c8c763 100644 --- a/backends/pixel/pixel.go +++ b/backends/pixel/pixel.go @@ -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() diff --git a/buffer.go b/buffer.go index 5a296c8..f2c7834 100644 --- a/buffer.go +++ b/buffer.go @@ -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) } diff --git a/examples/hello/main.go b/examples/hello/main.go index 93d8463..0623cde 100644 --- a/examples/hello/main.go +++ b/examples/hello/main.go @@ -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 }