1:     using System;

2: using System.Collections;
3: using System.Collections.Specialized;
4: using System.ComponentModel;
5: using System.Drawing;
6: using System.Windows.Forms;
7: using System.IO;
8: using System.Text.RegularExpressions;
9: using System.Threading;
10:
11: using ShizzFramework;
12: using ShizzFramework.Schema;
13: using ShizzFramework.ShizzObjects;
14:
15: namespace ShizzDefaultPlugin
16: {
17: public delegate void DelegateSmartTransfer
18: (ShizzContainer masterContainer, ShizzContainer containerCopy);
19: public delegate string DelegateGetText();
20:
21: public class TextEditor : ShizzFramework.Plugin.PluginForm
22: {
23: private System.Windows.Forms.SaveFileDialog sfdShizmoo;
24: private ShizzFramework.Controls.RichTextBoxHighlightable rtbCode;
25: private System.Windows.Forms.MenuItem mitOptions;
26: private System.Windows.Forms.MenuItem mitHighlight;
27: private System.Windows.Forms.Panel pnlCursorInfo;
28: private System.Windows.Forms.Label lblCol;
29: private System.Windows.Forms.Label lblLine;
30: private System.ComponentModel.IContainer components = null;
31:
32: private Thread _threadUpdateContainer = null;
33: private bool _bUpdateContainer = false;
34:
35: public DelegateSmartTransfer _delegateSmartTransfer;
36: public DelegateGetText _delegateGetText;
37: public TextEditor()
38: {
39: // This call is required by the Windows Form Designer.
40: InitializeComponent();
41:
42: this.Schemas.Add("Sumo Volleyball");
43: this.Schemas.Add("Word Ninja");
44: this.Schemas.Add("Kung Fu Chess");
45: this.Schemas.Add("Billiard Boxing");
46:
47: this.Author = "Daniel Gimenez";
48: this.Description = "Use this to edit the code your game generates. " +
49: "I made the control myself so that's why it's a bit buggy. " +
50: "Turn the highlighting off cause it's jumpy crap.";
51:
52: mitHighlight.Checked = rtbCode.Highlight;
53: //_threadUpdateContainer = new Thread(new ThreadStart(UpdateContainer));
54: _bUpdateContainer = false;
55: _delegateSmartTransfer = new DelegateSmartTransfer(
56: TextParser.SmartTransferContainer);
57: _delegateGetText = new DelegateGetText(GetText);
58: }
59:
60: /// <summary>
61: /// Clean up any resources being used.
62: /// </summary>
63: protected override void Dispose( bool disposing )
64: {
65: if( disposing )
66: {
67: if (components != null)
68: {
69: components.Dispose();
70: }
71: }
72: base.Dispose( disposing );
73: }
74:
75: #region Designer generated code
76: /// <summary>
77: /// Required method for Designer support - do not modify
78: /// the contents of this method with the code editor.
79: /// </summary>
80: private void InitializeComponent()
81: {
82: this.rtbCode = new ShizzFramework.Controls.RichTextBoxHighlightable();
83: this.sfdShizmoo = new System.Windows.Forms.SaveFileDialog();
84: this.mitOptions = new System.Windows.Forms.MenuItem();
85: this.mitHighlight = new System.Windows.Forms.MenuItem();
86: this.pnlCursorInfo = new System.Windows.Forms.Panel();
87: this.lblLine = new System.Windows.Forms.Label();
88: this.lblCol = new System.Windows.Forms.Label();
89: this.pnlCursorInfo.SuspendLayout();
90: this.SuspendLayout();
91: //
92: // mnuPlugin
93: //
94: this.mnuPlugin.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
95: this.mitOptions});
96: //
97: // rtbCode
98: //
99: this.rtbCode.AcceptsTab = true;
100: this.rtbCode.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
101: | System.Windows.Forms.AnchorStyles.Left)
102: | System.Windows.Forms.AnchorStyles.Right)));
103: this.rtbCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
104: this.rtbCode.Font = new System.Drawing.Font("Courier New", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
105: this.rtbCode.Highlight = true;
106: this.rtbCode.Location = new System.Drawing.Point(8, 8);
107: this.rtbCode.Name = "rtbCode";
108: this.rtbCode.Size = new System.Drawing.Size(392, 299);
109: this.rtbCode.TabIndex = 0;
110: this.rtbCode.Text = "";
111: this.rtbCode.WordWrap = false;
112: this.rtbCode.SelectionChanged += new System.EventHandler(this.rtbCode_SelectionChanged);
113: //
114: // mitOptions
115: //
116: this.mitOptions.Index = 1;
117: this.mitOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
118: this.mitHighlight});
119: this.mitOptions.Text = "&Options";
120: //
121: // mitHighlight
122: //
123: this.mitHighlight.Index = 0;
124: this.mitHighlight.Text = "&Highlight";
125: this.mitHighlight.Click += new System.EventHandler(this.mitHighlight_Click);
126: //
127: // pnlCursorInfo
128: //
129: this.pnlCursorInfo.BackColor = System.Drawing.Color.Maroon;
130: this.pnlCursorInfo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
131: this.pnlCursorInfo.Controls.Add(this.lblLine);
132: this.pnlCursorInfo.Controls.Add(this.lblCol);
133: this.pnlCursorInfo.Dock = System.Windows.Forms.DockStyle.Bottom;
134: this.pnlCursorInfo.Location = new System.Drawing.Point(0, 313);
135: this.pnlCursorInfo.Name = "pnlCursorInfo";
136: this.pnlCursorInfo.Size = new System.Drawing.Size(408, 32);
137: this.pnlCursorInfo.TabIndex = 1;
138: //
139: // lblLine
140: //
141: this.lblLine.Anchor = System.Windows.Forms.AnchorStyles.Right;
142: this.lblLine.BackColor = System.Drawing.Color.Indigo;
143: this.lblLine.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
144: this.lblLine.Location = new System.Drawing.Point(280, 8);
145: this.lblLine.Name = "lblLine";
146: this.lblLine.Size = new System.Drawing.Size(56, 16);
147: this.lblLine.TabIndex = 1;
148: this.lblLine.Text = "line";
149: //
150: // lblCol
151: //
152: this.lblCol.Anchor = System.Windows.Forms.AnchorStyles.Right;
153: this.lblCol.BackColor = System.Drawing.Color.Indigo;
154: this.lblCol.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
155: this.lblCol.Location = new System.Drawing.Point(344, 8);
156: this.lblCol.Name = "lblCol";
157: this.lblCol.Size = new System.Drawing.Size(56, 16);
158: this.lblCol.TabIndex = 0;
159: this.lblCol.Text = "col";
160: //
161: // TextEditor
162: //
163: this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
164: this.ClientSize = new System.Drawing.Size(408, 345);
165: this.Controls.Add(this.pnlCursorInfo);
166: this.Controls.Add(this.rtbCode);
167: this.Name = "TextEditor";
168: this.Text = "Text Editor";
169: this.Leave += new System.EventHandler(this.TextEditor_Leave);
170: this.NewContainer += new System.EventHandler(this.TextEditor_NewContainer);
171: this.Enter += new System.EventHandler(this.TextEditor_Enter);
172: this.pnlCursorInfo.ResumeLayout(false);
173: this.Closing += new CancelEventHandler(TextEditor_Closing);
174: this.ResumeLayout(false);
175:
176: }
177: #endregion
178:
179: private void TextEditor_NewContainer(object sender, EventArgs e)
180: {
181: SetHighlighter();
182: }
183:
184: private void SetHighlighter()
185: {
186: string sRegexKeywords = SetSchemaKeywordsRegex();
187:
188: rtbCode.ClearColorKeywords();
189: rtbCode.AddColorKeywords(Color.Blue,
190: new Regex(sRegexKeywords, RegexOptions.IgnoreCase | RegexOptions.Compiled));
191: rtbCode.AddColorKeywords(Color.Red,
192: new Regex("\"[^{}]+\"", RegexOptions.Compiled));
193: }
194:
195: private string SetSchemaKeywordsRegex()
196: {
197: string sRegexReturn =
198: SetSchemaKeywordsRegexSub(MasterContainer.Schema, new StringCollection());
199:
200: if (sRegexReturn.Length > 0)
201: sRegexReturn = sRegexReturn.Substring(0, sRegexReturn.Length - 1);
202:
203: return sRegexReturn;
204: }
205:
206: private string SetSchemaKeywordsRegexSub(ShizzSchema schema,
207: StringCollection scAddedKeys)
208: {
209: string sRegexReturn = "";
210:
211: foreach (ShizzSchema schemaSub in schema)
212: {
213: if (!scAddedKeys.Contains(schemaSub.Name))
214: {
215: scAddedKeys.Add(schemaSub.Name);
216: sRegexReturn += @"(^|\s)" + schemaSub.Name + @"(\s|$)" + "|";
217: //sRegexReturn += @"\b" + schemaSub.Name + @"\b" + "|";
218: }
219: sRegexReturn += SetSchemaKeywordsRegexSub(schemaSub, scAddedKeys);
220: }
221:
222: return sRegexReturn;
223: }
224:
225: private void TextEditor_Leave(object sender, EventArgs e)
226: {
227: if (this.rtbCode.Text != "")
228: {
229: //TextParser.SmartTransfer(MasterContainer, rtbCode.Text);
230: _bUpdateContainer = false;
231: _threadUpdateContainer.Abort();
232: TextParser.SmartTransfer(MasterContainer, rtbCode.Text);
233: }
234: }
235:
236: private void TextEditor_Enter(object sender, EventArgs e)
237: {
238: rtbCode.Text = TextParser.RenderToText(this.MasterContainer,
239: this.MasterContainer.Schema);
240: _bUpdateContainer = true;
241: _threadUpdateContainer = new Thread(new ThreadStart(UpdateContainer));
242: _threadUpdateContainer.Start();
243: }
244:
245: private void mitHighlight_Click(object sender, System.EventArgs e)
246: {
247: mitHighlight.Checked = !rtbCode.Highlight;
248: rtbCode.Highlight = mitHighlight.Checked;
249: }
250:
251: private void rtbCode_SelectionChanged(object sender, EventArgs e)
252: {
253: int iLine = rtbCode.GetLineFromCharIndex(rtbCode.SelectionStart) + 1;
254: int iCol = rtbCode.GetColumn();
255:
256: lblLine.Text = "line " + iLine.ToString();
257: lblCol.Text = "col " + iCol.ToString();
258: }
259:
260: public string GetText()
261: {
262: return rtbCode.Text;
263: }
264:
265: private void UpdateContainer()
266: {
267: while (true)
268: {
269: if ((_bUpdateContainer) && (MasterContainer != null))
270: {
271: string sText = (string) this.Invoke(_delegateGetText);
272: ShizzContainer containerCopy =
273: TextParser.RenderFromTextTokens(sText, MasterContainer.Schema);
274: this.Invoke(_delegateSmartTransfer,
275: new Object[] { MasterContainer, containerCopy });
276: }
277: Thread.Sleep(3000);
278: }
279: }
280:
281: private void TextEditor_Closing(object sender, CancelEventArgs e)
282: {
283: _threadUpdateContainer.Abort();
284: }
285: }
286: }
287:
288: