2019-11-12 23:44:04 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
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"
|
2019-11-12 23:44:04 -05:00
|
|
|
)
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
const (
|
|
|
|
scrollbarOffsetY = 30
|
|
|
|
halfScrollbarOffsetY = scrollbarOffsetY / 2
|
|
|
|
scrollbarSpriteOffsetY = 10
|
|
|
|
scrollbarFrameOffset = 4
|
|
|
|
scrollbarWidth = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
// Scrollbar is a vertical slider ui element
|
2019-11-12 23:44:04 -05:00
|
|
|
type Scrollbar struct {
|
2020-08-06 10:30:23 -04:00
|
|
|
manager *UIManager
|
2019-11-12 23:44:04 -05:00
|
|
|
x, y, height int
|
|
|
|
visible bool
|
|
|
|
enabled bool
|
|
|
|
currentOffset int
|
|
|
|
maxOffset int
|
|
|
|
lastDirChange int
|
|
|
|
onActivate func()
|
2020-02-01 18:55:56 -05:00
|
|
|
scrollbarSprite *Sprite
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// NewScrollbar creates a scrollbar instance
|
|
|
|
func (ui *UIManager) NewScrollbar(x, y, height int) *Scrollbar {
|
2020-09-12 16:51:30 -04:00
|
|
|
animation, _ := ui.asset.LoadAnimation(d2resource.Scrollbar, d2resource.PaletteSky)
|
2020-08-06 10:30:23 -04:00
|
|
|
scrollbarSprite, _ := ui.NewSprite(animation)
|
|
|
|
result := &Scrollbar{
|
2019-11-12 23:44:04 -05:00
|
|
|
visible: true,
|
|
|
|
enabled: true,
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
height: height,
|
2019-12-21 20:53:18 -05:00
|
|
|
scrollbarSprite: scrollbarSprite,
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
2020-08-06 10:30:23 -04:00
|
|
|
|
|
|
|
ui.addWidget(result)
|
|
|
|
|
2019-11-12 23:44:04 -05:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetEnabled sets the enabled state
|
2019-11-12 23:44:04 -05:00
|
|
|
func (v *Scrollbar) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// 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
|
|
|
|
2020-08-06 10:30:23 -04: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
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// Activate will call the onActivate callback (if set)
|
2019-11-12 23:44:04 -05:00
|
|
|
func (v *Scrollbar) Activate() {
|
2020-08-06 10:30:23 -04:00
|
|
|
_, my := v.manager.CursorPosition()
|
2019-11-12 23:44:04 -05:00
|
|
|
barPosition := v.getBarPosition()
|
2020-08-06 10:30:23 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 18:12:08 -04:00
|
|
|
|
2019-11-12 23:44:04 -05:00
|
|
|
if v.onActivate != nil {
|
|
|
|
v.onActivate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// GetLastDirChange get the last direction change
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) GetLastDirChange() int {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.lastDirChange
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// Render renders the scrollbar to the given surface
|
2020-07-26 14:52:54 -04:00
|
|
|
func (v *Scrollbar) Render(target d2interface.Surface) error {
|
2019-11-12 23:44:04 -05:00
|
|
|
if !v.visible || v.maxOffset == 0 {
|
2020-07-26 14:52:54 -04:00
|
|
|
return nil
|
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
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
v.scrollbarSprite.SetPosition(v.x, v.y)
|
2020-07-26 14:52:54 -04:00
|
|
|
|
|
|
|
if err := v.scrollbarSprite.RenderSegmented(target, 1, 1, 0+offset); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
v.scrollbarSprite.SetPosition(v.x, v.y+v.height-scrollbarSpriteOffsetY) // what is the magic?
|
2020-07-26 14:52:54 -04:00
|
|
|
|
|
|
|
if err := v.scrollbarSprite.RenderSegmented(target, 1, 1, 1+offset); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:44:04 -05:00
|
|
|
if v.maxOffset == 0 || v.currentOffset < 0 || v.currentOffset > v.maxOffset {
|
2020-07-26 14:52:54 -04:00
|
|
|
return nil
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
v.scrollbarSprite.SetPosition(v.x, v.y+10+v.getBarPosition())
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
offset = scrollbarFrameOffset
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2019-11-12 23:44:04 -05:00
|
|
|
if !v.enabled {
|
2020-08-06 10:30:23 -04:00
|
|
|
offset++
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
2020-07-26 14:52:54 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
if err := v.scrollbarSprite.RenderSegmented(target, 1, 1, offset); err != nil {
|
2020-07-26 14:52:54 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// bindManager binds the scrollbar to the UI manager
|
|
|
|
func (v *Scrollbar) bindManager(manager *UIManager) {
|
|
|
|
v.manager = manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance advances the scrollbar sprite
|
|
|
|
func (v *Scrollbar) Advance(elapsed float64) error {
|
|
|
|
return v.scrollbarSprite.Advance(elapsed)
|
2019-12-28 23:32:24 -05:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// GetSize returns the scrollbar width and height
|
2019-12-21 20:53:18 -05:00
|
|
|
func (v *Scrollbar) GetSize() (width, height int) {
|
2020-08-06 10:30:23 -04:00
|
|
|
return scrollbarWidth, v.height
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetPosition sets the scrollbar x,y position
|
2019-12-21 20:53:18 -05:00
|
|
|
func (v *Scrollbar) SetPosition(x, y int) {
|
2019-11-12 23:44:04 -05:00
|
|
|
v.x = x
|
|
|
|
v.y = y
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// GetPosition returns the scrollbar x,y position
|
2019-12-21 20:53:18 -05:00
|
|
|
func (v *Scrollbar) GetPosition() (x, y int) {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.x, v.y
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// GetVisible returns whether or not the scrollbar is visible
|
2019-11-12 23:44:04 -05:00
|
|
|
func (v *Scrollbar) GetVisible() bool {
|
|
|
|
return v.visible
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetVisible sets the scrollbar visibility state
|
2019-11-12 23:44:04 -05:00
|
|
|
func (v *Scrollbar) SetVisible(visible bool) {
|
|
|
|
v.visible = visible
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04: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
|
|
|
|
}
|
2020-08-06 10:30:23 -04:00
|
|
|
|
2019-11-12 23:44:04 -05:00
|
|
|
if v.currentOffset > v.maxOffset {
|
|
|
|
v.currentOffset = v.maxOffset
|
|
|
|
}
|
2020-08-06 10:30:23 -04:00
|
|
|
|
2019-11-12 23:44:04 -05:00
|
|
|
if v.maxOffset == 0 {
|
|
|
|
v.currentOffset = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetCurrentOffset sets the scrollbar's current offset
|
2019-11-12 23:44:04 -05:00
|
|
|
func (v *Scrollbar) SetCurrentOffset(currentOffset int) {
|
|
|
|
v.currentOffset = currentOffset
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// GetMaxOffset returns the max offset
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) GetMaxOffset() int {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.maxOffset
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// GetCurrentOffset gets the current max offset of the scrollbar
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) GetCurrentOffset() int {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.currentOffset
|
|
|
|
}
|