Sfall-ScriptEditor

Форк
0
/
DialogPreview.cs 
273 строки · 10.1 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Drawing;
4
using System.IO;
5
using System.Linq;
6
using System.Windows.Forms;
7

8
using ICSharpCode.TextEditor.Document;
9

10
using ScriptEditor.CodeTranslation;
11
using ScriptEditor.TextEditorUI;
12
using ScriptEditor.TextEditorUtilities;
13

14
namespace ScriptEditor
15
{
16
    public partial class DialogPreview : Form
17
    {
18
        private List<DialogueParser> Arguments = new List<DialogueParser>();
19

20
        private List<string> nodesNavigation = new List<string>();
21
        private int currentNavigation = 0;
22

23
        private TabInfo sourceTab;
24

25
        private IDocument document
26
        {
27
            get { return sourceTab.textEditor.Document; }
28
        }
29

30
        private Procedure[] scrProc
31
        {
32
            get { return sourceTab.parseInfo.procs; }
33
        }
34

35
        private int readMsgNum = -1;
36
        private string[] MessagesData;
37
        
38
        private bool allow;
39
        private bool user;
40
        private bool needUpdate;
41

42
        public bool InitReady 
43
        {
44
            get { return (MessagesData != null); }
45
        }
46

47
        public DialogPreview(TabInfo sourceTab)
48
        {
49
            InitializeComponent();
50

51
            this.Text += sourceTab.filename;
52
            this.sourceTab = sourceTab;
53

54
            NodesComboBox.Items.AddRange(DialogueParser.GetAllNodesName(scrProc).ToArray());
55

56
            Procedure curProc = sourceTab.parseInfo.GetProcedureFromPosition(sourceTab.textEditor.ActiveTextAreaControl.Caret.Line);
57
            if (curProc == null || !NodesComboBox.Items.Contains(curProc.name)) {
58
                int indx = sourceTab.parseInfo.GetProcedureIndex("talk_p_proc");
59
                if (indx == -1)
60
                    return;
61
                curProc = scrProc[indx];
62
            }
63
            
64
            MessagesData = File.ReadAllLines(sourceTab.msgFilePath, Settings.EncCodePage);
65
            if (sourceTab.messages.Count == 0) MessageFile.ParseMessages(sourceTab, MessagesData);
66

67
            NodesComboBox.Text = curProc.name;
68
            nodesNavigation.Add(curProc.name);
69
            GotoNode(curProc);
70
        }
71

72
        private void GotoNode(Procedure curProc)
73
        {
74
            Arguments.Clear();
75
            dgvMessages.Rows.Clear();
76

77
            string body = Utilities.GetProcedureCode(document, curProc);
78
            if (body == null)
79
                return;
80
            DialogueParser.ParseNodeCode(body, Arguments, sourceTab.parseInfo);
81

82
            BuildMessageDialog();
83
        }
84
      
85
        private void BuildMessageDialog()
86
        {
87
            int addFemale = 0;
88
            if (femaleToolStripMenuItem.Checked)
89
                int.TryParse(toolStripTextBox.Text, out addFemale);
90

91
            string msg, msgPath = null;
92

93
            foreach (DialogueParser line in Arguments)
94
            {
95
                if (line.opcode == OpcodeType.None) continue;
96
                int n = addFemale;
97
                bool error = false;
98
                if (line.numberMsgLine > 0) {
99
                    
100
                    if (readMsgNum != line.numberMsgFile) {
101
                        if (line.numberMsgFile != -1) {
102
                            string path;
103
                            if (!MessageFile.GetPath(sourceTab, line.numberMsgFile, out path)) {
104
                                msg = String.Format(MessageFile.msgfileError, line.numberMsgFile);
105
                                msgPath = null;
106
                                error = true;
107
                                goto skip;
108
                            }
109
                            msgPath = path;
110
                        } else
111
                            msgPath = sourceTab.msgFilePath;
112

113
                        readMsgNum = line.numberMsgFile;
114
                        MessagesData = File.ReadAllLines(msgPath, Settings.EncCodePage); // загрузить другой файл сообщений
115
                    }
116

117
                    msg = MessageFile.GetMessages(MessagesData, addFemale + line.numberMsgLine);
118
                    if (msg == null && addFemale > 0) {
119
                        msg = MessageFile.GetMessages(MessagesData, line.numberMsgLine);
120
                        n = 0;
121
                    }
122
                    if (msg == null) {
123
                        msg = MessageFile.messageError;
124
                        error = true;
125
                    }
126
                } else
127
                    msg = "<" + line.shortcode + ">";
128
skip:
129
                dgvMessages.Rows.Add(line.toNode.Trim('"', ' '), msg, line.iq, (line.numberMsgLine > 0) ? n + line.numberMsgLine : line.numberMsgLine);
130
                dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[0].Tag = line.opcode;
131
                if (line.opcode == OpcodeType.Option || line.opcode == OpcodeType.giq_option || line.opcode == OpcodeType.gsay_option) {
132
                    dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[1].Value = (char)0x25CF + " " + msg;
133
                    if (!error)
134
                        dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[1].Style.ForeColor = Color.Blue;
135
                }
136
                if (line.numberMsgFile != -1)
137
                    dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[3].Tag = msgPath;
138
                if (error)
139
                    dgvMessages.Rows[dgvMessages.Rows.Count - 1].Cells[1].Style.ForeColor = Color.Red;
140
            }
141
        }
142

143
        private void dgvMessages_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
144
        {
145
            string node = dgvMessages.CurrentRow.Cells[0].Value.ToString();
146
            OpcodeType opcode = (OpcodeType)dgvMessages.CurrentRow.Cells[0].Tag;
147
            
148
            if (e.ColumnIndex == 1) {
149
                if (opcode == OpcodeType.Option || opcode == OpcodeType.giq_option || opcode == OpcodeType.gsay_option || opcode == OpcodeType.call) {
150
                    OptionsTextLabel.Text = dgvMessages.CurrentRow.Cells[1].Value.ToString();
151
                    user = false;
152
                    NodesComboBox.Text = node;
153
                    AddToNavigation(node);
154
                    if (JumpToolStripMenuItem.Checked) 
155
                        JumpProcedure(node);
156
                }
157
            } else if (e.ColumnIndex == 0) {
158
                if (opcode == OpcodeType.Reply || opcode == OpcodeType.Message)
159
                    node = NodesComboBox.Text;
160

161
                JumpProcedure(node);
162
            }
163
        }
164
        
165
        private void JumpProcedure(string nodeName)
166
        {                
167
            int index = sourceTab.parseInfo.GetProcedureIndex(nodeName);
168
            if (index == -1)
169
                return;
170

171
            TextEditor te = this.Owner as TextEditor; 
172
            te.SelectLine(scrProc[index].fstart, scrProc[index].d.start, true);
173
        }
174

175
        private void dgvMessages_CellContentClick(object sender, DataGridViewCellEventArgs e)
176
        {
177
            if (e.ColumnIndex == 3) {
178
                int nLine = (int)dgvMessages.CurrentRow.Cells[3].Value;
179
                if (nLine != -1) {
180
                    string path = (string)dgvMessages.CurrentRow.Cells[3].Tag ?? sourceTab.msgFilePath;
181
                    MessageEditor.MessageEditorInit(path, nLine).ShowDialog();
182
                }
183
            }
184
        }
185
        
186
        private void AddToNavigation(string node)
187
        {
188
            int count = nodesNavigation.Count;
189
            if (++currentNavigation < count) {   
190
                nodesNavigation.RemoveRange(currentNavigation, count - currentNavigation);
191
            }
192
            nodesNavigation.Add(node);
193
        }
194

195
        private void toolStripButton1_Click(object sender, EventArgs e)
196
        {
197
            if (currentNavigation == 0) return;
198
            string name = nodesNavigation[--currentNavigation];
199
            allow = false;
200
            NodesComboBox.Text = name;
201
            GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(name)]);
202
        }
