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
|
|
|
)
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// Checkbox represents a checkbox UI element
|
2019-11-10 17:17:42 -05:00
|
|
|
type Checkbox struct {
|
2020-07-26 14:52:54 -04:00
|
|
|
Image d2interface.Surface
|
|
|
|
checkedImage d2interface.Surface
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
onClick func()
|
|
|
|
checkState bool
|
|
|
|
visible bool
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCheckbox creates a new instance of a checkbox
|
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-06 21:26:08 -04:00
|
|
|
result.Image, _ = renderer.NewSurface(result.width, result.height, d2enum.FilterNearest)
|
2019-11-10 17:17:42 -05:00
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
_ = checkboxSprite.RenderSegmented(result.Image, 1, 1, 0)
|
|
|
|
|
2020-07-06 21:26:08 -04:00
|
|
|
result.checkedImage, _ = renderer.NewSurface(result.width, result.height, d2enum.FilterNearest)
|
2020-07-03 14:00:56 -04:00
|
|
|
|
|
|
|
_ = checkboxSprite.RenderSegmented(result.checkedImage, 1, 1, 1)
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2019-11-10 17:17:42 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// Render renders the checkbox
|
|
|
|
func (v *Checkbox) Render(target d2interface.Surface) error {
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(v.x, v.y)
|
2020-07-06 21:26:08 -04:00
|
|
|
target.PushFilter(d2enum.FilterNearest)
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-07-08 21:57:35 -04:00
|
|
|
defer target.PopN(2)
|
2019-12-28 16:46:08 -05:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
|
|
|
|
return nil
|
2019-11-10 17:17:42 -05:00
|
|
|
}
|
2019-12-28 23:32:24 -05:00
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// Advance does nothing for checkboxes
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) Advance(elapsed float64) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// GetEnabled returns the enabled state of the checkbox
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetEnabled() bool {
|
2019-11-10 17:17:42 -05:00
|
|
|
return v.enabled
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// SetEnabled sets the enabled state of the checkbox
|
2019-11-10 17:17:42 -05:00
|
|
|
func (v *Checkbox) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// SetPressed does nothing for checkboxes
|
2020-07-03 14:00:56 -04:00
|
|
|
func (v *Checkbox) SetPressed(_ bool) {
|
2019-11-10 17:52:30 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// SetCheckState sets the check state of the checkbox
|
2019-11-10 17:52:30 -05:00
|
|
|
func (v *Checkbox) SetCheckState(checkState bool) {
|
|
|
|
v.checkState = checkState
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// GetCheckState returns the check state of the checkbox
|
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
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// GetPressed returns the pressed state of the checkbox
|
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
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// OnACtivated sets the callback function of the click event for the checkbox
|
2019-11-10 17:17:42 -05:00
|
|
|
func (v *Checkbox) OnActivated(callback func()) {
|
|
|
|
v.onClick = callback
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// Activate activates the checkbox
|
2019-11-10 17:17:42 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// GetPosition returns the position of the checkbox
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// GetSize returns the size of the checkbox
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// GetVisible returns the visibility state of the checkbox
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Checkbox) GetVisible() bool {
|
2019-11-10 17:17:42 -05:00
|
|
|
return v.visible
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// SetPosition sets the position of the checkbox
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-26 14:52:54 -04:00
|
|
|
// SetVisible sets the visibility of the checkbox
|
2019-11-10 17:17:42 -05:00
|
|
|
func (v *Checkbox) SetVisible(visible bool) {
|
|
|
|
v.visible = visible
|
|
|
|
}
|