Utility/Font: Replace Listener system by Signals

Former-commit-id: 0391c506c039c7aeb7acf4a01faa8a979be16749
This commit is contained in:
Lynix 2015-06-07 16:08:14 +02:00
parent cc9b0b307e
commit 022f082363
4 changed files with 71 additions and 43 deletions

View File

@ -95,15 +95,6 @@ class NAZARA_API NzFont : public NzRefCounted, public NzResource, NzNonCopyable
static void SetDefaultGlyphBorder(unsigned int borderSize); static void SetDefaultGlyphBorder(unsigned int borderSize);
static void SetDefaultMinimumStepSize(unsigned int minimumStepSize); static void SetDefaultMinimumStepSize(unsigned int minimumStepSize);
enum ModicationCode
{
ModificationCode_AtlasChanged,
ModificationCode_AtlasLayerChanged,
ModificationCode_GlyphCacheCleared,
ModificationCode_KerningCacheCleared,
ModificationCode_SizeInfoCacheCleared
};
struct Glyph struct Glyph
{ {
NzRecti aabb; NzRecti aabb;
@ -124,6 +115,13 @@ class NAZARA_API NzFont : public NzRefCounted, public NzResource, NzNonCopyable
float underlineThickness; float underlineThickness;
}; };
NazaraSignal(OnFontAtlasChanged, const NzFont*); //< Args: me
NazaraSignal(OnFontAtlasLayerChanged, const NzFont*); //< Args: me
NazaraSignal(OnFontGlyphCacheCleared, const NzFont*); //< Args: me
NazaraSignal(OnFontKerningCacheCleared, const NzFont*); //< Args: me
NazaraSignal(OnFontRelease, const NzFont*); //< Args: me
NazaraSignal(OnFontSizeInfoCacheCleared, const NzFont*); //< Args: me
private: private:
using GlyphMap = std::unordered_map<char32_t, Glyph>; using GlyphMap = std::unordered_map<char32_t, Glyph>;

View File

@ -15,10 +15,12 @@
#include <Nazara/Utility/Font.hpp> #include <Nazara/Utility/Font.hpp>
#include <vector> #include <vector>
class NAZARA_API NzSimpleTextDrawer : public NzAbstractTextDrawer, NzObjectListener class NAZARA_API NzSimpleTextDrawer : public NzAbstractTextDrawer
{ {
public: public:
NzSimpleTextDrawer(); NzSimpleTextDrawer();
NzSimpleTextDrawer(const NzSimpleTextDrawer& drawer);
NzSimpleTextDrawer(NzSimpleTextDrawer&&) = default;
virtual ~NzSimpleTextDrawer() = default; virtual ~NzSimpleTextDrawer() = default;
const NzRectui& GetBounds() const; const NzRectui& GetBounds() const;
@ -42,14 +44,18 @@ class NAZARA_API NzSimpleTextDrawer : public NzAbstractTextDrawer, NzObjectListe
static NzSimpleTextDrawer Draw(NzFont* font, const NzString& str, unsigned int characterSize, nzUInt32 style = nzTextStyle_Regular, const NzColor& color = NzColor::White); static NzSimpleTextDrawer Draw(NzFont* font, const NzString& str, unsigned int characterSize, nzUInt32 style = nzTextStyle_Regular, const NzColor& color = NzColor::White);
private: private:
bool OnObjectModified(const NzRefCounted* object, int index, unsigned int code) override; void OnFontInvalidated(const NzFont* font);
void OnObjectReleased(const NzRefCounted* object, int index) override; void OnFontRelease(const NzFont* object);
void UpdateGlyphs() const; void UpdateGlyphs() const;
NazaraSlot(NzFont, OnFontAtlasChanged, m_atlasChangedSlot);
NazaraSlot(NzFont, OnFontAtlasLayerChanged, m_atlasLayerChangedSlot);
NazaraSlot(NzFont, OnFontGlyphCacheCleared, m_glyphCacheClearedSlot);
NazaraSlot(NzFont, OnFontRelease, m_fontReleaseSlot);
mutable std::vector<Glyph> m_glyphs; mutable std::vector<Glyph> m_glyphs;
NzColor m_color; NzColor m_color;
NzFontRef m_font; NzFontRef m_font;
NzFontListener m_fontListener; // Doit se situer après le FontRef (pour être libéré avant)
mutable NzRectui m_bounds; mutable NzRectui m_bounds;
NzString m_text; NzString m_text;
nzUInt32 m_style; nzUInt32 m_style;

View File

@ -30,6 +30,8 @@ m_minimumStepSize(s_defaultMinimumStepSize)
NzFont::~NzFont() NzFont::~NzFont()
{ {
OnFontRelease(this);
Destroy(); Destroy();
SetAtlas(nullptr); // On libère l'atlas proprement SetAtlas(nullptr); // On libère l'atlas proprement
} }
@ -55,7 +57,8 @@ void NzFont::ClearGlyphCache()
// Destruction des glyphes mémorisés et notification // Destruction des glyphes mémorisés et notification
m_glyphes.clear(); m_glyphes.clear();
NotifyModified(ModificationCode_GlyphCacheCleared);
OnFontGlyphCacheCleared(this);
} }
} }
} }
@ -63,13 +66,15 @@ void NzFont::ClearGlyphCache()
void NzFont::ClearKerningCache() void NzFont::ClearKerningCache()
{ {
m_kerningCache.clear(); m_kerningCache.clear();
NotifyModified(ModificationCode_KerningCacheCleared);
OnFontKerningCacheCleared(this);
} }
void NzFont::ClearSizeInfoCache() void NzFont::ClearSizeInfoCache()
{ {
m_sizeInfoCache.clear(); m_sizeInfoCache.clear();
NotifyModified(ModificationCode_SizeInfoCacheCleared);
OnFontSizeInfoCacheCleared(this);
} }
bool NzFont::Create(NzFontData* data) bool NzFont::Create(NzFontData* data)
@ -304,7 +309,7 @@ void NzFont::SetAtlas(const std::shared_ptr<NzAbstractAtlas>& atlas)
NazaraDisconnect(m_atlasReleaseSlot); NazaraDisconnect(m_atlasReleaseSlot);
} }
NotifyModified(ModificationCode_AtlasChanged); OnFontAtlasChanged(this);
} }
} }
@ -425,7 +430,8 @@ void NzFont::OnAtlasCleared(const NzAbstractAtlas* atlas)
// Notre atlas vient d'être vidé, détruisons le cache de glyphe // Notre atlas vient d'être vidé, détruisons le cache de glyphe
m_glyphes.clear(); m_glyphes.clear();
NotifyModified(ModificationCode_GlyphCacheCleared);
OnFontGlyphCacheCleared(this);
} }
void NzFont::OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* oldLayer, NzAbstractImage* newLayer) void NzFont::OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* oldLayer, NzAbstractImage* newLayer)
@ -444,7 +450,7 @@ void NzFont::OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* o
#endif #endif
// Pour faciliter le travail des ressources qui nous écoutent // Pour faciliter le travail des ressources qui nous écoutent
NotifyModified(ModificationCode_AtlasLayerChanged); OnFontAtlasLayerChanged(this);
} }
void NzFont::OnAtlasRelease(const NzAbstractAtlas* atlas) void NzFont::OnAtlasRelease(const NzAbstractAtlas* atlas)

