Browse Source

- added Default font to TextField
- global font scale wip

dmuratshin 9 years ago
parent
commit
8d3b66cf1e

+ 34 - 18
oxygine/src/TextField.cpp

@@ -16,21 +16,24 @@
 
 namespace oxygine
 {
+    static ResFont* _defaultFont = 0;
+    void TextField::setDefaultFont(ResFont* f)
+    {
+        _defaultFont = f;
+    }
+
+    ResFont* TextField::getDefaultFont()
+    {
+        return _defaultFont;
+    }
+
     TextField::TextField():
         _root(0),
-        _textRect(0, 0, 0, 0)
+        _textRect(0, 0, 0, 0),
+        _rtscale(1.0f)
     {
         _flags |= flag_rebuild;
-        _style.font = NULL;
-        _rtscale = 0;
-
-        if (DebugActor::resSystem)
-        {
-            if (ResFont* fnt = DebugActor::resSystem->getResFont("system"))
-            {
-                _style.font = fnt;
-            }
-        }
+        _style.font = _defaultFont;
     }
 
     TextField::~TextField()
@@ -45,6 +48,7 @@ namespace oxygine
         _text = src._text;
         _style = src._style;
         _root = 0;
+        _rtscale = 1.0f;
 
         _flags |= flag_rebuild;
         _textRect = src._textRect;
@@ -117,6 +121,8 @@ namespace oxygine
     void TextField::setFont(const ResFont* font)
     {
         _style.font = font;
+        if (!_style.font)
+            _style.font = _defaultFont;
         needRebuild();
     }
 
@@ -164,6 +170,9 @@ namespace oxygine
         if (st.fontSize == 0)
             _style.fontSize = size;
 
+        if (!_style.font)
+            _style.font = _defaultFont;
+
         needRebuild();
     }
 
@@ -264,7 +273,7 @@ namespace oxygine
 
     text::Symbol* TextField::getSymbolAt(int pos) const
     {
-        return const_cast<TextField*>(this)->getRootNode(1)->getSymbol(pos);
+        return const_cast<TextField*>(this)->getRootNode(_rtscale)->getSymbol(pos);
     }
 
     const Rect& TextField::getTextRect() const
@@ -280,10 +289,21 @@ namespace oxygine
     }
 
 
-    text::Node* TextField::getRootNode(float scale)
+    text::Node* TextField::getRootNode(float globalScale)
     {
+        if (!_style.font)
+            return _root;
+
+
+        int fontSize = _style.fontSize;
+        float scale = globalScale;
+
+        _style.font->alignSize(globalScale, _style.fontSize, scale, fontSize);
+
         if ((_flags & flag_rebuild || _rtscale != scale) && _style.font)
         {
+            _rtscale = scale;
+            _realFontSize = fontSize;
             delete _root;
 
             _flags &= ~flag_rebuild;
@@ -298,11 +318,7 @@ namespace oxygine
                 _root = new text::TextNode(_text.c_str());
             }
 
-            _rtscale = scale;
-            text::Aligner rd(_style, scale);
-
-            rd.width = (int)getWidth();
-            rd.height = (int)getHeight();
+            text::Aligner rd(_style, fontSize, scale, getSize());
             rd.begin();
             _root->resize(rd);
             rd.end();

+ 4 - 0
oxygine/src/TextField.h

@@ -11,6 +11,9 @@ namespace oxygine
     {
         INHERITED(VStyleActor);
     public:
+        static void setDefaultFont(ResFont*);
+        static ResFont* getDefaultFont();
+
         DECLARE_COPYCLONE_NEW(TextField);
         TextField();
         ~TextField();
@@ -112,6 +115,7 @@ namespace oxygine
         text::Node* _root;
         Rect _textRect;
         float _rtscale;
+        int _realFontSize;
 
 
         void needRebuild();

+ 6 - 0
oxygine/src/core/oxygine.cpp

@@ -25,6 +25,7 @@
 #include "winnie_alloc/winnie_alloc.h"
 #include "ThreadDispatcher.h"
 #include "PostProcess.h"
+#include "TextField.h"
 
 #ifdef __S3E__
 #include "s3e.h"
@@ -469,6 +470,11 @@ namespace oxygine
             STDMaterial::instance = new STDMaterial(STDRenderer::instance);
 
             CHECKGL();
+
+#ifdef OX_DEBUG
+            DebugActor::initialize();
+            TextField::setDefaultFont(DebugActor::resSystem->getResFont("system"));
+#endif
             log::messageln("oxygine initialized");
         }
 

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

@@ -15,6 +15,8 @@ namespace oxygine
         virtual bool isSDF(int& size) const { size = 0; return false; }
         int getSize() const { return _size; }
 
+        virtual void alignSize(float scale, int fontSize, float& resScale, int& resFontSize) const = 0;
+
     protected:
         int _size;
     };

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

