From 18ea5681dee5bfaf021e2afe73843ddd01bde9bb Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 6 Nov 2022 12:42:22 -0500 Subject: [PATCH] Buffer is now drawn in the screen center (similar to kitty) --- application.go | 6 +++--- backends/pixel/pixel.go | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/application.go b/application.go index c20ccef..80c8443 100644 --- a/application.go +++ b/application.go @@ -30,10 +30,10 @@ func (application *Application) Run (callback func (application *Application)) { // TODO: load these from a file application.config.colors = [4]color.Color { - color.RGBA { R: 0x81, G: 0xA1, B: 0xC1, A: 0xFF }, - color.RGBA { R: 0x4C, G: 0x56, B: 0x6A, A: 0xFF }, - color.RGBA { R: 0xD8, G: 0xDE, B: 0xE9, A: 0xFF }, color.RGBA { R: 0x2B, G: 0x30, B: 0x3C, A: 0xFF }, + color.RGBA { R: 0x4C, G: 0x56, B: 0x6A, A: 0xFF }, + color.RGBA { R: 0x2E, G: 0x34, B: 0x40, A: 0xFF }, + color.RGBA { R: 0xA8, G: 0x55, B: 0x5D, A: 0xFF }, } application.config.padding = 4 diff --git a/backends/pixel/pixel.go b/backends/pixel/pixel.go index 2eb3445..af08abb 100644 --- a/backends/pixel/pixel.go +++ b/backends/pixel/pixel.go @@ -23,6 +23,9 @@ type Backend struct { metrics struct { cellWidth int cellHeight int + padding int + paddingX int + paddingY int } } @@ -125,7 +128,7 @@ func (backend *Backend) Resized () (resized bool) { func (backend *Backend) draw () { // didDrawing := false width, height := backend.application.Size() - + if backend.boundsDirty { backend.window.Clear ( backend.config.Color(stone.ColorApplication)) @@ -155,6 +158,8 @@ func (backend *Backend) draw () { } backend.textDrawer.Clear() + backend.textDrawer.Color = + backend.config.Color(stone.ColorForeground) for x := 0; x < width; x ++ { for y := 0; y < height; y ++ { clean := backend.application.Clean(x, y) @@ -165,7 +170,7 @@ func (backend *Backend) draw () { content := cell.Rune() if content < 32 { continue } - // draw cell + // draw cell backend.textDrawer.Dot = backend.vectorAtPosition(x, y + 1) backend.textDrawer.WriteRune(content) backend.textDrawer.Draw(backend.window, pixel.IM) @@ -190,17 +195,37 @@ func (backend *Backend) processEvents () { backend.windowBounds = newBounds if backend.boundsDirty { - // TODO: set size of buffer + // 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 } } func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) { - padding := backend.config.Padding() * backend.metrics.cellWidth - vector = pixel.V ( - float64 (x * backend.metrics.cellWidth + padding), + float64 ( + x * backend.metrics.cellWidth + + backend.metrics.paddingX), backend.windowBounds.Y - float64 ( - y * backend.metrics.cellHeight + padding)) + y * backend.metrics.cellHeight + + backend.metrics.paddingY)) return }