GioUtils/shapes/circle.go
2025-01-31 00:04:31 +01:00

62 lines
1.3 KiB
Go

package shapes
import (
"gioutils/internal"
"gioutils/points"
"image"
"image/color"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
type Circle struct {
color color.NRGBA
min image.Point
max image.Point
radius int
}
// CircleFromPoints returns a new Circle based on two rectangular corners.
func CircleFromPoints(min image.Point, max image.Point) *Circle {
return &Circle{
color: internal.DefaultBgColor,
min: min,
max: max,
}
}
// CircleFromCenter returns a new Circle based on a central point and a radius.
func CircleFromCenter(center image.Point, radius int) *Circle {
min, max := points.CenterSquare(center, radius)
return &Circle{
color: internal.DefaultBgColor,
min: min,
max: max,
radius: radius,
}
}
// Color sets the color of the Circle.
func (c *Circle) Color(color color.NRGBA) *Circle {
c.color = color
return c
}
// Draw returns the Circle widget.
func (c *Circle) Draw() layout.Widget {
return func(gtx layout.Context) layout.Dimensions {
circle := clip.Ellipse{
Min: c.min,
Max: c.max,
}.Op(gtx.Ops)
paint.FillShape(gtx.Ops, c.color, circle)
// width := c.max.X - c.min.X
// height := c.max.Y - c.min.Y
diameter := c.radius * 2
return layout.Dimensions{Size: image.Pt(diameter, diameter)}
}
}