1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-12-25 11:36:26 -05:00
OpenDiablo2/d2core/d2gui/label_button.go
gucio321 9ffbf1320c
D2core logger (#934)
* logger for d2audio & d2map

* logger for d2ui e.t.c

* d2inventory now passes on error messages

* no more importing log in d2core

* implemented #925

* added logger to part of d2networking & fixed "need to be changed" comments

* fixed lints

* fixed errors

Co-authored-by: M. Sz <mszeptuch@protonmail.com>
2020-11-21 02:33:22 -08:00

107 lines
2.5 KiB
Go

package d2gui
import (
"image/color"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
// LabelButton is a label that can change when hovered and has
// a callback function that can be called when clicked
type LabelButton struct {
label string
callback func()
hoverColor color.RGBA
canHover bool
isHovered bool
layout *Layout
x, y int
*d2util.Logger
}
// NewLabelButton generates a new instance of LabelButton
func NewLabelButton(x, y int, text string, col color.RGBA, l d2util.LogLevel, callback func()) *LabelButton {
lb := &LabelButton{
x: x,
y: y,
hoverColor: col,
label: text,
callback: callback,
canHover: true,
}
lb.Logger = d2util.NewLogger()
lb.Logger.SetLevel(l)
lb.Logger.SetPrefix(logPrefix)
return lb
}
// IsInRect checks if the given point is within the overlay layout rectangle
func (lb *LabelButton) IsInRect(px, py int) bool {
if lb.layout == nil {
return false
}
ww, hh := lb.layout.GetSize()
x, y := lb.layout.Sx, lb.layout.Sy
if px >= x && px <= x+ww && py >= y && py <= y+hh {
return true
}
return false
}
// Load sets the button handlers and sets the layouts
func (lb *LabelButton) Load(renderer d2interface.Renderer, asset *d2asset.AssetManager) {
mainLayout := CreateLayout(renderer, PositionTypeAbsolute, asset)
l, _ := mainLayout.AddLabelWithColor(lb.label, FontStyleFormal11Units, d2util.Color(ColorBrown))
if lb.canHover {
l.SetHoverColor(lb.hoverColor)
}
mainLayout.SetMouseEnterHandler(func(event d2interface.MouseMoveEvent) {
if err := l.SetIsHovered(true); err != nil {
lb.Errorf("could not change label to hover state: %v", err)
}
})
mainLayout.SetMouseLeaveHandler(func(event d2interface.MouseMoveEvent) {
if err := l.SetIsHovered(false); err != nil {
lb.Errorf("could not change label to hover state: %v", err)
}
})
lb.layout = mainLayout
}
// SetLabel sets the text of label label
func (lb *LabelButton) SetLabel(val string) {
lb.label = val
}
// SetHoverColor sets the hover color of the Label
func (lb *LabelButton) SetHoverColor(col color.RGBA) {
lb.hoverColor = col
}
// SetCanHover sets the value of canHover
func (lb *LabelButton) SetCanHover(val bool) {
lb.canHover = val
}
// IsHovered returns the value of isHovered
func (lb *LabelButton) IsHovered() bool {
return lb.isHovered
}
// GetLayout returns the laout of the label
func (lb *LabelButton) GetLayout() *Layout {
return lb.layout
}