Added visibility changing abilities for drawers.

This commit is contained in:
Andrey Parhomenko 2023-05-23 16:19:55 +03:00
parent 874ddb14e2
commit 5966d20865
6 changed files with 45 additions and 20 deletions

View file

@ -29,6 +29,7 @@ func NewPlayer() *Player {
RA: gx.Vector{320, 240}, RA: gx.Vector{320, 240},
}, },
I: playerImg, I: playerImg,
Visible: true,
}, },
MoveSpeed: 90., MoveSpeed: 90.,
ScaleSpeed: .2, ScaleSpeed: .2,
@ -110,6 +111,8 @@ func (d *Debug) Draw(
e.DebugPrint(i, strings.Join(keyStrs, ", ")) e.DebugPrint(i, strings.Join(keyStrs, ", "))
} }
func (d *Debug) IsVisible() bool {return true}
func main() { func main() {
e := gx.New(&gx.WindowConfig{ e := gx.New(&gx.WindowConfig{
Title: "Test title", Title: "Test title",
@ -127,16 +130,19 @@ func main() {
e.Add(0, NewPlayer()) e.Add(0, NewPlayer())
e.Add(1, &Debug{}) e.Add(1, &Debug{})
e.Add(-1, gx.Rectangle{ e.Add(-1, gx.DrawableRectangle{
W: 100, Rectangle: gx.Rectangle{
H: 100, W: 100,
T: gx.T(), H: 100,
C: gx.Color{ T: gx.T(),
gx.MaxColorV, },
gx.MaxColorV, Color: gx.Color{
gx.MaxColorV, gx.MaxColorV,
0,
0,
gx.MaxColorV, gx.MaxColorV,
}, },
Visible: true,
}) })
e.Run() e.Run()
} }

View file

@ -20,6 +20,7 @@ type Color struct {
// the layers order. // the layers order.
type Drawer interface { type Drawer interface {
Draw(*Engine, *Image) Draw(*Engine, *Image)
IsVisible() bool
} }
const ( const (

View file

@ -149,6 +149,9 @@ func (e *engine) Draw(i *ebiten.Image) {
eng := (*Engine)(e) eng := (*Engine)(e)
for p := range e.layers.Vals() { for p := range e.layers.Vals() {
for pj := range p.V.Range() { for pj := range p.V.Range() {
if !pj.V.IsVisible() {
continue
}
pj.V.Draw(eng, i) pj.V.Draw(eng, i)
} }
} }

View file

@ -3,7 +3,7 @@ package gx
import ( import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
//"github.com/hajimehoshi/ebiten/v2/vector" //"github.com/hajimehoshi/ebiten/v2/vector"
"fmt" //"fmt"
//"image" //"image"
) )
@ -18,8 +18,15 @@ type Rectangle struct {
// relation of width and height. // relation of width and height.
// Change transform to actually change things. // Change transform to actually change things.
W, H Float 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. // Return points of corners of the rectangle.
@ -45,14 +52,18 @@ func NewImage(w, h int) (*Image) {
return ebiten.NewImage(w, h) return ebiten.NewImage(w, h)
} }
func (r Rectangle) Draw( func (r DrawableRectangle) IsVisible() bool {
return r.Visible
}
func (r DrawableRectangle) Draw(
e *Engine, e *Engine,
i *Image, i *Image,
) { ) {
fmt.Println("drawing the rectangle:", r) // Draw solid color if no shader.
if r.S == nil { if r.Shader == nil {
img := NewImage(1, 1) img := NewImage(1, 1)
img.Set(0, 0, r.C) img.Set(0, 0, r.Color)
t := r.T t := r.T
t.S.X *= r.W t.S.X *= r.W
@ -65,13 +76,13 @@ func (r Rectangle) Draw(
GeoM: m, GeoM: m,
} }
i.DrawImage(img, opts) i.DrawImage(img, opts)
fmt.Println("done")
return return
} }
opts := &ebiten.DrawRectShaderOptions{ opts := &ebiten.DrawRectShaderOptions{
GeoM: r.T.Matrix(e), GeoM: r.T.Matrix(e),
Images: [4]*Image{ Images: [4]*Image{
NewImage(1000, 1000), NewImage(1, 1),
nil, nil,
nil, nil,
nil, nil,
@ -81,6 +92,6 @@ func (r Rectangle) Draw(
//w := int(r.W * r.T.S.X) //w := int(r.W * r.T.S.X)
//h := int(r.H * r.T.S.Y) //h := int(r.H * r.T.S.Y)
i.DrawRectShader(1000, 1000, r.S, opts) i.DrawRectShader(1000, 1000, r.Shader, opts)
} }

View file

@ -9,11 +9,11 @@ type Shader = ebiten.Shader
var ( var (
// The shader does not // The shader does not
SolidColorShader = MustNewShader([]byte(` SolidWhiteColorShader = MustNewShader([]byte(`
package main package main
func Fragment(p vec4, coord vec2, color vec4) vec4 { func Fragment(p vec4, coord vec2, color vec4) vec4 {
return vec4(1, 0, 1, 1) return vec4(1, 1, 1, 1)
} }
`)) `))
) )

View file

@ -8,7 +8,7 @@ type Sprite struct {
I *Image I *Image
T Transform T Transform
S *Shader S *Shader
Floating bool Floating, Visible bool
} }
func (s *Sprite) Draw( func (s *Sprite) Draw(
@ -36,3 +36,7 @@ func (s *Sprite) Draw(
i.DrawImage(s.I, op) i.DrawImage(s.I, op)
} }
func (s *Sprite) IsVisible() bool {
return s.Visible
}