From 5966d2086558c1b0605b513ae0ee2e04a67abaec Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 23 May 2023 16:19:55 +0300 Subject: [PATCH] Added visibility changing abilities for drawers. --- src/cmd/test/main.go | 20 +++++++++++++------- src/gx/img.go | 1 + src/gx/main.go | 3 +++ src/gx/rect.go | 31 +++++++++++++++++++++---------- src/gx/shader.go | 4 ++-- src/gx/sprite.go | 6 +++++- 6 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index be83bdb..5edf799 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -29,6 +29,7 @@ func NewPlayer() *Player { RA: gx.Vector{320, 240}, }, I: playerImg, + Visible: true, }, MoveSpeed: 90., ScaleSpeed: .2, @@ -110,6 +111,8 @@ func (d *Debug) Draw( e.DebugPrint(i, strings.Join(keyStrs, ", ")) } +func (d *Debug) IsVisible() bool {return true} + func main() { e := gx.New(&gx.WindowConfig{ Title: "Test title", @@ -127,16 +130,19 @@ func main() { e.Add(0, NewPlayer()) e.Add(1, &Debug{}) - e.Add(-1, gx.Rectangle{ - W: 100, - H: 100, - T: gx.T(), - C: gx.Color{ - gx.MaxColorV, - gx.MaxColorV, + e.Add(-1, gx.DrawableRectangle{ + Rectangle: gx.Rectangle{ + W: 100, + H: 100, + T: gx.T(), + }, + Color: gx.Color{ gx.MaxColorV, + 0, + 0, gx.MaxColorV, }, + Visible: true, }) e.Run() } diff --git a/src/gx/img.go b/src/gx/img.go index c4e7ca5..ed551fc 100644 --- a/src/gx/img.go +++ b/src/gx/img.go @@ -20,6 +20,7 @@ type Color struct { // the layers order. type Drawer interface { Draw(*Engine, *Image) + IsVisible() bool } const ( diff --git a/src/gx/main.go b/src/gx/main.go index ab27673..a28c964 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -149,6 +149,9 @@ func (e *engine) Draw(i *ebiten.Image) { eng := (*Engine)(e) for p := range e.layers.Vals() { for pj := range p.V.Range() { + if !pj.V.IsVisible() { + continue + } pj.V.Draw(eng, i) } } diff --git a/src/gx/rect.go b/src/gx/rect.go index 35fa080..641880b 100644 --- a/src/gx/rect.go +++ b/src/gx/rect.go @@ -3,7 +3,7 @@ package gx import ( "github.com/hajimehoshi/ebiten/v2" //"github.com/hajimehoshi/ebiten/v2/vector" - "fmt" + //"fmt" //"image" ) @@ -18,8 +18,15 @@ type Rectangle struct { // relation of width and height. // Change transform to actually change things. W, H Float - S *Shader - C Color + +} + +// The type describes rectangle that can be drawn. +type DrawableRectangle struct { + Rectangle + Shader *Shader + Color Color + Visible bool } // Return points of corners of the rectangle. @@ -45,14 +52,18 @@ func NewImage(w, h int) (*Image) { return ebiten.NewImage(w, h) } -func (r Rectangle) Draw( +func (r DrawableRectangle) IsVisible() bool { + return r.Visible +} + +func (r DrawableRectangle) Draw( e *Engine, i *Image, ) { - fmt.Println("drawing the rectangle:", r) - if r.S == nil { + // Draw solid color if no shader. + if r.Shader == nil { img := NewImage(1, 1) - img.Set(0, 0, r.C) + img.Set(0, 0, r.Color) t := r.T t.S.X *= r.W @@ -65,13 +76,13 @@ func (r Rectangle) Draw( GeoM: m, } i.DrawImage(img, opts) - fmt.Println("done") return } + opts := &ebiten.DrawRectShaderOptions{ GeoM: r.T.Matrix(e), Images: [4]*Image{ - NewImage(1000, 1000), + NewImage(1, 1), nil, nil, nil, @@ -81,6 +92,6 @@ func (r Rectangle) Draw( //w := int(r.W * r.T.S.X) //h := int(r.H * r.T.S.Y) - i.DrawRectShader(1000, 1000, r.S, opts) + i.DrawRectShader(1000, 1000, r.Shader, opts) } diff --git a/src/gx/shader.go b/src/gx/shader.go index 5c7fb85..1cf7980 100644 --- a/src/gx/shader.go +++ b/src/gx/shader.go @@ -9,11 +9,11 @@ type Shader = ebiten.Shader var ( // The shader does not - SolidColorShader = MustNewShader([]byte(` + SolidWhiteColorShader = MustNewShader([]byte(` package main func Fragment(p vec4, coord vec2, color vec4) vec4 { - return vec4(1, 0, 1, 1) + return vec4(1, 1, 1, 1) } `)) ) diff --git a/src/gx/sprite.go b/src/gx/sprite.go index 344af1d..699f240 100644 --- a/src/gx/sprite.go +++ b/src/gx/sprite.go @@ -8,7 +8,7 @@ type Sprite struct { I *Image T Transform S *Shader - Floating bool + Floating, Visible bool } func (s *Sprite) Draw( @@ -36,3 +36,7 @@ func (s *Sprite) Draw( i.DrawImage(s.I, op) } +func (s *Sprite) IsVisible() bool { + return s.Visible +} +