203

204
        private void toolStripButton2_Click(object sender, EventArgs e)
205
        {
206
            if (currentNavigation >= nodesNavigation.Count - 1) return; 
207
            string name = nodesNavigation[++currentNavigation];
208
            allow = false;
209
            NodesComboBox.Text = name;
210
            GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(name)]);
211
        }
212

213
        private void NodesComboBox_SelectedIndexChanged(object sender, EventArgs e)
214
        {
215
            if (allow) {
216
                string name = NodesComboBox.Text;
217
                if (user) {
218
                    nodesNavigation.Clear();
219
                    currentNavigation = 0;
220
                    nodesNavigation.Add(name);
221
                }
222
                GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(name)]);
223
            } else 
224
                allow = true;
225
            user = false;
226
        }
227

228
        private void NodesComboBox_DropDownClosed(object sender, EventArgs e)
229
        {
230
            user = true;
231
        }
232

233
        private void femaleToolStripMenuItem_Click(object sender, EventArgs e)
234
        {
235
            toolStripTextBox.Enabled = femaleToolStripMenuItem.Checked;
236
            //update messages
237
            GotoNode(scrProc[sourceTab.parseInfo.GetProcedureIndex(NodesComboBox.Text)]);
238
        }
239

240
        private void DialogPreview_Activated(object sender, EventArgs e)
241
        {
242
            if (!needUpdate)
243
                return;
244
            
245
            List<string> nodes = DialogueParser.GetAllNodesName(scrProc);
246
            if (NodesComboBox.Items.Count != nodes.Count) {
247
                var sItem = NodesComboBox.SelectedItem;
248
                
249
                allow = false;
250
                NodesComboBox.Items.Clear();
251
                NodesComboBox.Items.AddRange(nodes.ToArray());
252
                
253
                if (sItem == null)
254
                    return;
255
                
256
                foreach (var item in NodesComboBox.Items)
257
                {
258
                    if (item.ToString() == sItem.ToString()) {
259
                        NodesComboBox.SelectedItem = item;
260
                        break;
261
                    }
262
                }
263
            }
264
            needUpdate = false;
265
        }
266

267
        private void DialogPreview_Shown(object sender, EventArgs e)
268
        {
269
            TextEditor te = this.Owner as TextEditor;
270
            te.ParserUpdatedInfo += delegate { needUpdate = true; };
271
        }
272
    }
273
}
274

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.