2019-11-12 23:44:04 -05:00
|
|
|
package d2ui
|
|
|
|
|
|
|
|
import (
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2resource"
|
2019-11-12 23:44:04 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render"
|
2019-12-28 16:46:08 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2surface"
|
2019-11-12 23:44:04 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Scrollbar struct {
|
|
|
|
x, y, height int
|
|
|
|
visible bool
|
|
|
|
enabled bool
|
|
|
|
currentOffset int
|
|
|
|
maxOffset int
|
|
|
|
lastDirChange int
|
|
|
|
onActivate func()
|
2019-12-21 20:53:18 -05:00
|
|
|
scrollbarSprite *d2render.Sprite
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func CreateScrollbar(x, y, height int) Scrollbar {
|
|
|
|
scrollbarSprite, _ := d2render.LoadSprite(d2resource.Scrollbar, d2resource.PaletteSky)
|
2019-11-12 23:44:04 -05:00
|
|
|
result := Scrollbar{
|
|
|
|
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
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Scrollbar) GetEnabled() bool {
|
|
|
|
return v.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) SetEnabled(enabled bool) {
|
|
|
|
v.enabled = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) SetPressed(pressed bool) {}
|
|
|
|
func (v *Scrollbar) GetPressed() bool { return false }
|
|
|
|
|
|
|
|
func (v *Scrollbar) OnActivated(callback func()) {
|
|
|
|
v.onActivate = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Scrollbar) getBarPosition() int {
|
|
|
|
return int((float32(v.currentOffset) / float32(v.maxOffset)) * float32(v.height-30))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) Activate() {
|
|
|
|
_, my := ebiten.CursorPosition()
|
|
|
|
barPosition := v.getBarPosition()
|
|
|
|
if my <= v.y+barPosition+15 {
|
|
|
|
if v.currentOffset > 0 {
|
|
|
|
v.currentOffset--
|
|
|
|
v.lastDirChange = -1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if v.currentOffset < v.maxOffset {
|
|
|
|
v.currentOffset++
|
|
|
|
v.lastDirChange = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v.onActivate != nil {
|
|
|
|
v.onActivate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) GetLastDirChange() int {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.lastDirChange
|
|
|
|
}
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
func (v *Scrollbar) Render(target *d2surface.Surface) {
|
2019-11-12 23:44:04 -05:00
|
|
|
if !v.visible || v.maxOffset == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
offset := 0
|
|
|
|
if !v.enabled {
|
|
|
|
offset = 2
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
v.scrollbarSprite.SetPosition(v.x, v.y)
|
|
|
|
v.scrollbarSprite.RenderSegmented(target, 1, 1, 0+offset)
|
|
|
|
v.scrollbarSprite.SetPosition(v.x, v.y+v.height-10)
|
|
|
|
v.scrollbarSprite.RenderSegmented(target, 1, 1, 1+offset)
|
2019-11-12 23:44:04 -05:00
|
|
|
if v.maxOffset == 0 || v.currentOffset < 0 || v.currentOffset > v.maxOffset {
|
|
|
|
return
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
v.scrollbarSprite.SetPosition(v.x, v.y+10+v.getBarPosition())
|
2019-11-12 23:44:04 -05:00
|
|
|
offset = 0
|
|
|
|
if !v.enabled {
|
|
|
|
offset = 1
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
v.scrollbarSprite.RenderSegmented(target, 1, 1, 4+offset)
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) Advance(elapsed float64) {
|
|
|
|
v.scrollbarSprite.Advance(elapsed)
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (v *Scrollbar) GetSize() (width, height int) {
|
|
|
|
return 10, int(v.height)
|
2019-11-12 23:44:04 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) GetVisible() bool {
|
|
|
|
return v.visible
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) SetVisible(visible bool) {
|
|
|
|
v.visible = visible
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) SetMaxOffset(maxOffset int) {
|
|
|
|
v.maxOffset = maxOffset
|
|
|
|
if v.maxOffset < 0 {
|
|
|
|
v.maxOffset = 0
|
|
|
|
}
|
|
|
|
if v.currentOffset > v.maxOffset {
|
|
|
|
v.currentOffset = v.maxOffset
|
|
|
|
}
|
|
|
|
if v.maxOffset == 0 {
|
|
|
|
v.currentOffset = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Scrollbar) SetCurrentOffset(currentOffset int) {
|
|
|
|
v.currentOffset = currentOffset
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) GetMaxOffset() int {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.maxOffset
|
|
|
|
}
|
|
|
|
|
2019-12-28 23:32:24 -05:00
|
|
|
func (v *Scrollbar) GetCurrentOffset() int {
|
2019-11-12 23:44:04 -05:00
|
|
|
return v.currentOffset
|
|
|
|
}
|