2019-11-10 08:51:02 -05:00
|
|
|
package d2ui
|
2019-10-25 18:40:27 -04:00
|
|
|
|
2020-11-06 07:28:24 -05:00
|
|
|
// RenderPriority determines in which order ui elements are drawn.
|
|
|
|
// The higher the number the later an element is drawn.
|
|
|
|
type RenderPriority int
|
|
|
|
|
2020-11-21 05:35:32 -05:00
|
|
|
// Render priorities that determine the order in which widgets/widgetgroups are
|
|
|
|
// rendered. The higher the later it is rendered
|
2020-11-06 07:28:24 -05:00
|
|
|
const (
|
|
|
|
RenderPriorityBackground RenderPriority = iota
|
2020-11-09 12:09:46 -05:00
|
|
|
RenderPrioritySkilltree
|
2020-12-02 02:19:15 -05:00
|
|
|
RenderPriorityInventory
|
2020-11-09 12:09:46 -05:00
|
|
|
RenderPrioritySkilltreeIcon
|
2020-11-13 15:08:43 -05:00
|
|
|
RenderPriorityHeroStatsPanel
|
2020-12-02 02:19:15 -05:00
|
|
|
RenderPriorityQuestLog
|
2020-11-21 05:35:32 -05:00
|
|
|
RenderPriorityHUDPanel
|
|
|
|
RenderPriorityMinipanel
|
|
|
|
RenderPriorityHelpPanel
|
2020-11-06 07:28:24 -05:00
|
|
|
RenderPriorityForeground
|
|
|
|
)
|
|
|
|
|
2019-10-25 18:40:27 -04:00
|
|
|
// Widget defines an object that is a UI widget
|
|
|
|
type Widget interface {
|
2020-01-31 23:18:11 -05:00
|
|
|
Drawable
|
2020-08-06 10:30:23 -04:00
|
|
|
bindManager(ui *UIManager)
|
2020-11-13 15:08:43 -05:00
|
|
|
GetManager() (ui *UIManager)
|
2020-11-16 04:41:01 -05:00
|
|
|
OnMouseMove(x int, y int)
|
2020-11-13 15:08:43 -05:00
|
|
|
OnHoverStart(callback func())
|
|
|
|
OnHoverEnd(callback func())
|
|
|
|
isHovered() bool
|
|
|
|
hoverStart()
|
|
|
|
hoverEnd()
|
|
|
|
Contains(x, y int) (contained bool)
|
2020-11-06 05:48:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClickableWidget defines an object that can be clicked
|
|
|
|
type ClickableWidget interface {
|
|
|
|
Widget
|
2019-10-25 22:20:36 -04:00
|
|
|
SetEnabled(enabled bool)
|
2019-10-25 23:41:54 -04:00
|
|
|
SetPressed(pressed bool)
|
2020-11-06 05:48:49 -05:00
|
|
|
GetEnabled() bool
|
2019-10-25 23:41:54 -04:00
|
|
|
GetPressed() bool
|
|
|
|
OnActivated(callback func())
|
|
|
|
Activate()
|
2019-10-25 18:40:27 -04:00
|
|
|
}
|
2020-11-06 06:38:20 -05:00
|
|
|
|
|
|
|
// BaseWidget contains default functionality that all widgets share
|
|
|
|
type BaseWidget struct {
|
2020-11-06 07:28:24 -05:00
|
|
|
manager *UIManager
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
renderPriority RenderPriority
|
|
|
|
visible bool
|
2020-11-13 15:08:43 -05:00
|
|
|
|
|
|
|
hovered bool
|
|
|
|
onHoverStartCb func()
|
|
|
|
onHoverEndCb func()
|
2020-11-06 06:38:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseWidget creates a new BaseWidget with defaults
|
|
|
|
func NewBaseWidget(manager *UIManager) *BaseWidget {
|
|
|
|
return &BaseWidget{
|
2020-11-06 07:28:24 -05:00
|
|
|
manager: manager,
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
visible: true,
|
|
|
|
renderPriority: RenderPriorityBackground,
|
2020-11-06 06:38:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseWidget) bindManager(manager *UIManager) {
|
|
|
|
b.manager = manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSize returns the size of the widget
|
|
|
|
func (b *BaseWidget) GetSize() (width, height int) {
|
|
|
|
return b.width, b.height
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPosition sets the position of the widget
|
|
|
|
func (b *BaseWidget) SetPosition(x, y int) {
|
|
|
|
b.x, b.y = x, y
|
|
|
|
}
|
|
|
|
|
|
|
|
// OffsetPosition moves the widget by x and y
|
|
|
|
func (b *BaseWidget) OffsetPosition(x, y int) {
|
|
|
|
b.x += x
|
|
|
|
b.y += y
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPosition returns the position of the widget
|
|
|
|
func (b *BaseWidget) GetPosition() (x, y int) {
|
|
|
|
return b.x, b.y
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVisible returns whether the widget is visible
|
|
|
|
func (b *BaseWidget) GetVisible() (visible bool) {
|
|
|
|
return b.visible
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVisible make the widget visible, not visible
|
|
|
|
func (b *BaseWidget) SetVisible(visible bool) {
|
|
|
|
b.visible = visible
|
|
|
|
}
|
2020-11-06 07:28:24 -05:00
|
|
|
|
|
|
|
// GetRenderPriority returns the order in which this widget is rendered
|
|
|
|
func (b *BaseWidget) GetRenderPriority() (prio RenderPriority) {
|
|
|
|
return b.renderPriority
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRenderPriority sets the order in which this widget is rendered
|
|
|
|
func (b *BaseWidget) SetRenderPriority(prio RenderPriority) {
|
|
|
|
b.renderPriority = prio
|
|
|
|
}
|
2020-11-13 15:08:43 -05:00
|
|
|
|
|
|
|
// OnHoverStart sets a function that is called if the hovering of the widget starts
|
|
|
|
func (b *BaseWidget) OnHoverStart(callback func()) {
|
|
|
|
b.onHoverStartCb = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// HoverStart is called when the hovering of the widget starts
|
|
|
|
func (b *BaseWidget) hoverStart() {
|
|
|
|
b.hovered = true
|
|
|
|
if b.onHoverStartCb != nil {
|
|
|
|
b.onHoverStartCb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnHoverEnd sets a function that is called if the hovering of the widget ends
|
|
|
|
func (b *BaseWidget) OnHoverEnd(callback func()) {
|
|
|
|
b.onHoverEndCb = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// hoverEnd is called when the widget hovering ends
|
|
|
|
func (b *BaseWidget) hoverEnd() {
|
|
|
|
b.hovered = false
|
|
|
|
if b.onHoverEndCb != nil {
|
|
|
|
b.onHoverEndCb()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseWidget) isHovered() bool {
|
|
|
|
return b.hovered
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains determines whether a given x,y coordinate lands within a Widget
|
|
|
|
func (b *BaseWidget) Contains(x, y int) bool {
|
|
|
|
wx, wy := b.GetPosition()
|
|
|
|
ww, wh := b.GetSize()
|
|
|
|
|
|
|
|
return x >= wx && x <= wx+ww && y >= wy && y <= wy+wh
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetManager returns the uiManager
|
|
|
|
func (b *BaseWidget) GetManager() (ui *UIManager) {
|
|
|
|
return b.manager
|
|
|
|
}
|
2020-11-16 04:41:01 -05:00
|
|
|
|
|
|
|
// OnMouseMove is called when the mouse is moved
|
|
|
|
func (b *BaseWidget) OnMouseMove(x, y int) {
|
|
|
|
if b.Contains(x, y) {
|
|
|
|
if !b.isHovered() {
|
|
|
|
b.hoverStart()
|
|
|
|
}
|
|
|
|
} else if b.isHovered() {
|
|
|
|
b.hoverEnd()
|
|
|
|
}
|
|
|
|
}
|