using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
public class ImgAnalysis : TRichAnalysis<ImgAnalysis>
|
{
|
public static Regex Img_Regex = new Regex(@"<Img (.*?)/>", RegexOptions.Singleline);
|
public static Regex Unity_Img_Regex = new Regex(@"<quad (.*?) index=([0-9]+)/>", RegexOptions.Singleline);
|
|
private RichTextMgr.ImgInfo presentImgInfo = null;
|
|
public override string Analysis(string val, bool IsRich)
|
{
|
if (!Img_Regex.IsMatch(val))
|
{
|
return val;
|
}
|
m_StringBuilder.Length = 0;
|
int index = 0;
|
int imgCnt = 0;
|
foreach (Match match in Img_Regex.Matches(val))
|
{
|
RichTextMgr.ImgInfo imgInfo = new RichTextMgr.ImgInfo();
|
imgInfo.width = 30;
|
imgInfo.height = 30;
|
|
presentImgInfo = imgInfo;
|
m_StringBuilder.Append(val.Substring(index, match.Index - index));
|
AnalysisSplitEvent(match.Groups[1].Value);
|
|
if (IsRich)
|
{
|
presentImgInfo.index = imgCnt;
|
InverseToRichText();
|
if (RichTextMgr.Inst.GetImgList() != null) RichTextMgr.Inst.GetImgList().Add(presentImgInfo);
|
else imgInfo = null;
|
imgCnt++;
|
}
|
|
index = match.Index + match.Length;
|
}
|
m_StringBuilder.Append(val.Substring(index, val.Length - index));
|
presentImgInfo = null;
|
return m_StringBuilder.ToString();
|
}
|
|
public override string CalculateTextIndex(string val, int index)
|
{
|
if (!Unity_Img_Regex.IsMatch(val))
|
{
|
return val;
|
}
|
MatchCollection matchArray = Unity_Img_Regex.Matches(val);
|
if (index < matchArray.Count)
|
{
|
Match match = matchArray[index];
|
if (RichTextMgr.Inst.GetImgList() != null)
|
{
|
RichTextMgr.ImgInfo imgInfo = RichTextMgr.Inst.GetImgList()[index];
|
imgInfo.end = match.Index * 4 + 3;
|
}
|
}
|
return val;
|
}
|
|
private void AnalysisSplitEvent(string val)
|
{
|
string[] array = GetSplitEvent(val);
|
if (array.Length > 0)
|
{
|
foreach (var split_event in array)
|
{
|
AnalysisSplitData(split_event);
|
}
|
}
|
LoadSprite();
|
}
|
|
private void AnalysisSplitData(string val)
|
{
|
string[] array = GetSplitData(val);
|
if (array.Length > 0)
|
{
|
foreach (var split_data in array)
|
{
|
AnalysisSplitValue(split_data);
|
}
|
}
|
}
|
|
private void AnalysisSplitValue(string val)
|
{
|
string[] array = GetSplitValue(val);
|
if (array.Length == 2)
|
{
|
string split_value = array[0].ToLower();
|
switch (split_value)
|
{
|
case "img":
|
case "chat":
|
{
|
presentImgInfo.spriteName = array[1];
|
}
|
break;
|
case "face":
|
{
|
presentImgInfo.spriteName = array[1];
|
presentImgInfo.IsFace = true;
|
if (RichTextMgr.Inst.presentRichText != null)
|
{
|
presentImgInfo.height = RichTextMgr.Inst.presentRichText.FaceSize;
|
presentImgInfo.width = presentImgInfo.height;
|
}
|
}
|
break;
|
case "size":
|
{
|
presentImgInfo.height = float.Parse(array[1]);
|
presentImgInfo.width = float.Parse(array[1]);
|
}
|
break;
|
}
|
}
|
}
|
|
private void InverseToRichText()
|
{
|
m_StringBuilder.Append("<quad ");
|
m_StringBuilder.Append(string.Format("size={0} ", presentImgInfo.height));
|
float ratio = (float)Math.Round((float)presentImgInfo.width / presentImgInfo.height, 1);
|
m_StringBuilder.Append(string.Format("width={0} ", ratio));
|
m_StringBuilder.Append(string.Format("index={0}", presentImgInfo.index));
|
m_StringBuilder.Append("/>");
|
}
|
|
private void LoadSprite()
|
{
|
if (presentImgInfo.IsFace) return;
|
if (IconConfig.inited)
|
{
|
presentImgInfo.sprite = UILoader.LoadSprite(presentImgInfo.spriteName);
|
}
|
|
if (presentImgInfo.sprite != null)
|
{
|
RichText text = RichTextMgr.Inst.presentRichText;
|
if (text != null)
|
{
|
if (text.LockImgSize)
|
{
|
presentImgInfo.width = presentImgInfo.height = text.fontSize;
|
return;
|
}
|
else if (text.ModifyImgSiez)
|
{
|
presentImgInfo.width = text.ModifyImgWidth;
|
presentImgInfo.height = text.ModifyImgHeight;
|
return;
|
}
|
}
|
presentImgInfo.width = presentImgInfo.sprite.rect.width;
|
presentImgInfo.height = presentImgInfo.sprite.rect.height;
|
}
|
}
|
|
private const string FACE_REPLACE = @"#~([0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z])";
|
public static Regex FaceRegex = new Regex(FACE_REPLACE, RegexOptions.Singleline);
|
public static string ReplaceFace(string msg)
|
{
|
if (!FaceRegex.IsMatch(msg))
|
{
|
return msg;
|
}
|
else
|
{
|
m_StringBuilder.Length = 0;
|
int index = 0;
|
foreach (Match match in FaceRegex.Matches(msg))
|
{
|
string result = match.Groups[1].Value.Replace("0", "");
|
m_StringBuilder.Append(msg.Substring(index, match.Index - index));
|
if (UIFrameMgr.Inst.ContainsFace(result))
|
{
|
m_StringBuilder.Append(string.Format("<Img face={0}/>", result));
|
}
|
else
|
{
|
m_StringBuilder.Append(match.Groups[0].Value);
|
}
|
index = match.Index + match.Length;
|
}
|
m_StringBuilder.Append(msg.Substring(index, msg.Length - index));
|
return m_StringBuilder.ToString();
|
}
|
}
|
public static string ReplaceFace(string msg, out int cnt)
|
{
|
cnt = 0;
|
if (!FaceRegex.IsMatch(msg))
|
{
|
return msg;
|
}
|
else
|
{
|
m_StringBuilder.Length = 0;
|
int index = 0;
|
foreach (Match match in FaceRegex.Matches(msg))
|
{
|
cnt++;
|
m_StringBuilder.Append(msg.Substring(index, match.Index - index));
|
index = match.Index + match.Length;
|
}
|
m_StringBuilder.Append(msg.Substring(index, msg.Length - index));
|
return m_StringBuilder.ToString();
|
}
|
}
|
}
|