Sfall-ScriptEditor
273 строки · 10.1 Кб
1using System;2using System.Collections.Generic;3using System.Drawing;4using System.IO;5using System.Linq;6using System.Windows.Forms;7
8using ICSharpCode.TextEditor.Document;9
10using ScriptEditor.CodeTranslation;11using ScriptEditor.TextEditorUI;12using ScriptEditor.TextEditorUtilities;13
14namespace ScriptEditor15{
16public partial class DialogPreview : Form17{18private List<DialogueParser> Arguments = new List<DialogueParser>();19
20private List<string> nodesNavigation = new List<string>();21private int currentNavigation = 0;22
23private TabInfo sourceTab;24
25private IDocument document26{27get { return sourceTab.textEditor.Document; }28}29
30private Procedure[] scrProc31{32get { return sourceTab.parseInfo.procs; }33}34
35private int readMsgNum = -1;36private string[] MessagesData;37
38private bool allow;39private bool user;40private bool needUpdate;41
42public bool InitReady43{44get { return (MessagesData != null); }45}46
47public DialogPreview(TabInfo sourceTab)48{49InitializeComponent();50
51this.Text += sourceTab.filename;52this.sourceTab = sourceTab;53
54NodesComboBox.Items.AddRange(DialogueParser.GetAllNodesName(scrProc).ToArray());55
56Procedure curProc = sourceTab.parseInfo.GetProcedureFromPosition(sourceTab.textEditor.ActiveTextAreaControl.Caret.Line);57if (curProc == null || !NodesComboBox.Items.Contains(curProc.name)) {58int indx = sourceTab.parseInfo.GetProcedureIndex("talk_p_proc");59if (indx == -1)60return;61curProc = scrProc[indx];62}63
64MessagesData = File.ReadAllLines(sourceTab.msgFilePath, Settings.EncCodePage);65if (sourceTab.messages.Count == 0) MessageFile.ParseMessages(sourceTab, MessagesData);66
67NodesComboBox.Text = curProc.name;68nodesNavigation.Add(curProc.name);69GotoNode(curProc);70}71
72private void GotoNode(Procedure curProc)73{74Arguments.Clear();75dgvMessages.Rows.Clear();76
77string body = Utilities.GetProcedureCode(document, curProc);78if (body == null)79return;80DialogueParser.ParseNodeCode(body, Arguments, sourceTab.parseInfo);81
82BuildMessageDialog();83}84
85private void BuildMessageDialog()86{87int addFemale = 0;88if (femaleToolStripMenuItem.Checked)89int.TryParse(toolStripTextBox.Text, out addFemale);90
91string msg, msgPath = null;92
93foreach (DialogueParser line in Arguments)94{95if (line.opcode == OpcodeType.None) continue;96int n = addFemale;97bool error = false;98if (line.numberMsgLine > 0) {99
100if (readMsgNum != line.numberMsgFile) {101if (line.numberMsgFile != -1) {102string path;103if (!MessageFile.GetPath(sourceTab, line.numberMsgFile, out path)) {104msg = String.Format(MessageFile.msgfileError, line.numberMsgFile);105msgPath = null;106error = true;107goto skip;108}109msgPath = path;110} else111msgPath = sourceTab.msgFilePath;112
113readMsgNum = line.numberMsgFile;114MessagesData = File.ReadAllLines(msgPath, Settings.EncCodePage); // загрузить другой файл сообщений115}116
117msg = MessageFile.GetMessages(MessagesData, addFemale + line.numberMsgLine);118if (msg == null && addFemale > 0) {119msg = MessageFile.GetMessages(MessagesData, line.numberMsgLine);120n = 0;121}122if (msg == null) {123msg = MessageFile.messageError;124error = true;125}126} else127msg = "<" + line.shortcode + ">";128skip:129dgvMessages.Rows.Add(line.toNode.Trim('"', ' '), msg, line.iq, (line.numberMsgLine > 0) ? n + line.numberMsgLine : line.numberMsgLine);130dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[0].Tag = line.opcode;131if (line.opcode == OpcodeType.Option || line.opcode == OpcodeType.giq_option || line.opcode == OpcodeType.gsay_option) {132dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[1].Value = (char)0x25CF + " " + msg;133if (!error)134dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[1].Style.ForeColor = Color.Blue;135}136if (line.numberMsgFile != -1)137dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[3].Tag = msgPath;138if (error)139dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[1].Style.ForeColor = Color.Red;140}141}142
143private void dgvMessages_CellDoubleClick(object sender, DataGridViewCellEventArgs e)144{145string node = dgvMessages.CurrentRow.Cells[0].Value.ToString();146OpcodeType opcode = (OpcodeType)dgvMessages.CurrentRow.Cells[0].Tag;147
148if (e.ColumnIndex == 1) {149if (opcode == OpcodeType.Option || opcode == OpcodeType.giq_option || opcode == OpcodeType.gsay_option || opcode == OpcodeType.call) {150OptionsTextLabel.Text = dgvMessages.CurrentRow.Cells[1].Value.ToString();151user = false;152NodesComboBox.Text = node;153AddToNavigation(node);154if (JumpToolStripMenuItem.Checked)155JumpProcedure(node);156}157} else if (e.ColumnIndex == 0) {158if (opcode == OpcodeType.Reply || opcode == OpcodeType.Message)159node = NodesComboBox.Text;160
161JumpProcedure(node);162}163}164
165private void JumpProcedure(string nodeName)166{167int index = sourceTab.parseInfo.GetProcedureIndex(nodeName);168if (index == -1)169return;170
171TextEditor te = this.Owner as TextEditor;172te.SelectLine(scrProc[index].fstart, scrProc[index].d.start, true);173}174
175private void dgvMessages_CellContentClick(object sender, DataGridViewCellEventArgs e)176{177if (e.ColumnIndex == 3) {178int nLine = (int)dgvMessages.CurrentRow.Cells[3].Value;179if (nLine != -1) {180string path = (string)dgvMessages.CurrentRow.Cells[3].Tag ?? sourceTab.msgFilePath;181MessageEditor.MessageEditorInit(path, nLine).ShowDialog();182}183}184}185
186private void AddToNavigation(string node)187{188int count = nodesNavigation.Count;189if (++currentNavigation < count) {190nodesNavigation.RemoveRange(currentNavigation, count - currentNavigation);191}192nodesNavigation.Add(node);193}194
195private void toolStripButton1_Click(object sender, EventArgs e)196{197if (currentNavigation == 0) return;198string name = nodesNavigation[--currentNavigation];199allow = false;200NodesComboBox.Text = name;201GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(name)]);202}203
204private void toolStripButton2_Click(object sender, EventArgs e)205{206if (currentNavigation >= nodesNavigation.Count - 1) return;207string name = nodesNavigation[++currentNavigation];208allow = false;209NodesComboBox.Text = name;210GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(name)]);211}212
213private void NodesComboBox_SelectedIndexChanged(object sender, EventArgs e)214{215if (allow) {216string name = NodesComboBox.Text;217if (user) {218nodesNavigation.Clear();219currentNavigation = 0;220nodesNavigation.Add(name);221}222GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(name)]);223} else224allow = true;225user = false;226}227
228private void NodesComboBox_DropDownClosed(object sender, EventArgs e)229{230user = true;231}232
233private void femaleToolStripMenuItem_Click(object sender, EventArgs e)234{235toolStripTextBox.Enabled = femaleToolStripMenuItem.Checked;236//update messages237GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(NodesComboBox.Text)]);238}239
240private void DialogPreview_Activated(object sender, EventArgs e)241{242if (!needUpdate)243return;244
245List<string> nodes = DialogueParser.GetAllNodesName(scrProc);246if (NodesComboBox.Items.Count != nodes.Count) {247var sItem = NodesComboBox.SelectedItem;248
249allow = false;250NodesComboBox.Items.Clear();251NodesComboBox.Items.AddRange(nodes.ToArray());252
253if (sItem == null)254return;255
256foreach (var item in NodesComboBox.Items)257{258if (item.ToString() == sItem.ToString()) {259NodesComboBox.SelectedItem = item;260break;261}262}263}264needUpdate = false;265}266
267private void DialogPreview_Shown(object sender, EventArgs e)268{269TextEditor te = this.Owner as TextEditor;270te.ParserUpdatedInfo += delegate { needUpdate = true; };271}272}273}
274