From 321a353461ec2409933ab6c587a0f15937f92e4b Mon Sep 17 00:00:00 2001 From: dk Date: Sun, 5 Jul 2020 12:56:48 -0700 Subject: [PATCH] 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 --- d2common/d2interface/archived_font_manager.go | 8 ++++++++ .../d2interface/archived_palette_manager.go | 1 + d2common/d2interface/font.go | 13 +++++++++++++ d2core/d2asset/asset_manager.go | 2 +- d2core/d2asset/d2asset.go | 17 ++++++++--------- d2core/d2asset/font.go | 5 +++-- d2core/d2asset/font_manager.go | 18 +++++++++++++++--- d2core/d2asset/palette_manager.go | 13 ++++++++++++- d2core/d2gui/common.go | 2 +- d2core/d2gui/label.go | 3 +-- 10 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 d2common/d2interface/archived_font_manager.go create mode 100644 d2common/d2interface/font.go diff --git a/d2common/d2interface/archived_font_manager.go b/d2common/d2interface/archived_font_manager.go new file mode 100644 index 00000000..f6b1022d --- /dev/null +++ b/d2common/d2interface/archived_font_manager.go @@ -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) +} diff --git a/d2common/d2interface/archived_palette_manager.go b/d2common/d2interface/archived_palette_manager.go index c7a3097a..cff12fa7 100644 --- a/d2common/d2interface/archived_palette_manager.go +++ b/d2common/d2interface/archived_palette_manager.go @@ -1,5 +1,6 @@ package d2interface type ArchivedPaletteManager interface { + Cacher LoadPalette(palettePath string) (Palette, error) } diff --git a/d2common/d2interface/font.go b/d2common/d2interface/font.go new file mode 100644 index 00000000..6fc8fc1b --- /dev/null +++ b/d2common/d2interface/font.go @@ -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 +} diff --git a/d2core/d2asset/asset_manager.go b/d2core/d2asset/asset_manager.go index f372c933..60d76b85 100644 --- a/d2core/d2asset/asset_manager.go +++ b/d2core/d2asset/asset_manager.go @@ -13,7 +13,7 @@ type assetManager struct { paletteManager d2interface.ArchivedPaletteManager paletteTransformManager *paletteTransformManager animationManager d2interface.ArchivedAnimationManager - fontManager *fontManager + fontManager d2interface.ArchivedFontManager } func loadDC6(dc6Path string) (*d2dc6.DC6, error) { diff --git a/d2core/d2asset/d2asset.go b/d2core/d2asset/d2asset.go index dfbde880..01a57fe8 100644 --- a/d2core/d2asset/d2asset.go +++ b/d2core/d2asset/d2asset.go @@ -41,7 +41,7 @@ func Initialize(renderer d2interface.Renderer, archiveManager.GetCache().SetVerbose(verbose) archivedFileManager.GetCache().SetVerbose(verbose) - paletteManager.cache.SetVerbose(verbose) + paletteManager.GetCache().SetVerbose(verbose) paletteTransformManager.cache.SetVerbose(verbose) animationManager.cache.SetVerbose(verbose) }); err != nil { @@ -49,7 +49,6 @@ func Initialize(renderer d2interface.Renderer, } if err := term.BindAction("assetstat", "display asset manager cache statistics", func() { - var cacheStatistics = func(c d2interface.Cache) float64 { const percent = 100.0 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("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("animation cache: %f", cacheStatistics(animationManager.cache)) - term.OutputInfof("font cache: %f", cacheStatistics(fontManager.cache)) + term.OutputInfof("font cache: %f", cacheStatistics(fontManager.GetCache())) }); err != nil { return err } @@ -68,10 +67,10 @@ func Initialize(renderer d2interface.Renderer, if err := term.BindAction("assetclear", "clear asset manager cache", func() { archiveManager.ClearCache() archivedFileManager.GetCache().Clear() - paletteManager.cache.Clear() + paletteManager.ClearCache() paletteTransformManager.cache.Clear() - animationManager.cache.Clear() - fontManager.cache.Clear() + animationManager.ClearCache() + fontManager.ClearCache() }); err != nil { return err } @@ -121,8 +120,8 @@ func LoadComposite(baseType d2enum.ObjectType, token, palettePath string) (*Comp } // LoadFont loads a font the resource files -func LoadFont(tablePath, spritePath, palettePath string) (*Font, error) { - return singleton.fontManager.loadFont(tablePath, spritePath, palettePath) +func LoadFont(tablePath, spritePath, palettePath string) (d2interface.Font, error) { + return singleton.fontManager.LoadFont(tablePath, spritePath, palettePath) } // LoadPalette loads a palette from a given palette path diff --git a/d2core/d2asset/font.go b/d2core/d2asset/font.go index 8a539b81..f1938a55 100644 --- a/d2core/d2asset/font.go +++ b/d2core/d2asset/font.go @@ -24,7 +24,7 @@ type Font struct { 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) if err != nil { return nil, err @@ -62,6 +62,7 @@ func loadFont(tablePath, spritePath, palettePath string) (*Font, error) { return font, nil } +// SetColor sets the fonts color func (f *Font) SetColor(c color.Color) { f.color = c } @@ -94,7 +95,7 @@ func (f *Font) GetTextMetrics(text string) (width, height int) { } // Clone creates a shallow copy of the Font -func (f *Font) Clone() *Font { +func (f *Font) Clone() d2interface.Font { return &Font{ sheet: f.sheet, glyphs: f.glyphs, diff --git a/d2core/d2asset/font_manager.go b/d2core/d2asset/font_manager.go index 9266c178..9853aab9 100644 --- a/d2core/d2asset/font_manager.go +++ b/d2core/d2asset/font_manager.go @@ -15,14 +15,16 @@ type fontManager struct { cache d2interface.Cache } -func createFontManager() *fontManager { +func createFontManager() d2interface.ArchivedFontManager { 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) 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) @@ -36,3 +38,13 @@ func (fm *fontManager) loadFont(tablePath, spritePath, palettePath string) (*Fon 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 +} diff --git a/d2core/d2asset/palette_manager.go b/d2core/d2asset/palette_manager.go index 0b72a92d..1145c9a4 100644 --- a/d2core/d2asset/palette_manager.go +++ b/d2core/d2asset/palette_manager.go @@ -14,10 +14,11 @@ const ( paletteBudget = 64 ) -func createPaletteManager() *paletteManager { +func createPaletteManager() d2interface.ArchivedPaletteManager { return &paletteManager{d2common.CreateCache(paletteBudget)} } +// LoadPalette loads a palette from archives managed by the ArchiveManager func (pm *paletteManager) LoadPalette(palettePath string) (d2interface.Palette, error) { if palette, found := pm.cache.Retrieve(palettePath); found { return palette.(d2interface.Palette), nil @@ -39,3 +40,13 @@ func (pm *paletteManager) LoadPalette(palettePath string) (d2interface.Palette, 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 +} diff --git a/d2core/d2gui/common.go b/d2core/d2gui/common.go index 30569721..d4b7b762 100644 --- a/d2core/d2gui/common.go +++ b/d2core/d2gui/common.go @@ -9,7 +9,7 @@ import ( "github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset" ) -func loadFont(fontStyle FontStyle) (*d2asset.Font, error) { +func loadFont(fontStyle FontStyle) (d2interface.Font, error) { config, ok := fontStyleConfigs[fontStyle] if !ok { return nil, errors.New("invalid font style") diff --git a/d2core/d2gui/label.go b/d2core/d2gui/label.go index ffeb66cf..94951849 100644 --- a/d2core/d2gui/label.go +++ b/d2core/d2gui/label.go @@ -2,7 +2,6 @@ package d2gui import ( "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" - "github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset" ) type Label struct { @@ -10,7 +9,7 @@ type Label struct { renderer d2interface.Renderer text string - font *d2asset.Font + font d2interface.Font surface d2interface.Surface }