2019-11-10 08:51:02 -05:00
|
|
|
package d2ui
|
2019-10-25 18:40:27 -04:00
|
|
|
|
2019-10-25 22:20:36 -04:00
|
|
|
import (
|
2020-07-23 12:56:50 -04:00
|
|
|
"fmt"
|
2019-10-25 23:41:54 -04:00
|
|
|
"image"
|
2020-09-23 13:30:54 -04:00
|
|
|
"log"
|
2019-10-25 22:20:36 -04:00
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gui"
|
2019-10-25 22:20:36 -04:00
|
|
|
)
|
|
|
|
|
2019-10-25 23:41:54 -04:00
|
|
|
// ButtonType defines the type of button
|
|
|
|
type ButtonType int
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
// ButtonType constants
|
2019-10-25 23:41:54 -04:00
|
|
|
const (
|
2019-11-11 23:48:55 -05:00
|
|
|
ButtonTypeWide ButtonType = 1
|
|
|
|
ButtonTypeMedium ButtonType = 2
|
|
|
|
ButtonTypeNarrow ButtonType = 3
|
|
|
|
ButtonTypeCancel ButtonType = 4
|
|
|
|
ButtonTypeTall ButtonType = 5
|
|
|
|
ButtonTypeShort ButtonType = 6
|
|
|
|
ButtonTypeOkCancel ButtonType = 7
|
2019-10-25 23:41:54 -04:00
|
|
|
|
|
|
|
// Game UI
|
|
|
|
|
|
|
|
ButtonTypeSkill ButtonType = 7
|
|
|
|
ButtonTypeRun ButtonType = 8
|
|
|
|
ButtonTypeMenu ButtonType = 9
|
|
|
|
ButtonTypeGoldCoin ButtonType = 10
|
|
|
|
ButtonTypeClose ButtonType = 11
|
|
|
|
ButtonTypeSecondaryInvHand ButtonType = 12
|
|
|
|
ButtonTypeMinipanelCharacter ButtonType = 13
|
|
|
|
ButtonTypeMinipanelInventory ButtonType = 14
|
|
|
|
ButtonTypeMinipanelSkill ButtonType = 15
|
|
|
|
ButtonTypeMinipanelAutomap ButtonType = 16
|
|
|
|
ButtonTypeMinipanelMessage ButtonType = 17
|
|
|
|
ButtonTypeMinipanelQuest ButtonType = 18
|
|
|
|
ButtonTypeMinipanelMen ButtonType = 19
|
2020-09-20 02:54:24 -04:00
|
|
|
ButtonTypeSquareClose ButtonType = 20
|
2020-10-22 12:54:45 -04:00
|
|
|
ButtonTypeSkillTreeTab ButtonType = 21
|
|
|
|
|
|
|
|
ButtonNoFixedWidth int = -1
|
|
|
|
ButtonNoFixedHeight int = -1
|
2019-10-25 23:41:54 -04:00
|
|
|
)
|
|
|
|
|
2020-10-22 10:02:32 -04:00
|
|
|
const (
|
|
|
|
closeButtonBaseFrame = 10 // base frame offset of the "close" button dc6
|
|
|
|
)
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
const (
|
2020-09-20 02:54:24 -04:00
|
|
|
greyAlpha100 = 0x646464ff
|
|
|
|
lightGreyAlpha75 = 0x808080c3
|
2020-10-22 12:54:45 -04:00
|
|
|
whiteAlpha100 = 0xffffffff
|
2020-08-06 10:30:23 -04:00
|
|
|
)
|
|
|
|
|
2019-10-25 23:41:54 -04:00
|
|
|
// ButtonLayout defines the type of buttons
|
|
|
|
type ButtonLayout struct {
|
2020-07-23 12:56:50 -04:00
|
|
|
ResourceName string
|
|
|
|
PaletteName string
|
|
|
|
FontPath string
|
|
|
|
XSegments int
|
|
|
|
YSegments int
|
|
|
|
BaseFrame int
|
|
|
|
DisabledFrame int
|
|
|
|
ClickableRect *image.Rectangle
|
|
|
|
TextOffset int
|
|
|
|
Toggleable bool
|
|
|
|
AllowFrameChange bool
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage bool
|
|
|
|
FixedWidth int
|
|
|
|
FixedHeight int
|
|
|
|
LabelColor uint32
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
const (
|
|
|
|
buttonWideSegmentsX = 2
|
|
|
|
buttonWideSegmentsY = 1
|
|
|
|
buttonWideDisabledFrame = -1
|
|
|
|
buttonWideTextOffset = 1
|
|
|
|
|
|
|
|
buttonShortSegmentsX = 1
|
|
|
|
buttonShortSegmentsY = 1
|
|
|
|
buttonShortDisabledFrame = -1
|
|
|
|
buttonShortTextOffset = -1
|
|
|
|
|
|
|
|
buttonMediumSegmentsX = 1
|
|
|
|
buttonMediumSegmentsY = 1
|
|
|
|
|
|
|
|
buttonTallSegmentsX = 1
|
|
|
|
buttonTallSegmentsY = 1
|
|
|
|
buttonTallTextOffset = 5
|
|
|
|
|
|
|
|
buttonOkCancelSegmentsX = 1
|
|
|
|
buttonOkCancelSegmentsY = 1
|
|
|
|
buttonOkCancelDisabledFrame = -1
|
|
|
|
|
2020-09-20 02:54:24 -04:00
|
|
|
buttonBuySellSegmentsX = 1
|
|
|
|
buttonBuySellSegmentsY = 1
|
|
|
|
buttonBuySellDisabledFrame = 1
|
2020-09-15 23:37:08 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
buttonRunSegmentsX = 1
|
|
|
|
buttonRunSegmentsY = 1
|
|
|
|
buttonRunDisabledFrame = -1
|
|
|
|
|
|
|
|
pressedButtonOffset = 2
|
|
|
|
)
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func getButtonLayouts() map[ButtonType]ButtonLayout {
|
|
|
|
return map[ButtonType]ButtonLayout{
|
|
|
|
ButtonTypeWide: {
|
2020-08-06 10:30:23 -04:00
|
|
|
XSegments: buttonWideSegmentsX,
|
|
|
|
YSegments: buttonWideSegmentsY,
|
|
|
|
DisabledFrame: buttonWideDisabledFrame,
|
|
|
|
TextOffset: buttonWideTextOffset,
|
|
|
|
ResourceName: d2resource.WideButtonBlank,
|
|
|
|
PaletteName: d2resource.PaletteUnits,
|
|
|
|
FontPath: d2resource.FontExocet10,
|
|
|
|
AllowFrameChange: true,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
2020-08-06 10:30:23 -04:00
|
|
|
},
|
2020-07-23 12:56:50 -04:00
|
|
|
ButtonTypeShort: {
|
2020-08-06 10:30:23 -04:00
|
|
|
XSegments: buttonShortSegmentsX,
|
|
|
|
YSegments: buttonShortSegmentsY,
|
|
|
|
DisabledFrame: buttonShortDisabledFrame,
|
|
|
|
TextOffset: buttonShortTextOffset,
|
|
|
|
ResourceName: d2resource.ShortButtonBlank,
|
|
|
|
PaletteName: d2resource.PaletteUnits,
|
|
|
|
FontPath: d2resource.FontRediculous,
|
|
|
|
AllowFrameChange: true,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
2020-08-06 10:30:23 -04:00
|
|
|
},
|
2020-07-23 12:56:50 -04:00
|
|
|
ButtonTypeMedium: {
|
2020-08-06 10:30:23 -04:00
|
|
|
XSegments: buttonMediumSegmentsX,
|
|
|
|
YSegments: buttonMediumSegmentsY,
|
|
|
|
ResourceName: d2resource.MediumButtonBlank,
|
|
|
|
PaletteName: d2resource.PaletteUnits,
|
|
|
|
FontPath: d2resource.FontExocet10,
|
|
|
|
AllowFrameChange: true,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
2020-08-06 10:30:23 -04:00
|
|
|
},
|
2020-07-23 12:56:50 -04:00
|
|
|
ButtonTypeTall: {
|
2020-08-06 10:30:23 -04:00
|
|
|
XSegments: buttonTallSegmentsX,
|
|
|
|
YSegments: buttonTallSegmentsY,
|
|
|
|
TextOffset: buttonTallTextOffset,
|
|
|
|
ResourceName: d2resource.TallButtonBlank,
|
|
|
|
PaletteName: d2resource.PaletteUnits,
|
|
|
|
FontPath: d2resource.FontExocet10,
|
|
|
|
AllowFrameChange: true,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
2020-08-06 10:30:23 -04:00
|
|
|
},
|
2020-07-23 12:56:50 -04:00
|
|
|
ButtonTypeOkCancel: {
|
2020-08-06 10:30:23 -04:00
|
|
|
XSegments: buttonOkCancelSegmentsX,
|
|
|
|
YSegments: buttonOkCancelSegmentsY,
|
|
|
|
DisabledFrame: buttonOkCancelDisabledFrame,
|
|
|
|
ResourceName: d2resource.CancelButton,
|
|
|
|
PaletteName: d2resource.PaletteUnits,
|
|
|
|
FontPath: d2resource.FontRediculous,
|
|
|
|
AllowFrameChange: true,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
2020-08-06 10:30:23 -04:00
|
|
|
},
|
2020-07-23 12:56:50 -04:00
|
|
|
ButtonTypeRun: {
|
2020-08-06 10:30:23 -04:00
|
|
|
XSegments: buttonRunSegmentsX,
|
|
|
|
YSegments: buttonRunSegmentsY,
|
|
|
|
DisabledFrame: buttonRunDisabledFrame,
|
|
|
|
ResourceName: d2resource.RunButton,
|
|
|
|
PaletteName: d2resource.PaletteSky,
|
|
|
|
Toggleable: true,
|
|
|
|
FontPath: d2resource.FontRediculous,
|
|
|
|
AllowFrameChange: true,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
2020-08-06 10:30:23 -04:00
|
|
|
},
|
2020-09-20 02:54:24 -04:00
|
|
|
ButtonTypeSquareClose: {
|
|
|
|
XSegments: buttonBuySellSegmentsX,
|
|
|
|
YSegments: buttonBuySellSegmentsY,
|
|
|
|
DisabledFrame: buttonBuySellDisabledFrame,
|
|
|
|
ResourceName: d2resource.BuySellButton,
|
|
|
|
PaletteName: d2resource.PaletteUnits,
|
|
|
|
Toggleable: true,
|
|
|
|
FontPath: d2resource.Font30,
|
2020-09-15 23:37:08 -04:00
|
|
|
AllowFrameChange: true,
|
2020-10-22 10:02:32 -04:00
|
|
|
BaseFrame: closeButtonBaseFrame,
|
2020-10-22 12:54:45 -04:00
|
|
|
HasImage: true,
|
|
|
|
FixedWidth: ButtonNoFixedWidth,
|
|
|
|
FixedHeight: ButtonNoFixedHeight,
|
|
|
|
LabelColor: greyAlpha100,
|
|
|
|
},
|
|
|
|
ButtonTypeSkillTreeTab: {
|
|
|
|
XSegments: 1,
|
|
|
|
YSegments: 1,
|
|
|
|
DisabledFrame: 7,
|
|
|
|
ResourceName: d2resource.SkillsPanelAmazon,
|
|
|
|
PaletteName: d2resource.PaletteSky,
|
|
|
|
Toggleable: false,
|
|
|
|
FontPath: d2resource.Font16,
|
|
|
|
AllowFrameChange: false,
|
|
|
|
BaseFrame: 7,
|
|
|
|
HasImage: false,
|
|
|
|
FixedWidth: 93,
|
|
|
|
FixedHeight: 107,
|
|
|
|
LabelColor: whiteAlpha100,
|
2020-09-15 23:37:08 -04:00
|
|
|
},
|
2020-07-23 12:56:50 -04:00
|
|
|
}
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
var _ Widget = &Button{} // static check to ensure button implements widget
|
|
|
|
|
2019-10-25 22:20:36 -04:00
|
|
|
// Button defines a standard wide UI button
|
|
|
|
type Button struct {
|
2020-08-06 10:30:23 -04:00
|
|
|
manager *UIManager
|
2020-07-23 12:56:50 -04:00
|
|
|
buttonLayout ButtonLayout
|
2020-06-29 00:41:58 -04:00
|
|
|
normalSurface d2interface.Surface
|
|
|
|
pressedSurface d2interface.Surface
|
|
|
|
toggledSurface d2interface.Surface
|
|
|
|
pressedToggledSurface d2interface.Surface
|
|
|
|
disabledSurface d2interface.Surface
|
2020-07-23 12:56:50 -04:00
|
|
|
x int
|
|
|
|
y int
|
|
|
|
width int
|
|
|
|
height int
|
2020-01-31 23:18:11 -05:00
|
|
|
onClick func()
|
2020-07-23 12:56:50 -04:00
|
|
|
enabled bool
|
|
|
|
visible bool
|
|
|
|
pressed bool
|
|
|
|
toggled bool
|
2019-10-25 22:20:36 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// NewButton creates an instance of Button
|
|
|
|
func (ui *UIManager) NewButton(buttonType ButtonType, text string) *Button {
|
|
|
|
btn := &Button{
|
2019-12-21 20:53:18 -05:00
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
visible: true,
|
|
|
|
enabled: true,
|
|
|
|
pressed: false,
|
2019-10-25 22:20:36 -04:00
|
|
|
}
|
2020-08-02 21:26:07 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
buttonLayout := getButtonLayouts()[buttonType]
|
|
|
|
btn.buttonLayout = buttonLayout
|
|
|
|
lbl := ui.NewLabel(buttonLayout.FontPath, d2resource.PaletteUnits)
|
2020-08-02 21:26:07 -04:00
|
|
|
|
2020-07-07 20:16:22 -04:00
|
|
|
lbl.SetText(text)
|
2020-10-22 12:54:45 -04:00
|
|
|
lbl.Color[0] = d2util.Color(buttonLayout.LabelColor)
|
2020-07-07 20:16:22 -04:00
|
|
|
lbl.Alignment = d2gui.HorizontalAlignCenter
|
2019-11-10 03:36:53 -05:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
buttonSprite, err := ui.NewSprite(buttonLayout.ResourceName, buttonLayout.PaletteName)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 12:54:45 -04:00
|
|
|
if buttonLayout.FixedWidth > 0 {
|
|
|
|
btn.width = buttonLayout.FixedWidth
|
|
|
|
} else {
|
|
|
|
for i := 0; i < buttonLayout.XSegments; i++ {
|
|
|
|
w, _, err := buttonSprite.GetFrameSize(i)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
btn.width += w
|
|
|
|
}
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-10-22 12:54:45 -04:00
|
|
|
if buttonLayout.FixedHeight > 0 {
|
|
|
|
btn.height = buttonLayout.FixedHeight
|
|
|
|
} else {
|
|
|
|
for i := 0; i < buttonLayout.YSegments; i++ {
|
|
|
|
_, h, err := buttonSprite.GetFrameSize(i * buttonLayout.YSegments)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-23 13:30:54 -04:00
|
|
|
|
2020-10-22 12:54:45 -04:00
|
|
|
btn.height += h
|
|
|
|
}
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
btn.normalSurface, err = ui.renderer.NewSurface(btn.width, btn.height, d2enum.FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-27 02:58:37 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
buttonSprite.SetPosition(0, 0)
|
2020-07-08 21:57:35 -04:00
|
|
|
buttonSprite.SetEffect(d2enum.DrawEffectModulate)
|
2020-07-07 20:16:22 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
ui.addWidget(btn) // important that this comes before renderFrames!
|
|
|
|
|
|
|
|
btn.renderFrames(buttonSprite, &buttonLayout, lbl)
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
return btn
|
2020-07-23 12:56:50 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
func (v *Button) renderFrames(btnSprite *Sprite, btnLayout *ButtonLayout, label *Label) {
|
2020-09-23 13:30:54 -04:00
|
|
|
var err error
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
totalButtonTypes := btnSprite.GetFrameCount() / (btnLayout.XSegments * btnLayout.YSegments)
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-10-22 12:54:45 -04:00
|
|
|
if v.buttonLayout.HasImage {
|
|
|
|
err = btnSprite.RenderSegmented(v.normalSurface, btnLayout.XSegments, btnLayout.YSegments, btnLayout.BaseFrame)
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-10-22 12:54:45 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to render button normalSurface, err: %v\n", err)
|
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, labelHeight := label.GetSize()
|
2020-08-06 10:30:23 -04:00
|
|
|
textY := half(v.height - labelHeight)
|
|
|
|
xOffset := half(v.width)
|
2020-07-23 12:56:50 -04:00
|
|
|
|
|
|
|
label.SetPosition(xOffset, textY)
|
|
|
|
label.Render(v.normalSurface)
|
|
|
|
|
2020-10-22 12:54:45 -04:00
|
|
|
if btnLayout.HasImage && btnLayout.AllowFrameChange {
|
2020-08-06 10:30:23 -04:00
|
|
|
frameOffset := 0
|
|
|
|
xSeg, ySeg, baseFrame := btnLayout.XSegments, btnLayout.YSegments, btnLayout.BaseFrame
|
|
|
|
|
|
|
|
totalButtonTypes--
|
|
|
|
if totalButtonTypes > 0 { // button has more than one type
|
|
|
|
frameOffset++
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
v.pressedSurface, err = v.manager.renderer.NewSurface(v.width, v.height,
|
2020-08-06 10:30:23 -04:00
|
|
|
d2enum.FilterNearest)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
err = btnSprite.RenderSegmented(v.pressedSurface, xSeg, ySeg, baseFrame+frameOffset)
|
2020-07-23 12:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to render button pressedSurface, err: %v\n", err)
|
|
|
|
}
|
2020-07-07 20:16:22 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
label.SetPosition(xOffset-pressedButtonOffset, textY+pressedButtonOffset)
|
2020-07-23 12:56:50 -04:00
|
|
|
label.Render(v.pressedSurface)
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-09-20 02:54:24 -04:00
|
|
|
if btnLayout.ResourceName == d2resource.BuySellButton {
|
|
|
|
// Without returning early, the button UI gets all subsequent (unrelated) frames stacked on top
|
|
|
|
// Only 2 frames from this sprite are applicable to the button in question
|
|
|
|
// The presentation is incorrect without this hack
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
totalButtonTypes--
|
|
|
|
if totalButtonTypes > 0 { // button has more than two types
|
|
|
|
frameOffset++
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
v.toggledSurface, err = v.manager.renderer.NewSurface(v.width, v.height,
|
2020-08-06 10:30:23 -04:00
|
|
|
d2enum.FilterNearest)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
err = btnSprite.RenderSegmented(v.pressedSurface, xSeg, ySeg, baseFrame+frameOffset)
|
2020-07-23 12:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to render button toggledSurface, err: %v\n", err)
|
|
|
|
}
|
2020-07-07 20:16:22 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
label.SetPosition(xOffset, textY)
|
|
|
|
label.Render(v.toggledSurface)
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
totalButtonTypes--
|
|
|
|
if totalButtonTypes > 0 { // button has more than three types
|
|
|
|
frameOffset++
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
v.pressedToggledSurface, err = v.manager.renderer.NewSurface(v.width, v.height,
|
2020-08-06 10:30:23 -04:00
|
|
|
d2enum.FilterNearest)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
err = btnSprite.RenderSegmented(v.pressedSurface, xSeg, ySeg, baseFrame+frameOffset)
|
2020-07-23 12:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to render button pressedToggledSurface, err: %v\n", err)
|
|
|
|
}
|
2020-07-07 20:16:22 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
label.SetPosition(xOffset, textY)
|
|
|
|
label.Render(v.pressedToggledSurface)
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
if btnLayout.DisabledFrame != -1 {
|
2020-09-23 13:30:54 -04:00
|
|
|
v.disabledSurface, err = v.manager.renderer.NewSurface(v.width, v.height,
|
2020-08-06 10:30:23 -04:00
|
|
|
d2enum.FilterNearest)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-07-07 20:16:22 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
err = btnSprite.RenderSegmented(v.disabledSurface, xSeg, ySeg, btnLayout.DisabledFrame)
|
2020-07-23 12:56:50 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to render button disabledSurface, err: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
label.SetPosition(xOffset, textY)
|
|
|
|
label.Render(v.disabledSurface)
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-25 22:20:36 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// bindManager binds the button to the UI manager
|
|
|
|
func (v *Button) bindManager(manager *UIManager) {
|
|
|
|
v.manager = manager
|
|
|
|
}
|
|
|
|
|
2019-10-25 23:41:54 -04:00
|
|
|
// OnActivated defines the callback handler for the activate event
|
|
|
|
func (v *Button) OnActivated(callback func()) {
|
|
|
|
v.onClick = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activate calls the on activated callback handler, if any
|
2019-11-11 23:48:55 -05:00
|
|
|
func (v *Button) Activate() {
|
2019-10-25 23:41:54 -04:00
|
|
|
if v.onClick == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2019-10-25 23:41:54 -04:00
|
|
|
v.onClick()
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
// Render renders the button
|
2020-07-26 14:52:54 -04:00
|
|
|
func (v *Button) Render(target d2interface.Surface) error {
|
2020-07-06 21:26:08 -04:00
|
|
|
target.PushFilter(d2enum.FilterNearest)
|
2020-08-06 10:30:23 -04:00
|
|
|
defer target.Pop()
|
2020-07-23 12:56:50 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
target.PushTranslation(v.x, v.y)
|
|
|
|
defer target.Pop()
|
2019-10-25 23:41:54 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case !v.enabled:
|
2020-09-08 15:58:35 -04:00
|
|
|
target.PushColor(d2util.Color(lightGreyAlpha75))
|
2019-12-28 16:46:08 -05:00
|
|
|
defer target.Pop()
|
2020-07-23 12:56:50 -04:00
|
|
|
err = target.Render(v.disabledSurface)
|
|
|
|
case v.toggled && v.pressed:
|
|
|
|
err = target.Render(v.pressedToggledSurface)
|
|
|
|
case v.pressed:
|
2020-10-22 12:54:45 -04:00
|
|
|
if v.buttonLayout.AllowFrameChange {
|
|
|
|
err = target.Render(v.pressedSurface)
|
|
|
|
} else {
|
|
|
|
err = target.Render(v.normalSurface)
|
|
|
|
}
|
2020-07-23 12:56:50 -04:00
|
|
|
case v.toggled:
|
|
|
|
err = target.Render(v.toggledSurface)
|
|
|
|
default:
|
|
|
|
err = target.Render(v.normalSurface)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to render button surface, err: %v\n", err)
|
2019-10-25 22:20:36 -04:00
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
|
|
|
|
return nil
|
2019-10-25 22:20:36 -04:00
|
|
|
}
|
2020-06-24 15:23:38 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
// Toggle negates the toggled state of the button
|
2020-06-24 15:23:38 -04:00
|
|
|
func (v *Button) Toggle() {
|
|
|
|
v.toggled = !v.toggled
|
|
|
|
}
|
2019-10-25 22:20:36 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
// Advance advances the button state
|
2020-08-06 10:30:23 -04:00
|
|
|
func (v *Button) Advance(_ float64) error {
|
|
|
|
return nil
|
2019-12-28 23:32:24 -05:00
|
|
|
}
|
|
|
|
|
2019-10-25 22:20:36 -04:00
|
|
|
// GetEnabled returns the enabled state
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Button) GetEnabled() bool {
|
2019-10-25 22:20:36 -04:00
|
|
|
return v.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEnabled sets the enabled state
|
|
|
|
func (v *Button) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
// GetSize returns the size of the button
|
|
|
|
func (v *Button) GetSize() (width, height int) {
|
2019-10-25 22:20:36 -04:00
|
|
|
return v.width, v.height
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
// SetPosition moves the button
|
|
|
|
func (v *Button) SetPosition(x, y int) {
|
2019-10-25 22:20:36 -04:00
|
|
|
v.x = x
|
|
|
|
v.y = y
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
// GetPosition returns the location of the button
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Button) GetPosition() (x, y int) {
|
2019-10-25 22:20:36 -04:00
|
|
|
return v.x, v.y
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVisible returns the visibility of the button
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Button) GetVisible() bool {
|
2019-10-25 22:20:36 -04:00
|
|
|
return v.visible
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVisible sets the visibility of the button
|
|
|
|
func (v *Button) SetVisible(visible bool) {
|
|
|
|
v.visible = visible
|
2019-10-25 18:40:27 -04:00
|
|
|
}
|
2019-10-25 23:41:54 -04:00
|
|
|
|
|
|
|
// GetPressed returns the pressed state of the button
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Button) GetPressed() bool {
|
2019-10-25 23:41:54 -04:00
|
|
|
return v.pressed
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPressed sets the pressed state of the button
|
|
|
|
func (v *Button) SetPressed(pressed bool) {
|
|
|
|
v.pressed = pressed
|
|
|
|
}
|
2020-08-06 10:30:23 -04:00
|
|
|
|
|
|
|
func half(n int) int {
|
|
|
|
return n / 2
|
|
|
|
}
|