View File

@ -8,12 +8,22 @@
NzSimpleTextDrawer::NzSimpleTextDrawer() : NzSimpleTextDrawer::NzSimpleTextDrawer() :
m_color(NzColor::White), m_color(NzColor::White),
m_fontListener(this), m_style(nzTextStyle_Regular),
m_style(nzTextStyle_Regular) m_glyphUpdated(false)
{ {
SetFont(NzFont::GetDefault()); SetFont(NzFont::GetDefault());
} }
NzSimpleTextDrawer::NzSimpleTextDrawer(const NzSimpleTextDrawer& drawer) :
m_color(drawer.m_color),
m_text(drawer.m_text),
m_style(drawer.m_style),
m_glyphUpdated(false),
m_characterSize(drawer.m_characterSize)
{
SetFont(drawer.m_font);
}
const NzRectui& NzSimpleTextDrawer::GetBounds() const const NzRectui& NzSimpleTextDrawer::GetBounds() const
{ {
if (!m_glyphUpdated) if (!m_glyphUpdated)
@ -97,10 +107,26 @@ void NzSimpleTextDrawer::SetColor(const NzColor& color)
void NzSimpleTextDrawer::SetFont(NzFont* font) void NzSimpleTextDrawer::SetFont(NzFont* font)
{ {
m_font = font; if (m_font != font)
m_fontListener = font; {
m_font = font;
if (m_font)
{
m_atlasChangedSlot = NazaraConnect(*m_font, OnFontAtlasChanged, OnFontInvalidated);
m_atlasLayerChangedSlot = NazaraConnect(*m_font, OnFontAtlasLayerChanged, OnFontInvalidated);
m_fontReleaseSlot = NazaraConnect(*m_font, OnFontRelease, OnFontRelease);
m_glyphCacheClearedSlot = NazaraConnect(*m_font, OnFontGlyphCacheCleared, OnFontInvalidated);
}
else
{
NazaraDisconnect(m_atlasChangedSlot);
NazaraDisconnect(m_atlasLayerChangedSlot);
NazaraDisconnect(m_fontReleaseSlot);
NazaraDisconnect(m_glyphCacheClearedSlot);
}
m_glyphUpdated = false; m_glyphUpdated = false;
}
} }
void NzSimpleTextDrawer::SetStyle(nzUInt32 style) void NzSimpleTextDrawer::SetStyle(nzUInt32 style)
@ -140,38 +166,30 @@ NzSimpleTextDrawer NzSimpleTextDrawer::Draw(NzFont* font, const NzString& str, u
return drawer; return drawer;
} }
bool NzSimpleTextDrawer::OnObjectModified(const NzRefCounted* object, int index, unsigned int code) void NzSimpleTextDrawer::OnFontInvalidated(const NzFont* font)
{ {
NazaraUnused(object); NazaraUnused(font);
NazaraUnused(index);
#ifdef NAZARA_DEBUG #ifdef NAZARA_DEBUG
if (m_font != object) if (m_font != font)
{ {
NazaraInternalError("Not listening to " + NzString::Pointer(object)); NazaraInternalError("Not listening to " + NzString::Pointer(font));
return false; return;
} }
#endif #endif
if (code == NzFont::ModificationCode_AtlasChanged || m_glyphUpdated = false;
code == NzFont::ModificationCode_AtlasLayerChanged ||
code == NzFont::ModificationCode_GlyphCacheCleared)
{
m_glyphUpdated = false;
}
return true;
} }
void NzSimpleTextDrawer::OnObjectReleased(const NzRefCounted* object, int index) void NzSimpleTextDrawer::OnFontRelease(const NzFont* font)
{ {
NazaraUnused(object); NazaraUnused(font);
NazaraUnused(index); NazaraUnused(font);
#ifdef NAZARA_DEBUG #ifdef NAZARA_DEBUG
if (m_font != object) if (m_font != font)
{ {
NazaraInternalError("Not listening to " + NzString::Pointer(object)); NazaraInternalError("Not listening to " + NzString::Pointer(font));
return; return;
} }
#endif #endif