2019-11-10 17:17:42 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
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-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"
|
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-06-29 00:41:58 -04:00
|
|
|
Image d2interface.Surface
|
|
|
|
checkedImage d2interface.Surface
|
2019-11-10 17:17:42 -05:00
|
|
|
onClick func()
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
func CreateCheckbox(renderer d2interface.Renderer, 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-07-03 14:00:56 -04:00
|
|
|
result.Image, _ = renderer.NewSurface(result.width, result.height, d2interface.FilterNearest)
|
2019-11-10 17:17:42 -05:00
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
_ = checkboxSprite.RenderSegmented(result.Image, 1, 1, 0)
|
|
|
|
|
|
|
|
result.checkedImage, _ = renderer.NewSurface(result.width, result.height, d2interface.FilterNearest)
|
|
|
|
|
|
|
|
_ = checkboxSprite.RenderSegmented(result.checkedImage, 1, 1, 1)
|
2019-11-10 17:17:42 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (v *Checkbox) Render(target d2interface.Surface) {
|
2020-06-30 09:58:53 -04:00
|
|
|
target.PushCompositeMode(d2enum.CompositeModeSourceAtop)
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(v.x, v.y)
|
2020-06-29 00:41:58 -04:00
|
|
|
target.PushFilter(d2interface.FilterNearest)
|
2019-12-28 16:46:08 -05:00
|
|
|
defer target.PopN(3)
|
|
|
|
|
|
|
|
if v.checkState {
|
2020-07-03 14:00:56 -04:00
|
|
|
_ = target.Render(v.checkedImage)
|
2019-11-10 17:17:42 -05:00
|
|
|
} else {
|
2020-07-03 14:00:56 -04: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
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
func (v *Checkbox) SetPressed(_ 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
|
|
|
|
}
|