-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathGUIContent_Extend.cs
More file actions
180 lines (162 loc) · 5.03 KB
/
GUIContent_Extend.cs
File metadata and controls
180 lines (162 loc) · 5.03 KB
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
#region 注 释
/***
*
* Title:
*
* Description:
*
* Date:
* Version:
* Writer: 半只龙虾人
* Github: https://github.com/HalfLobsterMan
* Blog: https://www.crosshair.top/
*
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace CZToolKit.Core.Editors
{
public static class CSVLoader
{
private const char lineSperator = '\n';
private const char surround = '"';
private static string fieldSperator = "\",\"";
private static Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
public static string SerializeTableLine(string[] _fields)
{
for (int f = 0; f < _fields.Length; f++)
{
if (string.IsNullOrEmpty(_fields[f]))
_fields[f] = "";
else
_fields[f] = _fields[f].Replace("\"", "\"\"");
}
return string.Concat("\"", string.Join(fieldSperator, _fields), "\"");
}
public static string SerializeTable(string[][] _dataTable)
{
StringBuilder sb = new StringBuilder();
for (int lineIndex = 0; lineIndex < _dataTable.Length; lineIndex++)
{
sb.AppendLine(SerializeTableLine(_dataTable[lineIndex]));
}
return sb.ToString();
}
public static string[] DeserializeTableLine(string line)
{
string[] fields = CSVParser.Split(line);
for (int f = 0; f < fields.Length; f++)
{
if (fields[f].Contains(","))
{
fields[f] = fields[f].Substring(1);
fields[f] = fields[f].Remove(fields[f].LastIndexOf("\""));
}
fields[f] = fields[f].Replace("\"\"", "\"");
}
return fields;
}
public static void DeserializeTable(string text, Action<string[]> eachLineCallback)
{
string[] lines = text.Split(lineSperator);
string[][] dataTable = new string[lines.Length][];
bool callback = eachLineCallback != null;
for (int i = 0; i < lines.Length; i++)
{
if (string.IsNullOrEmpty(lines[i])) continue;
string[] fields = DeserializeTableLine(lines[i]);
eachLineCallback(fields);
dataTable[i] = fields;
}
}
}
public class Localization
{
int language;
Dictionary<string, string[]> texts = new Dictionary<string, string[]>();
public event Action onLanguageChanged;
public string[] Languages
{
get { return texts["Key"]; }
}
public int Language
{
get { return language; }
set
{
if (language == value) return;
language = value;
onLanguageChanged?.Invoke();
}
}
public Localization(string _text)
{
CSVLoader.DeserializeTable(_text, _ =>
{
texts[_[0]] = _.Skip(1).ToArray();
});
}
public string GetText(string _key)
{
if (texts.TryGetValue(_key, out string[] _texts))
{
if (_texts.Length > language)
return _texts[language];
}
return _key;
}
Dictionary<string, GUIContent_Extend> contents = new Dictionary<string, GUIContent_Extend>();
public GUIContent GetGUIContent(string _key)
{
if (contents.TryGetValue(_key, out GUIContent_Extend content))
return content;
return contents[_key] = content = new GUIContent_Extend(_key, this);
}
}
public class GUIContent_Extend : GUIContent
{
string key;
Localization owner;
public string Key
{
get { return key; }
set
{
if (key == value) return;
key = value;
text = owner.GetText(key);
}
}
public GUIContent_Extend(Localization _owner)
{
owner = _owner;
owner.onLanguageChanged += Refresh;
}
public GUIContent_Extend(string _key, Localization _owner) : base(_owner.GetText(_key))
{
key = _key;
owner = _owner;
owner.onLanguageChanged += Refresh;
}
public GUIContent_Extend(Texture image, Localization _owner) : base(image)
{
owner = _owner;
owner.onLanguageChanged += Refresh;
}
public GUIContent_Extend(string _key, Texture image, Localization _owner) : base(_owner.GetText(_key), image)
{
key = _key;
owner = _owner;
owner.onLanguageChanged += Refresh;
}
void Refresh()
{
text = owner.GetText(key);
}
}
}