| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- using System.Text;
- using UnityEngine;
- using System.Collections.Generic;
- // 文本解析
- namespace WXB
- {
- public enum Anchor
- {
- UpperLeft = 0,
- UpperCenter = 1,
- UpperRight = 2,
- MiddleLeft = 3,
- MiddleCenter = 4,
- MiddleRight = 5,
- LowerLeft = 6,
- LowerCenter = 7,
- LowerRight = 8,
- Null,
- }
- public partial class TextParser
- {
- public T CreateNode<T>() where T : NodeBase, new()
- {
- T t = new T();
- t.Reset(mOwner, currentConfig.anchor);
- return t;
- }
- static bool Get(char c, out Anchor a)
- {
- switch (c)
- {
- case '1': a = Anchor.MiddleLeft; return true;
- case '2': a = Anchor.MiddleCenter; return true;
- case '3': a = Anchor.MiddleRight; return true;
- }
- a = Anchor.MiddleCenter;
- return false;
- }
- static bool Get(char c, out LineAlignment a)
- {
- switch (c)
- {
- case '1': a = LineAlignment.Top; return true;
- case '2': a = LineAlignment.Center; return true;
- case '3': a = LineAlignment.Bottom; return true;
- }
- a = LineAlignment.Default;
- return false;
- }
- public TextParser()
- {
- clear();
- Reg();
- RegTag();
- }
- Owner mOwner;
- static bool ParserInt(ref int d_curPos, string text, ref int value, int num = 3)
- {
- using (PD<StringBuilder> psb = Pool.GetSB())
- {
- StringBuilder sb = psb.value;
- d_curPos++;
- while (text.Length > d_curPos && ((text[d_curPos] >= '0' && text[d_curPos] <= '9')))
- {
- sb.Append(text[d_curPos]);
- d_curPos++;
- if (sb.Length >= num)
- break;
- }
- value = Tools.stringToInt(sb.ToString(), -1);
- if (sb.Length == 0)
- {
- d_curPos--;
- return false;
- }
- return true;
- }
- }
- static bool ParserFloat(ref int d_curPos, string text, ref float value, int num = 3)
- {
- using (PD<StringBuilder> psb = Pool.GetSB())
- {
- var sb = psb.value;
- d_curPos++;
- bool bInPoint = false;
- while (text.Length > d_curPos && ((text[d_curPos] >= '0' && text[d_curPos] <= '9') || (text[d_curPos] == '.')))
- {
- if (text[d_curPos] == '.')
- bInPoint = true;
- sb.Append(text[d_curPos]);
- d_curPos++;
- int size = (bInPoint == true ? (num + 1) : num);
- if (sb.Length >= size)
- break;
- }
- value = Tools.stringToFloat(sb.ToString(), 0);
- if (sb.Length == 0)
- {
- d_curPos--;
- return false;
- }
- return true;
- }
- }
- public struct Config
- {
- public Anchor anchor;
- public Font font;
- public FontStyle fontStyle;
- public int fontSize;
- public Color fontColor;
- public bool isUnderline;
- public bool isStrickout;
- public bool isBlink;
- public bool isDyncUnderline; // 动态下划线
- public bool isDyncStrickout; // 动态删除线
- public int dyncSpeed;
- public bool isOffset;
- public Rect offsetRect;
- public EffectType effectType;
- public Color effectColor;
- public Vector2 effectDistance;
- public LineAlignment lineAlignment;
- public int nextLineX; // 下一行的起始偏移量
- public void Clear()
- {
- anchor = Anchor.Null;
- font = null;
- fontStyle = FontStyle.Normal;
- fontSize = 0;
- fontColor = Color.white;
- isUnderline = false;
- isStrickout = false;
- isBlink = false;
- isDyncUnderline = false;
- isDyncStrickout = false;
- dyncSpeed = 0;
- isOffset = false;
- offsetRect.Set(0, 0, 0, 0);
- effectType = EffectType.Null;
- effectColor = Color.black;
- effectDistance = Vector2.zero;
- lineAlignment = LineAlignment.Default;
- nextLineX = 0;
- }
- public void Set(Config c)
- {
- anchor = c.anchor;
- font = c.font;
- fontStyle = c.fontStyle;
- fontSize = c.fontSize;
- fontColor = c.fontColor;
- isUnderline = c.isUnderline;
- isStrickout = c.isStrickout;
- isBlink = c.isBlink;
- dyncSpeed = c.dyncSpeed;
- isOffset = c.isOffset;
- offsetRect = c.offsetRect;
- effectType = c.effectType;
- effectColor = c.effectColor;
- effectDistance = c.effectDistance;
- isDyncUnderline = c.isDyncUnderline;
- isDyncStrickout = c.isDyncStrickout;
- lineAlignment = c.lineAlignment;
- nextLineX = c.nextLineX;
- }
- public bool isSame(Config c)
- {
- return anchor == c.anchor &&
- font == c.font &&
- fontStyle == c.fontStyle &&
- isUnderline == c.isUnderline &&
- fontColor == c.fontColor &&
- isStrickout == c.isStrickout &&
- isBlink == c.isBlink &&
- fontSize == c.fontSize &&
- lineAlignment == c.lineAlignment &&
- isDyncUnderline == c.isDyncUnderline &&
- isDyncStrickout == c.isDyncStrickout &&
- nextLineX == c.nextLineX &&
- dyncSpeed == c.dyncSpeed &&
- (
- (effectType == EffectType.Null && c.effectType == EffectType.Null) ||
- (effectType == c.effectType && effectColor == c.effectColor && effectDistance == c.effectDistance)
- ) &&
- (
- (isOffset == false && c.isOffset == false) ||
- (isOffset == c.isOffset && offsetRect == c.offsetRect)
- );
- }
- }
- System.Func<TagAttributes, IExternalNode> getExternalNode = null;
- public void parser(Owner owner, string text, Config config, List<NodeBase> vList, System.Func<TagAttributes, IExternalNode> getExternalNode)
- {
- clear();
- mOwner = owner;
- this.getExternalNode = getExternalNode;
- d_nodeList = vList;
- startConfig.Set(config);
- currentConfig.Set(config);
- if (currentConfig.font == null)
- {
- Debug.LogError("TextParser pFont == null");
- return;
- }
- if (string.IsNullOrEmpty(text))
- {
- return;
- }
- int lenght = text.Length;
- while (lenght > d_curPos)
- {
- if (d_bBegin == false)
- {
- switch (text[d_curPos])
- {
- case '#':
- {
- // 未遇到功能字符,开始功能字符的解析
- d_bBegin = true;
- ++d_curPos;
- }
- break;
- /*case '<':
- {
- int endpos = text.IndexOf('>', d_curPos);
- if (endpos != -1)
- {
- string tag = null;
- string param = null;
- int tagend = text.IndexOfAny(new char[] { ' ', '=' }, d_curPos);
- if (tagend != -1 && tagend < endpos)
- {
- tag = text.Substring(d_curPos + 1, tagend - d_curPos);
- param = text.Substring(tagend + 1, endpos - tagend - 1);
- }
- else
- {
- tag = text.Substring(d_curPos + 1, endpos - d_curPos - 1);
- }
- if (d_text.Length != 0)
- save(false);
- TagParam(tag, param);
- d_curPos = endpos + 1;
- break;
- }
- else
- {
- d_text.Append(text[d_curPos]);
- }
- ++d_curPos;
- }
- break;*/
- case '[':
- {
- int endpos = text.IndexOf(']', d_curPos);
- if (endpos != -1)
- {
- string tag = null;
- string param = null;
- int tagend = text.IndexOf("[e", d_curPos);
- if (tagend != -1 && tagend < endpos)
- {
- tag = text.Substring(d_curPos + 1, tagend + 1 - d_curPos);
- param = text.Substring(tagend + 2, endpos - tagend - 2);
- if (!System.Text.RegularExpressions.Regex.IsMatch(param, "^[0-9]+$"))
- {
- d_text.Append(text[d_curPos]);
- ++d_curPos;
- break;
- }
- }
- else
- {
- tag = text.Substring(d_curPos + 1, endpos - d_curPos - 1);
- }
- if (d_text.Length != 0)
- save(false);
- TagParam(tag, param);
- d_curPos = endpos + 1;
- break;
- }
- else
- {
- d_text.Append(text[d_curPos]);
- }
- ++d_curPos;
- }
- break;
- /* case '\n':
- {
- // 这个是换行
- save(true);
- d_curPos++;
- }
- break;*/
- default:
- {
- d_text.Append(text[d_curPos]);
- ++d_curPos;
- }
- break;
- }
- }
- else
- {
- char c = text[d_curPos];
- OnFun fun = null;
- if (c < 128 && ((fun = OnFuns[c]) != null))
- {
- fun(text);
- }
- else
- {
- d_text.Append(text[d_curPos]);
- ++d_curPos;
- }
- d_bBegin = false;
- }
- }
- if (d_text.Length != 0)
- save(false);
- clear();
- }
- protected void save(bool isNewLine)
- {
- if (d_text.Length == 0)
- {
- if (isNewLine == true)
- {
- if (d_nodeList.Count != 0)
- {
- NodeBase node = d_nodeList.back();
- if (node.isNewLine() == false)
- {
- node.setNewLine(true);
- return;
- }
- }
- // 添加一个换行的结点
- LineNode nodeY = CreateNode<LineNode>();
- nodeY.SetConfig(currentConfig);
- nodeY.font = currentConfig.font;
- nodeY.fontSize = currentConfig.fontSize;
- nodeY.fs = currentConfig.fontStyle;
- nodeY.setNewLine(true);
- d_nodeList.Add(nodeY);
- return;
- }
- else
- {
- return;
- }
- }
- // 为文本
- TextNode textNode = CreateNode<TextNode>();
- {
- textNode.d_text = d_text.ToString();
- textNode.SetConfig(currentConfig);
- }
- textNode.setNewLine(isNewLine);
- d_nodeList.Add(textNode);
- d_text.Remove(0, d_text.Length);
- }
- protected void saveX(float value)
- {
- XSpaceNode node = CreateNode<XSpaceNode>();
- node.d_offset = value;
- d_nodeList.Add(node);
- }
- protected void saveY(float value)
- {
- if (d_nodeList.Count != 0 && d_nodeList.back().isNewLine() == false)
- {
- d_nodeList.back().setNewLine(true);
- }
- YSpaceNode node = CreateNode<YSpaceNode>();
- node.d_offset = value;
- node.setNewLine(true);
- d_nodeList.Add(node);
- }
- protected void saveZ(float value)
- {
- YSpaceNode node = CreateNode<YSpaceNode>();
- node.d_offset = value;
- node.setNewLine(false);
- d_nodeList.Add(node);
- }
- protected void saveHy()
- {
- if (d_text.Length == 0)
- return;
- string text = d_text.ToString();
- d_text.Remove(0, d_text.Length);
- HyperlinkNode node = CreateNode<HyperlinkNode>();
- string hytext = string.Empty;
- if (text[text.Length - 1] == '}')
- {
- int beginPos = text.IndexOf('{', 0);
- if (beginPos != -1)
- {
- hytext = text.Substring(beginPos, text.Length - beginPos);
- node.d_link = hytext.Replace("{", "").Replace("}", "");
- text = text.Remove(beginPos, text.Length - beginPos);
- }
- }
- node.d_text = "";
- node.SetConfig(currentConfig);
- ParseHyText(text, node);
- d_nodeList.Add(node);
- }
- protected void clear()
- {
- getExternalNode = null;
- startConfig.Clear();
- currentConfig.Clear();
- d_nodeList = null;
- d_curPos = 0;
- d_text.Clear();
- d_bBegin = false;
- mOwner = null;
- }
- protected int d_curPos = 0;
- protected Config startConfig;
- protected Config currentConfig;
- protected List<NodeBase> d_nodeList;
- protected StringBuilder d_text = new StringBuilder();
- protected bool d_bBegin;
- static Color GetColour(uint code)
- {
- switch (code)
- {
- case 'R': return Color.red;
- case 'G': return Color.green;
- case 'B': return Color.blue;
- case 'K': return Color.black;
- case 'Y': return Color.yellow;
- case 'W': return Color.white;
- }
- return Color.white;
- }
- }
- }
|