package shapes import ( "gioutils/internal" "gioutils/points" "image" "image/color" "gioui.org/layout" "gioui.org/op/clip" "gioui.org/op/paint" ) type Rectangle struct { color color.NRGBA min image.Point max image.Point } // RectangleFromPoints returns a new Rectangle based on two corner points. func RectangleFromPoints(min image.Point, max image.Point) *Rectangle { return &Rectangle{ color: internal.DefaultBgColor, min: min, max: max, } } // RectangleFromCenter returns a new Rectangle based on a central point and a given apothem. func RectangleFromCenter(center image.Point, apothem int) *Rectangle { min, max := points.CenterSquare(center, apothem) return &Rectangle{ color: internal.DefaultBgColor, min: min, max: max, } } // Color sets the color of the Rectangle. func (r *Rectangle) Color(color color.NRGBA) *Rectangle { r.color = color return r } // Draw returns the Rectangle widget. func (r *Rectangle) Draw() layout.Widget { return func(gtx layout.Context) layout.Dimensions { rect := clip.Rect{ Min: r.min, Max: r.max, }.Op() paint.FillShape(gtx.Ops, r.color, rect) width := r.max.X - r.min.X height := r.max.Y - r.min.Y return layout.Dimensions{Size: image.Pt(width, height)} } }