-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkImageTextPro.cs
324 lines (297 loc) · 11.2 KB
/
LinkImageTextPro.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Xml;
/// <summary>
/// 文本控件,支持超链接、图片
/// </summary>
namespace UI.UGUIExtension
{
public class LinkImageTextPro : Text, IPointerClickHandler
{
private string no_breaking_space = "\u00A0";
/// <summary>
/// 解析完最终的文本
/// </summary>
private string m_OutputText;
/// <summary>
/// 图片
/// </summary>
public Image m_Image;
/// <summary>
/// 超链接信息列表
/// </summary>
private readonly List<HrefInfo> m_HrefInfos = new List<HrefInfo>();
/// <summary>
/// 文本构造器
/// </summary>
protected static readonly StringBuilder s_TextBuilder = new StringBuilder();
/// <summary>
/// 正则取出所需要的属性
/// </summary>
private static readonly Regex s_ImageRegex =
new Regex("<image name=\"(.+?)\" jump_id=\"(\\d{1,4})\" jump_url=\"(\\S+)\">(</image>)", RegexOptions.Singleline);
/// <summary>
/// 超链接正则
/// </summary>
private static readonly Regex s_HrefRegex =
new Regex("<a href=\"([^>\n\\s]+?)\">(.*?)(</a>)", RegexOptions.Singleline);
private int link_text_type = 1;
private bool isHaveImage = false;
public override void SetVerticesDirty()
{
base.SetVerticesDirty();
UpdateImage();
}
public void SetLinkedText(String str)
{
isHaveImage = false;
text = str;
SetVerticesDirty();
}
public String GetLinkedText()
{
return text;
}
public int GetLinkedTextType()
{
return link_text_type;
}
protected void UpdateImage()
{
#if UNITY_EDITOR
if (UnityEditor.PrefabUtility.GetPrefabType(this) == UnityEditor.PrefabType.Prefab)
{
return;
}
#endif
m_OutputText = GetOutputText(text);
foreach (Match match in s_ImageRegex.Matches(m_OutputText))
{
isHaveImage = true;
if (m_Image != null)
{
var spriteName = match.Groups[1].Value;
var jump_id = int.Parse(match.Groups[2].Value);
var jump_url = match.Groups[3].Value;
if (m_Image.sprite == null || m_Image.sprite.name != spriteName)
{
m_Image.sprite = Framework.GameApplication.Get().Resources.Load(spriteName, typeof(Sprite)) as Sprite;
}
Button button = m_Image.GetComponent<Button>();
object[] parameters = new object[2];
parameters[0] = jump_id;
parameters[1] = jump_url;
if (button != null)
{
button.onClick.RemoveAllListeners();
button.onClick.AddListener(()=>
{
Framework.GameApplication.Get().Events.Raise("events.banner.ClickLinkImage", parameters);
});
}
}
}
if (isHaveImage)
{
if (m_OutputText.EndsWith("</image>"))
{
link_text_type = 2;
}
else
{
link_text_type = 3;
}
}
else
{
link_text_type = 1;
}
m_OutputText = s_ImageRegex.Replace(m_OutputText, "");
m_OutputText = m_OutputText.Replace(" ", no_breaking_space);
}
protected override void OnPopulateMesh(VertexHelper toFill)
{
var orignText = m_Text;
m_Text = m_OutputText;
base.OnPopulateMesh(toFill);
m_Text = orignText;
UIVertex vert = new UIVertex();
// 处理超链接包围框
foreach (var hrefInfo in m_HrefInfos)
{
hrefInfo.boxes.Clear();
if (hrefInfo.startIndex >= toFill.currentVertCount)
{
continue;
}
// 将超链接里面的文本顶点索引坐标加入到包围框
toFill.PopulateUIVertex(ref vert, hrefInfo.startIndex);
var pos = vert.position;
var bounds = new Bounds(pos, Vector3.zero);
for (int i = hrefInfo.startIndex, m = hrefInfo.endIndex; i < m; i++)
{
if (i >= toFill.currentVertCount)
{
break;
}
toFill.PopulateUIVertex(ref vert, i);
pos = vert.position;
if (pos.x < bounds.min.x) // 换行重新添加包围框
{
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
bounds = new Bounds(pos, Vector3.zero);
}
else
{
bounds.Encapsulate(pos); // 扩展包围框
}
}
hrefInfo.boxes.Add(new Rect(bounds.min, bounds.size));
}
}
/// <summary>
/// 获取超链接解析后的最后输出文本
/// </summary>
/// <returns></returns>
protected virtual string GetOutputText(string outputText)
{
outputText = ParseXML(outputText);
s_TextBuilder.Length = 0;
m_HrefInfos.Clear();
var indexText = 0;
foreach (Match match in s_HrefRegex.Matches(outputText))
{
s_TextBuilder.Append(outputText.Substring(indexText, match.Index - indexText));
s_TextBuilder.Append("<color=blue>"); // 超链接颜色
var group = match.Groups[1];
var hrefInfo = new HrefInfo
{
startIndex = s_TextBuilder.Length * 4, // 超链接里的文本起始顶点索引
endIndex = (s_TextBuilder.Length + match.Groups[2].Length - 1) * 4 + 3,
name = group.Value
};
m_HrefInfos.Add(hrefInfo);
s_TextBuilder.Append(match.Groups[2].Value);
s_TextBuilder.Append("</color>");
indexText = match.Index + match.Length;
}
s_TextBuilder.Append(outputText.Substring(indexText, outputText.Length - indexText));
return s_TextBuilder.ToString();
}
///将标准HTML转换为RichText
protected string ParseXML(string content)
{
content = "<root>" + content + "</root>";
StringBuilder builder = new StringBuilder();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(content);
XmlNode rootNode = xmlDocument.SelectSingleNode("root");
XmlNodeList p_nodeList = rootNode.SelectNodes("p");
foreach (XmlNode node in p_nodeList)
{
foreach (XmlNode child in node.ChildNodes)
{
if (child.NodeType == XmlNodeType.Text)
{
builder.Append(child.InnerText);
}
else if (child.NodeType == XmlNodeType.Element)
{
builder.Append(ParseElement(child));
}
}
builder.Append("\n");
}
return builder.ToString();
}
///迭代解析XmlElement
protected string ParseElement(XmlNode node)
{
StringBuilder builder = new StringBuilder();
string endStr = "";
switch (node.Name)
{
case "em":
builder.Append("<i>");
endStr = endStr + "</i>";
break;
case "strong":
builder.Append("<b>");
endStr = endStr + "</b>";
break;
case "span":
string attribute = ((XmlElement)node).GetAttribute("style");
string[] infos = attribute.Split(':');
if (infos[0] == "font-size")
{
builder.Append("<size=" + infos[1].Replace("px", ">"));
endStr = "</size>" + endStr;
}
else if (infos[0] == "color")
{
builder.Append("<color=" + infos[1] + ">");
endStr = "</color>" + endStr;
}
break;
case "a":
builder.Append(node.OuterXml);
break;
case "image":
builder.Append(node.OuterXml);
break;
default:
break;
}
foreach (XmlNode item in node.ChildNodes)
{
if (item.NodeType == XmlNodeType.Text && node.Name != "a")
{
builder.Append(item.InnerText);
}
else if (item.NodeType == XmlNodeType.Element)
{
builder.Append(ParseElement(item));
}
}
builder.Append(endStr);
return builder.ToString();
}
/// <summary>
/// 点击事件检测是否点击到超链接文本
/// </summary>
/// <param name="eventData"></param>
public void OnPointerClick(PointerEventData eventData)
{
Vector2 lp;
RectTransformUtility.ScreenPointToLocalPointInRectangle(
rectTransform, eventData.position, eventData.pressEventCamera, out lp);
foreach (var hrefInfo in m_HrefInfos)
{
var boxes = hrefInfo.boxes;
for (var i = 0; i < boxes.Count; ++i)
{
if (boxes[i].Contains(lp))
{
Framework.GameApplication.Get().Events.Raise("events.banner.ClickHref", hrefInfo.name);
return;
}
}
}
}
/// <summary>
/// 超链接信息类
/// </summary>
private class HrefInfo
{
public int startIndex;
public int endIndex;
public string name;
public readonly List<Rect> boxes = new List<Rect>();
}
}
}