2019-11-10 17:17:42 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
2020-01-26 00:39:13 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-11-10 17:17:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Checkbox struct {
|
|
|
|
x, y int
|
2019-11-10 17:52:30 -05:00
|
|
|
checkState bool
|
2019-11-10 17:17:42 -05:00
|
|
|
visible bool
|
2019-12-21 20:53:18 -05:00
|
|
|
width, height int
|
2020-02-01 20:39:28 -05:00
|
|
|
Image d2render.Surface
|
|
|
|
checkedImage d2render.Surface
|
2019-11-10 17:17:42 -05:00
|
|
|
onClick func()
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func CreateCheckbox(checkState bool) Checkbox {
|
2019-11-10 17:17:42 -05:00
|
|
|
result := Checkbox{
|
2019-11-10 17:52:30 -05:00
|
|
|
checkState: checkState,
|
|
|
|
visible: true,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
enabled: true,
|
2019-11-10 17:17:42 -05:00
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
animation, _ := d2asset.LoadAnimation(d2resource.Checkbox, d2resource.PaletteFechar)
|
|
|
|
checkboxSprite, _ := LoadSprite(animation)
|
2019-12-21 20:53:18 -05:00
|
|
|
result.width, result.height, _ = checkboxSprite.GetFrameSize(0)
|
|
|
|
checkboxSprite.SetPosition(0, 0)
|
2019-11-10 17:17:42 -05:00
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
_, result.Image = d2render.NewSurface(int(result.width), int(result.height), d2render.FilterNearest)
|
2020-01-31 23:18:11 -05:00
|
|
|
checkboxSprite.RenderSegmented(result.Image, 1, 1, 0)
|
2019-11-10 17:17:42 -05:00
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
_, result.checkedImage = d2render.NewSurface(int(result.width), int(result.height), d2render.FilterNearest)
|
2020-01-31 23:18:11 -05:00
|
|
|
checkboxSprite.RenderSegmented(result.checkedImage, 1, 1, 1)
|
2019-11-10 17:17:42 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (v *Checkbox) Render(target d2render.Surface) {
|
|
|
|
target.PushCompositeMode(d2render.CompositeModeSourceAtop)
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(v.x, v.y)
|
2020-02-01 20:39:28 -05:00
|
|
|
target.PushFilter(d2render.FilterNearest)
|
2019-12-28 16:46:08 -05:00
|
|
|
defer target.PopN(3)
|
|
|
|
|
|
|
|
if v.checkState {
|
|
|
|
target.Render(v.checkedImage)
|
2019-11-10 17:17:42 -05:00
|
|
|
} else {
|
2019-12-28 16:46:08 -05:00
|
|
|
target.Render(v.Image)
|
2019-11-10 17:17:42 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-28 23:32:24 -05:00
|
|
|
|
|
|
|
func (v *Checkbox) Advance(elapsed float64) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Checkbox) GetEnabled() bool {
|
2019-11-10 17:17:42 -05:00
|
|
|
return v.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Checkbox) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) SetPressed(pressed bool) {
|
2019-11-10 17:52:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Checkbox) SetCheckState(checkState bool) {
|
|
|
|
v.checkState = checkState
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetCheckState() bool {
|
2019-11-10 17:52:30 -05:00
|
|
|
return v.checkState
|
2019-11-10 17:17:42 -05:00
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetPressed() bool {
|
2019-11-10 17:52:30 -05:00
|
|
|
return v.checkState
|
2019-11-10 17:17:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Checkbox) OnActivated(callback func()) {
|
|
|
|
v.onClick = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Checkbox) Activate() {
|
2019-11-10 17:52:30 -05:00
|
|
|
v.checkState = !v.checkState
|
2019-11-10 17:17:42 -05:00
|
|
|
if v.onClick == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.onClick()
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetPosition() (int, int) {
|
2019-11-10 17:17:42 -05:00
|
|
|
return v.x, v.y
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetSize() (int, int) {
|
2019-11-10 17:17:42 -05:00
|
|
|
return v.width, v.height
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetVisible() bool {
|
2019-11-10 17:17:42 -05:00
|
|
|
return v.visible
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (v *Checkbox) SetPosition(x int, y int) {
|
2019-11-10 17:17:42 -05:00
|
|
|
v.x = x
|
|
|
|
v.y = y
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Checkbox) SetVisible(visible bool) {
|
|
|
|
v.visible = visible
|
|
|
|
}
|