1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 02:16:23 -05:00
OpenDiablo2/d2core/d2gui/manager.go
lord 854fce3b14
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

179 lines
3.6 KiB
Go

package d2gui
import (
"image/color"
"math"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
type manager struct {
asset *d2asset.AssetManager
layout *Layout
cursorAnim d2interface.Animation
cursorX int
cursorY int
loadingAnim d2interface.Animation
cursorVisible bool
loading bool
}
func createGuiManager(asset *d2asset.AssetManager, inputManager d2interface.InputManager) (*manager, error) {
cursorAnim, err := asset.LoadAnimation(d2resource.CursorDefault, d2resource.PaletteUnits)
if err != nil {
return nil, err
}
loadingAnim, err := asset.LoadAnimation(d2resource.LoadingScreen, d2resource.PaletteLoading)
if err != nil {
return nil, err
}
manager := &manager{
asset: asset,
cursorAnim: cursorAnim,
loadingAnim: loadingAnim,
cursorVisible: true,
}
manager.clear()
if err := inputManager.BindHandler(manager); err != nil {
return nil, err
}
return manager, nil
}
func (m *manager) SetLayout(layout *Layout) {
m.layout = layout
if m.layout != nil {
m.layout.AdjustEntryPlacement()
}
}
func (m *manager) OnMouseButtonDown(event d2interface.MouseEvent) bool {
if m.layout == nil {
return false
}
return m.layout.onMouseButtonDown(event)
}
func (m *manager) OnMouseButtonUp(event d2interface.MouseEvent) bool {
if m.layout == nil {
return false
}
return m.layout.onMouseButtonUp(event)
}
func (m *manager) OnMouseMove(event d2interface.MouseMoveEvent) bool {
m.cursorX = event.X()
m.cursorY = event.Y()
if m.layout == nil {
return false
}
return m.layout.onMouseMove(event)
}
func (m *manager) render(target d2interface.Surface) error {
if m.loading {
if err := m.renderLoadScreen(target); err != nil {
return err
}
} else if m.layout != nil {
m.layout.SetSize(target.GetSize())
if err := m.layout.render(target); err != nil {
return err
}
}
if m.cursorVisible {
if err := m.renderCursor(target); err != nil {
return err
}
}
return nil
}
func (m *manager) renderLoadScreen(target d2interface.Surface) error {
if clearErr := target.Clear(color.Black); clearErr != nil {
return clearErr
}
pushCount := 0
screenWidth, screenHeight := target.GetSize()
animWidth, animHeight := m.loadingAnim.GetCurrentFrameSize()
target.PushTranslation(screenWidth/2-animWidth/2, screenHeight/2+animHeight/2)
pushCount++
target.PushTranslation(0, -animHeight)
pushCount++
defer target.PopN(pushCount)
return m.loadingAnim.Render(target)
}
func (m *manager) renderCursor(target d2interface.Surface) error {
_, height := m.cursorAnim.GetCurrentFrameSize()
pushCount := 0
target.PushTranslation(m.cursorX, m.cursorY)
pushCount++
target.PushTranslation(0, -height)
pushCount++
defer target.PopN(pushCount)
return m.cursorAnim.Render(target)
}
func (m *manager) advance(elapsed float64) error {
if !m.loading && m.layout != nil {
if err := m.layout.advance(elapsed); err != nil {
return err
}
}
return nil
}
func (m *manager) showLoadScreen(progress float64) {
progress = math.Min(progress, 1.0)
progress = math.Max(progress, 0.0)
animation := m.loadingAnim
frameCount := animation.GetFrameCount()
_ = animation.SetCurrentFrame(int(float64(frameCount-1) * progress))
m.loading = true
}
func (m *manager) hideLoadScreen() {
m.loading = false
}
func (m *manager) showCursor() {
m.cursorVisible = true
}
func (m *manager) hideCursor() {
m.cursorVisible = false
}
func (m *manager) clear() {
m.SetLayout(nil)
m.hideLoadScreen()
}