]> icculus.org git repositories - taylor/freespace2.git/blob - src/fred2/cmdbrief.cpp
fix issue with looping audio streams
[taylor/freespace2.git] / src / fred2 / cmdbrief.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/FRED2/CmdBrief.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Command Briefing Editor
16  *
17  * $Log$
18  * Revision 1.3  2002/06/09 04:41:16  relnev
19  * added copyright header
20  *
21  * Revision 1.2  2002/05/07 03:16:44  theoddone33
22  * The Great Newline Fix
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:08  root
25  * Initial import.
26  *
27  * 
28  * 2     10/07/98 6:28p Dave
29  * Initial checkin. Renamed all relevant stuff to be Fred2 instead of
30  * Fred. Globalized mission and campaign file extensions. Removed Silent
31  * Threat specific code.
32  * 
33  * 1     10/07/98 3:01p Dave
34  * 
35  * 1     10/07/98 3:00p Dave
36  * 
37  * 8     4/30/98 8:23p John
38  * Fixed some bugs with Fred caused by my new cfile code.
39  * 
40  * 7     4/22/98 9:56a Sandeep
41  * 
42  * 6     4/20/98 4:40p Hoffoss
43  * Added a button to 4 editors to play the chosen wave file.
44  * 
45  * 5     4/03/98 12:39p Hoffoss
46  * Changed starting directory for browse buttons in several editors.
47  * 
48  * 4     3/19/98 4:24p Hoffoss
49  * Added remaining support for command brief screen (ANI and WAVE file
50  * playing).
51  * 
52  * 3     3/06/98 2:36p Hoffoss
53  * Placed correct text size limits on edit boxes.
54  * 
55  * 2     3/05/98 3:59p Hoffoss
56  * Added a bunch of new command brief stuff, and asteroid initialization
57  * to Fred.
58  *
59  * $NoKeywords: $
60  */
61
62 #include "stdafx.h"
63 #include <mmsystem.h>
64 #include "fred.h"
65 #include "cmdbrief.h"
66 #include "cfile.h"
67
68 #ifdef _DEBUG
69 #define new DEBUG_NEW
70 #undef THIS_FILE
71 static char THIS_FILE[] = __FILE__;
72 #endif
73
74 /////////////////////////////////////////////////////////////////////////////
75 // cmd_brief_dlg dialog
76
77 cmd_brief_dlg::cmd_brief_dlg(CWnd* pParent /*=NULL*/)
78         : CDialog(cmd_brief_dlg::IDD, pParent)
79 {
80         //{{AFX_DATA_INIT(cmd_brief_dlg)
81         m_ani_filename = _T("");
82         m_text = _T("");
83         m_stage_title = _T("");
84         m_wave_filename = _T("");
85         //}}AFX_DATA_INIT
86 }
87
88 void cmd_brief_dlg::DoDataExchange(CDataExchange* pDX)
89 {
90         CDialog::DoDataExchange(pDX);
91         //{{AFX_DATA_MAP(cmd_brief_dlg)
92         DDX_Text(pDX, IDC_ANI_FILENAME, m_ani_filename);
93         DDX_Text(pDX, IDC_TEXT, m_text);
94         DDX_Text(pDX, IDC_STAGE_TITLE, m_stage_title);
95         DDX_Text(pDX, IDC_WAVE_FILENAME, m_wave_filename);
96         //}}AFX_DATA_MAP
97
98         DDV_MaxChars(pDX, m_text, CMD_BRIEF_TEXT_MAX - 1);
99         DDV_MaxChars(pDX, m_ani_filename, MAX_FILENAME_LEN - 1);
100         DDV_MaxChars(pDX, m_wave_filename, MAX_FILENAME_LEN - 1);
101 }
102
103 BEGIN_MESSAGE_MAP(cmd_brief_dlg, CDialog)
104         //{{AFX_MSG_MAP(cmd_brief_dlg)
105         ON_BN_CLICKED(IDC_NEXT, OnNext)
106         ON_BN_CLICKED(IDC_PREV, OnPrev)
107         ON_BN_CLICKED(IDC_ADD_STAGE, OnAddStage)
108         ON_BN_CLICKED(IDC_INSERT_STAGE, OnInsertStage)
109         ON_BN_CLICKED(IDC_DELETE_STAGE, OnDeleteStage)
110         ON_BN_CLICKED(IDC_BROWSE_ANI, OnBrowseAni)
111         ON_BN_CLICKED(IDC_BROWSE_WAVE, OnBrowseWave)
112         ON_BN_CLICKED(IDC_PLAY, OnPlay)
113         //}}AFX_MSG_MAP
114 END_MESSAGE_MAP()
115
116 /////////////////////////////////////////////////////////////////////////////
117 // cmd_brief_dlg message handlers
118
119 BOOL cmd_brief_dlg::OnInitDialog() 
120 {
121         Cur_cmd_brief = Cmd_briefs;  // default to first cmd briefing
122         m_cur_stage = 0;
123         last_cmd_brief = NULL;
124
125         CDialog::OnInitDialog();
126         m_play_bm.LoadBitmap(IDB_PLAY);
127         ((CButton *) GetDlgItem(IDC_PLAY)) -> SetBitmap(m_play_bm);
128
129         update_data();
130         return TRUE;
131 }
132
133 void cmd_brief_dlg::update_data(int update)
134 {
135         int enable;
136
137         if (update)
138                 UpdateData(TRUE);
139
140         // save previously editing data before we load over it.
141         if (last_cmd_brief && m_last_stage >= 0 && m_last_stage < last_cmd_brief->num_stages) {
142                 char buf[CMD_BRIEF_TEXT_MAX];
143
144                 if (last_cmd_brief->stage[m_last_stage].text)
145                         free(last_cmd_brief->stage[m_last_stage].text);
146
147                 deconvert_multiline_string(buf, m_text, CMD_BRIEF_TEXT_MAX);
148                 last_cmd_brief->stage[m_last_stage].text = strdup(buf);
149                 string_copy(last_cmd_brief->stage[m_last_stage].ani_filename, m_ani_filename, MAX_FILENAME_LEN);
150                 string_copy(last_cmd_brief->stage[m_last_stage].wave_filename, m_wave_filename, MAX_FILENAME_LEN);
151         }
152
153         // load data of new stage into dialog
154         if (Cur_cmd_brief && Cur_cmd_brief->num_stages > 0) {
155                 if (m_cur_stage < 0 || m_cur_stage >= Cur_cmd_brief->num_stages)
156                         m_cur_stage = 0;
157
158                 m_stage_title.Format("Stage %d of %d", m_cur_stage + 1, Cur_cmd_brief->num_stages);
159                 m_text = convert_multiline_string(Cur_cmd_brief->stage[m_cur_stage].text);
160                 m_ani_filename = Cur_cmd_brief->stage[m_cur_stage].ani_filename;
161                 m_wave_filename = Cur_cmd_brief->stage[m_cur_stage].wave_filename;
162                 enable = TRUE;
163
164         } else {
165                 m_stage_title = _T("No stages");
166                 m_text = _T("");
167                 m_ani_filename = _T("");
168                 m_wave_filename = _T("");
169                 enable = FALSE;
170                 m_cur_stage = -1;
171         }
172
173         if (m_cur_stage < Cur_cmd_brief->num_stages - 1)
174                 GetDlgItem(IDC_NEXT) -> EnableWindow(enable);
175         else
176                 GetDlgItem(IDC_NEXT) -> EnableWindow(FALSE);
177
178         if (m_cur_stage)
179                 GetDlgItem(IDC_PREV) -> EnableWindow(enable);
180         else
181                 GetDlgItem(IDC_PREV) -> EnableWindow(FALSE);
182
183         if (Cur_cmd_brief->num_stages >= CMD_BRIEF_STAGES_MAX)
184                 GetDlgItem(IDC_ADD_STAGE) -> EnableWindow(FALSE);
185         else
186                 GetDlgItem(IDC_ADD_STAGE) -> EnableWindow(TRUE);
187
188         if (Cur_cmd_brief->num_stages) {
189                 GetDlgItem(IDC_DELETE_STAGE) -> EnableWindow(enable);
190                 GetDlgItem(IDC_INSERT_STAGE) -> EnableWindow(enable);
191
192         } else {
193                 GetDlgItem(IDC_DELETE_STAGE) -> EnableWindow(FALSE);
194                 GetDlgItem(IDC_INSERT_STAGE) -> EnableWindow(FALSE);
195         }
196
197         GetDlgItem(IDC_WAVE_FILENAME) -> EnableWindow(enable);
198         GetDlgItem(IDC_ANI_FILENAME) -> EnableWindow(enable);
199         GetDlgItem(IDC_BROWSE_ANI) -> EnableWindow(enable);
200         GetDlgItem(IDC_BROWSE_WAVE) -> EnableWindow(enable);
201         GetDlgItem(IDC_TEXT) -> EnableWindow(enable);
202
203         UpdateData(FALSE);
204
205         last_cmd_brief = Cur_cmd_brief;
206         m_last_stage = m_cur_stage;
207 }
208
209 void cmd_brief_dlg::OnOK()
210 {
211         update_data();
212         CDialog::OnOK();
213 }
214
215 void cmd_brief_dlg::OnNext() 
216 {
217         m_cur_stage++;
218         update_data();
219 }
220
221 void cmd_brief_dlg::OnPrev() 
222 {
223         m_cur_stage--;
224         update_data();
225 }
226
227 void cmd_brief_dlg::OnAddStage() 
228 {
229         int i;
230
231         if (Cur_cmd_brief->num_stages >= CMD_BRIEF_STAGES_MAX)
232                 return;
233
234         m_cur_stage = i = Cur_cmd_brief->num_stages++;
235         copy_stage(i - 1, i);
236         update_data(1);
237 }
238
239 void cmd_brief_dlg::OnInsertStage() 
240 {
241         int i, z;
242
243         if (Cur_cmd_brief->num_stages >= CMD_BRIEF_STAGES_MAX)
244                 return;
245
246         if (!Cur_cmd_brief->num_stages) {
247                 OnAddStage();
248                 return;
249         }
250
251         z = m_cur_stage;
252         m_cur_stage = -1;
253         update_data(1);
254         for (i=Cur_cmd_brief->num_stages; i>z; i--)
255                 Cur_cmd_brief->stage[i] = Cur_cmd_brief->stage[i - 1];
256
257         Cur_cmd_brief->num_stages++;
258         copy_stage(z, z + 1);
259         m_cur_stage = z;
260         update_data(0);
261 }
262
263 void cmd_brief_dlg::OnDeleteStage() 
264 {
265         int i, z;
266
267         if (m_cur_stage < 0)
268                 return;
269         
270         SDL_assert(Cur_cmd_brief->num_stages);
271         z = m_cur_stage;
272         m_cur_stage = -1;
273         update_data(1);
274         if (Cur_cmd_brief->stage[z].text)
275                 free(Cur_cmd_brief->stage[z].text);
276
277         for (i=z+1; i<Cur_cmd_brief->num_stages; i++)
278                 Cur_cmd_brief->stage[i-1] = Cur_cmd_brief->stage[i];
279
280         Cur_cmd_brief->num_stages--;
281         m_cur_stage = z;
282         if (m_cur_stage >= Cur_cmd_brief->num_stages)
283                 m_cur_stage = Cur_cmd_brief->num_stages - 1;
284
285         update_data(0);
286 }
287
288 void cmd_brief_dlg::copy_stage(int from, int to)
289 {
290         if ((from < 0) || (from >= Cur_cmd_brief->num_stages)) {
291                 Cur_cmd_brief->stage[to].text = strdup("<Text here>");
292                 strcpy(Cur_cmd_brief->stage[to].ani_filename, "<default>");
293                 strcpy(Cur_cmd_brief->stage[to].wave_filename, "none");
294                 return;
295         }
296
297         Cur_cmd_brief->stage[to] = Cur_cmd_brief->stage[from];
298         Cur_cmd_brief->stage[to].text = strdup(Cur_cmd_brief->stage[from].text);
299 }
300
301 void cmd_brief_dlg::OnBrowseAni() 
302 {
303         int z;
304         CString name;   
305
306         UpdateData(TRUE);
307         z = cfile_push_chdir(CF_TYPE_INTERFACE);
308         CFileDialog dlg(TRUE, "ani", NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR,
309                 "Ani Files (*.ani)|*.ani|Avi Files (*.avi)|*.avi|Both (*.ani, *.avi)|*.ani;*.avi||");
310
311         if (dlg.DoModal() == IDOK) {
312                 m_ani_filename = dlg.GetFileName();
313                 UpdateData(FALSE);
314         }
315
316         if (!z)
317                 cfile_pop_dir();
318 }
319
320 void cmd_brief_dlg::OnBrowseWave() 
321 {
322         int z;
323         CString name;
324
325         UpdateData(TRUE);
326         z = cfile_push_chdir(CF_TYPE_VOICE_CMD_BRIEF);
327         CFileDialog dlg(TRUE, "wav", NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR,
328                 "Wave Files (*.wav)|*.wav||");
329
330         if (dlg.DoModal() == IDOK) {
331                 m_wave_filename = dlg.GetFileName();
332                 UpdateData(FALSE);
333         }
334
335         if (!z)
336                 cfile_pop_dir();
337 }
338
339 BOOL cmd_brief_dlg::DestroyWindow() 
340 {
341         m_play_bm.DeleteObject();
342         return CDialog::DestroyWindow();
343 }
344
345 void cmd_brief_dlg::OnPlay() 
346 {
347         char path[MAX_PATH_LEN + 1];
348         GetDlgItem(IDC_WAVE_FILENAME)->GetWindowText(m_wave_filename);
349
350         int size, offset;
351         cf_find_file_location((char *) (LPCSTR) m_wave_filename, CF_TYPE_ANY, path, &size, &offset );
352
353         PlaySound(path, NULL, SND_ASYNC | SND_FILENAME);
354 }
355