49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package views
|
|
|
|
import (
|
|
"image"
|
|
|
|
"git.milar.in/milarin/gui"
|
|
)
|
|
|
|
// MarginView is a gui.Wrapper which applies margin around its view
|
|
type MarginView struct {
|
|
gui.WrapperTmpl
|
|
Margin map[gui.Side]int
|
|
}
|
|
|
|
var _ gui.Wrapper = &MarginView{}
|
|
|
|
func NewMarginView(view gui.View, top, right, bottom, left int) *MarginView {
|
|
v := new(MarginView)
|
|
v.SetView(view)
|
|
v.SetMargin(top, right, bottom, left)
|
|
return v
|
|
}
|
|
|
|
func (v *MarginView) Layout(ctx gui.AppContext) (prefWidth, prefHeight int) {
|
|
w, h := v.View().Layout(ctx)
|
|
w = iff(w > 0, w+v.Margin[gui.SideLeft]+v.Margin[gui.SideRight], w)
|
|
h = iff(h > 0, h+v.Margin[gui.SideTop]+v.Margin[gui.SideBottom], h)
|
|
return w, h
|
|
}
|
|
|
|
func (g *MarginView) Draw(img *gui.Image, ctx gui.AppContext) {
|
|
x := img.Bounds().Min.X + g.Margin[gui.SideLeft]
|
|
y := img.Bounds().Min.Y + g.Margin[gui.SideTop]
|
|
w := img.Bounds().Max.X - g.Margin[gui.SideRight]
|
|
h := img.Bounds().Max.Y - g.Margin[gui.SideBottom]
|
|
|
|
g.ViewTmpl.Draw(img, ctx)
|
|
g.View().Draw(img.SubImage(image.Rect(x, y, w, h)).(*gui.Image), ctx)
|
|
}
|
|
|
|
func (v *MarginView) SetMargin(top, right, bottom, left int) {
|
|
v.Margin = map[gui.Side]int{
|
|
gui.SideTop: top,
|
|
gui.SideRight: right,
|
|
gui.SideBottom: bottom,
|
|
gui.SideLeft: left,
|
|
}
|
|
}
|