/* * uGUI-Hypertext (https://github.com/setchi/uGUI-Hypertext) * Copyright (c) 2019 setchi * Licensed under MIT (https://github.com/setchi/uGUI-Hypertext/blob/master/LICENSE) */ using System; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; namespace Hypertext { public class RegexHypertext : HypertextBase { readonly List entries = new List(); struct Entry { public readonly string RegexPattern; public readonly Color Color; public readonly Action Callback; public Entry(string regexPattern, Color color, Action callback) { RegexPattern = regexPattern; Color = color; Callback = callback; } } /// /// 正規表現にマッチした部分文字列にクリックイベントリスナを登録します /// /// 正規表現 /// クリック時のコールバック public void OnClick(string regexPattern, Action onClick) { OnClick(regexPattern, color, onClick); } /// /// 正規表現にマッチした部分文字列に色とクリックイベントリスナを登録します /// /// 正規表現 /// テキストカラー /// クリック時のコールバック public void OnClick(string regexPattern, Color color, Action onClick) { if (string.IsNullOrEmpty(regexPattern) || onClick == null) { return; } entries.Add(new Entry(regexPattern, color, onClick)); } public override void RemoveListeners() { base.RemoveListeners(); entries.Clear(); } /// /// イベントリスナを追加します /// テキストの変更などでイベントの再登録が必要なときにも呼び出されます /// を使ってクリックイベントリスナを登録してください /// protected override void AddListeners() { foreach (var entry in entries) { foreach (Match match in Regex.Matches(text, entry.RegexPattern)) { OnClick(match.Index, match.Value.Length, entry.Color, entry.Callback); } } } } }