2
0
Эх сурвалжийг харах

refactoring
const Font* TextStyle::font
replaced with
const ResFont* TextStyle::font

dmuratshin 9 жил өмнө
parent
commit
6f840d5176

+ 1 - 1
examples/Demo/src/TestColorFont.h

@@ -146,7 +146,7 @@ public:
         _txt = txt;
 
         TextStyle st;
-        st.font = resources.getResFont("num_fnt_shdr")->getFont();
+        st.font = resources.getResFont("num_fnt_shdr");
         st.vAlign = TextStyle::VALIGN_MIDDLE;
         st.hAlign = TextStyle::HALIGN_MIDDLE;
         //st.color = Color::CornflowerBlue;

+ 1 - 1
examples/Demo/src/TestInputText.h

@@ -20,7 +20,7 @@ public:
         style.hAlign = TextStyle::HALIGN_CENTER;
         style.vAlign = TextStyle::VALIGN_MIDDLE;
         style.multiline = true;
-        style.font = Test::resourcesUI.getResFont("big")->getFont();
+        style.font = Test::resourcesUI.getResFont("big");
         text->setStyle(style);
         text->setText(defText);
 

+ 1 - 1
examples/Demo/src/TestManageRes.h

@@ -74,7 +74,7 @@ public:
         text->setPosition(140.0f, (float)getHeight() - text->getHeight());
 
         TextStyle st;
-        st.font = resources.getResFont("font")->getFont();
+        st.font = resources.getResFont("font");
         st.vAlign = TextStyle::VALIGN_TOP;
         st.color = Color::CornflowerBlue;
         st.multiline = true;

+ 1 - 1
examples/Demo/src/TestMask.h

@@ -47,7 +47,7 @@ public:
 
 
         TextStyle style;
-        style.font = resourcesUI.getResFont("big")->getFont();
+        style.font = resourcesUI.getResFont("big");
         style.color = Color::OrangeRed;
         style.vAlign = TextStyle::VALIGN_MIDDLE;
         style.hAlign = TextStyle::HALIGN_CENTER;

+ 1 - 1
examples/Demo/src/TestSignedDistanceFont.h

@@ -37,7 +37,7 @@ public:
         _txt = txt;
 
         TextStyle st;
-        st.font = font.getFont();
+        st.font = &font;
         st.vAlign = TextStyle::VALIGN_MIDDLE;
         st.hAlign = TextStyle::HALIGN_MIDDLE;
         st.color = Color::CornflowerBlue;

+ 1 - 1
examples/Demo/src/TestSliding.h

@@ -44,7 +44,7 @@ public:
         title->setSize(map->getSize());
         title->setAlign(TextStyle::VALIGN_MIDDLE, TextStyle::HALIGN_MIDDLE);
         title->setY(-50);
-        title->setFont(resourcesUI.getResFont("big")->getFont());
+        title->setFont(resourcesUI.getResFont("big"));
         title->setColor(Color::Black);
         title->attachTo(map);
 

+ 2 - 2
examples/Demo/src/TestText.h

@@ -49,7 +49,7 @@ public:
         textDetails->setPosition(200.0f, getStage()->getHeight() - 10);
 
         TextStyle st;
-        st.font = resourcesUI.getResFont("main")->getFont();
+        st.font = resourcesUI.getResFont("main");
         st.vAlign = TextStyle::VALIGN_BOTTOM;
         st.color = Color(0xffffffff);
         st.multiline = true;
@@ -132,7 +132,7 @@ public:
         style.multiline = test.multiline;
         style.vAlign = test.vAlign;
         style.hAlign = test.hAlign;
-        style.font = resourcesUI.getResFont("big")->getFont();
+        style.font = resourcesUI.getResFont("big");
         style.color = Color(0xffffffff);
         style.fontSize = test.scale2size;
         if (!style.fontSize)

+ 1 - 1
examples/Demo/src/TestTouches.h

@@ -133,7 +133,7 @@ public:
         tf->setX(content->getWidth() - tf->getWidth());
         tf->setY(y);
         tf->setMultiline(true);
-        tf->setFont(Test::resourcesUI.getResFont("big")->getFont());
+        tf->setFont(Test::resourcesUI.getResFont("big"));
         y += 200;
         return tf;
     }

+ 1 - 1
examples/Demo/src/TestTweenText.h

@@ -41,7 +41,7 @@ public:
         content->addChild(text);
 
         TextStyle st;
-        st.font = resourcesUI.getResFont("big")->getFont();
+        st.font = resourcesUI.getResFont("big");
         st.vAlign = TextStyle::VALIGN_MIDDLE;
         st.hAlign = TextStyle::HALIGN_MIDDLE;
         st.multiline = true;

