2020-02-24 22:35:21 -05:00
|
|
|
package d2gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-24 22:35:21 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
|
|
)
|
|
|
|
|
|
|
|
type buttonState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
buttonStateDefault buttonState = iota
|
|
|
|
buttonStatePressed
|
|
|
|
buttonStatePressedToggled
|
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
const (
|
|
|
|
grey = 0x404040ff
|
|
|
|
)
|
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
// Button is a user actionable drawable toggle switch
|
2020-02-24 22:35:21 -05:00
|
|
|
type Button struct {
|
|
|
|
widgetBase
|
|
|
|
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
state buttonState
|
2020-06-29 00:41:58 -04:00
|
|
|
surfaces []d2interface.Surface
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
func createButton(renderer d2interface.Renderer, text string, buttonStyle ButtonStyle) (*Button, error) {
|
2020-02-24 22:35:21 -05:00
|
|
|
config, ok := buttonStyleConfigs[buttonStyle]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("invalid button style")
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
animation, loadErr := d2asset.LoadAnimation(config.animationPath, config.palettePath)
|
|
|
|
if loadErr != nil {
|
|
|
|
return nil, loadErr
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var buttonWidth int
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
for i := 0; i < config.segmentsX; i++ {
|
|
|
|
w, _, err := animation.GetFrameSize(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonWidth += w
|
|
|
|
}
|
|
|
|
|
|
|
|
var buttonHeight int
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
for i := 0; i < config.segmentsY; i++ {
|
|
|
|
_, h, err := animation.GetFrameSize(i * config.segmentsY)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonHeight += h
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
font, loadErr := loadFont(config.fontStyle)
|
|
|
|
if loadErr != nil {
|
|
|
|
return nil, loadErr
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
textColor := rgbaColor(grey)
|
2020-02-24 22:35:21 -05:00
|
|
|
textWidth, textHeight := font.GetTextMetrics(text)
|
2020-07-18 18:06:36 -04:00
|
|
|
textX := half(buttonWidth) - half(textWidth)
|
|
|
|
textY := half(buttonHeight) - half(textHeight) + config.textOffset
|
2020-02-24 22:35:21 -05:00
|
|
|
|
|
|
|
surfaceCount := animation.GetFrameCount() / (config.segmentsX * config.segmentsY)
|
2020-06-29 00:41:58 -04:00
|
|
|
surfaces := make([]d2interface.Surface, surfaceCount)
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
for i := 0; i < surfaceCount; i++ {
|
2020-07-18 18:06:36 -04:00
|
|
|
surface, surfaceErr := renderer.NewSurface(buttonWidth, buttonHeight, d2enum.FilterNearest)
|
|
|
|
if surfaceErr != nil {
|
|
|
|
return nil, surfaceErr
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
segX, segY, frame := config.segmentsX, config.segmentsY, i
|
|
|
|
if segErr := renderSegmented(animation, segX, segY, frame, surface); segErr != nil {
|
|
|
|
return nil, segErr
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
font.SetColor(textColor)
|
|
|
|
|
|
|
|
var textOffsetX, textOffsetY int
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-02-24 22:35:21 -05:00
|
|
|
switch buttonState(i) {
|
|
|
|
case buttonStatePressed, buttonStatePressedToggled:
|
|
|
|
textOffsetX = -2
|
|
|
|
textOffsetY = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
surface.PushTranslation(textX+textOffsetX, textY+textOffsetY)
|
2020-07-18 18:06:36 -04:00
|
|
|
surfaceErr = font.RenderText(text, surface)
|
2020-02-24 22:35:21 -05:00
|
|
|
surface.Pop()
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
if surfaceErr != nil {
|
|
|
|
return nil, surfaceErr
|
2020-02-24 22:35:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
surfaces[i] = surface
|
|
|
|
}
|
|
|
|
|
|
|
|
button := &Button{width: buttonWidth, height: buttonHeight, surfaces: surfaces}
|
|
|
|
button.SetVisible(true)
|
|
|
|
|
|
|
|
return button, nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (b *Button) onMouseButtonDown(_ d2interface.MouseEvent) bool {
|
2020-02-24 22:35:21 -05:00
|
|
|
b.state = buttonStatePressed
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (b *Button) onMouseButtonUp(_ d2interface.MouseEvent) bool {
|
2020-02-24 22:35:21 -05:00
|
|
|
b.state = buttonStateDefault
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (b *Button) onMouseLeave(_ d2interface.MouseMoveEvent) bool {
|
2020-02-24 22:35:21 -05:00
|
|
|
b.state = buttonStateDefault
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (b *Button) render(target d2interface.Surface) error {
|
2020-02-24 22:35:21 -05:00
|
|
|
return target.Render(b.surfaces[b.state])
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (b *Button) getSize() (width, height int) {
|
2020-02-24 22:35:21 -05:00
|
|
|
return b.width, b.height
|
|
|
|
}
|