1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-02 17:27:23 -04:00
OpenDiablo2/d2core/d2gui/label_button.go
Julien Ganichot 0d691dbffa
Key binding menu (#918)
* Feat(KeyBindingMenu): Adds dynamic box system with scrollbar

* Feat(Hotkeys): WIP Adds a lot of things

* Feat(KeyBindingMenu): WIP Adds logic to binding

* Feat(KeyBindingMenu): Fixes assignment logic

* Feat(KeyBindingMenu): Adds buttons logic

* Feat(KeyBindingMenu): Fixes sprites positions+add padding to Box

* Feat(KeyBindingMenu): Adds label blinking cap

* Feat(KeyBindingMenu): Removes commented func

* Feat(KeyBindingMenu): Fixes lint errors and refactors a bit

* Feat(KeyBindingMenu): Corrects few minor things from Grave

* Feat(KeyBindingMenu): removes forgotten key to string mapping
2020-11-13 12:03:30 -08:00

100 lines
2.4 KiB
Go

package d2gui
import (
"image/color"
"log"
"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
}
// NewLabelButton generates a new instance of LabelButton
func NewLabelButton(x, y int, text string, col color.RGBA, callback func()) *LabelButton {
return &LabelButton{
x: x,
y: y,
hoverColor: col,
label: text,
callback: callback,
canHover: true,
}
}
// 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 {
log.Printf("could not change label to hover state: %v", err)
}
})
mainLayout.SetMouseLeaveHandler(func(event d2interface.MouseMoveEvent) {
if err := l.SetIsHovered(false); err != nil {
log.Printf("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
}