Hello!
I haven't found any tutorial about this here, so i decide to share how to make a textbox fully as lua interpreter using C# windows forms.
1 - First of all we need to download our textbox plugin, and that DLL to our winform project.
1.1 - FastColoredTextBox
2 - Right click on win form project -> Manage NuGet Packages, and search for MoonSharp, install the Interpreter.
After doing both, your solution should look like this.
3- Open the toolbox -> Right click -> Choose Items -> Browse , and look for FastColoredTextBox.dll, if it works this will appear on your toolbox control.
4 - Now drag&drop FastColoredTextBox to your win form, set the ID you want. and in C# section add that code.
Code:
private void InitStylesPriority()
{
// we making this to define that our textbox will have lua style
// because this plugin, can handle a lot of languages.
//add this style explicitly for drawing under other styles
txtAction.AddStyle(SameWordsStyle);
txtAction.ClearStylesBuffer();
txtAction.Range.ClearStyle(StyleIndex.All);
txtAction.AddStyle(SameWordsStyle);
txtAction.AutoIndentNeeded -= txtAction_AutoIndentNeeded;
txtAction.Language = Language.Lua;
txtAction.OnSyntaxHighlight(new TextChangedEventArgs(txtAction.Range));
}
private void txtAction_SelectionChangedDelayed(object sender, EventArgs e)
{
txtAction.VisibleRange.ClearStyle(SameWordsStyle);
if (!txtAction.Selection.IsEmpty)
return;//user selected diapason
//get fragment around caret
var fragment = txtAction.Selection.GetFragment(@"\w");
string text = fragment.Text;
if (text.Length == 0)
return;
//highlight same words
var ranges = txtAction.VisibleRange.GetRanges("\\b" + text + "\\b").ToArray();
if (ranges.Length > 1)
foreach (var r in ranges)
r.SetStyle(SameWordsStyle);
}
private void txtAction_AutoIndentNeeded(object sender, AutoIndentEventArgs args)
{
//block {}
if (Regex.IsMatch(args.LineText, @"^[^""']*\{.*\}[^""']*$"))
return;
//start of block {}
if (Regex.IsMatch(args.LineText, @"^[^""']*\{"))
{
args.ShiftNextLines = args.TabLength;
return;
}
//end of block {}
if (Regex.IsMatch(args.LineText, @"}[^""']*$"))
{
args.Shift = -args.TabLength;
args.ShiftNextLines = -args.TabLength;
return;
}
//label
if (Regex.IsMatch(args.LineText, @"^\s*\w+\s*:\s*($|//)") &&
!Regex.IsMatch(args.LineText, @"^\s*default\s*:"))
{
args.Shift = -args.TabLength;
return;
}
//some statements: case, default
if (Regex.IsMatch(args.LineText, @"^\s*(case|default)\b.*:\s*($|//)"))
{
args.Shift = -args.TabLength / 2;
return;
}
//is unclosed operator in previous line ?
if (Regex.IsMatch(args.PrevLineText, @"^\s*(if|for|foreach|while|[\}\s]*else)\b[^{]*$"))
if (!Regex.IsMatch(args.PrevLineText, @"(;\s*$)|(;\s*//)"))//operator is unclosed
{
args.Shift = args.TabLength;
return;
}
}
5 - Now in your Form_Load event call that the event we created.
Code:
private void Form_Load(object sender, EventArgs e)
{
// our event we created
InitStylesPriority();
}
6 - With that our textbox already fully syntax highlight, now we need to interpret Lua. We must add MoonSharp references, and we can define a static method as beeing a new lua function like this.
Code:
using MoonSharp.Interpreter;
Code:
Script script = new Script();
private void Form_Load(object sender, EventArgs e)
{
// our event we created
InitStylesPriority();
// we are telling to moonsharp that C# method Say in lua will be reconigze as say(string)
script.Globals["say"] = (Action<String>)(Say);
}
/// <summary>
/// This method will be interpreted on lua, just because its static, it's automated to recognize.
/// </summary>
/// <param name="message">Parameter message</param>
public static void Say(String message)
{
MessageBox.Show(message);
}
7 - Now just create a button and inside button click event, make that
Code:
// we telling the moonsharp to interpret the text inside our textbox
script.DoString(txtAction.Text);
8 - See these 2 pictures with examples.
Hope it help someone !