-
Notifications
You must be signed in to change notification settings - Fork 9
/
OfficerPreviewForm.cs
361 lines (338 loc) · 12.1 KB
/
OfficerPreviewForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* YOGEME.exe, All-in-one Mission Editor for the X-wing series, XW through XWA
* Copyright (C) 2007-2024 Michael Gaisser ([email protected])
* Licensed under the MPL v2.0 or later
*
* VERSION: 1.16
*
* CHANGELOG
* v1.16, 241013
* [UPD] Updated the FONT.TotalChars to FONT.NumberOfGlyphs rename
* v1.8.1, 201213
* [UPD] Settings passed in instead of re-init
* v1.5, 180910
* [UPD] ctor now inits on a question, display code broken out [JB]
* [NEW] r-click reverse navigation [JB]
* [FIX] page count
* [FIX] possible OutOfBounds exception in displayString, now displays red X for bad chars
* [FIX] page string now always normal color
* [ADD] coloring helper function to allow active color tracking in the answer
* [FIX] highlighting now works across pages
* v1.3, 170107
* [UPD] changed form size due to W10 style
* v1.2.3, 141214
* [UPD] change to MPL
* v1.1.1, 120814
* - removed/renamed some methods, condensed
* - class renamed
* v1.1, 120715
* - Created
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using Idmr.LfdReader;
using Idmr.Platform.Tie;
namespace Idmr.Yogeme
{
/// <summary>Form for Briefing and Debriefing officer question preview</summary>
public partial class OfficerPreviewForm : Form
{
string[] _answerLines;
int _page = 1;
int _selectedIndex = 0;
readonly Questions _questions;
readonly RadioButton[] _opts = new RadioButton[4];
readonly Color _normalText = Color.FromArgb(84, 84, 252);
readonly Color _highlight = Color.FromArgb(0, 168, 0);
Color _activeColor;
readonly LfdFile _empire;
readonly LfdFile _talk;
readonly Bitmap _preview = new Bitmap(320, 200);
readonly byte[] _indexes = new byte[5];
readonly string _fontID = "FONTfont8";
public OfficerPreviewForm(Questions questions, int officer, int question)
{
try
{
string path = Settings.GetInstance().TiePath + "\\RESOURCE\\";
_empire = new LfdFile(path + "EMPIRE.LFD");
_talk = new LfdFile(path + "TALK.LFD");
}
catch (System.IO.FileNotFoundException)
{
MessageBox.Show("TIE resource files not found, preview unavailable", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Close();
}
_questions = questions;
InitializeComponent();
#region Pltt assignments
Pltt standard = (Pltt)_empire.Resources["PLTTstandard"];
Pltt offcr21 = (Pltt)_talk.Resources["PLTToffcr21"];
Pltt offbak = (Pltt)_talk.Resources["PLTToffbak"];
Pltt ssrobe9 = (Pltt)_talk.Resources["PLTTssrobe9"];
Pltt ssbak = (Pltt)_talk.Resources["PLTTssbak"];
Pltt doffbak = (Pltt)_talk.Resources["PLTTdoffbak"];
Pltt dssbak = (Pltt)_talk.Resources["PLTTdssbak"];
ColorPalette brf_off = Pltt.ConvertToPalette(new Pltt[] { standard, offcr21, offbak });
ColorPalette brf_ss = Pltt.ConvertToPalette(new Pltt[] { standard, ssrobe9, ssbak });
ColorPalette dbrf_off = Pltt.ConvertToPalette(new Pltt[] { standard, offcr21, doffbak });
ColorPalette dbrf_ss = Pltt.ConvertToPalette(new Pltt[] { standard, ssrobe9, dssbak });
((Delt)_talk.Resources["DELToffbak"]).Palette = brf_off;
Delt temp = (Delt)_talk.Resources["DELTofftxt"];
temp.Palette = brf_off;
temp.Image.MakeTransparent(Color.Black);
((Delt)_talk.Resources["DELTdoffbak"]).Palette = dbrf_off;
temp = (Delt)_talk.Resources["DELTdofftxt"];
temp.Palette = dbrf_off;
temp.Image.MakeTransparent(Color.Black);
temp = (Delt)_talk.Resources["DELToffcr21"];
temp.Palette = brf_off;
temp.Image.MakeTransparent(Color.Black);
((Anim)_talk.Resources["ANIMeyes"]).SetPalette(brf_off);
((Anim)_talk.Resources["ANIMmouth"]).SetPalette(brf_off);
((Delt)_talk.Resources["DELTssbak"]).Palette = brf_ss;
temp = (Delt)_talk.Resources["DELTsstxt"];
temp.Palette = brf_ss;
temp.Image.MakeTransparent(Color.Black);
((Delt)_talk.Resources["DELTdssbak"]).Palette = dbrf_ss;
temp = (Delt)_talk.Resources["DELTdsstxt"];
temp.Palette = dbrf_ss;
temp.Image.MakeTransparent(Color.Black);
temp = (Delt)_talk.Resources["DELTssrobe9"];
temp.Palette = brf_ss;
temp.Image.MakeTransparent(Color.Black);
((Anim)_talk.Resources["ANIMssface"]).SetPalette(brf_ss);
// offcr21, eyes, mouth, ssrobe9 and ssface technically change between brf and dbrf, but not really
((LfdReader.Font)_empire.Resources[_fontID]).SetColor(_normalText);
#endregion
_opts[0] = optPreOff;
_opts[1] = optPreSec;
_opts[2] = optPostOff;
_opts[3] = optPostSec;
for (int i = 0; i < 4; i++) _opts[i].CheckedChanged += new EventHandler(optsArr_CheckedChanged);
_activeColor = _normalText;
if (officer < 0 || officer >= 4) officer = 0;
_opts[officer].Checked = true;
if (question >= 0 && question <= 4)
{
_selectedIndex = -1; //Prevent "selecting" the wrong question when drawing the selection highlight.
loadBackAndQuestions();
for (int i = 0; i < _indexes.Length; i++) if (question == _indexes[i]) displayQuestion(i); //This will set the actual _selectedIndex
}
}
#region controls
void optsArr_CheckedChanged(object sender, EventArgs e)
{
// really this is only to make sure that paint only fires once per event
if (!((RadioButton)sender).Checked) return;
loadBackAndQuestions();
}
void pctPreview_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// blow image up to 640x400
g.DrawImage(_preview, 0, 0, _preview.Width * 2, _preview.Height * 2);
}
void pctPreview_MouseUp(object sender, MouseEventArgs e)
{
if (e.X <= 244 || e.X >= 622 || e.Y >= 390) return; // whitespace
if (e.Y < 232) // encompasses answer and page blocks
{
if (e.Button == MouseButtons.Right && cmdPrevious.Enabled) cmdPrevious_Click("pctPreview_MouseUp", new EventArgs());
if (e.Button != MouseButtons.Right && cmdNext.Enabled) cmdNext_Click("pctPreview_MouseUp", new EventArgs());
}
else if (e.Y > 290) // questions
{
int question = (e.Y - 290) / 20; // the question line clicked, 0-4
displayQuestion(question);
}
// else whitespace
}
void cmdClose_Click(object sender, EventArgs e) => Close();
void cmdPrevious_Click(object sender, EventArgs e)
{
_page--;
if (_page == 1) cmdPrevious.Enabled = false;
cmdNext.Enabled = true;
loadPage();
}
void cmdNext_Click(object sender, EventArgs e)
{
_page++;
if (_page == _numberOfPages) cmdNext.Enabled = false;
cmdPrevious.Enabled = true;
loadPage();
}
#endregion controls
#region methods
void loadPage()
{
loadBackAndQuestions();
changeAnswerColor(_activeColor); // this allows highlighting across pages
int offset = (_page - 1) * 10;
for (int i = 0; i < 10; i++)
{
if (i + offset == _answerLines.Length) break;
displayString(_answerLines[i + offset], 122, (short)(6 + i * 10));
}
((LfdReader.Font)_empire.Resources[_fontID]).SetColor(_normalText);
displayString("Page " + _page + " of " + _numberOfPages, 234, 106);
pctPreview.Invalidate();
}
void displayQuestion(int question)
{
if (_indexes[question] == 255) return; // blank Q/A set
_selectedIndex = _indexes[question];
_answerLines = _currentAnswer.Split(new string[] { "\r\n" }, StringSplitOptions.None);
_page = 1;
cmdPrevious.Enabled = false;
cmdNext.Enabled = (_numberOfPages > 1);
changeAnswerColor(_normalText);
loadPage();
}
void displayString(string text, short left, short top)
{
// in-game there's a shadow at (+1, +1) that I'm ignoring here
LfdReader.Font font8 = (LfdReader.Font)_empire.Resources[_fontID];
char[] chars = text.ToCharArray();
int offset = left;
byte glyph;
Graphics g = Graphics.FromImage(_preview);
bool badChar = false;
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] == '[')
{
changeAnswerColor(_highlight);
continue;
}
else if (chars[i] == ']')
{
changeAnswerColor(_normalText);
continue;
}
glyph = Convert.ToByte(chars[i] - font8.StartingChar);
if (glyph >= font8.NumberOfGlyphs)
{
glyph = Convert.ToByte('X' - font8.StartingChar);
badChar = true;
font8.SetColor(Color.Red);
}
g.DrawImageUnscaled(font8.Glyphs[glyph], offset, top);
offset += font8.Glyphs[glyph].Width + 1;
if (badChar)
{
badChar = false;
font8.SetColor(_activeColor);
}
}
g.Dispose();
}
void changeAnswerColor(Color newColor)
{
((LfdReader.Font)_empire.Resources[_fontID]).SetColor(newColor);
_activeColor = newColor;
}
void loadBackAndQuestions()
{
#region graphics
Graphics g = Graphics.FromImage(_preview);
Delt offcr21 = (Delt)_talk.Resources["DELToffcr21"];
Delt ssrobe9 = (Delt)_talk.Resources["DELTssrobe9"];
Anim eyes = (Anim)_talk.Resources["ANIMeyes"];
Anim ssface = (Anim)_talk.Resources["ANIMssface"];
Anim mouth = (Anim)_talk.Resources["ANIMmouth"];
switch (_qaSet)
{
// original graphics are 320x200, shift values come from the appropriate FILM
case 0:
Delt offbak = (Delt)_talk.Resources["DELToffbak"];
Delt offtxt = (Delt)_talk.Resources["DELTofftxt"];
g.DrawImageUnscaled(offbak.Image, 0, 0);
g.DrawImageUnscaled(offtxt.Image, offtxt.Left, offtxt.Top);
g.DrawImageUnscaled(offcr21.Image, offcr21.Left, offcr21.Top + 10);
g.DrawImageUnscaled(eyes.Frames[0].Image, eyes.Left, eyes.Top);
g.DrawImageUnscaled(mouth.Frames[0].Image, mouth.Left, mouth.Top);
break;
case 1:
Delt ssbak = (Delt)_talk.Resources["DELTssbak"];
Delt sstxt = (Delt)_talk.Resources["DELTsstxt"];
g.DrawImageUnscaled(ssbak.Image, 0, 0);
g.DrawImageUnscaled(sstxt.Image, sstxt.Left, sstxt.Top);
g.DrawImageUnscaled(ssrobe9.Image, ssrobe9.Left - 24, ssrobe9.Top + 12);
g.DrawImageUnscaled(ssface.Frames[0].Image, ssface.Left - 24, ssface.Top + 12);
break;
case 2:
Delt doffbak = (Delt)_talk.Resources["DELTdoffbak"];
Delt dofftxt = (Delt)_talk.Resources["DELTdofftxt"];
g.DrawImageUnscaled(doffbak.Image, 0, 0);
g.DrawImageUnscaled(dofftxt.Image, dofftxt.Left, dofftxt.Top);
g.DrawImageUnscaled(offcr21.Image, offcr21.Left, offcr21.Top + 10);
g.DrawImageUnscaled(eyes.Frames[0].Image, eyes.Left, eyes.Top);
g.DrawImageUnscaled(mouth.Frames[0].Image, mouth.Left, mouth.Top);
break;
case 3:
Delt dssbak = (Delt)_talk.Resources["DELTdssbak"];
Delt dsstxt = (Delt)_talk.Resources["DELTdsstxt"];
g.DrawImageUnscaled(dssbak.Image, 0, 0);
g.DrawImageUnscaled(dsstxt.Image, dsstxt.Left, dsstxt.Top);
g.DrawImageUnscaled(ssrobe9.Image, ssrobe9.Left - 24, ssrobe9.Top + 12);
g.DrawImageUnscaled(ssface.Frames[0].Image, ssface.Left - 24, ssface.Top + 12);
break;
}
g.Dispose();
#endregion graphics
int used = 0;
int curIndex = _selectedIndex;
Color active = _activeColor;
((LfdReader.Font)_empire.Resources[_fontID]).SetColor(_normalText);
for (_selectedIndex = 4; _selectedIndex > -1; _selectedIndex--)
{
_indexes[_selectedIndex] = 255;
string q = _currentQuestion;
if (q != "" || _currentAnswer != "")
{
if (q == "") q = "(empty question)"; //[JB] Display a placeholder for empty questions.
if (curIndex == _selectedIndex) q = "[" + q + "]"; //[JB] Simulate highlighting of selected question.
displayString(q, 122, (short)(145 + (4 - used) * 10));
_indexes[4 - used] = (byte)_selectedIndex;
used++;
}
}
_activeColor = active;
_selectedIndex = curIndex; //[JB] Restore selected index. This allows highlighting to remain functional.
pctPreview.Invalidate();
}
#endregion methods
#region properties
int _qaSet
{
get
{
for (int i = 0; i < 4; i++) if (_opts[i].Checked) return i;
return -1; // this never happen, but makes the compiler happy
}
}
int _offset => (_qaSet % 2) * 5;
string _currentQuestion
{
get
{
if (_qaSet < 2) return _questions.PreMissQuestions[_selectedIndex + _offset];
else return _questions.PostMissQuestions[_selectedIndex + _offset];
}
}
string _currentAnswer
{
get
{
if (_qaSet < 2) return _questions.PreMissAnswers[_selectedIndex + _offset];
else return _questions.PostMissAnswers[_selectedIndex + _offset];
}
}
int _numberOfPages => (_answerLines.Length - 1) / 10 + 1;
#endregion
}
}