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

209 lines
4.8 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
)
// Scrollbar is a vertical slider ui element
2019-11-12 23:44:04 -05:00
type Scrollbar struct {
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()
scrollbarSprite *Sprite
2019-11-12 23:44:04 -05:00
}
// NewScrollbar creates a scrollbar instance
func (ui *UIManager) NewScrollbar(x, y, height int) *Scrollbar {
remove d2asset singleton (#726) * export d2asset singleton * add *d2asset.AssetManager to d2app - d2app now has a reference to an asset manager which it will use for loading - added asset loader methods to the asset manager - functions in d2asset are now wrappers for asset manager methods * add asset manager reference to audio provider - d2app asset manager reference is now passed to audio provider - asset manager is created in main.go for now to pass into audio provider - CreateSoundEffect is now a method, no longer exported, uses the asset manager reference * d2app passes asset manager refence to map engine test * in d2asset, all calls to LoadFile replaced with call to Singleton.Loadfile * blizzard intro and credits screen - d2app passes reference to the asset manager to these screens * asset manager for d2map - adding MapStampFactory, takes an asset manager reference - embedded MapStampFactory into the MapEngine - LoadStamp is now a method of the MapStampFactory * d2asset: removed LoadFileStream, LoadFile, and FileExists * d2gui changes - singleton now has an asset manager reference - calls to d2asset loader functions removed - createButton is now a method of LayoutManager - moved LayoutEntry to its own file * map entity factory - Map engine has an embedded map entity factory - Map stamp factory gets a reference to the map engine's entity factory - Stamps are given a reference to the map engine entity factory when created - Character select gets a map entity factory - Embedded the stamp factory into the MapEngine * asset manager for d2ui - d2ui is passed an asset manager reference when created - all calls to d2asset loader functions in d2ui now refer to the asset manager - d2gamescreen gets a ui manager when created - help overlay is now passed a ui manager when created * d2gamescreen + d2player: asset manager references added an asset manager reference to - inventory panel + inventory grid - mini panel - game controls - help overlay - character select - main menu - select hero class - hero stats panel * Removed d2asset.LoadAnimation all references to this function have been replaced with calls to the asset manager method * adding asset to help overlay, bugfix for 4d59c91 * Removed d2asset.LoadFont and d2asset.LoadAnimationWithEffect all references to these have been replaced with calls to the asset manager methods * MapRenderer now gets an asset manager reference * removed d2asset.LoadPalette all references have been replaced with calls to an asset manager instance * merged d2object with d2mapentity d2object was only being used to create objects in the map, so the provider function is now a method of the map entity factory. calls to d2asset have been removed. * removed d2asset.LoadComposite all calls are now made to the asset manager method * removed d2asset singleton all singleton references have been removed, a single instance of the asset manager is passed around the entire app * rename Initialize to NewAssetManager
2020-09-12 16:51:30 -04:00
animation, _ := ui.asset.LoadAnimation(d2resource.Scrollbar, d2resource.PaletteSky)
scrollbarSprite, _ := ui.NewSprite(animation)
result := &Scrollbar{
2019-11-12 23:44:04 -05:00
visible: true,
enabled: true,
x: x,
y: y,
height: height,
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
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
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
}
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
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
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
}
// 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)
}
// 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
}
// SetPosition sets the scrollbar x,y position
func (v *Scrollbar) SetPosition(x, y int) {
2019-11-12 23:44:04 -05:00
v.x = x
v.y = y
}
// GetPosition returns the scrollbar x,y position
func (v *Scrollbar) GetPosition() (x, y int) {
2019-11-12 23:44:04 -05:00
return v.x, v.y
}
// 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
}
// SetVisible sets the scrollbar visibility state
2019-11-12 23:44:04 -05:00
func (v *Scrollbar) SetVisible(visible bool) {
v.visible = visible
}
// 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
}