@@ -72,6 +72,25 @@ namespace oxygine
         return _sdf;
     }
 
+    void ResFontBM::alignSize(float scale, int fontSize, float& resScale, int& resFontSize) const
+    {
+        resFontSize = fontSize;
+        resScale = scale;
+
+
+
+        //_scale = _font->getScale();
+        //if (st.fontSize)
+        //    _scale = _font->getSize() / float(st.fontSize);
+        if (fontSize)
+            resScale = _font->getSize() / float(fontSize);
+        else
+        {
+            resFontSize = _font->getScale();
+            resScale = _font->getScale();
+        }
+    }
+
     void ResFontBM::init(const char* path, bool premultipliedAlpha)
     {
         _premultipliedAlpha = premultipliedAlpha;

+ 2 - 0
oxygine/src/res/ResFontBM.h

@@ -26,6 +26,8 @@ namespace oxygine
 
         bool isSDF(int& size) const OVERRIDE;
 
+        void alignSize(float scale, int fontSize, float& resScale, int& resFontSize) const OVERRIDE;
+
     private:
         struct page
         {

+ 4 - 31
oxygine/src/text_utils/Aligner.cpp

@@ -8,30 +8,13 @@ namespace oxygine
     {
 #define GSCALE 1
 
-        Aligner::Aligner(const TextStyle& Style, float gscale): width(0), height(0), _x(0), _y(0), _lineWidth(0), bounds(0, 0, 0, 0), style(Style), _globalScale(gscale)
+        Aligner::Aligner(const TextStyle& Style, int fs, float gscale, const Vector2& size): width((int)size.x), height((int)size.y), _x(0), _y(0), _lineWidth(0), bounds(0, 0, 0, 0), style(Style), _scale(gscale), _fontSize(fs)
         {
-            _fontSize = style.fontSize;
-
-#ifdef GSCALE
-            float fs = _fontSize * _globalScale;
-            _fontSize = fs;
-            const int X = 20;
-            if (_fontSize > X)
-            {
-                int x = _fontSize % X;
-                if (x)
-                    _fontSize = _fontSize + (X - x);
-            }
-
-            _adjScale = fs / _fontSize;
-            _globalScale = (float)_fontSize / style.fontSize;
-#endif
-
-
-            log::messageln("gscale %f, adjScale %f globscale %f, %d %f", gscale, _adjScale, _globalScale, _fontSize, fs);
+            //log::messageln("gscale %f, adjScale %f globscale %f, %d %f", gscale, _globalScale, _fontSize, fs);
 
             _font = style.font->getFont(0, _fontSize);
             _line.reserve(50);
+            _lineSkip = (int)(_font->getBaselineDistance() * style.baselineScale) + style.linesOffset;
         }
 
         Aligner::~Aligner()
@@ -86,16 +69,6 @@ namespace oxygine
             _x = 0;
             _y = 0;
 
-            const TextStyle& st = getStyle();
-            _scale = _font->getScale();
-            if (st.fontSize)
-                _scale = _font->getSize() / float(st.fontSize);
-
-
-#ifdef GSCALE
-            _scale =  _globalScale;
-#endif
-
             width = int(width * _scale);
             height = int(height * _scale);
 
@@ -129,7 +102,7 @@ namespace oxygine
 
         int Aligner::getLineSkip() const
         {
-            return _font->getBaselineDistance() * getStyle().baselineScale + getStyle().linesOffset;
+            return _lineSkip;
         }
 
         void Aligner::_alignLine(line& ln)

+ 2 - 4
oxygine/src/text_utils/Aligner.h

@@ -27,7 +27,7 @@ namespace oxygine
         class Aligner
         {
         public:
-            Aligner(const TextStyle& style, float gscale);
+            Aligner(const TextStyle& style, int fs, float gscale, const Vector2& size);
             ~Aligner();
 
 
@@ -49,8 +49,6 @@ namespace oxygine
 
             const Font* _font;
 
-            float _adjScale;
-
         private:
             int getLineWidth()const;
             int getLineSkip()const;
@@ -64,11 +62,11 @@ namespace oxygine
             void _nextLine(line& ln);
 
             float _scale;
-            float _globalScale;
             int _x, _y;
             line _line;
             int _lineWidth;
             int _fontSize;
+            int _lineSkip;
         };
     }
 }