]> icculus.org git repositories - taylor/freespace2.git/blob - src/fred2/orienteditor.cpp
Initial revision
[taylor/freespace2.git] / src / fred2 / orienteditor.cpp
1 /*
2  * $Logfile: /Freespace2/code/FRED2/OrientEditor.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Object orientation editor (or just object editor) dialog box handling code
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:08  root
11  * Initial revision
12  *
13  * 
14  * 2     10/07/98 6:28p Dave
15  * Initial checkin. Renamed all relevant stuff to be Fred2 instead of
16  * Fred. Globalized mission and campaign file extensions. Removed Silent
17  * Threat specific code.
18  * 
19  * 1     10/07/98 3:02p Dave
20  * 
21  * 1     10/07/98 3:00p Dave
22  * 
23  * 18    7/13/98 10:04a Hoffoss
24  * Fixed bug where jump nodes in mission would screw up indexing.
25  * 
26  * 17    3/12/98 2:21p Johnson
27  * Fixed some Fred bugs related to jump nodes.
28  * 
29  * 16    9/16/97 9:41p Hoffoss
30  * Changed Fred code around to stop using Parse_player structure for
31  * player information, and use actual ships instead.
32  * 
33  * 15    8/16/97 2:02a Hoffoss
34  * Made docked objects move together in Fred.
35  * 
36  * 14    8/15/97 8:24p Hoffoss
37  * Fixed bug with orient editor.
38  * 
39  * 13    8/14/97 3:14p Hoffoss
40  * Fixed name displayed for players.
41  * 
42  * 12    7/29/97 2:20p Hoffoss
43  * Fixed some minor bugs.
44  * 
45  * 11    6/26/97 11:18a Hoffoss
46  * Fixed bug in orient editor.  OBJ_POINT type wasn't accounted for.
47  * 
48  * 10    5/20/97 2:28p Hoffoss
49  * Added message box queries for close window operation on all modal
50  * dialog boxes.
51  * 
52  * 9     5/14/97 4:08p Lawrance
53  * removing my_index from game arrays
54  * 
55  * 8     4/17/97 2:01p Hoffoss
56  * All dialog box window states are saved between sessions now.
57  * 
58  * 7     3/12/97 4:33p Hoffoss
59  * added spin controls to orient editor, light intensity level can be
60  * specified in BG editor.
61  * 
62  * 6     2/28/97 11:31a Hoffoss
63  * Implemented modeless dialog saving and restoring, and changed some
64  * variables names.
65  * 
66  * 5     2/21/97 5:34p Hoffoss
67  * Added extensive modification detection and fixed a bug in initial
68  * orders editor.
69  * 
70  * 4     2/17/97 5:28p Hoffoss
71  * Checked RCS headers, added them were missing, changing description to
72  * something better, etc where needed.
73  *
74  * $NoKeywords: $
75  */
76
77 #include "stdafx.h"
78 #include "fred.h"
79 #include "orienteditor.h"
80 #include "management.h"
81 #include "linklist.h"
82 #include "fredview.h"
83
84 #ifdef _DEBUG
85 #define new DEBUG_NEW
86 #undef THIS_FILE
87 static char THIS_FILE[] = __FILE__;
88 #endif
89
90 #define PREC    0.0001f
91
92 /////////////////////////////////////////////////////////////////////////////
93 // orient_editor dialog
94
95 orient_editor::orient_editor(CWnd* pParent /*=NULL*/)
96         : CDialog(orient_editor::IDD, pParent)
97 {
98         vector pos;
99
100         //{{AFX_DATA_INIT(orient_editor)
101         m_object_index = 0;
102         m_point_to = FALSE;
103         m_position_z = _T("");
104         m_position_y = _T("");
105         m_position_x = _T("");
106         m_location_x = _T("0.0");
107         m_location_y = _T("0.0");
108         m_location_z = _T("0.0");
109         //}}AFX_DATA_INIT
110         Assert(query_valid_object());
111         pos = Objects[cur_object_index].pos;
112         m_position_x.Format("%.1f", pos.x);
113         m_position_y.Format("%.1f", pos.y);
114         m_position_z.Format("%.1f", pos.z);
115 }
116
117 void orient_editor::DoDataExchange(CDataExchange* pDX)
118 {
119         CDialog::DoDataExchange(pDX);
120         //{{AFX_DATA_MAP(orient_editor)
121         DDX_Control(pDX, IDC_SPIN6, m_spin6);
122         DDX_Control(pDX, IDC_SPIN5, m_spin5);
123         DDX_Control(pDX, IDC_SPIN4, m_spin4);
124         DDX_Control(pDX, IDC_SPIN3, m_spin3);
125         DDX_Control(pDX, IDC_SPIN2, m_spin2);
126         DDX_Control(pDX, IDC_SPIN1, m_spin1);
127         DDX_CBIndex(pDX, IDC_OBJECT_LIST, m_object_index);
128         DDX_Check(pDX, IDC_POINT_TO_CHECKBOX, m_point_to);
129         DDX_Text(pDX, IDC_POSITION_Z, m_position_z);
130         DDX_Text(pDX, IDC_POSITION_Y, m_position_y);
131         DDX_Text(pDX, IDC_POSITION_X, m_position_x);
132         DDX_Text(pDX, IDC_LOCATION_X, m_location_x);
133         DDX_Text(pDX, IDC_LOCATION_Y, m_location_y);
134         DDX_Text(pDX, IDC_LOCATION_Z, m_location_z);
135         //}}AFX_DATA_MAP
136 }
137
138 BEGIN_MESSAGE_MAP(orient_editor, CDialog)
139         //{{AFX_MSG_MAP(orient_editor)
140         ON_WM_CLOSE()
141         //}}AFX_MSG_MAP
142 END_MESSAGE_MAP()
143
144 /////////////////////////////////////////////////////////////////////////////
145 // orient_editor message handlers
146
147 BOOL orient_editor::OnInitDialog() 
148 {
149         char text[80];
150         int type;
151         CComboBox *box;
152         object *ptr;
153
154         CDialog::OnInitDialog();
155         theApp.init_window(&Object_wnd_data, this);
156         ((CButton *) GetDlgItem(IDC_POINT_TO_OBJECT))->SetCheck(1);
157         box = (CComboBox *) GetDlgItem(IDC_OBJECT_LIST);
158         box->ResetContent();
159
160         total = 0;
161         ptr = GET_FIRST(&obj_used_list);
162         while (ptr != END_OF_LIST(&obj_used_list)) {
163                 if (Marked != 1 || OBJ_INDEX(ptr) != cur_object_index) {
164                         if ((ptr->type == OBJ_START) || (ptr->type == OBJ_SHIP)) {
165                                 box->AddString(Ships[ptr->instance].ship_name);
166                                 index[total++] = OBJ_INDEX(ptr);
167
168                         } else if (ptr->type == OBJ_WAYPOINT) {
169                                 sprintf(text, "%s:%d", Waypoint_lists[ptr->instance / 65536].name,
170                                         (ptr->instance & 0xffff) + 1);
171
172                                 box->AddString(text);
173                                 index[total++] = OBJ_INDEX(ptr);
174
175                         } else if ((ptr->type == OBJ_POINT) || (ptr->type == OBJ_JUMP_NODE)) {
176                         } else
177                                 Assert(0);  // unknown object type.
178                 }
179
180                 ptr = GET_NEXT(ptr);
181         }
182
183         type = Objects[cur_object_index].type;
184         if (Marked == 1 && type == OBJ_WAYPOINT) {
185                 GetDlgItem(IDC_POINT_TO_CHECKBOX)->EnableWindow(0);
186                 GetDlgItem(IDC_POINT_TO_OBJECT)->EnableWindow(0);
187                 GetDlgItem(IDC_POINT_TO_LOCATION)->EnableWindow(0);
188                 GetDlgItem(IDC_OBJECT_LIST)->EnableWindow(0);
189                 GetDlgItem(IDC_LOCATION_X)->EnableWindow(0);
190                 GetDlgItem(IDC_LOCATION_Y)->EnableWindow(0);
191                 GetDlgItem(IDC_LOCATION_Z)->EnableWindow(0);
192                 m_object_index = -1;
193
194         } else {
195                 m_object_index = 0;
196         }
197
198         m_spin1.SetRange(99999, -99999);
199         m_spin1.SetPos((int) convert(m_position_x));
200         m_spin2.SetRange(99999, -99999);
201         m_spin2.SetPos((int) convert(m_position_y));
202         m_spin3.SetRange(99999, -99999);
203         m_spin3.SetPos((int) convert(m_position_z));
204         m_spin4.SetRange(99999, -99999);
205         m_spin5.SetRange(99999, -99999);
206         m_spin6.SetRange(99999, -99999);
207         UpdateData(FALSE);
208         return TRUE;
209 }
210
211 int orient_editor::query_modified()
212 {
213         float dif;
214
215         dif = Objects[cur_object_index].pos.x - convert(m_position_x);
216         if ((dif > PREC) || (dif < -PREC))
217                 return 1;
218         dif = Objects[cur_object_index].pos.y - convert(m_position_y);
219         if ((dif > PREC) || (dif < -PREC))
220                 return 1;
221         dif = Objects[cur_object_index].pos.z - convert(m_position_z);
222         if ((dif > PREC) || (dif < -PREC))
223                 return 1;
224
225         if (((CButton *) GetDlgItem(IDC_POINT_TO_CHECKBOX))->GetCheck() == 1)
226                 return 1;
227
228         return 0;
229 }
230
231 void orient_editor::OnOK()
232 {
233         vector delta, pos;
234         object *ptr;
235
236         UpdateData(TRUE);
237         pos.x = convert(m_position_x);
238         pos.y = convert(m_position_y);
239         pos.z = convert(m_position_z);
240
241         if ((((CButton *) GetDlgItem(IDC_POINT_TO_OBJECT))->GetCheck() == 1) ||
242                 (((CButton *) GetDlgItem(IDC_POINT_TO_LOCATION))->GetCheck() == 1))
243                         set_modified();
244
245         vm_vec_sub(&delta, &pos, &Objects[cur_object_index].pos);
246         if (delta.x || delta.y || delta.z)
247                 set_modified();
248
249         ptr = GET_FIRST(&obj_used_list);
250         while (ptr != END_OF_LIST(&obj_used_list)) {
251                 if (ptr->flags & OF_MARKED) {
252                         vm_vec_add2(&ptr->pos, &delta);
253                         update_object(ptr);
254                 }
255
256                 ptr = GET_NEXT(ptr);
257         }
258
259         ptr = GET_FIRST(&obj_used_list);
260         while (ptr != END_OF_LIST(&obj_used_list)) {
261                 if (ptr->flags & OF_MARKED)
262                         object_moved(ptr);
263
264                 ptr = GET_NEXT(ptr);
265         }
266
267         theApp.record_window_data(&Object_wnd_data, this);
268         CDialog::OnOK();
269 }
270
271 void orient_editor::update_object(object *ptr)
272 {
273         if (ptr->type != OBJ_WAYPOINT && m_point_to) {
274                 vector v, loc;
275                 matrix m;
276
277                 memset(&v, 0, sizeof(vector));
278                 loc.x = convert(m_location_x);
279                 loc.y = convert(m_location_y);
280                 loc.z = convert(m_location_z);
281                 if (((CButton *) GetDlgItem(IDC_POINT_TO_OBJECT))->GetCheck() == 1) {
282                         v = Objects[index[m_object_index]].pos;
283                         vm_vec_sub2(&v, &ptr->pos);
284
285                 } else if (((CButton *) GetDlgItem(IDC_POINT_TO_LOCATION))->GetCheck() == 1) {
286                         vm_vec_sub(&v, &loc, &ptr->pos);
287
288                 } else {
289                         Assert(0);  // neither radio button is checked.
290                 }
291
292                 if (!v.x && !v.y && !v.z){
293                         return;  // can't point to itself.
294                 }
295
296                 vm_vector_2_matrix(&m, &v, NULL, NULL);
297                 ptr->orient = m;
298         }
299 }
300
301 float orient_editor::convert(CString &str)
302 {
303         char buf[256];
304         int i, j, len;
305
306         string_copy(buf, str, 255);
307         len = strlen(buf);
308         for (i=j=0; i<len; i++)
309                 if (buf[i] != ',')
310                         buf[j++] = buf[i];
311
312         buf[j] = 0;
313         return (float) atof(buf);
314 }
315
316 void orient_editor::OnCancel()
317 {
318         theApp.record_window_data(&Object_wnd_data, this);
319         CDialog::OnCancel();
320 }
321
322 void orient_editor::OnClose() 
323 {
324         int z;
325
326         if (query_modified()) {
327                 z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
328                 if (z == IDCANCEL)
329                         return;
330
331                 if (z == IDYES) {
332                         OnOK();
333                         return;
334                 }
335         }
336
337         CDialog::OnClose();
338 }