gg/rect.go

107 lines
2 KiB
Go
Raw Normal View History

2023-10-23 15:45:18 +03:00
package gg
2023-05-22 23:39:01 +03:00
import (
"github.com/hajimehoshi/ebiten/v2"
//"github.com/hajimehoshi/ebiten/v2/vector"
//"fmt"
2023-05-22 23:39:01 +03:00
//"image"
)
// The type describes rectangle geometry.
2023-05-22 23:39:01 +03:00
type Rectangle struct {
Object
Transform
}
// The type describes rectangle that can be drawn.
type DrawableRectangle struct {
Rectangle
2023-12-23 00:09:07 +03:00
ShaderOptions
2023-05-25 00:42:48 +03:00
// Solid color of the rectangle.
// Will be ignored if the Shader
// field is not nil.
2023-12-23 00:09:07 +03:00
Colority
2023-05-25 00:42:48 +03:00
2023-05-26 18:31:04 +03:00
// Should be draw or not.
2023-12-23 00:09:07 +03:00
Visibility
2023-05-22 23:39:01 +03:00
}
2023-06-03 11:25:19 +03:00
// Return points of vertices of the rectangle.
func (r Rectangle) Vertices() Points {
2023-05-30 14:34:10 +03:00
m := r.Matrix()
p1 := V(0, 0).Apply(m)
p2 := V(1, 0).Apply(m)
p3 := V(1, 1).Apply(m)
p4 := V(0, 1).Apply(m)
2023-06-03 11:25:19 +03:00
return Points{p1, p2, p3, p4}
}
2023-06-03 22:41:00 +03:00
func (r Rectangle) Edges() Edges {
2023-06-03 12:26:31 +03:00
vs := r.Vertices()
return LineSegments{
LineSegment{vs[0], vs[1]},
LineSegment{vs[1], vs[2]},
LineSegment{vs[2], vs[3]},
2023-06-03 22:41:00 +03:00
LineSegment{vs[3], vs[0]},
2023-06-03 12:26:31 +03:00
}
2023-06-03 11:25:19 +03:00
}
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) Triangles() Triangles {
pts := r.Vertices()
p1 := pts[0]
p2 := pts[1]
p3 := pts[2]
p4 := pts[3]
2023-05-22 23:39:01 +03:00
2023-05-30 14:34:10 +03:00
return Triangles{
Triangle{p1, p2, p3},
Triangle{p1, p4, p3},
}
}
2023-05-30 14:34:10 +03:00
// Check whether the rectangle contains the point.
func (r Rectangle) ContainsPoint(p Point) bool {
return r.Triangles().ContainsPoint(p)
}
2023-05-30 14:34:10 +03:00
// Check whether the drawable rectangle should be drawn.
2023-05-26 18:31:04 +03:00
func (r *DrawableRectangle) IsVisible() bool {
return r.Visible
}
func (r *DrawableRectangle) Draw(c *Context) {
m := r.Matrix()
2023-12-23 00:09:07 +03:00
rm := c.Camera.RealMatrix()
m.Concat(rm)
// Draw solid color if no shader.
if r.Shader == nil {
2023-05-22 23:39:01 +03:00
img := NewImage(1, 1)
img.Set(0, 0, r.Color)
2023-05-26 18:31:04 +03:00
c.DrawImage(img, &ebiten.DrawImageOptions{
GeoM: m,
})
2023-05-22 23:39:01 +03:00
return
}
2023-05-26 18:31:04 +03:00
2023-05-25 00:42:48 +03:00
// Use the Color as base image if no is provided.
//var did bool
2023-05-26 18:31:04 +03:00
if r.Images[0] == nil {
r.Images[0] = NewImage(1, 1)
r.Images[0].Set(0, 0, r.Color)
2023-05-25 00:42:48 +03:00
}
2023-05-26 18:31:04 +03:00
w, h := r.Images[0].Size()
2023-05-25 00:42:48 +03:00
// Drawing with shader.
c.DrawRectShader(w, h, r.Shader, &ebiten.DrawRectShaderOptions{
GeoM: m,
2023-05-26 18:31:04 +03:00
Images: r.Images,
Uniforms: r.Uniforms,
})
}