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},
},
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()
}

View file

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

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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)
}
`))
)

View file

@ -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
}