1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-30 11:05:23 +00:00

Font manager abstraction (#545)

* abstracted palettes, colors, and palette manager

* make asset manager use the palette manager interface

* added BGRA setter/getter, fixed lint errors

* abstraction for animation and animation manager

* abstracted font and font manager
This commit is contained in:
dk 2020-07-05 12:56:48 -07:00 committed by GitHub
parent c1a88c9cf7
commit 321a353461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 19 deletions

View File

@ -0,0 +1,8 @@
package d2interface
// ArchivedFontManager manages fonts that are in archives being
// managed by the ArchiveManager
type ArchivedFontManager interface {
Cacher
LoadFont(tablePath, spritePath, palettePath string) (Font, error)
}

View File

@ -1,5 +1,6 @@
package d2interface package d2interface
type ArchivedPaletteManager interface { type ArchivedPaletteManager interface {
Cacher
LoadPalette(palettePath string) (Palette, error) LoadPalette(palettePath string) (Palette, error)
} }

View File

@ -0,0 +1,13 @@
package d2interface
import (
"image/color"
)
// Font is a font
type Font interface {
SetColor(c color.Color)
GetTextMetrics(text string) (width, height int)
Clone() Font
RenderText(text string, target Surface) error
}

View File

@ -13,7 +13,7 @@ type assetManager struct {
paletteManager d2interface.ArchivedPaletteManager paletteManager d2interface.ArchivedPaletteManager
paletteTransformManager *paletteTransformManager paletteTransformManager *paletteTransformManager
animationManager d2interface.ArchivedAnimationManager animationManager d2interface.ArchivedAnimationManager
fontManager *fontManager fontManager d2interface.ArchivedFontManager
} }
func loadDC6(dc6Path string) (*d2dc6.DC6, error) { func loadDC6(dc6Path string) (*d2dc6.DC6, error) {

View File

@ -41,7 +41,7 @@ func Initialize(renderer d2interface.Renderer,
archiveManager.GetCache().SetVerbose(verbose) archiveManager.GetCache().SetVerbose(verbose)
archivedFileManager.GetCache().SetVerbose(verbose) archivedFileManager.GetCache().SetVerbose(verbose)
paletteManager.cache.SetVerbose(verbose) paletteManager.GetCache().SetVerbose(verbose)
paletteTransformManager.cache.SetVerbose(verbose) paletteTransformManager.cache.SetVerbose(verbose)
animationManager.cache.SetVerbose(verbose) animationManager.cache.SetVerbose(verbose)
}); err != nil { }); err != nil {
@ -49,7 +49,6 @@ func Initialize(renderer d2interface.Renderer,
} }
if err := term.BindAction("assetstat", "display asset manager cache statistics", func() { if err := term.BindAction("assetstat", "display asset manager cache statistics", func() {
var cacheStatistics = func(c d2interface.Cache) float64 { var cacheStatistics = func(c d2interface.Cache) float64 {
const percent = 100.0 const percent = 100.0
return float64(c.GetWeight()) / float64(c.GetBudget()) * percent return float64(c.GetWeight()) / float64(c.GetBudget()) * percent
@ -57,10 +56,10 @@ func Initialize(renderer d2interface.Renderer,
term.OutputInfof("archive cache: %f", cacheStatistics(archiveManager.GetCache())) term.OutputInfof("archive cache: %f", cacheStatistics(archiveManager.GetCache()))
term.OutputInfof("file cache: %f", cacheStatistics(archivedFileManager.GetCache())) term.OutputInfof("file cache: %f", cacheStatistics(archivedFileManager.GetCache()))
term.OutputInfof("palette cache: %f", cacheStatistics(paletteManager.cache)) term.OutputInfof("palette cache: %f", cacheStatistics(paletteManager.GetCache()))
term.OutputInfof("palette transform cache: %f", cacheStatistics(paletteTransformManager.cache)) term.OutputInfof("palette transform cache: %f", cacheStatistics(paletteTransformManager.cache))
term.OutputInfof("animation cache: %f", cacheStatistics(animationManager.cache)) term.OutputInfof("animation cache: %f", cacheStatistics(animationManager.cache))
term.OutputInfof("font cache: %f", cacheStatistics(fontManager.cache)) term.OutputInfof("font cache: %f", cacheStatistics(fontManager.GetCache()))
}); err != nil { }); err != nil {
return err return err
} }
@ -68,10 +67,10 @@ func Initialize(renderer d2interface.Renderer,
if err := term.BindAction("assetclear", "clear asset manager cache", func() { if err := term.BindAction("assetclear", "clear asset manager cache", func() {
archiveManager.ClearCache() archiveManager.ClearCache()
archivedFileManager.GetCache().Clear() archivedFileManager.GetCache().Clear()
paletteManager.cache.Clear() paletteManager.ClearCache()
paletteTransformManager.cache.Clear() paletteTransformManager.cache.Clear()
animationManager.cache.Clear() animationManager.ClearCache()
fontManager.cache.Clear() fontManager.ClearCache()
}); err != nil { }); err != nil {
return err return err
} }
@ -121,8 +120,8 @@ func LoadComposite(baseType d2enum.ObjectType, token, palettePath string) (*Comp
} }
// LoadFont loads a font the resource files // LoadFont loads a font the resource files
func LoadFont(tablePath, spritePath, palettePath string) (*Font, error) { func LoadFont(tablePath, spritePath, palettePath string) (d2interface.Font, error) {
return singleton.fontManager.loadFont(tablePath, spritePath, palettePath) return singleton.fontManager.LoadFont(tablePath, spritePath, palettePath)
} }
// LoadPalette loads a palette from a given palette path // LoadPalette loads a palette from a given palette path

View File

@ -24,7 +24,7 @@ type Font struct {
color color.Color color color.Color
} }
func loadFont(tablePath, spritePath, palettePath string) (*Font, error) { func loadFont(tablePath, spritePath, palettePath string) (d2interface.Font, error) {
sheet, err := LoadAnimation(spritePath, palettePath) sheet, err := LoadAnimation(spritePath, palettePath)
if err != nil { if err != nil {
return nil, err return nil, err
@ -62,6 +62,7 @@ func loadFont(tablePath, spritePath, palettePath string) (*Font, error) {
return font, nil return font, nil
} }
// SetColor sets the fonts color
func (f *Font) SetColor(c color.Color) { func (f *Font) SetColor(c color.Color) {
f.color = c f.color = c
} }
@ -94,7 +95,7 @@ func (f *Font) GetTextMetrics(text string) (width, height int) {
} }
// Clone creates a shallow copy of the Font // Clone creates a shallow copy of the Font
func (f *Font) Clone() *Font { func (f *Font) Clone() d2interface.Font {
return &Font{ return &Font{
sheet: f.sheet, sheet: f.sheet,
glyphs: f.glyphs, glyphs: f.glyphs,

View File

@ -15,14 +15,16 @@ type fontManager struct {
cache d2interface.Cache cache d2interface.Cache
} }
func createFontManager() *fontManager { func createFontManager() d2interface.ArchivedFontManager {
return &fontManager{d2common.CreateCache(fontBudget)} return &fontManager{d2common.CreateCache(fontBudget)}
} }
func (fm *fontManager) loadFont(tablePath, spritePath, palettePath string) (*Font, error) { // LoadFont loads a font from the archives managed by the ArchiveManager
func (fm *fontManager) LoadFont(tablePath, spritePath, palettePath string) (d2interface.Font,
error) {
cachePath := fmt.Sprintf("%s;%s;%s", tablePath, spritePath, palettePath) cachePath := fmt.Sprintf("%s;%s;%s", tablePath, spritePath, palettePath)
if font, found := fm.cache.Retrieve(cachePath); found { if font, found := fm.cache.Retrieve(cachePath); found {
return font.(*Font).Clone(), nil return font.(d2interface.Font).Clone(), nil
} }
font, err := loadFont(tablePath, spritePath, palettePath) font, err := loadFont(tablePath, spritePath, palettePath)
@ -36,3 +38,13 @@ func (fm *fontManager) loadFont(tablePath, spritePath, palettePath string) (*Fon
return font, nil return font, nil
} }
// ClearCache clears the font cache
func (fm *fontManager) ClearCache() {
fm.cache.Clear()
}
// GetCache returns the font managers cache
func (fm *fontManager) GetCache() d2interface.Cache {
return fm.cache
}

View File

@ -14,10 +14,11 @@ const (
paletteBudget = 64 paletteBudget = 64
) )
func createPaletteManager() *paletteManager { func createPaletteManager() d2interface.ArchivedPaletteManager {
return &paletteManager{d2common.CreateCache(paletteBudget)} return &paletteManager{d2common.CreateCache(paletteBudget)}
} }
// LoadPalette loads a palette from archives managed by the ArchiveManager
func (pm *paletteManager) LoadPalette(palettePath string) (d2interface.Palette, error) { func (pm *paletteManager) LoadPalette(palettePath string) (d2interface.Palette, error) {
if palette, found := pm.cache.Retrieve(palettePath); found { if palette, found := pm.cache.Retrieve(palettePath); found {
return palette.(d2interface.Palette), nil return palette.(d2interface.Palette), nil
@ -39,3 +40,13 @@ func (pm *paletteManager) LoadPalette(palettePath string) (d2interface.Palette,
return palette, nil return palette, nil
} }
// ClearCache clears the palette cache
func (pm *paletteManager) ClearCache() {
pm.cache.Clear()
}
// GetCache returns the palette managers cache
func (pm *paletteManager) GetCache() d2interface.Cache {
return pm.cache
}

View File

@ -9,7 +9,7 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
) )
func loadFont(fontStyle FontStyle) (*d2asset.Font, error) { func loadFont(fontStyle FontStyle) (d2interface.Font, error) {
config, ok := fontStyleConfigs[fontStyle] config, ok := fontStyleConfigs[fontStyle]
if !ok { if !ok {
return nil, errors.New("invalid font style") return nil, errors.New("invalid font style")

View File

@ -2,7 +2,6 @@ package d2gui
import ( import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
) )
type Label struct { type Label struct {
@ -10,7 +9,7 @@ type Label struct {
renderer d2interface.Renderer renderer d2interface.Renderer
text string text string
font *d2asset.Font font d2interface.Font
surface d2interface.Surface surface d2interface.Surface
} }