2023-05-04 19:31:33 +03:00
|
|
|
package gx
|
|
|
|
|
2023-05-22 23:39:01 +03:00
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
//"github.com/hajimehoshi/ebiten/v2/vector"
|
2023-05-23 16:19:55 +03:00
|
|
|
//"fmt"
|
2023-05-22 23:39:01 +03:00
|
|
|
//"image"
|
|
|
|
)
|
|
|
|
|
2023-05-04 19:31:33 +03:00
|
|
|
// The type describes rectangle geometry.
|
2023-05-22 23:39:01 +03:00
|
|
|
type Rectangle struct {
|
|
|
|
// Position of up left corner
|
|
|
|
// and the point to
|
|
|
|
// rotate around(relatively of position, not absolute).
|
2023-05-30 22:35:49 +03:00
|
|
|
// Scale represent width and height.
|
2023-05-30 13:24:14 +03:00
|
|
|
Transform
|
2023-05-23 16:19:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The type describes rectangle that can be drawn.
|
|
|
|
type DrawableRectangle struct {
|
|
|
|
Rectangle
|
2023-05-30 13:24:14 +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-05-23 16:19:55 +03:00
|
|
|
Color Color
|
2023-05-25 00:42:48 +03:00
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
// Should be draw or not.
|
2023-05-23 16:19:55 +03:00
|
|
|
Visible bool
|
2023-05-22 23:39:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return points of corners of the rectangle.
|
|
|
|
func (r Rectangle) Corners() []Point {
|
|
|
|
return []Point{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 2 triangles that the rectangle consists of.
|
|
|
|
func (r Rectangle) Triangles() Triangles {
|
2023-05-30 14:34:10 +03:00
|
|
|
m := r.Matrix()
|
2023-05-30 22:35:49 +03:00
|
|
|
|
|
|
|
p1 := V(0, 0).Apply(&m)
|
|
|
|
p2 := V(1, 0).Apply(&m)
|
2023-05-30 22:51:50 +03:00
|
|
|
p3 := V(1, 1).Apply(&m)
|
|
|
|
p4 := V(0, 1).Apply(&m)
|
2023-05-30 22:35:49 +03:00
|
|
|
|
|
|
|
//fmt.Println("in:", p1, p2, p3, p4)
|
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 13:12:11 +03:00
|
|
|
}
|
|
|
|
|
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-04 19:31:33 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-23 16:19:55 +03:00
|
|
|
return r.Visible
|
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
func (r *DrawableRectangle) Draw(
|
2023-05-22 23:39:01 +03:00
|
|
|
e *Engine,
|
|
|
|
i *Image,
|
|
|
|
) {
|
2023-05-30 22:51:50 +03:00
|
|
|
m := r.Matrix()
|
|
|
|
rm := e.Camera().RealMatrix(e)
|
|
|
|
m.Concat(rm)
|
2023-05-23 16:19:55 +03:00
|
|
|
// Draw solid color if no shader.
|
|
|
|
if r.Shader == nil {
|
2023-05-22 23:39:01 +03:00
|
|
|
img := NewImage(1, 1)
|
2023-05-23 16:19:55 +03:00
|
|
|
img.Set(0, 0, r.Color)
|
2023-05-26 18:31:04 +03:00
|
|
|
|
|
|
|
|
2023-05-22 23:39:01 +03:00
|
|
|
opts := &ebiten.DrawImageOptions{
|
|
|
|
GeoM: m,
|
|
|
|
}
|
|
|
|
i.DrawImage(img, opts)
|
|
|
|
return
|
|
|
|
}
|
2023-05-23 16:19:55 +03:00
|
|
|
|
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.
|
2023-05-30 22:35:49 +03:00
|
|
|
//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-30 22:35:49 +03:00
|
|
|
//did = true
|
2023-05-25 00:42:48 +03:00
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
w, h := r.Images[0].Size()
|
2023-05-30 22:35:49 +03:00
|
|
|
/*if !did {
|
2023-05-26 18:31:04 +03:00
|
|
|
t.S.X /= Float(w)
|
|
|
|
t.S.Y /= Float(h)
|
|
|
|
|
|
|
|
t.S.X *= r.W
|
|
|
|
t.S.Y *= r.H
|
2023-05-30 22:35:49 +03:00
|
|
|
}*/
|
2023-05-26 18:31:04 +03:00
|
|
|
|
2023-05-25 00:42:48 +03:00
|
|
|
// Drawing with shader.
|
2023-05-22 23:39:01 +03:00
|
|
|
opts := &ebiten.DrawRectShaderOptions{
|
2023-05-23 16:25:21 +03:00
|
|
|
GeoM: m,
|
2023-05-26 18:31:04 +03:00
|
|
|
Images: r.Images,
|
|
|
|
Uniforms: r.Uniforms,
|
2023-05-22 23:39:01 +03:00
|
|
|
}
|
2023-05-25 00:42:48 +03:00
|
|
|
i.DrawRectShader(w, h, r.Shader, opts)
|
2023-05-04 19:31:33 +03:00
|
|
|
}
|
|
|
|
|