1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-04 15:46:51 -05:00
OpenDiablo2/d2game/d2player/globeWidget.go
juander-ux 12821147ce
Ui panel refactor part 2 (#921)
* d2ui/skilltree: Don't render availSPLabel

this is handled by the ui_manager now.

* d2ui/custom_widget: Allow them to be cached into static images

* d2player/hero_stats_panel: Remove render() function from game_controls

all ui elements are now grouped into a WidgetGroup, thus rendering is
done by the ui manager.

* d2player/hero_stats_panel: Remove unnecessary widgets from struct

we don't need to store them in the HeroStatsPanel struct anymore as they
are completly handled by the uiManager.

* d2ui/widget_group: Remove priority member

this is already defined by the BaseWidget.

* d2ui/widget: Move uiManager.contains() to the baseWidgets

this method makes more sense on a widget anyways.

* d2ui/widget: Add methods to handle widget hovering

* d2ui/custom_widget: Require define width/height

since the custom render() method can do whatever, we need the user to specify
the width/height such that GetSize() calls are meaningful.

* d2ui/widget: Allow widgets to return the uiManager

* d2player/HUD: Refactor health/mana globe into its own widget

* d2player/hud: Refactor load()

seperate each type of loading into its own method.

* d2player/HUD: Move stamina/exp bar into widgets

* d2player/HUD: Refactor left/right skills into widget

* d2ui/custom_widget: cached custom widgets should use widget.x/y

since we render to an image, we use widget.x/y to position the cached
image.

* d2player/HUD: User cached custom widget for all static images
2020-11-13 12:08:43 -08:00

146 lines
3.0 KiB
Go

package d2player
import (
"image"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
)
type globeType = int
const (
typeHealthGlobe globeType = iota
typeManaGlobe
)
const (
globeHeight = 80
globeWidth = 80
globeSpriteOffsetX = 28
globeSpriteOffsetY = -5
healthStatusOffsetX = 30
healthStatusOffsetY = -13
manaStatusOffsetX = 7
manaStatusOffsetY = -12
manaGlobeScreenOffsetX = 117
)
// static check that globeWidget implements Widget
var _ d2ui.Widget = &globeWidget{}
type globeFrame struct {
sprite *d2ui.Sprite
offsetX int
offsetY int
idx int
}
func (gf *globeFrame) setFrameIndex() {
if err := gf.sprite.SetCurrentFrame(gf.idx); err != nil {
log.Print(err)
}
}
func (gf *globeFrame) setPosition(x, y int) {
gf.sprite.SetPosition(x+gf.offsetX, y+gf.offsetY)
}
func (gf *globeFrame) getSize() (x, y int) {
w, h := gf.sprite.GetSize()
return w + gf.offsetX, h + gf.offsetY
}
type globeWidget struct {
*d2ui.BaseWidget
value *int
valueMax *int
globe *globeFrame
overlap *globeFrame
}
func newGlobeWidget(ui *d2ui.UIManager, x, y int, gtype globeType, value, valueMax *int) *globeWidget {
var globe, overlap *globeFrame
base := d2ui.NewBaseWidget(ui)
base.SetPosition(x, y)
if gtype == typeHealthGlobe {
globe = &globeFrame{
offsetX: healthStatusOffsetX,
offsetY: healthStatusOffsetY,
idx: frameHealthStatus,
}
overlap = &globeFrame{
offsetX: globeSpriteOffsetX,
offsetY: globeSpriteOffsetY,
idx: frameHealthStatus,
}
} else if gtype == typeManaGlobe {
globe = &globeFrame{
offsetX: manaStatusOffsetX,
offsetY: manaStatusOffsetY,
idx: frameManaStatus,
}
overlap = &globeFrame{
offsetX: rightGlobeOffsetX,
offsetY: rightGlobeOffsetY,
idx: frameRightGlobe,
}
}
return &globeWidget{
BaseWidget: base,
value: value,
valueMax: valueMax,
globe: globe,
overlap: overlap,
}
}
func (g *globeWidget) load() {
var err error
g.globe.sprite, err = g.GetManager().NewSprite(d2resource.HealthManaIndicator, d2resource.PaletteSky)
if err != nil {
log.Print(err)
}
g.globe.setFrameIndex()
g.overlap.sprite, err = g.GetManager().NewSprite(d2resource.GameGlobeOverlap, d2resource.PaletteSky)
if err != nil {
log.Print(err)
}
g.overlap.setFrameIndex()
}
// Render draws the widget to the screen
func (g *globeWidget) Render(target d2interface.Surface) {
valuePercent := float64(*g.value) / float64(*g.valueMax)
barHeight := int(valuePercent * float64(globeHeight))
maskRect := image.Rect(0, globeHeight-barHeight, globeWidth, globeHeight)
g.globe.setPosition(g.GetPosition())
g.globe.sprite.RenderSection(target, maskRect)
g.overlap.setPosition(g.GetPosition())
g.overlap.sprite.Render(target)
}
func (g *globeWidget) GetSize() (x, y int) {
return g.overlap.getSize()
}
func (g *globeWidget) Advance(elapsed float64) error {
return nil
}