2012-07-19 00:19:47 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
2012-07-19 04:19:24 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2012-07-22 20:25:53 +00:00
|
|
|
|
using System.IO;
|
2012-07-23 01:02:04 +00:00
|
|
|
|
using BizHawk.MultiClient.tools;
|
2012-07-19 00:19:47 +00:00
|
|
|
|
|
2012-07-19 04:19:24 +00:00
|
|
|
|
namespace BizHawk.MultiClient
|
2012-07-19 00:19:47 +00:00
|
|
|
|
{
|
2012-07-22 19:54:40 +00:00
|
|
|
|
public partial class LuaWriter : Form
|
|
|
|
|
{
|
2012-07-23 01:02:04 +00:00
|
|
|
|
//TODO:
|
2012-07-23 12:28:03 +00:00
|
|
|
|
//make functions is string part of string or comment since the actual way of validating it isn't correct
|
2012-07-23 02:24:48 +00:00
|
|
|
|
//fix tabs (tab should be 4 characters)
|
2012-07-23 01:02:04 +00:00
|
|
|
|
//Line numbers
|
|
|
|
|
//Option to toggle line numbers
|
|
|
|
|
//Go to line number Ctrl+G
|
|
|
|
|
//Auto-complete drop down on functions in libraries
|
|
|
|
|
//intellisense on library functions
|
|
|
|
|
//New lua script menu item on console
|
|
|
|
|
//Option to turn off basic lua script
|
|
|
|
|
//Color config menu item
|
2012-07-23 01:32:41 +00:00
|
|
|
|
//Load/Save font
|
2012-07-23 01:02:04 +00:00
|
|
|
|
//Tool strip
|
|
|
|
|
//function toolstrip button (inserts a function end block and puts cursor on blank line between them
|
|
|
|
|
//when pressing enter on function blah, it should put the end afterwards
|
|
|
|
|
//on if then + enter key, put end afterwards
|
|
|
|
|
//error checking logic on library functions (check parameters, etc)
|
|
|
|
|
//fix so drag & drop text file on edit box works (not just the edges around it
|
|
|
|
|
//listview object with lua functions, double click inserts them into the script
|
|
|
|
|
|
2012-07-22 20:25:53 +00:00
|
|
|
|
public string CurrentFile = "";
|
2012-07-22 22:24:02 +00:00
|
|
|
|
|
|
|
|
|
bool changes = false;
|
|
|
|
|
bool hasChanged;
|
|
|
|
|
public Regex keyWords = new Regex("and|break|do|else|if|end|false|for|function|in|local|nil|not|or|repeat|return|then|true|until|while|elseif");
|
2012-07-23 01:02:04 +00:00
|
|
|
|
char[] Symbols = { '+', '-', '*', '/', '%', '^', '#', '=', '<', '>', '(', ')', '{', '}', '[', ']', ';', ':', ',', '.' };
|
|
|
|
|
public Regex libraryWords;
|
2012-07-24 03:35:28 +00:00
|
|
|
|
public Regex LuaLibraryWords = new Regex("coroutine|package|debug|file|io|math|os|package|string|table");
|
2012-07-23 01:32:41 +00:00
|
|
|
|
Font LuaTextFont = new Font("Courier New", 8);
|
2012-07-22 22:24:02 +00:00
|
|
|
|
|
2012-07-22 19:54:40 +00:00
|
|
|
|
public LuaWriter()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void timer_Tick(object sender, EventArgs e)
|
2012-07-22 22:24:02 +00:00
|
|
|
|
{
|
|
|
|
|
if (!hasChanged)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProcessText();
|
|
|
|
|
|
|
|
|
|
hasChanged = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ProcessText()
|
2012-07-22 19:54:40 +00:00
|
|
|
|
{
|
|
|
|
|
int selPos = LuaText.SelectionStart;
|
|
|
|
|
int selChars = LuaText.SelectedText.Length;
|
2012-07-19 19:58:27 +00:00
|
|
|
|
|
2012-07-21 04:17:49 +00:00
|
|
|
|
LuaText.SelectAll();
|
2012-07-22 22:16:44 +00:00
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaDefaultTextColor);
|
2012-07-21 04:17:49 +00:00
|
|
|
|
|
2012-07-22 19:54:40 +00:00
|
|
|
|
ColorReservedWords();
|
2012-07-24 03:35:28 +00:00
|
|
|
|
ColorLibraries();
|
|
|
|
|
ColorLuaLibraries();
|
2012-07-22 19:54:40 +00:00
|
|
|
|
ColorComments();
|
|
|
|
|
ColorStrings();
|
2012-07-23 01:02:04 +00:00
|
|
|
|
ColorSymbols();
|
2012-07-22 19:54:40 +00:00
|
|
|
|
LuaText.Select(selPos, selChars);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-24 03:35:28 +00:00
|
|
|
|
private void ColorLuaLibraries()
|
|
|
|
|
{
|
|
|
|
|
foreach (Match libraryWordMatch in LuaLibraryWords.Matches(LuaText.Text))
|
|
|
|
|
{
|
|
|
|
|
if (libraryWordMatch.Index >= 0)
|
|
|
|
|
{
|
|
|
|
|
char before = ' ', after = ' ';
|
|
|
|
|
|
|
|
|
|
if (libraryWordMatch.Index > 0)
|
|
|
|
|
before = LuaText.Text[libraryWordMatch.Index - 1];
|
|
|
|
|
|
|
|
|
|
if (libraryWordMatch.Index + libraryWordMatch.Length != LuaText.Text.Length)
|
|
|
|
|
after = LuaText.Text[libraryWordMatch.Index + libraryWordMatch.Length];
|
|
|
|
|
|
|
|
|
|
if (!char.IsLetterOrDigit(before))
|
|
|
|
|
{
|
|
|
|
|
if (after == '.')
|
|
|
|
|
{
|
|
|
|
|
LuaText.Select(libraryWordMatch.Index, libraryWordMatch.Length);
|
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaLibraryColor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 01:02:04 +00:00
|
|
|
|
private void ColorSymbols()
|
|
|
|
|
{
|
|
|
|
|
foreach (char mark in Symbols)
|
|
|
|
|
{
|
|
|
|
|
int currPos = 0;
|
|
|
|
|
while (LuaText.Find(mark.ToString(), currPos, RichTextBoxFinds.None) >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (LuaText.SelectionColor.ToArgb() != Global.Config.LuaCommentColor && LuaText.SelectionColor.ToArgb() != Global.Config.LuaStringColor)
|
2012-07-24 01:11:36 +00:00
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaSymbolColor);
|
2012-07-23 01:02:04 +00:00
|
|
|
|
currPos = LuaText.SelectionStart + 1;
|
|
|
|
|
|
|
|
|
|
if (currPos == LuaText.Text.Length)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-22 22:16:44 +00:00
|
|
|
|
|
2012-07-22 19:54:40 +00:00
|
|
|
|
private void ColorStrings()
|
|
|
|
|
{
|
|
|
|
|
int firstMark, opening, ending, endLine;
|
|
|
|
|
|
|
|
|
|
char[] chars = { '"', '\'' };
|
|
|
|
|
foreach (char mark in chars)
|
|
|
|
|
{
|
|
|
|
|
firstMark = LuaText.Find(mark.ToString());
|
2012-07-23 01:02:04 +00:00
|
|
|
|
while (firstMark >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (LuaText.SelectionColor.ToArgb() != Global.Config.LuaCommentColor)
|
|
|
|
|
{
|
|
|
|
|
opening = firstMark;
|
|
|
|
|
if (LuaText.GetLineFromCharIndex(opening) + 1 == LuaText.Lines.Count())
|
|
|
|
|
endLine = LuaText.Text.Length - 1;
|
|
|
|
|
else
|
|
|
|
|
endLine = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(opening) + 1) - 1;
|
|
|
|
|
|
|
|
|
|
ending = 0;
|
|
|
|
|
|
|
|
|
|
if (opening != LuaText.Text.Length - 1)
|
|
|
|
|
{
|
|
|
|
|
if (opening + 1 != endLine)
|
|
|
|
|
{
|
|
|
|
|
ending = LuaText.Find(mark.ToString(), opening + 1, endLine, RichTextBoxFinds.MatchCase);
|
|
|
|
|
if (ending > 0)
|
|
|
|
|
{
|
|
|
|
|
while (ending > 0)
|
|
|
|
|
{
|
|
|
|
|
if (!IsThisPartOfTheString(LuaText.Text.Substring(opening, ending - opening + 1)))
|
|
|
|
|
break;
|
|
|
|
|
else
|
|
|
|
|
ending++;
|
|
|
|
|
|
|
|
|
|
ending = LuaText.Find(mark.ToString(), ending, endLine, RichTextBoxFinds.MatchCase);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ending = endLine;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ending = endLine;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ending = endLine;
|
|
|
|
|
|
|
|
|
|
if (opening != LuaText.Text.Length)
|
|
|
|
|
{
|
|
|
|
|
LuaText.Select(opening, ending - opening + 1);
|
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaStringColor);
|
|
|
|
|
if (ending >= LuaText.Text.Length)
|
|
|
|
|
ending++;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
firstMark = LuaText.Find(mark.ToString(), ending + 1, LuaText.Text.Length, RichTextBoxFinds.MatchCase);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
firstMark = LuaText.Find(mark.ToString(), firstMark + 1, LuaText.Text.Length, RichTextBoxFinds.MatchCase);
|
|
|
|
|
}
|
2012-07-22 19:54:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsThisPartOfTheString(string wholestring)
|
|
|
|
|
{
|
|
|
|
|
int ammount = 0;
|
|
|
|
|
for (int x = wholestring.Length - 2; x > -1; x--)
|
|
|
|
|
{
|
|
|
|
|
if (wholestring[x] == '\\')
|
|
|
|
|
ammount++;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !(ammount % 2 == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ColorComments()
|
|
|
|
|
{
|
|
|
|
|
foreach (Match CommentMatch in new Regex("--").Matches(LuaText.Text))
|
|
|
|
|
{
|
|
|
|
|
int endComment;
|
|
|
|
|
|
|
|
|
|
if (LuaText.Text.Substring(CommentMatch.Index, 4) == "--[[")
|
|
|
|
|
{
|
|
|
|
|
if (LuaText.Find("]]", RichTextBoxFinds.MatchCase) > 0)
|
|
|
|
|
endComment = LuaText.SelectionStart - CommentMatch.Index + 2;
|
|
|
|
|
else
|
|
|
|
|
endComment = LuaText.Text.Length;
|
|
|
|
|
|
|
|
|
|
LuaText.Select(CommentMatch.Index, endComment);
|
2012-07-21 19:29:45 +00:00
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaCommentColor);
|
2012-07-22 19:54:40 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (LuaText.GetLineFromCharIndex(CommentMatch.Index) + 1 == LuaText.Lines.Count())
|
|
|
|
|
endComment = LuaText.Text.Length - CommentMatch.Index;
|
|
|
|
|
else
|
|
|
|
|
endComment = LuaText.GetFirstCharIndexFromLine(LuaText.GetLineFromCharIndex(CommentMatch.Index) + 1) - CommentMatch.Index;
|
|
|
|
|
|
|
|
|
|
LuaText.Select(CommentMatch.Index, endComment);
|
2012-07-21 19:29:45 +00:00
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaCommentColor);
|
2012-07-22 19:54:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-23 01:02:04 +00:00
|
|
|
|
|
2012-07-22 19:54:40 +00:00
|
|
|
|
private void ColorReservedWords()
|
|
|
|
|
{
|
|
|
|
|
foreach (Match keyWordMatch in keyWords.Matches(LuaText.Text))
|
|
|
|
|
{
|
|
|
|
|
char before = ' ', after = ' ';
|
|
|
|
|
|
|
|
|
|
if (keyWordMatch.Index > 0)
|
|
|
|
|
if (keyWordMatch.Index > 5 && keyWordMatch.Value != "if" && LuaText.Text.Substring(keyWordMatch.Index - 4, 4) != "else")
|
|
|
|
|
before = LuaText.Text[keyWordMatch.Index - 1];
|
|
|
|
|
|
|
|
|
|
if (keyWordMatch.Index + keyWordMatch.Length != LuaText.Text.Length)
|
|
|
|
|
if (keyWordMatch.Value != "else" && LuaText.Text.Substring(keyWordMatch.Index, 2) != "if")
|
|
|
|
|
after = LuaText.Text[keyWordMatch.Index + keyWordMatch.Length];
|
|
|
|
|
|
|
|
|
|
if (!char.IsLetterOrDigit(before) && !char.IsLetterOrDigit(after))
|
|
|
|
|
{
|
|
|
|
|
LuaText.Select(keyWordMatch.Index, keyWordMatch.Length);
|
2012-07-21 19:29:45 +00:00
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaKeyWordColor);
|
2012-07-22 19:54:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-19 00:19:47 +00:00
|
|
|
|
|
2012-07-23 01:02:04 +00:00
|
|
|
|
private void ColorLibraries()
|
|
|
|
|
{
|
|
|
|
|
foreach (Match libraryWordMatch in libraryWords.Matches(LuaText.Text))
|
|
|
|
|
{
|
2012-07-23 12:28:03 +00:00
|
|
|
|
if (libraryWordMatch.Index >= 0)
|
2012-07-23 01:02:04 +00:00
|
|
|
|
{
|
2012-07-23 12:28:03 +00:00
|
|
|
|
char before = ' ', after = ' ';
|
|
|
|
|
|
|
|
|
|
if (libraryWordMatch.Index > 0)
|
|
|
|
|
before = LuaText.Text[libraryWordMatch.Index - 1];
|
|
|
|
|
|
|
|
|
|
if (libraryWordMatch.Index + libraryWordMatch.Length != LuaText.Text.Length)
|
|
|
|
|
after = LuaText.Text[libraryWordMatch.Index + libraryWordMatch.Length];
|
|
|
|
|
|
2012-07-24 03:35:28 +00:00
|
|
|
|
if (!char.IsLetterOrDigit(before))
|
2012-07-23 01:02:04 +00:00
|
|
|
|
{
|
2012-07-23 12:28:03 +00:00
|
|
|
|
if (after == '.')
|
2012-07-23 01:02:04 +00:00
|
|
|
|
{
|
|
|
|
|
LuaText.Select(libraryWordMatch.Index, libraryWordMatch.Length);
|
2012-07-26 00:48:19 +00:00
|
|
|
|
LuaText.SelectionColor = Color.FromArgb(Global.Config.LuaLibraryColor);
|
2012-07-23 01:02:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GenerateLibraryRegex()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder list = new StringBuilder();
|
|
|
|
|
List<string> Libs = Global.MainForm.LuaConsole1.LuaImp.docs.GetLibraryList();
|
|
|
|
|
for (int i = 0; i < Libs.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
list.Append(Libs[i]);
|
|
|
|
|
if (i < Libs.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
list.Append('|');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
libraryWords = new Regex(list.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-22 19:54:40 +00:00
|
|
|
|
private void LuaWriter_Load(object sender, EventArgs e)
|
2012-07-23 01:02:04 +00:00
|
|
|
|
{
|
|
|
|
|
GenerateLibraryRegex();
|
2012-07-22 20:25:53 +00:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(CurrentFile))
|
|
|
|
|
{
|
|
|
|
|
LoadCurrentFile();
|
|
|
|
|
}
|
2012-07-25 17:36:26 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LuaText.Text = "while true do\n\temu.frameadvance()\nend";
|
|
|
|
|
}
|
|
|
|
|
UpdateLineNumber();
|
|
|
|
|
ProcessText();
|
|
|
|
|
NoChanges();
|
2012-07-22 20:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-22 22:24:02 +00:00
|
|
|
|
private void NoChanges()
|
|
|
|
|
{
|
|
|
|
|
changes = false;
|
|
|
|
|
MessageLabel.Text = CurrentFile;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-22 20:25:53 +00:00
|
|
|
|
private void LoadCurrentFile()
|
|
|
|
|
{
|
|
|
|
|
var file = new FileInfo(CurrentFile);
|
|
|
|
|
if (file.Exists == false)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (StreamReader sr = file.OpenText())
|
|
|
|
|
{
|
|
|
|
|
StringBuilder luaText = new StringBuilder();
|
|
|
|
|
string s = "";
|
|
|
|
|
while ((s = sr.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
luaText.Append(s);
|
|
|
|
|
luaText.Append('\n');
|
|
|
|
|
}
|
2012-07-22 19:54:40 +00:00
|
|
|
|
|
2012-07-22 20:25:53 +00:00
|
|
|
|
if (luaText.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
LuaText.Text = luaText.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-22 22:24:02 +00:00
|
|
|
|
|
|
|
|
|
MessageLabel.Text = CurrentFile;
|
2012-07-22 20:25:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LuaWriter_DragEnter(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None; string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LuaWriter_DragDrop(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
string[] filePaths = (string[])e.Data.GetData(DataFormats.FileDrop);
|
|
|
|
|
if (Path.GetExtension(filePaths[0]) == (".lua") || Path.GetExtension(filePaths[0]) == (".txt"))
|
|
|
|
|
{
|
|
|
|
|
//TODO: save changes
|
|
|
|
|
CurrentFile = filePaths[0];
|
|
|
|
|
LoadCurrentFile();
|
|
|
|
|
}
|
2012-07-22 19:54:40 +00:00
|
|
|
|
}
|
2012-07-22 22:24:02 +00:00
|
|
|
|
|
|
|
|
|
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(CurrentFile))
|
|
|
|
|
{
|
|
|
|
|
SaveScript();
|
|
|
|
|
}
|
|
|
|
|
else if (changes)
|
|
|
|
|
{
|
|
|
|
|
SaveScriptAs();
|
|
|
|
|
MessageLabel.Text = Path.GetFileName(CurrentFile) + " saved.";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveScript()
|
|
|
|
|
{
|
|
|
|
|
var file = new FileInfo(CurrentFile);
|
|
|
|
|
|
|
|
|
|
using (StreamWriter sw = new StreamWriter(CurrentFile))
|
|
|
|
|
{
|
|
|
|
|
foreach (string s in LuaText.Lines)
|
|
|
|
|
{
|
|
|
|
|
sw.WriteLine(s + '\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NoChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveScriptAs()
|
|
|
|
|
{
|
|
|
|
|
var file = GetSaveFileFromUser(CurrentFile);
|
|
|
|
|
if (file != null)
|
|
|
|
|
{
|
|
|
|
|
CurrentFile = file.FullName;
|
|
|
|
|
SaveScript();
|
|
|
|
|
MessageLabel.Text = Path.GetFileName(CurrentFile) + " saved.";
|
|
|
|
|
Global.Config.RecentWatches.Add(file.FullName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static FileInfo GetSaveFileFromUser(string currentFile)
|
|
|
|
|
{
|
|
|
|
|
var sfd = new SaveFileDialog();
|
|
|
|
|
if (currentFile.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
sfd.FileName = Path.GetFileNameWithoutExtension(currentFile);
|
|
|
|
|
sfd.InitialDirectory = Path.GetDirectoryName(currentFile);
|
|
|
|
|
}
|
|
|
|
|
else if (!(Global.Emulator is NullEmulator))
|
|
|
|
|
{
|
|
|
|
|
sfd.FileName = PathManager.FilesystemSafeName(Global.Game);
|
|
|
|
|
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sfd.FileName = "NULL";
|
|
|
|
|
sfd.InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.LuaPath, "");
|
|
|
|
|
}
|
|
|
|
|
sfd.Filter = "Watch Files (*.lua)|*.lua|All Files|*.*";
|
|
|
|
|
sfd.RestoreDirectory = true;
|
|
|
|
|
Global.Sound.StopSound();
|
|
|
|
|
var result = sfd.ShowDialog();
|
|
|
|
|
Global.Sound.StartSound();
|
|
|
|
|
if (result != DialogResult.OK)
|
|
|
|
|
return null;
|
|
|
|
|
var file = new FileInfo(sfd.FileName);
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LuaText_KeyUp(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
2012-07-23 02:24:48 +00:00
|
|
|
|
|
2012-07-22 22:24:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Changes()
|
|
|
|
|
{
|
|
|
|
|
changes = true;
|
|
|
|
|
MessageLabel.Text = CurrentFile + " *";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LuaText_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2012-07-24 03:35:28 +00:00
|
|
|
|
hasChanged = true;
|
2012-07-22 22:24:02 +00:00
|
|
|
|
Changes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SaveScriptAs();
|
|
|
|
|
}
|
2012-07-23 01:32:41 +00:00
|
|
|
|
|
|
|
|
|
private void syntaxHighlightingToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
LuaWriterColorConfig l = new LuaWriterColorConfig();
|
2012-07-26 00:48:19 +00:00
|
|
|
|
if (l.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
ProcessText(); //Update display with new settings
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 01:32:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
FontDialog f = new FontDialog();
|
|
|
|
|
DialogResult result = f.ShowDialog();
|
|
|
|
|
if (result == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
LuaText.Font = LuaTextFont = f.Font;
|
2012-07-24 01:11:36 +00:00
|
|
|
|
ProcessText(); //Re-update coloring and such when font changes
|
2012-07-23 01:32:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-23 02:24:48 +00:00
|
|
|
|
|
|
|
|
|
private void LuaText_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == Keys.Escape)
|
|
|
|
|
{
|
|
|
|
|
AutoCompleteView.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.KeyCode == Keys.OemPeriod)
|
|
|
|
|
{
|
|
|
|
|
string currentword = CurrentWord();
|
|
|
|
|
if (IsLibraryWord(currentword))
|
|
|
|
|
{
|
|
|
|
|
List<string> libfunctions = Global.MainForm.LuaConsole1.LuaImp.docs.GetFunctionsByLibrary(currentword);
|
|
|
|
|
AutoCompleteView.Visible = true;
|
|
|
|
|
AutoCompleteView.Items.Clear();
|
|
|
|
|
foreach(string function in libfunctions)
|
|
|
|
|
{
|
|
|
|
|
ListViewItem item = new ListViewItem(function);
|
|
|
|
|
AutoCompleteView.Items.Add(item);
|
|
|
|
|
}
|
|
|
|
|
AutoCompleteView.Location = new Point(0, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-25 22:53:11 +00:00
|
|
|
|
|
|
|
|
|
if (e.KeyCode == Keys.Enter)
|
|
|
|
|
{
|
|
|
|
|
string[] Words = { "if", "for", "while", "function" };
|
|
|
|
|
foreach (string Word in Words)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int linenumber = LuaText.GetLineFromCharIndex(LuaText.GetFirstCharIndexOfCurrentLine());
|
|
|
|
|
if (LuaText.Lines[linenumber].Substring(0, Word.Length) == Word)
|
|
|
|
|
{
|
|
|
|
|
string str = LuaText.Text.Insert(LuaText.SelectionStart, "\n\nend");
|
|
|
|
|
LuaText.Text = str;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-23 02:24:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CurrentWord()
|
|
|
|
|
{
|
|
|
|
|
int last = LuaText.SelectionStart;
|
|
|
|
|
|
|
|
|
|
int lastSpace = LuaText.Text.Substring(0, last).LastIndexOf(' ');
|
|
|
|
|
int lastLine = LuaText.Text.Substring(0, last).LastIndexOf('\n');
|
|
|
|
|
int start = 0;
|
|
|
|
|
if (lastSpace > lastLine)
|
|
|
|
|
{
|
|
|
|
|
start = lastSpace;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start = lastLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (start == -1)
|
|
|
|
|
{
|
|
|
|
|
start = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int length = last - start - 1;
|
|
|
|
|
string word = LuaText.Text.Substring(start + 1, length);
|
|
|
|
|
|
|
|
|
|
return word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsLibraryWord(string word)
|
|
|
|
|
{
|
|
|
|
|
List<string> Libs = Global.MainForm.LuaConsole1.LuaImp.docs.GetLibraryList();
|
|
|
|
|
if (Libs.Contains(word))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AutoCompleteView_MouseDoubleClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ListView.SelectedIndexCollection indexes = AutoCompleteView.SelectedIndices;
|
|
|
|
|
if (indexes.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
string str = AutoCompleteView.Items[indexes[0]].Text;
|
|
|
|
|
int start = LuaText.SelectionStart;
|
|
|
|
|
LuaText.Text = LuaText.Text.Insert(start, str);
|
|
|
|
|
AutoCompleteView.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-24 03:35:28 +00:00
|
|
|
|
|
|
|
|
|
private void LuaText_SelectionChanged(object sender, EventArgs e)
|
2012-07-25 17:36:26 +00:00
|
|
|
|
{
|
|
|
|
|
UpdateLineNumber();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateLineNumber()
|
2012-07-24 03:35:28 +00:00
|
|
|
|
{
|
|
|
|
|
if (!hasChanged)
|
|
|
|
|
{
|
|
|
|
|
int currentLineIndex = LuaText.GetLineFromCharIndex(LuaText.SelectionStart);
|
|
|
|
|
int lastLineIndex = LuaText.GetLineFromCharIndex(LuaText.TextLength);
|
|
|
|
|
int currentColumnIndex = LuaText.SelectionStart - LuaText.GetFirstCharIndexFromLine(currentLineIndex);
|
|
|
|
|
|
|
|
|
|
PositionLabel.Text = string.Format("Line {0}/{1}, Column {2}", currentLineIndex + 1, lastLineIndex + 1, currentColumnIndex + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-25 22:53:11 +00:00
|
|
|
|
|
|
|
|
|
private void LuaText_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2012-07-22 19:54:40 +00:00
|
|
|
|
}
|
2012-07-20 22:02:14 +00:00
|
|
|
|
}
|