2019-11-10 22:17:42 +00:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
2020-09-23 17:30:54 +00:00
|
|
|
"log"
|
|
|
|
|
2020-06-30 13:58:53 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 04:41:58 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-26 05:39:13 +00:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2019-11-10 22:17:42 +00:00
|
|
|
)
|
|
|
|
|
2020-11-09 14:46:08 +00:00
|
|
|
// static check that Checkbox implements Widget
|
|
|
|
var _ Widget = &Checkbox{}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// Checkbox represents a checkbox UI element
|
2019-11-10 22:17:42 +00:00
|
|
|
type Checkbox struct {
|
2020-11-06 11:38:20 +00:00
|
|
|
*BaseWidget
|
2020-07-26 18:52:54 +00:00
|
|
|
Image d2interface.Surface
|
|
|
|
checkedImage d2interface.Surface
|
|
|
|
onClick func()
|
|
|
|
checkState bool
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:30:23 +00:00
|
|
|
// NewCheckbox creates a new instance of a checkbox
|
|
|
|
func (ui *UIManager) NewCheckbox(checkState bool) *Checkbox {
|
2020-09-23 17:30:54 +00:00
|
|
|
var err error
|
2020-10-22 05:12:06 +00:00
|
|
|
|
2020-11-06 11:38:20 +00:00
|
|
|
base := NewBaseWidget(ui)
|
|
|
|
|
2020-08-06 14:30:23 +00:00
|
|
|
result := &Checkbox{
|
2020-11-06 11:38:20 +00:00
|
|
|
BaseWidget: base,
|
2019-11-10 22:52:30 +00:00
|
|
|
checkState: checkState,
|
|
|
|
enabled: true,
|
2019-11-10 22:17:42 +00:00
|
|
|
}
|
2020-02-01 04:18:11 +00:00
|
|
|
|
2020-09-23 17:30:54 +00:00
|
|
|
checkboxSprite, err := ui.NewSprite(d2resource.Checkbox, d2resource.PaletteFechar)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result.width, result.height, err = checkboxSprite.GetFrameSize(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 05:12:06 +00:00
|
|
|
|
2019-12-22 01:53:18 +00:00
|
|
|
checkboxSprite.SetPosition(0, 0)
|
2019-11-10 22:17:42 +00:00
|
|
|
|
2020-10-28 18:17:42 +00:00
|
|
|
result.Image = ui.renderer.NewSurface(result.width, result.height)
|
2019-11-10 22:17:42 +00:00
|
|
|
|
2020-11-11 13:55:59 +00:00
|
|
|
checkboxSprite.RenderSegmented(result.Image, 1, 1, 0)
|
2020-07-03 18:00:56 +00:00
|
|
|
|
2020-10-28 18:17:42 +00:00
|
|
|
result.checkedImage = ui.renderer.NewSurface(result.width, result.height)
|
2020-07-03 18:00:56 +00:00
|
|
|
|
2020-11-11 13:55:59 +00:00
|
|
|
checkboxSprite.RenderSegmented(result.checkedImage, 1, 1, 1)
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2020-08-06 14:30:23 +00:00
|
|
|
ui.addWidget(result)
|
|
|
|
|
2019-11-10 22:17:42 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// Render renders the checkbox
|
2020-11-11 13:55:59 +00:00
|
|
|
func (v *Checkbox) Render(target d2interface.Surface) {
|
2019-12-28 21:46:08 +00:00
|
|
|
target.PushTranslation(v.x, v.y)
|
2020-08-06 14:30:23 +00:00
|
|
|
defer target.Pop()
|
2020-07-26 18:52:54 +00:00
|
|
|
|
2020-08-06 14:30:23 +00:00
|
|
|
target.PushFilter(d2enum.FilterNearest)
|
|
|
|
defer target.Pop()
|
2019-12-28 21:46:08 +00:00
|
|
|
|
|
|
|
if v.checkState {
|
2020-10-28 18:17:42 +00:00
|
|
|
target.Render(v.checkedImage)
|
2019-11-10 22:17:42 +00:00
|
|
|
} else {
|
2020-10-28 18:17:42 +00:00
|
|
|
target.Render(v.Image)
|
2019-11-10 22:17:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 04:32:24 +00:00
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// Advance does nothing for checkboxes
|
2020-08-06 14:30:23 +00:00
|
|
|
func (v *Checkbox) Advance(_ float64) error {
|
|
|
|
return nil
|
2019-12-29 04:32:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// GetEnabled returns the enabled state of the checkbox
|
2019-12-29 04:32:24 +00:00
|
|
|
func (v *Checkbox) GetEnabled() bool {
|
2019-11-10 22:17:42 +00:00
|
|
|
return v.enabled
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// SetEnabled sets the enabled state of the checkbox
|
2019-11-10 22:17:42 +00:00
|
|
|
func (v *Checkbox) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// SetPressed does nothing for checkboxes
|
2020-07-03 18:00:56 +00:00
|
|
|
func (v *Checkbox) SetPressed(_ bool) {
|
2019-11-10 22:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// SetCheckState sets the check state of the checkbox
|
2019-11-10 22:52:30 +00:00
|
|
|
func (v *Checkbox) SetCheckState(checkState bool) {
|
|
|
|
v.checkState = checkState
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// GetCheckState returns the check state of the checkbox
|
2019-12-29 04:32:24 +00:00
|
|
|
func (v *Checkbox) GetCheckState() bool {
|
2019-11-10 22:52:30 +00:00
|
|
|
return v.checkState
|
2019-11-10 22:17:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// GetPressed returns the pressed state of the checkbox
|
2019-12-29 04:32:24 +00:00
|
|
|
func (v *Checkbox) GetPressed() bool {
|
2019-11-10 22:52:30 +00:00
|
|
|
return v.checkState
|
2019-11-10 22:17:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 14:30:23 +00:00
|
|
|
// OnActivated sets the callback function of the click event for the checkbox
|
2019-11-10 22:17:42 +00:00
|
|
|
func (v *Checkbox) OnActivated(callback func()) {
|
|
|
|
v.onClick = callback
|
|
|
|
}
|
|
|
|
|
2020-07-26 18:52:54 +00:00
|
|
|
// Activate activates the checkbox
|
2019-11-10 22:17:42 +00:00
|
|
|
func (v *Checkbox) Activate() {
|
2019-11-10 22:52:30 +00:00
|
|
|
v.checkState = !v.checkState
|
2019-11-10 22:17:42 +00:00
|
|
|
if v.onClick == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 14:30:23 +00:00
|
|
|
|
2019-11-10 22:17:42 +00:00
|
|
|
v.onClick()
|
|
|
|
}
|