1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-16 17:35:57 -05:00
OpenDiablo2/d2core/d2ui/scrollbar.go

181 lines
4.1 KiB
Go
Raw Normal View History

2019-11-12 23:44:04 -05:00
package d2ui
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
2019-11-12 23:44:04 -05:00
)
const (
scrollbarOffsetY = 30
halfScrollbarOffsetY = scrollbarOffsetY / 2
scrollbarSpriteOffsetY = 10
scrollbarFrameOffset = 4
scrollbarWidth = 10
)
// static check that Scrollbar implements clickable widget
var _ ClickableWidget = &Scrollbar{}
// Scrollbar is a vertical slider ui element
2019-11-12 23:44:04 -05:00
type Scrollbar struct {
*BaseWidget
2019-11-12 23:44:04 -05:00
enabled bool
currentOffset int
maxOffset int
lastDirChange int
onActivate func()
scrollbarSprite *Sprite
2019-11-12 23:44:04 -05:00
}
// NewScrollbar creates a scrollbar instance
func (ui *UIManager) NewScrollbar(x, y, height int) *Scrollbar {
scrollbarSprite, err := ui.NewSprite(d2resource.Scrollbar, d2resource.PaletteSky)
if err != nil {
ui.Error(err.Error())
return nil
}
base := NewBaseWidget(ui)
base.SetPosition(x, y)
base.height = height
result := &Scrollbar{
BaseWidget: base,
2019-11-12 23:44:04 -05:00
enabled: true,
scrollbarSprite: scrollbarSprite,
2019-11-12 23:44:04 -05:00
}
ui.addWidget(result)
2019-11-12 23:44:04 -05:00
return result
}
// GetEnabled returns whether or not the scrollbar is enabled
func (v *Scrollbar) GetEnabled() bool {
2019-11-12 23:44:04 -05:00
return v.enabled
}
// SetEnabled sets the enabled state
2019-11-12 23:44:04 -05:00
func (v *Scrollbar) SetEnabled(enabled bool) {
v.enabled = enabled
}
// SetPressed is not used by the scrollbar, but is present to satisfy the ui widget interface
func (v *Scrollbar) SetPressed(_ bool) {}
// GetPressed is not used by the scrollbar, but is present to satisfy the ui widget interface
func (v *Scrollbar) GetPressed() bool { return false }
2019-11-12 23:44:04 -05:00
// OnActivated sets the onActivate callback function for the scrollbar
2019-11-12 23:44:04 -05:00
func (v *Scrollbar) OnActivated(callback func()) {
v.onActivate = callback
}
func (v *Scrollbar) getBarPosition() int {
maxOffset := float32(v.maxOffset) * float32(v.height-scrollbarOffsetY)
return int(float32(v.currentOffset) / maxOffset)
2019-11-12 23:44:04 -05:00
}
// Activate will call the onActivate callback (if set)
2019-11-12 23:44:04 -05:00
func (v *Scrollbar) Activate() {
_, my := v.manager.CursorPosition()
2019-11-12 23:44:04 -05:00
barPosition := v.getBarPosition()
if my <= v.y+barPosition+halfScrollbarOffsetY {
2019-11-12 23:44:04 -05:00
if v.currentOffset > 0 {
v.currentOffset--
v.lastDirChange = -1
}
} else {
if v.currentOffset < v.maxOffset {
v.currentOffset++
v.lastDirChange = 1
}
}
2019-11-12 23:44:04 -05:00
if v.onActivate != nil {
v.onActivate()
}
}
// GetLastDirChange get the last direction change
func (v *Scrollbar) GetLastDirChange() int {
2019-11-12 23:44:04 -05:00
return v.lastDirChange
}
// Render renders the scrollbar to the given surface
func (v *Scrollbar) Render(target d2interface.Surface) {
2019-11-12 23:44:04 -05:00
if !v.visible || v.maxOffset == 0 {
return
2019-11-12 23:44:04 -05:00
}
2020-07-26 14:52:54 -04:00
2019-11-12 23:44:04 -05:00
offset := 0
2020-07-26 14:52:54 -04:00
2019-11-12 23:44:04 -05:00
if !v.enabled {
offset = 2
}
2020-07-26 14:52:54 -04:00
v.scrollbarSprite.SetPosition(v.x, v.y)
2020-07-26 14:52:54 -04:00
v.scrollbarSprite.RenderSegmented(target, 1, 1, 0+offset)
2020-07-26 14:52:54 -04:00
v.scrollbarSprite.SetPosition(v.x, v.y+v.height-scrollbarSpriteOffsetY) // what is the magic?
2020-07-26 14:52:54 -04:00
v.scrollbarSprite.RenderSegmented(target, 1, 1, 1+offset)
2020-07-26 14:52:54 -04:00
2019-11-12 23:44:04 -05:00
if v.maxOffset == 0 || v.currentOffset < 0 || v.currentOffset > v.maxOffset {
return
2019-11-12 23:44:04 -05:00
}
2020-07-26 14:52:54 -04:00
v.scrollbarSprite.SetPosition(v.x, v.y+10+v.getBarPosition())
2020-07-26 14:52:54 -04:00
offset = scrollbarFrameOffset
2020-07-26 14:52:54 -04:00
2019-11-12 23:44:04 -05:00
if !v.enabled {
offset++
2019-11-12 23:44:04 -05:00
}
2020-07-26 14:52:54 -04:00
v.scrollbarSprite.RenderSegmented(target, 1, 1, offset)
2019-11-12 23:44:04 -05:00
}
// Advance advances the scrollbar sprite
func (v *Scrollbar) Advance(elapsed float64) error {
return v.scrollbarSprite.Advance(elapsed)
}
// GetSize returns the scrollbar width and height
func (v *Scrollbar) GetSize() (width, height int) {
return scrollbarWidth, v.height
2019-11-12 23:44:04 -05:00
}
// SetMaxOffset sets the maximum offset of the scrollbar
2019-11-12 23:44:04 -05:00
func (v *Scrollbar) SetMaxOffset(maxOffset int) {
v.maxOffset = maxOffset
if v.maxOffset < 0 {
v.maxOffset = 0
}
2019-11-12 23:44:04 -05:00
if v.currentOffset > v.maxOffset {
v.currentOffset = v.maxOffset
}
2019-11-12 23:44:04 -05:00
if v.maxOffset == 0 {
v.currentOffset = 0
}
}
// SetCurrentOffset sets the scrollbar's current offset
2019-11-12 23:44:04 -05:00
func (v *Scrollbar) SetCurrentOffset(currentOffset int) {
v.currentOffset = currentOffset
}
// GetMaxOffset returns the max offset
func (v *Scrollbar) GetMaxOffset() int {
2019-11-12 23:44:04 -05:00
return v.maxOffset
}
// GetCurrentOffset gets the current max offset of the scrollbar
func (v *Scrollbar) GetCurrentOffset() int {
2019-11-12 23:44:04 -05:00
return v.currentOffset
}