forked from jadaradix/dsgamemaker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScript.cs
More file actions
434 lines (401 loc) · 18.2 KB
/
Script.cs
File metadata and controls
434 lines (401 loc) · 18.2 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using System.Drawing.Drawing2D;
namespace DS_Game_Maker
{
public partial class Script
{
public string ScriptName;
private string ScriptContent;
private bool DoIt = false;
private bool CanInsert = false;
private string WhatsDone = string.Empty;
private List<string> ArgumentNames = new List<string>();
private List<string> ArgumentTypes = new List<string>();
public Script()
{
InitializeComponent();
}
public void SaveChanges()
{
if (ScriptName != NameTextBox.Text)
{
File.Move(SessionsLib.SessionPath + "Scripts/" + ScriptName + ".dbas", SessionsLib.SessionPath + "Scripts/" + NameTextBox.Text + ".dbas");
}
DSGMlib.XDSChangeLine(DSGMlib.GetXDSLine("SCRIPT " + ScriptName + ","), "SCRIPT " + NameTextBox.Text + "," + (ParseDBASChecker.Checked ? "1" : "0"));
DSGMlib.XDSRemoveFilter("SCRIPTARG " + ScriptName + ",");
if (ArgumentNames.Count > 0)
{
for (byte P = 0, loopTo = (byte)(ArgumentNames.Count - 1); P <= loopTo; P++)
DSGMlib.XDSAddLine("SCRIPTARG " + NameTextBox.Text + "," + ArgumentNames[P] + "," + ArgumentTypes[P]);
}
File.WriteAllText(SessionsLib.SessionPath + "Scripts/" + NameTextBox.Text + ".dbas", MainTextBox.Text);
foreach (TreeNode X in Program.Forms.main_Form.ResourcesTreeView.Nodes[(int)DSGMlib.ResourceIDs.Script].Nodes)
{
if (X.Text == ScriptName)
X.Text = NameTextBox.Text;
}
}
private void UndoButton_Click(object sender, EventArgs e)
{
if (MainTextBox.CanUndo)
MainTextBox.Undo();
}
private void RedoButton_Click(object sender, EventArgs e)
{
if (MainTextBox.CanRedo)
MainTextBox.Redo();
}
public void GoToLine(short LineNumber, short Position, short SelLength)
{
if (LineNumber >= MainTextBox.Lines.Count)
return;
//MainTextBox.Caret.LineNumber = LineNumber;
// MainTextBox.Selection.Start += Position;
//MainTextBox.Selection.Length = SelLength;
}
private void Script_Load(object sender, EventArgs e)
{
// With MainTextBox.AutoComplete
// .List.Clear()
// .ListString = "Stylus_X,Stylus_Y"
// .ListSeparator = ","
// '.AutoHide = True
// '.AutomaticLengthEntered = True
// '.CancelAtStart = True
// '.DropRestOfWord = True
// '.FillUpCharacters = String.Empty
// '.ImageSeparator = Nothing
// .IsCaseSensitive = True
// '.SingleLineAccept = False
// '.StopCharacters = String.Empty
// End With
MainToolStrip.Renderer = new clsToolstripRenderer();
//MainTextBox.AcceptsTab = true;
//MainTextBox.Caret.HighlightCurrentLine = (int)Convert.ToByte(SettingsLib.GetSetting("HIGHLIGHT_CURRENT_LINE")) == 1;
// MsgError("""" + ScriptName + """")
ScriptContent = DSGMlib.PathToString(SessionsLib.SessionPath + "Scripts/" + ScriptName + ".dbas");
MainTextBox.Text = ScriptContent;
Text = ScriptName;
NameTextBox.Text = ScriptName;
TidyUp();
string XDSLine = DSGMlib.GetXDSLine("SCRIPT " + ScriptName + ",");
XDSLine = XDSLine.Substring(XDSLine.LastIndexOf(",") + 1);
ParseDBASChecker.Checked = XDSLine == "1";
foreach (string X in DSGMlib.GetXDSFilter("SCRIPTARG " + ScriptName + ","))
{
string TheName = DSGMlib.iGet(X, (byte)1, ",");
string TheType = DSGMlib.iGet(X, (byte)2, ",");
ArgumentNames.Add(TheName);
ArgumentTypes.Add(TheType);
}
ArrayToControl();
UpdateLineStats();
}
private void DAcceptButton_Click(object sender, EventArgs e)
{
// TODO LATER - after renaming, update references to this in:
// Other scripts, actions in events.
string NewName = NameTextBox.Text;
if (ScriptName != NewName)
{
if (DSGMlib.GUIResNameChecker(NameTextBox.Text))
return;
}
SaveChanges();
Close();
}
public void UpdateLineStats()
{
//StatsLabel.Text = "Ln " + MainTextBox.Caret.LineNumber.ToString() + " : ";
//StatsLabel.Text += MainTextBox.Lines.Count.ToString() + " Col " + MainTextBox.GetColumn(MainTextBox.CurrentPos).ToString();
//StatsLabel.Text += " Sel " + MainTextBox.Selection.Start.ToString();
}
private void NameTextBox_TextChanged(object sender, EventArgs e)
{
if (DoIt)
{
string NewText = ((TextBox)sender).Text;
NewText = DSGMlib.ResurrectResourceName(NewText);
short LengthDifference = (short)(((TextBox)sender).Text.Length - NewText.Length);
byte BackupCaret = (byte)NameTextBox.SelectionStart;
((TextBox)sender).Text = NewText;
NameTextBox.SelectionStart = BackupCaret - LengthDifference;
}
else
{
DoIt = true;
}
}
private void MainTextBox_LineStatCaller(object sender, EventArgs e)
{
UpdateLineStats();
}
private void MainTextBox_KeyUp(object sender, EventArgs e)
{
// Dim TheLine As String = MainTextBox.Lines.Current.Text.Replace(vbcrlf, String.Empty)
// If TheLine.Length > 0 Then
// For i As Byte = 0 To 200
// If TheLine(0).ToString = " " Then TheLine = TheLine.Substring(1) Else Exit For
// Next
// Else
// WhatsDone = String.Empty
// UpdateFunctionAssist()
// Exit Sub
// End If
// If TheLine.StartsWith("If Not ") Then TheLine = TheLine.Substring(7)
// If TheLine.StartsWith("If ") Then TheLine = TheLine.Substring(3)
// If TheLine.Contains("(") Then
// 'Bracketed off, so remember what they did.
// WhatsDone = TheLine.Substring(0, TheLine.LastIndexOf("("))
// UpdateFunctionAssist()
// End If
// FunctionsList.Items.Clear()
// For Each X As String In FunctionNames
// If TheLine.Length > 0 Then
// If X.StartsWith(TheLine) Then FunctionsList.Items.Add(X)
// Else
// FunctionsList.Items.Add(X)
// End If
// Next
// If FunctionsList.Items.Count > 0 Then
// FunctionsList.SelectedIndex = 0
// Else
// FunctionDescriptionLabel.Text = String.Empty
// End If
}
public void TidyUp()
{
ArgumentNames.Clear();
ArgumentTypes.Clear();
ArgumentsList.Items.Clear();
}
public void ArrayToControl()
{
ArgumentsList.Items.Clear();
if (ArgumentNames.Count > 0)
{
for (byte P = 0, loopTo = (byte)(ArgumentNames.Count - 1); P <= loopTo; P++)
ArgumentsList.Items.Add(P);
}
}
private void LoadInButton_Click(object sender, EventArgs e)
{
DialogResult MsgResponse = MessageBox.Show("Importing a Script will erase and replace the current code." + Constants.vbCrLf + Constants.vbCrLf + "Would you like to Continue?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (MsgResponse == DialogResult.No)
{
return;
}
string Response = DSGMlib.OpenFile(string.Empty, "Dynamic Basic Files|*.dbas");
if (Response.Length == 0)
return;
string Content = DSGMlib.PathToString(Response);
string FinalContent = string.Empty;
TidyUp();
foreach (string X_ in DSGMlib.StringToLines(Content))
{
string X = X_;
if (X.StartsWith("SCRIPTARG "))
{
X = X.Substring(10);
string TheName = X.Substring(0, X.IndexOf(","));
string TheType = X.Substring(X.IndexOf(",") + 1);
ArgumentNames.Add(TheName);
ArgumentTypes.Add(TheType);
}
else
{
FinalContent += X + Constants.vbCrLf;
}
}
ArrayToControl();
// If FinalContent.Length > 0 Then FinalContent = FinalContent.Substring(0, FinalContent.Length - 1)
MainTextBox.Text = FinalContent;
}
private void SaveOutButton_Click(object sender, EventArgs e)
{
string Response = DSGMlib.SaveFile(string.Empty, "Dynamic Basic Files|*.dbas", ScriptName + ".dbas");
if (Response.Length == 0)
return;
string ToWrite = MainTextBox.Text + Constants.vbCrLf;
for (byte P = 0, loopTo = (byte)(ArgumentNames.Count - 1); P <= loopTo; P++)
ToWrite += "SCRIPTARG " + ArgumentNames[P] + "," + ArgumentTypes[P] + Constants.vbCrLf;
if (ToWrite.Length > 0)
ToWrite = ToWrite.Substring(0, ToWrite.Length - 1);
File.WriteAllText(Response, ToWrite);
}
// Private Sub PreviewCButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
// Dim ArgumentsString As String = String.Empty
// Dim ArgumentTypesString As String = String.Empty
// If ArgumentNames.Count > 0 Then
// For P As Byte = 0 To ArgumentNames.Count - 1
// ArgumentsString += ArgumentNames(P) + ","
// ArgumentTypesString += ArgumentTypes(P) + ","
// Next
// ArgumentsString = ArgumentsString.Substring(0, ArgumentsString.Length - 1)
// ArgumentTypesString = ArgumentTypesString.Substring(0, ArgumentTypesString.Length - 1)
// End If
// For Each X As Form In MainForm.MdiChildren
// If Not X.Text = "Outputted C Preview for " + ScriptName Then Continue For
// For Each SC As Control In X.Controls
// If Not SC.Name = "MainTextBox" Then Continue For
// If ParseDBASChecker.Checked Then
// DirectCast(SC, ScintillaNet.Scintilla).Text = ScriptParseFromContent(ScriptName, MainTextBox.Text, ArgumentsString, ArgumentTypesString, True, False)
// Else
// DirectCast(SC, ScintillaNet.Scintilla).Text = ScriptParseFromContent(ScriptName, MainTextBox.Text, ArgumentsString, ArgumentTypesString, True, False)
// End If
// Next
// X.Focus()
// Exit Sub
// Next
// Dim CodeForm As New EditCode
// With CodeForm
// .Text = "Outputted C Preview for " + ScriptName
// .ReturnableCode = ScriptParseFromContent(ScriptName, MainTextBox.Text, ArgumentsString, ArgumentTypesString, True, False)
// .StartPosition = FormStartPosition.WindowsDefaultLocation
// .CodeMode = CodeMode.C
// End With
// ShowInternalForm(CodeForm)
// End Sub
private void InsertIntoCodeButton_Click(object sender, EventArgs e)
{
if (ArgumentsList.SelectedIndices.Count == 0)
return;
// Dim BackupPosition = MainTextBox.Caret.Position + ArgumentsListBox.Text.Length
MainTextBox.InsertText(MainTextBox.CurrentPosition, ArgumentNames[ArgumentsList.SelectedIndex]);
MainTextBox.CurrentPosition = MainTextBox.Text.Length;
MainTextBox.Focus();
// MainTextBox.Caret.Position = BackupPosition
}
private void AddArgumentButton_Click(object sender, EventArgs e)
{
var ArgumentForm = new Argument();
ArgumentForm.ArgumentName = string.Empty;
ArgumentForm.ArgumentType = "Integer";
ArgumentForm.Text = "Add Argument";
ArgumentForm.IsAction = false;
if (!(ArgumentForm.ShowDialog() == DialogResult.OK))
return;
if (ArgumentForm.ArgumentName.Length == 0)
return;
string NewName = ArgumentForm.ArgumentName;
string NewType = ArgumentForm.ArgumentType;
ArgumentForm.Dispose();
if (!DSGMlib.ValidateSomething(NewName, (byte)DSGMlib.ValidateLevel.Tight))
{
DSGMlib.MsgWarn("You must enter a valid name for the new Argument.");
return;
}
if (!DSGMlib.ValidateSomething(NewName, (byte)DSGMlib.ValidateLevel.NumberStart))
{
DSGMlib.MsgWarn("An Argument name may not start with a number.");
return;
}
bool AlreadyDone = false;
foreach (string X in ArgumentNames)
{
if (X == NewName)
AlreadyDone = true;
}
if (AlreadyDone)
{
DSGMlib.MsgError("There is already an Argument called '" + NewName + "'." + Constants.vbCrLf + Constants.vbCrLf + "You must choose another name.");
return;
}
ArgumentNames.Add(NewName);
ArgumentTypes.Add(NewType);
ArrayToControl();
}
private void EditArgumentButton_Click(object sender, EventArgs e)
{
if (ArgumentsList.SelectedIndices.Count == 0)
return;
byte ID = (byte)ArgumentsList.SelectedIndex;
var ArgumentForm = new Argument();
ArgumentForm.ArgumentName = ArgumentNames[ID];
ArgumentForm.ArgumentType = ArgumentTypes[ID];
ArgumentForm.Text = "Edit Argument";
ArgumentForm.IsAction = false;
if (!(ArgumentForm.ShowDialog() == DialogResult.OK))
return;
if (ArgumentForm.ArgumentName.Length == 0)
return;
string NewName = ArgumentForm.ArgumentName;
string NewType = ArgumentForm.ArgumentType;
ArgumentForm.Dispose();
if (!DSGMlib.ValidateSomething(NewName, (byte)DSGMlib.ValidateLevel.Tight))
{
DSGMlib.MsgWarn("You must enter a valid name for the Argument.");
return;
}
if (NewName != ArgumentNames[ID])
{
bool AlreadyDone = false;
foreach (string X in ArgumentNames)
{
if (X == NewName)
AlreadyDone = true;
}
if (AlreadyDone)
{
DSGMlib.MsgError("There is already an Argument called '" + NewName + "'." + Constants.vbCrLf + Constants.vbCrLf + "You must choose another name.");
return;
}
}
ArgumentNames[ID] = NewName;
ArgumentTypes[ID] = NewType;
ArgumentsList.Refresh();
DSGMlib.MsgInfo(Application.ProductName + " cannot update your code to use the new Argument name." + Constants.vbCrLf + Constants.vbCrLf + "You must do this yourself.");
}
private void DeleteArgumentButton_Click(object sender, EventArgs e)
{
if (ArgumentsList.SelectedIndices.Count == 0)
return;
ArgumentNames.RemoveAt(ArgumentsList.SelectedIndex);
ArgumentTypes.RemoveAt(ArgumentsList.SelectedIndex);
ArrayToControl();
// UX?
// MsgInfo("You must update your code so that it no longer uses or references the deleted argument.")
}
private void MainTextBox_CharAdded(object sender, ScintillaNET.CharAddedEventArgs e)
{
//if (!(e.Ch == '\r'))
// return;
ScintillaNET.Scintilla argTheControl = (ScintillaNET.Scintilla)sender;
DSGMlib.IntelliSense(ref argTheControl);
sender = argTheControl;
// Dim pos As Int32 = MainTextBox.NativeInterface.GetCurrentPos()
// Dim length As Int32 = pos - MainTextBox.NativeInterface.WordStartPosition(pos, True)
// MainTextBox.AutoComplete.Show(length)
}
private void ArgumentsList_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
return;
bool IsSelected = false;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
IsSelected = true;
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
string TheName = ArgumentNames[e.Index];
string TheType = ArgumentTypes[e.Index];
var TF = new Font("Tahoma", 11f, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF localMeasureString() { int argcharactersFitted = TheType.Length; int arglinesFilled = 1; var ret = e.Graphics.MeasureString(TheType, TF, e.Bounds.Size, StringFormat.GenericDefault, out argcharactersFitted, out arglinesFilled); return ret; }
byte TW = (byte)Math.Round(localMeasureString().Width);
if (IsSelected)
{
var x = new LinearGradientBrush(new Rectangle(0, e.Bounds.X, e.Bounds.Width, 16), Color.FromArgb(64, 64, 64), Color.Black, LinearGradientMode.Vertical);
e.Graphics.FillRectangle(x, e.Bounds);
e.Graphics.DrawString(TheName, TF, Brushes.White, 16f, e.Bounds.Y + 1);
e.Graphics.DrawString(TheType, TF, Brushes.LightGray, e.Bounds.Width - TW - 3, e.Bounds.Y + 1);
}
else
{
e.Graphics.DrawString(TheName, TF, Brushes.Black, 16f, e.Bounds.Y + 1);
e.Graphics.DrawString(TheType, TF, Brushes.LightGray, e.Bounds.Width - TW - 3, e.Bounds.Y + 1);
}
e.Graphics.DrawImageUnscaled(Properties.Resources.ArgumentIcon, new Point(0, e.Bounds.Y));
}
private void ArgumentsList_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 16;
}
}
}