-
Notifications
You must be signed in to change notification settings - Fork 1
/
Script.cs
329 lines (263 loc) · 13.7 KB
/
Script.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
325
326
327
328
329
using System.IO;
using FarsiLibrary.Win;
using i00SpellCheck;
using FastColoredTextBoxPlugin;
using FastColoredTextBoxNS;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;
using static System.Text.RegularExpressions.Regex;
using System;
using System.Collections.Generic;
using GlobalSearch;
namespace Personality_Creator.PersonaFiles
{
public class Script : PersonaFile
{
#region capsuled fields
private FastColoredTextBox editor;
public AutocompleteMenu autoMenu;
Style VocabStyle = new TextStyle(DataManager.settings.VocabStyle.ForeBrush, DataManager.settings.VocabStyle.BackgroundBrush, DataManager.settings.VocabStyle.FontStyle);
Style CommandStyle = new TextStyle(DataManager.settings.CommandStyle.ForeBrush, DataManager.settings.CommandStyle.BackgroundBrush, DataManager.settings.CommandStyle.FontStyle);
Style ResponseStyle = new TextStyle(DataManager.settings.ResponseStyle.ForeBrush, DataManager.settings.ResponseStyle.BackgroundBrush, DataManager.settings.ResponseStyle.FontStyle);
Style ParanthesisStyle = new TextStyle(DataManager.settings.ParanthesisStyle.ForeBrush, DataManager.settings.ParanthesisStyle.BackgroundBrush, DataManager.settings.ParanthesisStyle.FontStyle);
Style GotoStyle = new TextStyle(DataManager.settings.GotoStyle.ForeBrush, DataManager.settings.GotoStyle.BackgroundBrush, DataManager.settings.GotoStyle.FontStyle);
Style CheckFlagStyle = new TextStyle(DataManager.settings.CheckFlagStyle.ForeBrush, DataManager.settings.CheckFlagStyle.BackgroundBrush, DataManager.settings.CheckFlagStyle.FontStyle);
Style FragmentStyle = new TextStyle(DataManager.settings.FragmentStyle.ForeBrush, DataManager.settings.FragmentStyle.BackgroundBrush, DataManager.settings.FragmentStyle.FontStyle);
//Style CommentStyle = new TextStyle(Brushes.DarkGreen, Brushes.White, FontStyle.Regular);
private List<Range> highlightedText = new List<Range>();
#endregion
#region properties
#endregion
#region events
public override event FileReferencClickedEventHandler FileReferenceClicked;
#endregion
public Script(FileInfo file) : base (file)
{ }
public Script(string file) : base(file)
{ }
public override void Save()
{
TabStripUtils.unflagTabAsModified(this.tab);
System.IO.File.WriteAllText(this.File.FullName, this.editor.Text);
}
public string Read()
{
if(System.IO.File.Exists(this.File.FullName))
return System.IO.File.ReadAllText(this.File.FullName);
return "";
}
public override void ReApplyStyles()
{
this.VocabStyle = new TextStyle(DataManager.settings.VocabStyle.ForeBrush, DataManager.settings.VocabStyle.BackgroundBrush, DataManager.settings.VocabStyle.FontStyle);
this.CommandStyle = new TextStyle(DataManager.settings.CommandStyle.ForeBrush, DataManager.settings.CommandStyle.BackgroundBrush, DataManager.settings.CommandStyle.FontStyle);
this.ResponseStyle = new TextStyle(DataManager.settings.ResponseStyle.ForeBrush, DataManager.settings.ResponseStyle.BackgroundBrush, DataManager.settings.ResponseStyle.FontStyle);
this.ParanthesisStyle = new TextStyle(DataManager.settings.ParanthesisStyle.ForeBrush, DataManager.settings.ParanthesisStyle.BackgroundBrush, DataManager.settings.ParanthesisStyle.FontStyle);
this.GotoStyle = new TextStyle(DataManager.settings.GotoStyle.ForeBrush, DataManager.settings.GotoStyle.BackgroundBrush, DataManager.settings.GotoStyle.FontStyle);
this.CheckFlagStyle = new TextStyle(DataManager.settings.CheckFlagStyle.ForeBrush, DataManager.settings.CheckFlagStyle.BackgroundBrush, DataManager.settings.CheckFlagStyle.FontStyle);
this.FragmentStyle = new TextStyle(DataManager.settings.FragmentStyle.ForeBrush, DataManager.settings.FragmentStyle.BackgroundBrush, DataManager.settings.FragmentStyle.FontStyle);
this.Redraw();
}
public override void Redraw()
{
bool unsavedChangesBefore = false;
if (TabStripUtils.isTagFlaggedAsModified(this.tab))
{
unsavedChangesBefore = true;
}
this.editor.ClearStylesBuffer();
this.editor.OnTextChanged(); //redraws editor styles
if (!unsavedChangesBefore)
{
TabStripUtils.unflagTabAsModified(this.tab);
}
}
public override FATabStripItem CreateTab()
{
this.editor = new FastColoredTextBox();
this.tab = new FATabStripItem();
this.tab.Title = this.File.Name;
this.tab.Tag = this;
//load the spellchecker extension for FastColoredTextBox
SpellCheckFastColoredTextBox spellCheckerTextBox = new SpellCheckFastColoredTextBox();
ControlExtensions.LoadSingleControlExtension(editor, spellCheckerTextBox);
spellCheckerTextBox.SpellCheckMatch = @"^([\w']+)| ([\w']+)|>([\w']+)"; // Only process words starting a line, following a space or a tag
this.editor.Parent = this.tab;
this.editor.Dock = DockStyle.Fill;
this.editor.Text = this.Read();
this.editor.Focus();
this.editor.TextChanged += this.Editor_TextChanged;
this.editor.KeyDown += this.Editor_KeyDown;
this.editor.MouseMove += this.Editor_MouseMove;
this.editor.MouseUp += this.Editor_MouseUp;
this.editor.MouseDown += this.Editor_MouseDown;
autoMenu = new AutocompleteMenu(this.editor); //Autocompletion items will be set upon tab selection to be able to include all vocab files
autoMenu.MinFragmentLength = 2;
autoMenu.TopLevel = true;
autoMenu.Items.MaximumSize = new System.Drawing.Size(200, 300);
autoMenu.Items.Width = 200;
return this.tab;
}
private void Editor_TextChanged(object sender, FastColoredTextBoxNS.TextChangedEventArgs e)
{
this.OnContentChanged(EventArgs.Empty);
//colorization
e.ChangedRange.ClearStyle(VocabStyle);
e.ChangedRange.SetStyle(VocabStyle, @"(?i)(?<=\s|^)#[\w\-.]+(?=\s|$)", RegexOptions.None);
e.ChangedRange.ClearStyle(CommandStyle);
e.ChangedRange.SetStyle(CommandStyle, @"(?<![A-z_0-9öäüáéíóú+])\@[A-z_0-9öäüáéíóú+]+", RegexOptions.None);
e.ChangedRange.ClearStyle(ResponseStyle);
e.ChangedRange.SetStyle(ResponseStyle, @"\[.+\]", RegexOptions.None);
e.ChangedRange.ClearStyle(ParanthesisStyle);
e.ChangedRange.SetStyle(ParanthesisStyle, @"(?i)(?<=[A-z_0-9öäüáéíóú+\n])\([A-z_0-9öäüáéíóú,+\s]+\)", RegexOptions.None);
e.ChangedRange.ClearStyle(GotoStyle);
e.ChangedRange.SetStyle(GotoStyle, @"(?i)(\@goto|then|chance[0-9]{2})\([A-z_0-9öäüáéíóú+\s]+\)", RegexOptions.None);
e.ChangedRange.ClearStyle(CheckFlagStyle);
e.ChangedRange.SetStyle(CheckFlagStyle, @"(?i)(\@checkflag)\([A-z_0-9öäüáéíóú,+\s]+\)", RegexOptions.None);
e.ChangedRange.ClearStyle(FragmentStyle);
e.ChangedRange.SetStyle(FragmentStyle, @"(?i)\$\$frag\([A-z_0-9öäüáéíóú+\s]+\)", RegexOptions.None);
//e.ChangedRange.ClearStyle(CommentStyle);
//e.ChangedRange.SetStyle(CommentStyle, @"(?i)(?<!.)-.*", RegexOptions.None);
////code folding
//e.ChangedRange.SetFoldingMarkers(@"-region", @"-endregion");
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
Cursor.Position = Cursor.Position; //force cursor redraw
if (e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control)
{
this.Save();
}
}
private void Editor_MouseMove(object sender, MouseEventArgs e)
{
Place p = this.editor.PointToPlace(e.Location);
if (FastColoredEditorUtils.cursorIsOnTextOfStyle(p, GotoStyle, this.editor) && Control.ModifierKeys == Keys.Control
|| FastColoredEditorUtils.cursorIsOnTextOfStyle(p, CheckFlagStyle, this.editor) && Control.ModifierKeys == Keys.Control
|| FastColoredEditorUtils.cursorIsOnTextOfStyle(p, VocabStyle, this.editor) && Control.ModifierKeys == Keys.Control)
{
this.editor.Cursor = Cursors.Hand;
}
else
{
this.editor.Cursor = Cursors.IBeam;
}
}
private void Editor_MouseUp(object sender, MouseEventArgs e)
{
Place p = this.editor.PointToPlace(e.Location);
if (FastColoredEditorUtils.cursorIsOnTextOfStyle(p, GotoStyle, this.editor) && Control.ModifierKeys == Keys.Control)
{
string gotoName = findGotoClickTarget(p);
if (gotoName != null)
{
FastColoredEditorUtils.SelectText(gotoName, this.editor);
}
}
else if (FastColoredEditorUtils.cursorIsOnTextOfStyle(p, CheckFlagStyle, this.editor) && Control.ModifierKeys == Keys.Control)
{
string foundTarget = findCheckFlagClickedTarget(p);
FastColoredEditorUtils.SelectText(foundTarget.Trim(), this.editor);
}
else if(FastColoredEditorUtils.cursorIsOnTextOfStyle(p, VocabStyle, this.editor) && Control.ModifierKeys == Keys.Control)
{
string foundTarget = findVocabClickedTarget(p);
this.FileReferenceClicked(this, new FileReferenceClickedEventArgs { VocabFileName = foundTarget } );
}
}
private string findGotoClickTarget(Place p)
{
MatchCollection matches = Matches(this.editor.GetLineText(p.iLine), @"(?i)(?<=(\@goto\(|then\(|chance[0-9]{2}\())[A-z_0-9öäüáéíóú+\s]+(?=\))"); //extracting the goto specifier
string gotoName = null;
for (int i = matches.Count - 1; i >= 0 && gotoName == null; i--)
{
if (matches[i].Groups[1].Index <= p.iChar)
{
gotoName = matches[i].Groups[0].Value;
}
}
return gotoName;
}
private string findCheckFlagClickedTarget(Place p)
{
Match checkflagTargetsMatch = Match(this.editor.GetLineText(p.iLine), @"(?i)(?<=\@checkflag\()([A-z_0-9öäüáéíóú+\s]+)(,([A-z_0-9öäüáéíóú+\s]+))*(?=\))"); //extracting the checkflag specifier
string foundTarget = null;
for (int i = checkflagTargetsMatch.Groups[3].Captures.Count - 1; i >= 0 && foundTarget == null; i--)
{
if (checkflagTargetsMatch.Groups[3].Captures[i].Index <= p.iChar)
{
foundTarget = checkflagTargetsMatch.Groups[3].Captures[i].Value;
}
}
if (foundTarget == null)
{
foundTarget = checkflagTargetsMatch.Groups[1].Value;
}
return foundTarget;
}
private string findVocabClickedTarget(Place p)
{
MatchCollection vocabClickedTargetsMatches = Matches(this.editor.GetLineText(p.iLine), @"(?i)(?<=\s|^)#[\w\-.]+(?=\s|$)"); //extracting the vocab name
string foundTarget = null;
for (int i = vocabClickedTargetsMatches.Count - 1; i >= 0 && foundTarget == null; i--)
{
if (vocabClickedTargetsMatches[i].Index <= p.iChar)
{
foundTarget = vocabClickedTargetsMatches[i].Value;
}
}
return foundTarget;
}
private void Editor_KeyUp(object sender, KeyEventArgs e)
{
Cursor.Position = Cursor.Position; //force cursor redraw
}
private void Editor_MouseDown(Object sender, MouseEventArgs e)
{
FastColoredEditorUtils.clearHighlightedText(highlightedText);
}
private void Editor_DblClick(object sender, EventArgs e)
{
this.highlightedText = FastColoredEditorUtils.highlightText(this.editor.SelectedText, this.editor);
}
public override List<SearchResult> Search(string input, SearchCriteria criteria)
{
List<SearchResult> results = new List<SearchResult>();
bool CaseSensitive = criteria.HasFlag(SearchCriteria.CaseSensitive);
bool Regex = criteria.HasFlag(SearchCriteria.RegularExpression);
string searchRegex = Regex ? input : Escape(input);
string searchText = null;
if(this.editor != null)
{
searchText = this.editor.Text;
}
else
{
searchText = GetSearchContent();
}
List<string> lines = new List<string>();
lines.AddRange(searchText.Split('\n'));
for(int i = 0; i < lines.Count; i++)
{
string line = lines[i];
MatchCollection matches = Matches(line, searchRegex);
foreach(Match match in matches)
{
results.Add(new SearchResult(i + 1, match.Index, match.Length, match.Value, line, this));
}
}
return results;
}
public override string GetSearchContent()
{
return this.Read();
}
}
public enum ScriptDeclensionType
{
Default,
Chastity,
Edging,
Begging
}
}