Tanoda
TextEditorHelper.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
5using UnityEngine.UI;
6
7public class TextEditorHelper : MonoBehaviour
8{
9 public TMPro.TMP_InputField inField;
13
14 void Start()
15 {
16
17 }
18
19 public void BUTTON_Bold()
20 {
21 var x = inField.selectionStringAnchorPosition;
22 var y = inField.stringPosition;
23 var from = x < y ? x : y;
24 var to = x > y ? x : y;
25
26 if (from == to)
27 return;
28
29 var text = inField.text;
30 var origtext = inField.text;
31 try
32 {
33 text = origtext.Remove(to).Remove(0, from);
34 }
35 catch (ArgumentOutOfRangeException)
36 {
37 text = origtext.Remove(0, from);
38 }
39
40 var openingb = text.Contains("<b>");
41 var closingb = text.Contains("</b>");
42 if (!openingb)
43 {
44 try
45 {
46 openingb = origtext.Remove(to).Remove(0, from - 3).Contains("<b>");
47 if (openingb)
48 {
49 from -= 3;
50 }
51 }
52 catch (Exception)
53 {
54 // ignored
55 }
56 }
57 if (!closingb)
58 {
59 try
60 {
61 closingb = origtext.Remove(to + 4).Remove(0, from).Contains("</b>");
62 if (closingb)
63 {
64 to += 4;
65 }
66 }
67 catch (Exception)
68 {
69 // ignored
70 }
71 }
72 try
73 {
74 text = origtext.Remove(to).Remove(0, from);
75 }
76 catch (ArgumentOutOfRangeException)
77 {
78 text = origtext.Remove(0, from);
79 }
80
81 if (openingb && closingb && text.IndexOf("<b>") < text.IndexOf("</b>"))
82 {
83 text = text.Replace("<b>", "").Replace("</b>", "");
84 inField.text = origtext.Remove(from) + text + origtext.Remove(0, to);
85 }
86
87 if (openingb && !closingb)
88 {
89 inField.text = origtext.Remove(from) + text.Replace("<b>", "") + origtext.Remove(0, to).Remove(origtext.Remove(0, to).IndexOf("</b>"), 4);
90 }
91
92 if (closingb && !openingb)
93 {
94 inField.text = origtext.Remove(from).Remove(origtext.Remove(to).LastIndexOf("<b>"), 3) + text.Replace("</b>", "") + origtext.Remove(0, to);
95 }
96
97 if (!closingb && !openingb)
98 {
99 inField.text = inField.text.Insert(to, "</b>").Insert(from, "<b>");
100 }
101
102 inField.selectionStringFocusPosition = 0;
103 inField.selectionAnchorPosition = 0;
104 inField.selectionFocusPosition = 0;
105 inField.selectionStringAnchorPosition = 0;
106 }
107
108 public void BUTTON_Italic()
109 {
110 var x = inField.selectionStringAnchorPosition;
111 var y = inField.stringPosition;
112 var from = x < y ? x : y;
113 var to = x > y ? x : y;
114
115 if (from == to)
116 return;
117
118 var text = inField.text;
119 var origtext = inField.text;
120 try
121 {
122 text = origtext.Remove(to).Remove(0, from);
123 }
124 catch (ArgumentOutOfRangeException)
125 {
126 text = origtext.Remove(0, from);
127 }
128
129 var openingb = text.Contains("<i>");
130 var closingb = text.Contains("</i>");
131 if (!openingb)
132 {
133 try
134 {
135 openingb = origtext.Remove(to).Remove(0, from - 3).Contains("<i>");
136 if (openingb)
137 {
138 from -= 3;
139 }
140 }
141 catch (Exception)
142 {
143 // ignored
144 }
145 }
146 if (!closingb)
147 {
148 try
149 {
150 closingb = origtext.Remove(to + 4).Remove(0, from).Contains("</i>");
151 if (closingb)
152 {
153 to += 4;
154 }
155 }
156 catch (Exception)
157 {
158 // ignored
159 }
160 }
161 try
162 {
163 text = origtext.Remove(to).Remove(0, from);
164 }
165 catch (ArgumentOutOfRangeException)
166 {
167 text = origtext.Remove(0, from);
168 }
169
170 if (openingb && closingb && text.IndexOf("<i>") < text.IndexOf("</i>"))
171 {
172 text = text.Replace("<i>", "").Replace("</i>", "");
173 inField.text = origtext.Remove(from) + text + origtext.Remove(0, to);
174 }
175
176 if (openingb && !closingb)
177 {
178 inField.text = origtext.Remove(from) + text.Replace("<i>", "") + origtext.Remove(0, to).Remove(origtext.Remove(0, to).IndexOf("</i>"), 4);
179 }
180
181 if (closingb && !openingb)
182 {
183 inField.text = origtext.Remove(from).Remove(origtext.Remove(to).LastIndexOf("<i>"), 3) + text.Replace("</i>", "") + origtext.Remove(0, to);
184 }
185
186 if (!closingb && !openingb)
187 {
188 inField.text = inField.text.Insert(to, "</i>").Insert(from, "<i>");
189 }
190
191 inField.selectionStringFocusPosition = 0;
192 inField.selectionAnchorPosition = 0;
193 inField.selectionFocusPosition = 0;
194 inField.selectionStringAnchorPosition = 0;
195 }
196
197 public void BUTTON_Underline()
198 {
199 var x = inField.selectionStringAnchorPosition;
200 var y = inField.stringPosition;
201 var from = x < y ? x : y;
202 var to = x > y ? x : y;
203
204 if (from == to)
205 return;
206
207 var text = inField.text;
208 var origtext = inField.text;
209 try
210 {
211 text = origtext.Remove(to).Remove(0, from);
212 }
213 catch (ArgumentOutOfRangeException)
214 {
215 text = origtext.Remove(0, from);
216 }
217
218 var openingb = text.Contains("<u>");
219 var closingb = text.Contains("</u>");
220 if (!openingb)
221 {
222 try
223 {
224 openingb = origtext.Remove(to).Remove(0, from - 3).Contains("<u>");
225 if (openingb)
226 {
227 from -= 3;
228 }
229 }
230 catch (Exception)
231 {
232 // ignored
233 }
234 }
235 if (!closingb)
236 {
237 try
238 {
239 closingb = origtext.Remove(to + 4).Remove(0, from).Contains("</u>");
240 if (closingb)
241 {
242 to += 4;
243 }
244 }
245 catch (Exception)
246 {
247 // ignored
248 }
249 }
250 try
251 {
252 text = origtext.Remove(to).Remove(0, from);
253 }
254 catch (ArgumentOutOfRangeException)
255 {
256 text = origtext.Remove(0, from);
257 }
258
259 if (openingb && closingb && text.IndexOf("<u>") < text.IndexOf("</u>"))
260 {
261 text = text.Replace("<u>", "").Replace("</u>", "");
262 inField.text = origtext.Remove(from) + text + origtext.Remove(0, to);
263 }
264
265 if (openingb && !closingb)
266 {
267 inField.text = origtext.Remove(from) + text.Replace("<u>", "") + origtext.Remove(0, to).Remove(origtext.Remove(0, to).IndexOf("</u>"), 4);
268 }
269
270 if (closingb && !openingb)
271 {
272 inField.text = origtext.Remove(from).Remove(origtext.Remove(to).LastIndexOf("<u>"), 3) + text.Replace("</u>", "") + origtext.Remove(0, to);
273 }
274
275 if (!closingb && !openingb)
276 {
277 inField.text = inField.text.Insert(to, "</u>").Insert(from, "<u>");
278 }
279
280 inField.selectionStringFocusPosition = 0;
281 inField.selectionAnchorPosition = 0;
282 inField.selectionFocusPosition = 0;
283 inField.selectionStringAnchorPosition = 0;
284 }
285
286 public void SetTextColor()
287 {
288 inField.textComponent.color = colorPicker.CurrentColor;
291 }
292
293 public void SetBGColor()
294 {
295 var bgImage = inField.gameObject.GetComponent<Image>();
296 bgImage.color = colorPickerBG.CurrentColor;
298 }
299
300}
System.Drawing.Image Image
Definition: TestScript.cs:37
Color CurrentColor
Definition: ColorPicker.cs:23
ColorPicker colorPicker
ColorPicker colorPickerBG
TMPro.TMP_InputField inField