+ 1 - 1
examples/Demo/src/test.cpp

@@ -61,7 +61,7 @@ spTextField createText(const std::string& txt)
     spTextField text = new TextField();
 
     TextStyle style;
-    style.font = Test::resourcesUI.getResFont("main")->getFont();
+    style.font = Test::resourcesUI.getResFont("main");
     style.color = textColor;
     style.vAlign = TextStyle::VALIGN_MIDDLE;
     style.hAlign = TextStyle::HALIGN_CENTER;

+ 1 - 1
examples/Game/part4/src/GameMenu.cpp

@@ -25,7 +25,7 @@ GameMenu::GameMenu()
     //initialize TextStyle for TextField
     //TextStyle is plain struct with "setting" for Text
     TextStyle style;
-    style.font = res::ui.getResFont("big")->getFont();
+    style.font = res::ui.getResFont("big");
     //vertical align
     style.vAlign = TextStyle::VALIGN_BOTTOM;
     //horizontal align

+ 1 - 1
examples/Game/part4/src/MyButton.cpp

@@ -32,7 +32,7 @@ void MyButton::setText(const string& txt)
     {
         //create TextField if it wasn't created yet
         TextStyle style;
-        style.font = res::ui.getResFont("normal")->getFont();
+        style.font = res::ui.getResFont("normal");
         style.vAlign = TextStyle::VALIGN_MIDDLE;
         style.hAlign = TextStyle::HALIGN_CENTER;
 

+ 1 - 1
examples/Game/part5/src/GameMenu.cpp

@@ -25,7 +25,7 @@ GameMenu::GameMenu()
     //initialize TextStyle for TextField
     //TextStyle is plain struct with "setting" for Text
     TextStyle style;
-    style.font = res::ui.getResFont("big")->getFont();
+    style.font = res::ui.getResFont("big");
     //vertical align
     style.vAlign = TextStyle::VALIGN_BOTTOM;
     //horizontal align

+ 1 - 1
examples/Game/part5/src/MyButton.cpp

@@ -32,7 +32,7 @@ void MyButton::setText(const string& txt)
     {
         //create TextField if it wasn't created yet
         TextStyle style;
-        style.font = res::ui.getResFont("normal")->getFont();
+        style.font = res::ui.getResFont("normal");
         style.vAlign = TextStyle::VALIGN_MIDDLE;
         style.hAlign = TextStyle::HALIGN_CENTER;
 

+ 1 - 1
examples/HelloWorld/src/example.cpp

@@ -55,7 +55,7 @@ public:
 
         //initialize text style
         TextStyle style;
-        style.font = gameResources.getResFont("main")->getFont();
+        style.font = gameResources.getResFont("main");
         style.color = Color::White;
         style.vAlign = TextStyle::VALIGN_MIDDLE;
         style.hAlign = TextStyle::HALIGN_CENTER;

+ 1 - 1
oxygine/src/DebugActor.cpp

@@ -125,7 +125,7 @@ namespace oxygine
 
         if (ResFont* fnt = resSystem->getResFont("mono"))
         {
-            st.font = fnt->getFont();
+            st.font = fnt;
         }
 
         OX_ASSERT(st.font != NULL);

+ 4 - 3
oxygine/src/STDMaterial.cpp

@@ -10,6 +10,7 @@
 #include "MaskedSprite.h"
 #include "ClipRectActor.h"
 #include "core/oxygine.h"
+#include "res/ResFont.h"
 
 namespace oxygine
 {
@@ -176,13 +177,13 @@ namespace oxygine
         _renderer->setBlendMode(tf->getBlendMode());
         _renderer->setTransform(rs.transform);
 
-
-        if (tf->getFont()->isSDF())
+        int sdfSize;
+        if (tf->getFont()->isSDF(sdfSize))
         {
             float scale = sqrtf(rs.transform.a * rs.transform.a + rs.transform.c * rs.transform.c);
 
             if (tf->getFontSize())
-                scale = scale * tf->getFontSize() / tf->getFont()->getSize();
+                scale = scale * tf->getFontSize() / sdfSize;
 
             float contrast = 3.0f + scale * 8.0f;
             float offset = tf->getWeight();

+ 5 - 6
oxygine/src/TextField.cpp

@@ -27,7 +27,7 @@ namespace oxygine
         {
             if (ResFont* fnt = DebugActor::resSystem->getResFont("system"))
             {
-                _style.font = fnt->getFont();
+                _style.font = fnt;
             }
         }
     }
@@ -102,12 +102,12 @@ namespace oxygine
         needRebuild();
     }
 
-    const Font* TextField::getFont() const
+    const ResFont* TextField::getFont() const
     {
         return _style.font;
     }
 
-    void TextField::setFont(const Font* font)
+    void TextField::setFont(const ResFont* font)
     {
         _style.font = font;
         needRebuild();
@@ -286,11 +286,10 @@ namespace oxygine
                 _root = new text::TextNode(_text.c_str());
             }
 
-            text::Aligner rd;
+            text::Aligner rd(_style);
 
             rd.width = (int)getWidth();
             rd.height = (int)getHeight();
-            rd.style = _style;
             rd.begin();
             _root->resize(rd);
             rd.end();
@@ -440,7 +439,7 @@ namespace oxygine
         {
             ResFont* font = data->factory->getResFont(fnt);
             if (font)
-                _style.font = font->getFont();
+                _style.font = font;
         }
 
         needRebuild();

+ 2 - 2
oxygine/src/TextField.h

@@ -30,7 +30,7 @@ namespace oxygine
         const Rect&                 getTextRect() const;
         /**Returns current text*/
         const std::string&          getText() const { return _text; }
-        const Font*                 getFont() const;
+        const ResFont*              getFont() const;
         /**deprecated use getFontSize*/
         OXYGINE_DEPRECATED
         int                         getFontSize2Scale() const;
@@ -69,7 +69,7 @@ namespace oxygine
         void setFontSize(int size);
 
         /**Overwrites TextStyle font.*/
-        void setFont(const Font* rs);
+        void setFont(const ResFont* rs);
 
         /**Overwrites TextStyle outlineColor. works only with SD font*/
         void setOutlineColor(const Color&);

+ 4 - 2
oxygine/src/TextStyle.h

@@ -6,6 +6,8 @@
 namespace oxygine
 {
     class Font;
+    class ResFont;
+
     class TextStyle
     {
     public:
@@ -40,7 +42,7 @@ namespace oxygine
             outlineColor(Color::Black),
             weight(0.5f) {}
 
-        const Font* font;
+        const ResFont* font;
 
         HorizontalAlign hAlign;
         VerticalAlign vAlign;
@@ -65,7 +67,7 @@ namespace oxygine
 
 
 
-        TextStyle withFont(const Font* f) const { TextStyle st = *this; st.font = f; return st; }
+        TextStyle withFont(const ResFont* f) const { TextStyle st = *this; st.font = f; return st; }
 
         TextStyle alignTop() const { TextStyle st = *this; st.vAlign = VALIGN_TOP; return st; }
         TextStyle alignBottom() const { TextStyle st = *this; st.vAlign = VALIGN_BOTTOM; return st; }

+ 1 - 1
oxygine/src/dev_tools/DeveloperMenu.cpp

@@ -75,7 +75,7 @@ namespace oxygine
 
 
         TextStyle style;
-        style.font = _resSystem->getResFont("system")->getFont();
+        style.font = _resSystem->getResFont("system");
         style.vAlign = TextStyle::VALIGN_TOP;
 
         spButton close = initActor(new Button,

+ 1 - 1
oxygine/src/dev_tools/TreeInspector.cpp

@@ -131,7 +131,7 @@ namespace oxygine
 
 
         TextStyle style;
-        style.font = _resSystem->getResFont("system")->getFont();
+        style.font = _resSystem->getResFont("system");
         style.vAlign = TextStyle::VALIGN_TOP;
 
         updateSizes();

+ 1 - 1
oxygine/src/dev_tools/TreeInspectorLine.cpp

@@ -37,7 +37,7 @@ namespace oxygine
         const float minWidth = 300.0f;
 
         TextStyle style;
-        style.font = _page->getTreeInspector()->_resSystem->getResFont("system")->getFont();
+        style.font = _page->getTreeInspector()->_resSystem->getResFont("system");
         style.multiline = true;
         style.vAlign = TextStyle::VALIGN_TOP;
         tb->setStyle(style);

+ 1 - 1
oxygine/src/initActor.h

@@ -69,7 +69,7 @@ namespace oxygine
         typedef argT<TextStyle::HorizontalAlign, TextStyle::HorizontalAlign, TextField, &TextField::setHAlign> hAlign;
         typedef argT<TextStyle::VerticalAlign, TextStyle::VerticalAlign, TextField, &TextField::setVAlign> vAlign;
         typedef argT<int, int, TextField, &TextField::setFontSize> fontSize;
-        typedef argT<const Font*, const Font*, TextField, &TextField::setFont> font;
+        typedef argT<const ResFont*, const ResFont*, TextField, &TextField::setFont> font;
         typedef argT<bool, bool, TextField, &TextField::setMultiline> multiline;
 
 

+ 7 - 0
oxygine/src/res/ResFont.h

@@ -9,6 +9,13 @@ namespace oxygine
     class ResFont: public _Resource
     {
     public:
+        ResFont(): _size(0) {}
+
         virtual const Font* getFont(const char* name = 0, int size = 0) const = 0;
+        virtual bool isSDF(int& size) const { size = 0; return false; }
+        int getSize() const { return _size; }
+
+    protected:
+        int _size;
     };
 }

+ 9 - 0
oxygine/src/res/ResFontBM.cpp

@@ -54,6 +54,7 @@ namespace oxygine
         return font;
     }
 
+
     ResFontBM::ResFontBM(): _font(0), _format(TF_R8G8B8A8), _premultipliedAlpha(false), _sdf(false)
     {
 
@@ -65,6 +66,12 @@ namespace oxygine
     }
 
 
+    bool ResFontBM::isSDF(int& size) const
+    {
+        size = _font->getSize();
+        return _sdf;
+    }
+
     void ResFontBM::init(const char* path, bool premultipliedAlpha)
     {
         _premultipliedAlpha = premultipliedAlpha;
@@ -315,6 +322,7 @@ namespace oxygine
         fontSize = abs(fontSize);
         Font* font = new Font();
         font->init(getName().c_str(), fontSize, fontSize, lineHeight + fontSize - base, _sdf);
+        _size = fontSize;
         _font = font;
 
         if (context)
@@ -508,6 +516,7 @@ namespace oxygine
         fontSize = abs(fontSize);
         Font* font = new Font();
         font->init(getName().c_str(), fontSize, fontSize, lineHeight + fontSize - base, _sdf);
+        _size = fontSize;
         _font = font;
 
         if (context)

+ 1 - 1
oxygine/src/res/ResFontBM.h

@@ -31,7 +31,7 @@ namespace oxygine
 
         const Font* getFont(const char* name = 0, int size = 0) const OVERRIDE;
 
-        bool isSignedDistance() const { return _sdf; }
+        bool isSDF(int& size) const OVERRIDE;
 
     private:
         struct page

+ 6 - 5
oxygine/src/text_utils/Aligner.cpp

@@ -1,14 +1,15 @@
 #include "Aligner.h"
 #include "Font.h"
 #include <assert.h>
-
+#include "res/ResFont.h"
 namespace oxygine
 {
     namespace text
     {
 
-        Aligner::Aligner(): width(0), height(0), _x(0), _y(0), _lineWidth(0), bounds(0, 0, 0, 0)
+        Aligner::Aligner(const TextStyle& Style): width(0), height(0), _x(0), _y(0), _lineWidth(0), bounds(0, 0, 0, 0), style(Style)
         {
+            _font = style.font->getFont(0, style.fontSize);
             _line.reserve(50);
         }
 
@@ -65,9 +66,9 @@ namespace oxygine
             _y = 0;
 
             const TextStyle& st = getStyle();
-            _scale = st.font->getScale();
+            _scale = _font->getScale();
             if (st.fontSize)
-                _scale = st.font->getSize() / float(st.fontSize);
+                _scale = _font->getSize() / float(st.fontSize);
 
             width = int(width * _scale);
             height = int(height * _scale);
@@ -101,7 +102,7 @@ namespace oxygine
 
         int Aligner::getLineSkip() const
         {
-            return getStyle().font->getBaselineDistance() + getStyle().linesOffset;
+            return _font->getBaselineDistance() + getStyle().linesOffset;
         }
 
         void Aligner::_alignLine(line& ln)

+ 3 - 1
oxygine/src/text_utils/Aligner.h

@@ -27,7 +27,7 @@ namespace oxygine
         class Aligner
         {
         public:
-            Aligner();
+            Aligner(const TextStyle& style);
             ~Aligner();
 
 
@@ -50,6 +50,8 @@ namespace oxygine
             int getLineWidth()const;
             int getLineSkip()const;
 
+            const Font* _font;
+
             typedef std::vector<Symbol*> line;
 
             void _alignLine(line& ln);

+ 2 - 1
oxygine/src/text_utils/Node.cpp

@@ -4,6 +4,7 @@
 #include "utils/stringUtils.h"
 #include "RenderState.h"
 #include "AnimationFrame.h"
+#include "res/ResFont.h"
 
 namespace oxygine
 {
@@ -166,7 +167,7 @@ namespace oxygine
             if (!_data.empty())
             {
                 int i = 0;
-                const Font* font = rd.getStyle().font;
+                const Font* font = rd.getStyle().font->getFont(0, rd.getStyle().fontSize);
 
                 while (i != (int)_data.size())
                 {