1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-02 17:27:23 -04:00
OpenDiablo2/d2core/d2ui/checkbox.go

169 lines
3.5 KiB
Go
Raw Normal View History

package d2ui
import (
"log"
2020-06-30 09:58:53 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
)
2020-07-26 14:52:54 -04:00
// Checkbox represents a checkbox UI element
type Checkbox struct {
manager *UIManager
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
}
// NewCheckbox creates a new instance of a checkbox
func (ui *UIManager) NewCheckbox(checkState bool) *Checkbox {
var err error
result := &Checkbox{
checkState: checkState,
visible: true,
width: 0,
height: 0,
enabled: true,
}
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
}
checkboxSprite.SetPosition(0, 0)
result.Image = ui.renderer.NewSurface(result.width, result.height)
err = checkboxSprite.RenderSegmented(result.Image, 1, 1, 0)
if err != nil {
log.Print(err)
return nil
}
result.checkedImage = ui.renderer.NewSurface(result.width, result.height)
err = checkboxSprite.RenderSegmented(result.checkedImage, 1, 1, 1)
if err != nil {
log.Print(err)
return nil
}
2020-07-26 14:52:54 -04:00
ui.addWidget(result)
return result
}
// bindManager binds the checkbox to the UI manager
func (v *Checkbox) bindManager(manager *UIManager) {
v.manager = manager
}
2020-07-26 14:52:54 -04:00
// Render renders the checkbox
func (v *Checkbox) Render(target d2interface.Surface) error {
target.PushTranslation(v.x, v.y)
defer target.Pop()
2020-07-26 14:52:54 -04:00
target.PushFilter(d2enum.FilterNearest)
defer target.Pop()
if v.checkState {
target.Render(v.checkedImage)
} else {
target.Render(v.Image)
}
2020-07-26 14:52:54 -04:00
return nil
}
2020-07-26 14:52:54 -04:00
// Advance does nothing for checkboxes
func (v *Checkbox) Advance(_ float64) error {
return nil
}
2020-07-26 14:52:54 -04:00
// GetEnabled returns the enabled state of the checkbox
func (v *Checkbox) GetEnabled() bool {
return v.enabled
}
2020-07-26 14:52:54 -04:00
// SetEnabled sets the enabled state of the checkbox
func (v *Checkbox) SetEnabled(enabled bool) {
v.enabled = enabled
}
2020-07-26 14:52:54 -04:00
// SetPressed does nothing for checkboxes
func (v *Checkbox) SetPressed(_ bool) {
}
2020-07-26 14:52:54 -04:00
// SetCheckState sets the check state of the checkbox
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
func (v *Checkbox) GetCheckState() bool {
return v.checkState
}
2020-07-26 14:52:54 -04:00
// GetPressed returns the pressed state of the checkbox
func (v *Checkbox) GetPressed() bool {
return v.checkState
}
// OnActivated sets the callback function of the click event for the checkbox
func (v *Checkbox) OnActivated(callback func()) {
v.onClick = callback
}
2020-07-26 14:52:54 -04:00
// Activate activates the checkbox
func (v *Checkbox) Activate() {
v.checkState = !v.checkState
if v.onClick == nil {
return
}
v.onClick()
}
2020-07-26 14:52:54 -04:00
// GetPosition returns the position of the checkbox
func (v *Checkbox) GetPosition() (x, y int) {
return v.x, v.y
}
2020-07-26 14:52:54 -04:00
// GetSize returns the size of the checkbox
func (v *Checkbox) GetSize() (width, height int) {
return v.width, v.height
}
2020-07-26 14:52:54 -04:00
// GetVisible returns the visibility state of the checkbox
func (v *Checkbox) GetVisible() bool {
return v.visible
}
2020-07-26 14:52:54 -04:00
// SetPosition sets the position of the checkbox
func (v *Checkbox) SetPosition(x, y int) {
v.x = x
v.y = y
}
2020-07-26 14:52:54 -04:00
// SetVisible sets the visibility of the checkbox
func (v *Checkbox) SetVisible(visible bool) {
v.visible = visible
}