2020-11-09 06:34:56 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
|
|
|
// static check that CustomWidget implements widget
|
|
|
|
var _ Widget = &CustomWidget{}
|
|
|
|
|
|
|
|
// CustomWidget is a widget with a fully custom render function
|
|
|
|
type CustomWidget struct {
|
|
|
|
*BaseWidget
|
2020-11-11 08:55:59 -05:00
|
|
|
renderFunc func(target d2interface.Surface)
|
2020-11-13 15:08:43 -05:00
|
|
|
cached bool
|
|
|
|
cachedImg *d2interface.Surface
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCustomWidgetCached creates a new widget and caches anything rendered via the
|
|
|
|
// renderFunc into a static image to be displayed
|
|
|
|
func (ui *UIManager) NewCustomWidgetCached(renderFunc func(target d2interface.Surface), width, height int) *CustomWidget {
|
|
|
|
c := ui.NewCustomWidget(renderFunc, width, height)
|
|
|
|
c.cached = true
|
|
|
|
|
|
|
|
// render using the renderFunc to a cache
|
|
|
|
surface := ui.Renderer().NewSurface(width, height)
|
|
|
|
c.cachedImg = &surface
|
|
|
|
renderFunc(*c.cachedImg)
|
|
|
|
|
|
|
|
return c
|
2020-11-09 06:34:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCustomWidget creates a new widget with custom render function
|
2020-11-13 15:08:43 -05:00
|
|
|
func (ui *UIManager) NewCustomWidget(renderFunc func(target d2interface.Surface), width, height int) *CustomWidget {
|
2020-11-09 06:34:56 -05:00
|
|
|
base := NewBaseWidget(ui)
|
2020-11-13 15:08:43 -05:00
|
|
|
base.width = width
|
|
|
|
base.height = height
|
2020-11-09 06:34:56 -05:00
|
|
|
|
|
|
|
return &CustomWidget{
|
|
|
|
BaseWidget: base,
|
|
|
|
renderFunc: renderFunc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render draws the custom widget
|
2020-11-11 08:55:59 -05:00
|
|
|
func (c *CustomWidget) Render(target d2interface.Surface) {
|
2020-11-13 15:08:43 -05:00
|
|
|
if c.cached {
|
|
|
|
target.PushTranslation(c.GetPosition())
|
|
|
|
target.Render(*c.cachedImg)
|
|
|
|
target.Pop()
|
|
|
|
} else {
|
|
|
|
c.renderFunc(target)
|
|
|
|
}
|
2020-11-09 06:34:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advance is a no-op
|
|
|
|
func (c *CustomWidget) Advance(elapsed float64) error {
|
|
|
|
return nil
|
|
|
|
}
|