1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-19 21:55:24 +00:00
OpenDiablo2/d2core/d2ui/scrollbar.go

208 lines
4.8 KiB
Go
Raw Normal View History

2019-11-13 04:44:04 +00:00
package d2ui
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
2019-11-13 04:44:04 +00:00
)
const (
scrollbarOffsetY = 30
halfScrollbarOffsetY = scrollbarOffsetY / 2
scrollbarSpriteOffsetY = 10
scrollbarFrameOffset = 4
scrollbarWidth = 10
)
// Scrollbar is a vertical slider ui element
2019-11-13 04:44:04 +00:00
type Scrollbar struct {
manager *UIManager
2019-11-13 04:44:04 +00:00
x, y, height int
visible bool
enabled bool
currentOffset int
maxOffset int
lastDirChange int
onActivate func()
scrollbarSprite *Sprite
2019-11-13 04:44:04 +00:00
}
// NewScrollbar creates a scrollbar instance
func (ui *UIManager) NewScrollbar(x, y, height int) *Scrollbar {
Decouple asset manager from renderer (#730) * improve AssetManager implementation Notable changes are: * removed the individual managers inside of d2asset, only one asset manager * AssetManager now has caches for the types of files it loads * created a type for TextDictionary (the txt file structs) * fixed a file path bug in d2loader Source * fixed a asset stream bug in d2loader Asset * d2loader.Loader now needs a d2config.Config on creation (for resolving locale files) * updated the mpq file in d2asset test data, added test case for "sub-directory" * added a Data method to d2asset.Asset. The data is cached on first full read. * renamed ArchiveDataStream to DataStream in d2interface * moved palette utility func out of d2asset and into d2util * bugfix for MacOS mpq loader issue * lint fixes, added data caching to filesystem asset * adding comment for mpq asset close * Decouple d2asset from d2render Notable changes in d2common: * d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct * un-exported dcc.decodeDirection, it is only used in d2dcc * removed font interface from d2interface, we only have one font implementation * added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need * added `BindRenderer` method to animation interface Notable changes in d2common/d2asset: * **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render * exported Animation * Animation implementation binds to the renderer to create surfaces only on the first time it is rendered * font, dcc, dc6 initialization logic moved out of asset_manager.go * for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods * the d2asset.Font struct now stores font table data for initialization purposes Notable changes in d2core/d2render: * Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time **These last changes should have been a separate PR, sorry.** Notable changes in d2core/d2ui: * ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments Notable Changes in d2game: Because of the change in d2ui, all instances of this code pattern... ```golang animation, err := screen.asset.LoadAnimation(imgPath, palettePath) sprite, err := screen.ui.NewSprite(animation) ``` ... becomes this ... ```golang sprite, err := screen.ui.NewSprite(imgPath, palettePath) ```
2020-09-14 21:31:45 +00:00
scrollbarSprite, _ := ui.NewSprite(d2resource.Scrollbar, d2resource.PaletteSky)
result := &Scrollbar{
2019-11-13 04:44:04 +00:00
visible: true,
enabled: true,
x: x,
y: y,
height: height,
scrollbarSprite: scrollbarSprite,
2019-11-13 04:44:04 +00:00
}
ui.addWidget(result)
2019-11-13 04:44:04 +00:00
return result
}
// GetEnabled returns whether or not the scrollbar is enabled
func (v *Scrollbar) GetEnabled() bool {
2019-11-13 04:44:04 +00:00
return v.enabled
}
// SetEnabled sets the enabled state
2019-11-13 04:44:04 +00: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-13 04:44:04 +00:00
// OnActivated sets the onActivate callback function for the scrollbar
2019-11-13 04:44:04 +00: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-13 04:44:04 +00:00
}
// Activate will call the onActivate callback (if set)
2019-11-13 04:44:04 +00:00
func (v *Scrollbar) Activate() {
_, my := v.manager.CursorPosition()
2019-11-13 04:44:04 +00:00
barPosition := v.getBarPosition()
if my <= v.y+barPosition+halfScrollbarOffsetY {
2019-11-13 04:44:04 +00:00
if v.currentOffset > 0 {
v.currentOffset--
v.lastDirChange = -1
}
} else {
if v.currentOffset < v.maxOffset {
v.currentOffset++
v.lastDirChange = 1
}
}
2019-11-13 04:44:04 +00:00
if v.onActivate != nil {
v.onActivate()
}
}
// GetLastDirChange get the last direction change
func (v *Scrollbar) GetLastDirChange() int {
2019-11-13 04:44:04 +00:00
return v.lastDirChange
}
// Render renders the scrollbar to the given surface
2020-07-26 18:52:54 +00:00
func (v *Scrollbar) Render(target d2interface.Surface) error {
2019-11-13 04:44:04 +00:00
if !v.visible || v.maxOffset == 0 {
2020-07-26 18:52:54 +00:00
return nil
2019-11-13 04:44:04 +00:00
}
2020-07-26 18:52:54 +00:00
2019-11-13 04:44:04 +00:00
offset := 0
2020-07-26 18:52:54 +00:00
2019-11-13 04:44:04 +00:00
if !v.enabled {
offset = 2
}
2020-07-26 18:52:54 +00:00
v.scrollbarSprite.SetPosition(v.x, v.y)
2020-07-26 18:52:54 +00: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 18:52:54 +00:00
if err := v.scrollbarSprite.RenderSegmented(target, 1, 1, 1+offset); err != nil {
return err
}
2019-11-13 04:44:04 +00:00
if v.maxOffset == 0 || v.currentOffset < 0 || v.currentOffset > v.maxOffset {
2020-07-26 18:52:54 +00:00
return nil
2019-11-13 04:44:04 +00:00
}
2020-07-26 18:52:54 +00:00
v.scrollbarSprite.SetPosition(v.x, v.y+10+v.getBarPosition())
2020-07-26 18:52:54 +00:00
offset = scrollbarFrameOffset
2020-07-26 18:52:54 +00:00
2019-11-13 04:44:04 +00:00
if !v.enabled {
offset++
2019-11-13 04:44:04 +00:00
}
2020-07-26 18:52:54 +00:00
if err := v.scrollbarSprite.RenderSegmented(target, 1, 1, offset); err != nil {
2020-07-26 18:52:54 +00:00
return err
}
return nil
2019-11-13 04:44:04 +00: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-13 04:44:04 +00:00
}
// SetPosition sets the scrollbar x,y position
func (v *Scrollbar) SetPosition(x, y int) {
2019-11-13 04:44:04 +00:00
v.x = x
v.y = y
}
// GetPosition returns the scrollbar x,y position
func (v *Scrollbar) GetPosition() (x, y int) {
2019-11-13 04:44:04 +00:00
return v.x, v.y
}
// GetVisible returns whether or not the scrollbar is visible
2019-11-13 04:44:04 +00:00
func (v *Scrollbar) GetVisible() bool {
return v.visible
}
// SetVisible sets the scrollbar visibility state
2019-11-13 04:44:04 +00:00
func (v *Scrollbar) SetVisible(visible bool) {
v.visible = visible
}
// SetMaxOffset sets the maximum offset of the scrollbar
2019-11-13 04:44:04 +00:00
func (v *Scrollbar) SetMaxOffset(maxOffset int) {
v.maxOffset = maxOffset
if v.maxOffset < 0 {
v.maxOffset = 0
}
2019-11-13 04:44:04 +00:00
if v.currentOffset > v.maxOffset {
v.currentOffset = v.maxOffset
}
2019-11-13 04:44:04 +00:00
if v.maxOffset == 0 {
v.currentOffset = 0
}
}
// SetCurrentOffset sets the scrollbar's current offset
2019-11-13 04:44:04 +00:00
func (v *Scrollbar) SetCurrentOffset(currentOffset int) {
v.currentOffset = currentOffset
}
// GetMaxOffset returns the max offset
func (v *Scrollbar) GetMaxOffset() int {
2019-11-13 04:44:04 +00:00
return v.maxOffset
}
// GetCurrentOffset gets the current max offset of the scrollbar
func (v *Scrollbar) GetCurrentOffset() int {
2019-11-13 04:44:04 +00:00
return v.currentOffset
}