Unity DF-GUI 中文输入以及自动换行

      在使用Daikon Forge GUI过程中,发现dfTextbox编辑框无法输入中文,dfLabel和dfRichTextLabel文本控件中文无法自动换行。对源代码进行了更改,以便支持中文。不同版本可能更改的地方会不同,这边的版本为Daikon Forge GUI 1.0.15,Unity 4.3.1。

dfTextbox中文输入

当前无法输入中文,是因为没有开启Input.imeCompositionMode IME组合方式。打开 dfTextbox.cs文件,添加以下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     protected  internal  override  void OnGotFocus( dfFocusEventArgs args )
    {

        Input.imeCompositionMode = IMECompositionMode.On;

         base.OnGotFocus( args );

    }

     protected  internal  override  void OnLostFocus( dfFocusEventArgs args )
    {

         base.OnLostFocus( args );

        Input.imeCompositionMode = IMECompositionMode.Auto;

    }
效果图:



dfLabel和dfRichTextLabel中文自动换行

文本控件中文无法自动换行,是因为DF-GUI只把英文进行分词,方式是遇到空格就分词,这边要加上中文的处理,即遇到中文就要进行分词。打开 dfMarkupTokenizer.cs文件,添加一个扩展方法,用来判断是否中文,代码如下:
1
2
3
4
5
6
7
8
9
public  static  class CharExtensions
{
     public  static  bool IsChinese( this Char ch)
    {
        var low =  '\u4E00';
        var high =  '\u9FA5';
         return ch.CompareTo(low) * ch.CompareTo(high) <=  0;
    }
}
修改以下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public class dfMarkupTokenizer
{
    private dfMarkupToken parseNonWhitespace()
    {

        var startOffset = index;
        var endOffset = index;

        while( index < source.Length )
        {
             // 中文每个字单独分开
            if ( Peek().IsChinese() )
            {
                Advance();
                break;
            }


            var next = Advance();
            if( char.IsWhiteSpace( next ) || AtTagPosition() )
            {
                break;
            }

            endOffset += 1;

        }

        var token = dfMarkupToken.Obtain( source, dfMarkupTokenType.Text, startOffset, endOffset );
        return token;

    }
}

public class dfPlainTextTokenizer
{
    private List<dfMarkupToken> tokenize( string source )
    {

        // Flush the object pools
        dfMarkupToken.Reset();
        dfMarkupTokenAttribute.Reset();
        tokens.Clear();

        var i = 0;
        var x = 0;
        var length = source.Length;

        while( i < length )
        {

            // Skip carriage returns altogether
            if( source[ i ] == '\r' )
            {
                i += 1;
                x = i;
                continue;
            }

            #region Extract non-whitespace text

            while( i < length && !char.IsWhiteSpace( source[ i ] ) )
            {
                 // 中文每个字单独分开
                if ( source[ i ].IsChinese() )
                {
                    i += 1;
                    break;
                }


                i += 1;
            }

            if( i > x )
            {

                tokens.Add( dfMarkupToken.Obtain(
                    source,
                    dfMarkupTokenType.Text,
                    x,
                    i - 1
                ) );

                x = i;

            }

            #endregion

            // ……

        }

        return this.tokens;

    }
}
打开 dfDynamicFont.cs文件,修改以下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class dfDynamicFont : dfFontBase
{
    public class DynamicFontRenderer : dfFontRendererBase
    {
        private dfList<LineRenderInfo> calculateLinebreaks()
        {

            try
            {

                //@Profiler.BeginSample( "Calculate line breaks" );

                if( lines != null )
                {
                    return lines;
                }

                lines = dfList<LineRenderInfo>.Obtain();

                var font = (dfDynamicFont)Font;
                var lastBreak = 0;
                var startIndex = 0;
                var index = 0;
                var lineWidth = 0;
                var lineHeight = font.Baseline * TextScale;

                while( index < tokens.Count && lines.Count * lineHeight <= MaxSize.y + lineHeight )
                {

                    var token = tokens[ index ];
                    var type = token.TokenType;

                    if( type == dfMarkupTokenType.Newline )
                    {

                        lines.Add( LineRenderInfo.Obtain( startIndex, index ) );

                        startIndex = lastBreak = ++index;
                        lineWidth = 0;

                        continue;

                    }

                    var tokenWidth = Mathf.CeilToInt( token.Width );

                    var canWrap =
                        WordWrap &&
                        lastBreak > startIndex &&
                        (
                            type == dfMarkupTokenType.Text ||
                            ( type == dfMarkupTokenType.StartTag && token.Matches( "sprite" ) )
                        );

                    if( canWrap && lineWidth + tokenWidth >= MaxSize.x )
                    {

                        if( lastBreak > startIndex )
                        {

                             //lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak - 1 ) );
                            // 中文不要减1,不然会缺字
                            lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak ) );


                            startIndex = index = ++lastBreak;
                            lineWidth = 0;

                        }
                        else
                        {

                            lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak - 1 ) );

                            startIndex = lastBreak = ++index;
                            lineWidth = 0;

                        }

                        continue;

                    }

                    if( type == dfMarkupTokenType.Whitespace )
                    {
                        lastBreak = index;
                    }
                     // 增加中文换行
                    else if ( type == dfMarkupTokenType.Text && token.Length == 1 && token.Value[0].IsChinese() )
                    {
                        lastBreak = index;
                    }


                    lineWidth += tokenWidth;
                    index += 1;

                }

                if( startIndex < tokens.Count )
                {
                    lines.Add( LineRenderInfo.Obtain( startIndex, tokens.Count - 1 ) );
                }

                for( int i = 0; i < lines.Count; i++ )
                {
                    calculateLineSize( lines[ i ] );
                }

                return lines;

            }
            finally
            {
                //@Profiler.EndSample();
            }

        }
    }
打开 dfMarkupElement.cs文件,修改以下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class dfMarkupString : dfMarkupElement
{
    internal dfMarkupElement SplitWords()
    {

        //@Profiler.BeginSample( "dfMarkupString.SplitWords()" );

        var tag = dfMarkupTagSpan.Obtain();

        var i = 0;
        var x = 0;
        var length = Text.Length;

        while( i < length )
        {

            #region Words

            while( i < length && !char.IsWhiteSpace( Text[ i ] ) )
            {
                 // 中文每个字单独分开
                if ( Text[ i ].IsChinese() )
                {
                    i += 1;
                    break;
                }


                i += 1;
            }

            if( i > x )
            {
                tag.AddChildNode( dfMarkupString.Obtain( Text.Substring( x, i - x ) ) );
                x = i;
            }

            #endregion

            // ……

        }

        //@Profiler.EndSample();

        return tag;

    }
效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值