| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // 文本解析
- namespace WXB
- {
- public partial class TextParser
- {
- delegate void OnFun(string c);
- OnFun[] OnFuns = null;
- void Reg()
- {
- OnFuns = new OnFun[128];
- // 特定的颜色
- /*OnFuns['R'] = ParserSureColor;
- OnFuns['G'] = ParserSureColor;
- OnFuns['B'] = ParserSureColor;
- OnFuns['K'] = ParserSureColor;
- OnFuns['Y'] = ParserSureColor;
- OnFuns['W'] = ParserSureColor;
- // 输出一个#
- OnFuns['#'] = ParserOutputChar;
- OnFuns['['] = ParserFontColorS; // 自定义的颜色名
- OnFuns['b'] = ParserBlink; // 闪烁
- OnFuns['c'] = ParserFontColor; // 颜色
- OnFuns['d'] = ParserFontStyle; // 字体风格
- OnFuns['e'] = ParserStrickout; // 删除线
- OnFuns['m'] = ParserDynStrickout; // 动态删除线
- OnFuns['f'] = ParserFont; // 换字体
- OnFuns['n'] = ParserRestoreColor; // 恢复初始颜色
- OnFuns['g'] = ParserRestore; // 所有设置恢复默认
- OnFuns['r'] = ParserNewLine; // 换行
- OnFuns['u'] = ParserUnderLine; // 下划线
- OnFuns['t'] = ParserDynUnderline; // 动态下划线
- OnFuns['l'] = ParserDynSpeed; // 动态线速度*/
- OnFuns['r'] = ParserNewLine; // 换行
- OnFuns['c'] = ParserFontColor; // 颜色
- OnFuns['t'] = ParserDynUnderline; // 动态下划线
- OnFuns['h'] = ParserHyperlink; // 超链接
- /*OnFuns['s'] = ParserFontSize; // 字体大小
- OnFuns['x'] = ParserXYZ; // xyz的偏移
- OnFuns['y'] = ParserXYZ;
- OnFuns['z'] = ParserXYZ;
- OnFuns['w'] = ParserFormatting; // 对齐格式
- OnFuns['a'] = ParserLineAlignment; // 行对齐
- // #后面接数字,最多三位数字,为动画的名称
- OnFuns['0'] = ParserCartoon;
- OnFuns['1'] = ParserCartoon;
- OnFuns['2'] = ParserCartoon;
- OnFuns['3'] = ParserCartoon;
- OnFuns['4'] = ParserCartoon;
- OnFuns['5'] = ParserCartoon;
- OnFuns['6'] = ParserCartoon;
- OnFuns['7'] = ParserCartoon;
- OnFuns['8'] = ParserCartoon;
- OnFuns['9'] = ParserCartoon;
- OnFuns['&'] = ParserNextLineX;*/
- }
- void ParserNextLineX(string text)
- {
- int linex = -1;
- if (!ParserInt(ref d_curPos, text, ref linex, 5))
- return;
- if (linex == currentConfig.nextLineX)
- return;
- save(false);
- currentConfig.nextLineX = linex;
- }
- void ParserCartoon(string text)
- {
- Cartoon cartoon = null;
- int currentPos = d_curPos - 1;
- for (int i = 3; i >= 1; --i)
- {
- int size = -1;
- if (ParserInt(ref currentPos, text, ref size, i))
- {
- cartoon = Tools.GetCartoon(size.ToString());
- if (cartoon != null)
- break;
- }
- currentPos = d_curPos - 1;
- }
- if (cartoon == null)
- return;
- d_curPos = currentPos;
- save(false);
- CartoonNode cn = CreateNode<CartoonNode>();
- cn.cartoon = cartoon;
- cn.width = cartoon.width;
- cn.height = cartoon.height;
- cn.SetConfig(currentConfig);
- // 表情不变色
- cn.d_color = Color.white;
- d_nodeList.Add(cn);
- }
- void ParserDynSpeed(string text)
- {
- int size = -1;
- if (!ParserInt(ref d_curPos, text, ref size, 5))
- return;
- if (size <= 0 || size == currentConfig.dyncSpeed)
- return;
- save(false);
- currentConfig.dyncSpeed = (int)size;
- }
- // 动态下划线
- void ParserDynUnderline(string text)
- {
- save(false); // 保存内容
- currentConfig.isDyncUnderline = !currentConfig.isDyncUnderline; // 下划线
- d_curPos++;
- }
- // 动态删除线
- void ParserDynStrickout(string text)
- {
- save(false); // 保存内容
- currentConfig.isDyncStrickout = !currentConfig.isDyncStrickout; // 下划线
- d_curPos++;
- }
- void ParserRestoreColor(string text)
- {
- if (currentConfig.fontColor != startConfig.fontColor)
- {
- save(false); // 保存内容
- d_curPos++;
- currentConfig.Set(startConfig);
- }
- else
- {
- d_curPos++;
- }
- }
- void ParserRestore(string text)
- {
- if (!currentConfig.isSame(startConfig))
- {
- save(false); // 保存内容
- d_curPos++;
- currentConfig.Set(startConfig);
- }
- else
- {
- d_curPos++;
- }
- }
- void ParserSureColor(string text)
- {
- Color pCol = GetColour(text[d_curPos]);
- if (currentConfig.fontColor != pCol)
- {
- save(false);
- // 改变字体的颜色
- currentConfig.fontColor = pCol;
- }
- d_curPos++;
- }
- void ParserBlink(string text)
- {
- save(false); // 保存内容
- currentConfig.isBlink = !currentConfig.isBlink; // 下划线
- d_curPos++;
- }
- void ParserLineAlignment(string text)
- {
- if (text.Length > (d_curPos + 1))
- {
- LineAlignment a;
- if (Get(text[d_curPos + 1], out a))
- {
- ++d_curPos;
- if (currentConfig.lineAlignment != a)
- {
- currentConfig.lineAlignment = a;
- save(false);
- }
- }
- }
- ++d_curPos;
- }
- void ParserFormatting(string text)
- {
- if (text.Length > (d_curPos + 1))
- {
- Anchor a;
- if (Get(text[d_curPos + 1], out a))
- {
- ++d_curPos;
- if (currentConfig.anchor != a)
- {
- currentConfig.anchor = a;
- save(false);
- }
- }
- }
- ++d_curPos;
- }
- static bool GetFontStyle(char c, out FontStyle fs)
- {
- switch (c)
- {
- case '1': fs = FontStyle.Normal; return true;
- case '2': fs = FontStyle.Bold; return true;
- case '3': fs = FontStyle.Italic; return true;
- case '4': fs = FontStyle.BoldAndItalic; return true;
- }
- fs = FontStyle.Normal;
- return false;
- }
- void ParserFontStyle(string text)
- {
- if (text.Length > (d_curPos + 1))
- {
- FontStyle fs;
- if (GetFontStyle(text[d_curPos + 1], out fs))
- {
- d_curPos += 2;
- if (currentConfig.fontStyle != fs)
- {
- save(false); // 保存内容
- currentConfig.fontStyle = fs;
- }
- }
- else
- {
- ++d_curPos;
- }
- }
- else
- {
- ++d_curPos;
- }
- }
- void ParserStrickout(string text)
- {
- save(false); // 保存内容
- currentConfig.isStrickout = !currentConfig.isStrickout; // 下划线
- d_curPos++;
- }
- void ParserFontColorS(string text)
- {
- --d_curPos;
- Color c = Tools.ParserColorName(text, ref d_curPos, currentConfig.fontColor);
- if (c != currentConfig.fontColor)
- {
- save(false);
- currentConfig.fontColor = c;
- }
- }
- void ParserFontColor(string text)
- {
- Color c = Tools.ParserColorName(text, ref d_curPos, currentConfig.fontColor);
- if (c != currentConfig.fontColor)
- {
- save(false);
- currentConfig.fontColor = c;
- }
- }
- void ParserUnderLine(string text)
- {
- save(false); // 保存内容
- currentConfig.isUnderline = !currentConfig.isUnderline; // 下划线
- d_curPos++;
- }
- void ParserNewLine(string text)
- {
- save(true);
- d_curPos++;
- }
- void ParserOutputChar(string text)
- {
- d_text.Append('#');
- d_curPos++;
- }
- void ParserHyperlink(string text)
- {
- // 结束的标识
- int endPos = text.IndexOf("#h", d_curPos + 1);
- if (endPos == -1)
- {
- // 查找失败,说明是错误的超链接
- d_curPos++;
- }
- else
- {
- // 保存之前的文本
- save(false);
- // 保存超链接
- d_text.Remove(0, d_text.Length);
- d_text.Append(text, d_curPos + 1, endPos - d_curPos - 1);
- saveHy();
- d_curPos = endPos + 2;
- }
- }
- void ParserFontSize(string text)
- {
- float size = 1f;
- if (!ParserFloat(ref d_curPos, text, ref size, 2))
- return;
- save(false);
- currentConfig.fontSize = (int)size;
- }
- void ParserXYZ(string text)
- {
- int pos = d_curPos;
- float offset = 0f;
- if (!ParserFloat(ref d_curPos, text, ref offset))
- return;
- if (offset == 0f)
- return;
- if (text[pos] == 'x')
- {
- save(false);
- saveX(offset);
- }
- else if (text[pos] == 'y')
- {
- if (d_text.Length != 0)
- save(true);
- saveY(offset);
- }
- else if (text[pos] == 'z')
- {
- if (d_text.Length != 0)
- save(false);
- saveZ(offset);
- }
- }
- void ParserFont(string text)
- {
- d_curPos++;
- Font font = Tools.ParserFontName(text, ref d_curPos);
- if (font != null)
- {
- if (currentConfig.font != font)
- {
- save(false);
- currentConfig.font = font;
- }
- }
- else
- {
- d_curPos--;
- }
- }
- }
- }
|