Skip to content

Commit 740c9ed

Browse files
committed
Add namespace
1 parent f5aa4d2 commit 740c9ed

4 files changed

Lines changed: 235 additions & 227 deletions

File tree

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
using UnityEngine;
22

3-
public class RegexExample : MonoBehaviour
3+
namespace Hypertext
44
{
5-
[SerializeField]
6-
RegexHypertext _text;
5+
public class RegexExample : MonoBehaviour
6+
{
7+
[SerializeField]
8+
RegexHypertext _text;
79

8-
const string RegexURL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
9-
const string RegexHashtag = "[##][A-Za-zA-Za-z一-鿆0-90-9ぁ-ヶヲ-゚ー]+";
10+
const string RegexURL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
11+
const string RegexHashtag = "[##][A-Za-zA-Za-z一-鿆0-90-9ぁ-ヶヲ-゚ー]+";
1012

11-
void Start()
12-
{
13-
_text.SetClickableByRegex(RegexURL, Color.cyan, url => Debug.Log(url));
14-
_text.SetClickableByRegex(RegexHashtag, Color.green, hashtag => Debug.Log(hashtag));
13+
void Start()
14+
{
15+
_text.SetClickableByRegex(RegexURL, Color.cyan, url => Debug.Log(url));
16+
_text.SetClickableByRegex(RegexHashtag, Color.green, hashtag => Debug.Log(hashtag));
17+
}
1518
}
1619
}

Assets/Hypertext/Examples/RegexHypertext.cs

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,70 @@
33
using System.Text.RegularExpressions;
44
using UnityEngine;
55

6-
public class RegexHypertext : HypertextBase
6+
namespace Hypertext
77
{
8-
readonly Dictionary<string, Entry> _entryTable = new Dictionary<string, Entry>();
9-
10-
struct Entry
8+
public class RegexHypertext : HypertextBase
119
{
12-
public string RegexPattern;
13-
public Color Color;
14-
public Action<string> OnClick;
10+
readonly Dictionary<string, Entry> _entryTable = new Dictionary<string, Entry>();
1511

16-
public Entry(string regexPattern, Color color, Action<string> onClick)
12+
struct Entry
1713
{
18-
RegexPattern = regexPattern;
19-
Color = color;
20-
OnClick = onClick;
21-
}
22-
}
14+
public string RegexPattern;
15+
public Color Color;
16+
public Action<string> OnClick;
2317

24-
/// <summary>
25-
/// 正規表現にマッチした部分文字列にクリックイベントを登録します
26-
/// </summary>
27-
/// <param name="regexPattern">正規表現</param>
28-
/// <param name="onClick">クリック時のコールバック</param>
29-
public void SetClickableByRegex(string regexPattern, Action<string> onClick)
30-
{
31-
SetClickableByRegex(regexPattern, color, onClick);
32-
}
18+
public Entry(string regexPattern, Color color, Action<string> onClick)
19+
{
20+
RegexPattern = regexPattern;
21+
Color = color;
22+
OnClick = onClick;
23+
}
24+
}
3325

34-
/// <summary>
35-
/// 正規表現にマッチした部分文字列に色とクリックイベントを登録します
36-
/// </summary>
37-
/// <param name="regexPattern">正規表現</param>
38-
/// <param name="color">正規表現でマッチしたテキストの色</param>
39-
/// <param name="onClick">クリック時のコールバック</param>
40-
public void SetClickableByRegex(string regexPattern, Color color, Action<string> onClick)
41-
{
42-
if (string.IsNullOrEmpty(regexPattern) || onClick == null)
26+
/// <summary>
27+
/// 正規表現にマッチした部分文字列にクリックイベントを登録します
28+
/// </summary>
29+
/// <param name="regexPattern">正規表現</param>
30+
/// <param name="onClick">クリック時のコールバック</param>
31+
public void SetClickableByRegex(string regexPattern, Action<string> onClick)
4332
{
44-
return;
33+
SetClickableByRegex(regexPattern, color, onClick);
4534
}
4635

47-
_entryTable[regexPattern] = new Entry(regexPattern, color, onClick);
48-
}
36+
/// <summary>
37+
/// 正規表現にマッチした部分文字列に色とクリックイベントを登録します
38+
/// </summary>
39+
/// <param name="regexPattern">正規表現</param>
40+
/// <param name="color">正規表現でマッチしたテキストの色</param>
41+
/// <param name="onClick">クリック時のコールバック</param>
42+
public void SetClickableByRegex(string regexPattern, Color color, Action<string> onClick)
43+
{
44+
if (string.IsNullOrEmpty(regexPattern) || onClick == null)
45+
{
46+
return;
47+
}
4948

50-
public override void RemoveClickable()
51-
{
52-
base.RemoveClickable();
53-
_entryTable.Clear();
54-
}
49+
_entryTable[regexPattern] = new Entry(regexPattern, color, onClick);
50+
}
5551

56-
/// <summary>
57-
/// テキストの変更などでクリックする文字位置の再計算が必要なときに呼び出されます
58-
/// 親の RegisterClickable メソッドを使ってクリック対象文字の情報を登録してください
59-
/// </summary>
60-
protected override void RegisterClickable()
61-
{
62-
foreach (var entry in _entryTable.Values)
52+
public override void RemoveClickable()
53+
{
54+
base.RemoveClickable();
55+
_entryTable.Clear();
56+
}
57+
58+
/// <summary>
59+
/// テキストの変更などでクリックする文字位置の再計算が必要なときに呼び出されます
60+
/// 親の RegisterClickable メソッドを使ってクリック対象文字の情報を登録してください
61+
/// </summary>
62+
protected override void RegisterClickable()
6363
{
64-
foreach (Match match in Regex.Matches(text, entry.RegexPattern))
64+
foreach (var entry in _entryTable.Values)
6565
{
66-
RegisterClickable(match.Index, match.Value.Length, entry.Color, entry.OnClick);
66+
foreach (Match match in Regex.Matches(text, entry.RegexPattern))
67+
{
68+
RegisterClickable(match.Index, match.Value.Length, entry.Color, entry.OnClick);
69+
}
6770
}
6871
}
6972
}

0 commit comments

Comments
 (0)