File: orienteditor.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (492 lines) | stat: -rw-r--r-- 13,853 bytes parent folder | download | duplicates (2)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/



#include "stdafx.h"
#include "FRED.h"
#include "OrientEditor.h"
#include "Management.h"
#include "globalincs/linklist.h"
#include "FREDView.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

constexpr auto INPUT_THRESHOLD = 0.01f;		// smallest increment of input box
constexpr auto INPUT_FORMAT = "%.01f";

/////////////////////////////////////////////////////////////////////////////
// orient_editor dialog

orient_editor::orient_editor(CWnd* pParent /*=NULL*/)
	: CDialog(orient_editor::IDD, pParent)
{
	//{{AFX_DATA_INIT(orient_editor)
	m_object_index = 0;
	m_point_to = FALSE;
	m_position_x = _T("");
	m_position_y = _T("");
	m_position_z = _T("");
	m_location_x = _T("0.0");
	m_location_y = _T("0.0");
	m_location_z = _T("0.0");
	m_orientation_p = _T("");
	m_orientation_b = _T("");
	m_orientation_h = _T("");
	//}}AFX_DATA_INIT

	Assert(query_valid_object());
	auto pos = &Objects[cur_object_index].pos;
	m_position_x.Format(INPUT_FORMAT, pos->xyz.x);
	m_position_y.Format(INPUT_FORMAT, pos->xyz.y);
	m_position_z.Format(INPUT_FORMAT, pos->xyz.z);

	angles ang;
	vm_extract_angles_matrix(&ang, &Objects[cur_object_index].orient);
	m_orientation_p.Format(INPUT_FORMAT, to_degrees(ang.p));
	m_orientation_b.Format(INPUT_FORMAT, to_degrees(ang.b));
	m_orientation_h.Format(INPUT_FORMAT, to_degrees(ang.h));
}

void orient_editor::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(orient_editor)
	DDX_Control(pDX, IDC_SPIN1, m_spin1);
	DDX_Control(pDX, IDC_SPIN2, m_spin2);
	DDX_Control(pDX, IDC_SPIN3, m_spin3);
	DDX_Control(pDX, IDC_SPIN4, m_spin4);
	DDX_Control(pDX, IDC_SPIN5, m_spin5);
	DDX_Control(pDX, IDC_SPIN6, m_spin6);
	DDX_Control(pDX, IDC_SPIN11, m_spin11);
	DDX_Control(pDX, IDC_SPIN12, m_spin12);
	DDX_Control(pDX, IDC_SPIN13, m_spin13);
	DDX_CBIndex(pDX, IDC_OBJECT_LIST, m_object_index);
	DDX_Check(pDX, IDC_POINT_TO_CHECKBOX, m_point_to);
	DDX_Text(pDX, IDC_POSITION_X, m_position_x);
	DDX_Text(pDX, IDC_POSITION_Y, m_position_y);
	DDX_Text(pDX, IDC_POSITION_Z, m_position_z);
	DDX_Text(pDX, IDC_LOCATION_X, m_location_x);
	DDX_Text(pDX, IDC_LOCATION_Y, m_location_y);
	DDX_Text(pDX, IDC_LOCATION_Z, m_location_z);
	DDX_Text(pDX, IDC_ORIENTATION_P, m_orientation_p);
	DDX_Text(pDX, IDC_ORIENTATION_B, m_orientation_b);
	DDX_Text(pDX, IDC_ORIENTATION_H, m_orientation_h);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(orient_editor, CDialog)
	//{{AFX_MSG_MAP(orient_editor)
	ON_CBN_SELCHANGE(IDC_OBJECT_LIST, OnObjectSelected)
	ON_BN_CLICKED(IDC_POINT_TO_CHECKBOX, OnPointTo)
	ON_BN_CLICKED(IDC_POINT_TO_OBJECT, OnPointToObject)
	ON_BN_CLICKED(IDC_POINT_TO_LOCATION, OnPointToLocation)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// orient_editor message handlers

BOOL orient_editor::OnInitDialog() 
{
	char text[80];
	int type;
	CComboBox *box;
	object *ptr;

	CDialog::OnInitDialog();
	theApp.init_window(&Object_wnd_data, this);
	((CButton *) GetDlgItem(IDC_POINT_TO_OBJECT))->SetCheck(TRUE);
	((CButton *) GetDlgItem(IDC_TRANSFORM_INDEPENDENT))->SetCheck(TRUE);
	box = (CComboBox *) GetDlgItem(IDC_OBJECT_LIST);
	box->ResetContent();

	total = 0;
	ptr = GET_FIRST(&obj_used_list);
	while (ptr != END_OF_LIST(&obj_used_list)) {
		int objnum = OBJ_INDEX(ptr);

		if (Marked != 1 || objnum != cur_object_index) {
			if ((ptr->type == OBJ_START) || (ptr->type == OBJ_SHIP)) {
				box->AddString(Ships[ptr->instance].ship_name);
				index[total++] = objnum;

			} else if (ptr->type == OBJ_WAYPOINT) {
				int waypoint_num;
				waypoint_list *wp_list = find_waypoint_list_with_instance(ptr->instance, &waypoint_num);
				Assert(wp_list != NULL);
				sprintf(text, "%s:%d", wp_list->get_name(), waypoint_num + 1);

				box->AddString(text);
				index[total++] = objnum;

			} else if (ptr->type == OBJ_JUMP_NODE) {
				box->AddString(jumpnode_get_by_objnum(objnum)->GetName());
				index[total++] = objnum;

			} else if (ptr->type != OBJ_POINT)
				Warning(LOCATION, "Unknown object type %d", ptr->type);
		}

		ptr = GET_NEXT(ptr);
	}

	type = Objects[cur_object_index].type;
	if (Marked == 1 && (type == OBJ_WAYPOINT || type == OBJ_JUMP_NODE)) {
		GetDlgItem(IDC_POINT_TO_CHECKBOX)->EnableWindow(FALSE);
		GetDlgItem(IDC_POINT_TO_OBJECT)->EnableWindow(FALSE);
		GetDlgItem(IDC_POINT_TO_LOCATION)->EnableWindow(FALSE);
		GetDlgItem(IDC_OBJECT_LIST)->EnableWindow(FALSE);
		GetDlgItem(IDC_LOCATION_X)->EnableWindow(FALSE);
		GetDlgItem(IDC_LOCATION_Y)->EnableWindow(FALSE);
		GetDlgItem(IDC_LOCATION_Z)->EnableWindow(FALSE);
		GetDlgItem(IDC_ORIENTATION_P)->EnableWindow(FALSE);
		GetDlgItem(IDC_ORIENTATION_B)->EnableWindow(FALSE);
		GetDlgItem(IDC_ORIENTATION_H)->EnableWindow(FALSE);
		m_object_index = -1;

	} else {
		m_object_index = 0;
	}

	if (Marked == 1 || (type == OBJ_WAYPOINT || type == OBJ_JUMP_NODE)) {
		GetDlgItem(IDC_TRANSFORM_INDEPENDENT)->EnableWindow(FALSE);
		GetDlgItem(IDC_TRANSFORM_RELATIVE)->EnableWindow(FALSE);
	}

	m_spin1.SetRange((short)-9999, (short)9999);
	m_spin1.SetPos((int) convert(m_position_x));
	m_spin2.SetRange((short)-9999, (short)9999);
	m_spin2.SetPos((int) convert(m_position_y));
	m_spin3.SetRange((short)-9999, (short)9999);
	m_spin3.SetPos((int) convert(m_position_z));

	m_spin4.SetRange((short)-9999, (short)9999);
	m_spin5.SetRange((short)-9999, (short)9999);
	m_spin6.SetRange((short)-9999, (short)9999);

	m_spin11.SetRange((short)-180, (short)180);
	m_spin11.SetPos((int)convert(m_orientation_p));
	m_spin12.SetRange((short)-180, (short)180);
	m_spin12.SetPos((int)convert(m_orientation_b));
	m_spin13.SetRange((short)-180, (short)180);
	m_spin13.SetPos((int)convert(m_orientation_h));

	UpdateData(FALSE);
	return TRUE;
}

/**
 * Checks whether the position value is close enough to the input string value that we can assume the input has not changed.
 */
bool orient_editor::is_close(float val, const CString &input_str) const
{
	val = perform_input_rounding(val);
	float input_val = convert(input_str);

	float diff = val - input_val;
	return abs(diff) < INPUT_THRESHOLD;
}

/**
 * Checks whether the angle value is close enough to the input string value that we can assume the input has not changed.
 */
bool orient_editor::is_angle_close(float rad, const CString &input_str) const
{
	float deg = perform_input_rounding(to_degrees(rad));
	float input_deg = normalize_degrees(convert(input_str));

	float diff = deg - input_deg;
	return abs(diff) < INPUT_THRESHOLD;
}

bool orient_editor::query_modified()
{
	if (!is_close(Objects[cur_object_index].pos.xyz.x, m_position_x))
		return true;
	if (!is_close(Objects[cur_object_index].pos.xyz.y, m_position_y))
		return true;
	if (!is_close(Objects[cur_object_index].pos.xyz.z, m_position_z))
		return true;

	angles ang;
	vm_extract_angles_matrix(&ang, &Objects[cur_object_index].orient);

	if (!is_angle_close(ang.p, m_orientation_p))
		return true;
	if (!is_angle_close(ang.b, m_orientation_b))
		return true;
	if (!is_angle_close(ang.h, m_orientation_h))
		return true;

	if (((CButton *) GetDlgItem(IDC_POINT_TO_CHECKBOX))->GetCheck() == TRUE)
		return true;

	return false;
}

void orient_editor::OnOK()
{
	vec3d delta, *cur_pos;
	matrix m = vmd_identity_matrix;
	angles ang;
	bool change_pos = false, change_orient = false;

	UpdateData(TRUE);

	// there's enough difference in our position that we're changing it
	cur_pos = &Objects[cur_object_index].pos;
	if (!is_close(cur_pos->xyz.x, m_position_x) || !is_close(cur_pos->xyz.y, m_position_y) || !is_close(cur_pos->xyz.z, m_position_z))
	{
		vec3d pos;
		pos.xyz.x = convert(m_position_x);
		pos.xyz.y = convert(m_position_y);
		pos.xyz.z = convert(m_position_z);
		vm_vec_sub(&delta, &pos, cur_pos);
		change_pos = true;
		set_modified();
	}

	// there's enough difference in our orientation that we're changing it
	vm_extract_angles_matrix(&ang, &Objects[cur_object_index].orient);
	if (!is_angle_close(ang.p, m_orientation_p) || !is_angle_close(ang.b, m_orientation_b) || !is_angle_close(ang.h, m_orientation_h))
	{
		ang.p = fl_radians(convert(m_orientation_p));
		ang.b = fl_radians(convert(m_orientation_b));
		ang.h = fl_radians(convert(m_orientation_h));
		vm_angles_2_matrix(&m, &ang);
		change_orient = true;
		set_modified();
	}

	// we're pointing to something
	if (m_point_to)
		set_modified();

	object *origin_objp = nullptr;
	vec3d origin_prev_pos = vmd_zero_vector;
	matrix origin_rotation = vmd_identity_matrix;

	if (Marked > 1 && ((CButton*)GetDlgItem(IDC_TRANSFORM_RELATIVE))->GetCheck() == TRUE) {
		origin_objp = &Objects[cur_object_index];
		origin_prev_pos = origin_objp->pos;
		auto saved_orient = origin_objp->orient;

		// move the origin first
		if (change_pos) {
			vm_vec_add2(&origin_objp->pos, &delta);
		}
		if (m_point_to) {
			actually_point_object(origin_objp);
		}
		else if (change_orient) {
			origin_objp->orient = m;
		}

		// calculate the change in orient
		if (origin_objp->type != OBJ_WAYPOINT) {
			vm_transpose(&saved_orient);
			origin_rotation = saved_orient * origin_objp->orient;
		}
	}

	// move and/or point all the objects
	for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) {
		if (ptr != origin_objp && ptr->flags[Object::Object_Flags::Marked]) {
			// relative to the new position of the origin
			if (origin_objp) {
				vec3d relative_pos, transformed_pos;
				vm_vec_sub(&relative_pos, &ptr->pos, &origin_prev_pos);
				vm_vec_unrotate(&transformed_pos, &relative_pos, &origin_rotation);
				vm_vec_add(&ptr->pos, &transformed_pos, &origin_objp->pos);
				ptr->orient = ptr->orient * origin_rotation;
			} 
			// all objects moving independently
			else {
				if (change_pos) {
					vm_vec_add2(&ptr->pos, &delta);
				}
				if (m_point_to) {
					actually_point_object(ptr);
				} else if (change_orient) {
					ptr->orient = m;
				}
			}
		}
	}

	// do post-move cleanup
	if (change_pos) {
		for (auto ptr = GET_FIRST(&obj_used_list); ptr != END_OF_LIST(&obj_used_list); ptr = GET_NEXT(ptr)) {
			if (ptr->flags[Object::Object_Flags::Marked]) {
				object_moved(ptr);
			}
		}
	}

	theApp.record_window_data(&Object_wnd_data, this);
	CDialog::OnOK();

	FREDDoc_ptr->autosave("object editor");
}

void orient_editor::actually_point_object(object *ptr)
{
	if (ptr->type != OBJ_WAYPOINT && m_point_to) {
		vec3d v, loc;
		matrix m;

		vm_vec_zero(&v);
		loc.xyz.x = convert(m_location_x);
		loc.xyz.y = convert(m_location_y);
		loc.xyz.z = convert(m_location_z);

		if (((CButton *) GetDlgItem(IDC_POINT_TO_OBJECT))->GetCheck() == TRUE) {
			if (m_object_index >= 0) {
				v = Objects[index[m_object_index]].pos;
				vm_vec_sub2(&v, &ptr->pos);
			}
		} else if (((CButton *) GetDlgItem(IDC_POINT_TO_LOCATION))->GetCheck() == TRUE) {
			vm_vec_sub(&v, &loc, &ptr->pos);

		} else {
			Assertion(0, "neither radio button is checked!");
			return;
		}

		if (IS_VEC_NULL(&v)) {
			return;  // can't point to itself.
		}

		auto uvec = Point_using_uvec ? &ptr->orient.vec.uvec : nullptr;

		vm_vector_2_matrix(&m, &v, uvec, nullptr);
		ptr->orient = m;
	}
}

float orient_editor::to_degrees(float rad)
{
	float deg = fl_degrees(rad);
	return normalize_degrees(deg);
}

float orient_editor::normalize_degrees(float deg)
{
	while (deg < -180.0f)
		deg += 180.0f;
	while (deg > 180.0f)
		deg -= 180.0f;
	// check for negative zero...
	if (deg == -0.0f)
		return 0.0f;
	return deg;
}

/**
 * Extract a float from the CString, being mindful of any comma separators.
 */
float orient_editor::convert(const CString &str) const
{
	char buf[256];
	size_t i, j, len;

	string_copy(buf, str, 256 - 1);
	len = strlen(buf);
	for (i=j=0; i<len; i++)
		if (buf[i] != ',')
			buf[j++] = buf[i];

	buf[j] = 0;

	float val = (float)atof(buf);

	// disallow input larger than INT_MAX
	if (val > (float)INT_MAX) {
		val = (float)INT_MAX;
	}

	// disallow input smaller than INT_MIN
	if (val < (float)INT_MIN) {
		val = (float)INT_MIN;
	}

	return val;
}

/**
 * Perform rounding of the float value by loading it into the input box format string and parsing it again.
 * This accounts for not only decimal rounding to the precision of the input box, but also floating point rounding due to inexact fractions such as 1/10.
 * See also GitHub PR #4475.
 */
float orient_editor::perform_input_rounding(float val) const
{
	CString str;
	str.Format(INPUT_FORMAT, val);
	return convert(str);
}

void orient_editor::OnObjectSelected()
{
	((CButton*)GetDlgItem(IDC_POINT_TO_OBJECT))->SetCheck(TRUE);
	((CButton*)GetDlgItem(IDC_POINT_TO_LOCATION))->SetCheck(FALSE);
	OnPointToObject();
}

void orient_editor::OnPointTo()
{
	UpdateData(TRUE);

	GetDlgItem(IDC_ORIENTATION_P)->EnableWindow(!m_point_to);
	GetDlgItem(IDC_ORIENTATION_B)->EnableWindow(!m_point_to);
	GetDlgItem(IDC_ORIENTATION_H)->EnableWindow(!m_point_to);
	GetDlgItem(IDC_SPIN11)->EnableWindow(!m_point_to);
	GetDlgItem(IDC_SPIN12)->EnableWindow(!m_point_to);
	GetDlgItem(IDC_SPIN13)->EnableWindow(!m_point_to);
}

void orient_editor::OnPointToObject()
{
	((CButton*)GetDlgItem(IDC_POINT_TO_CHECKBOX))->SetCheck(TRUE);
	OnPointTo();
}

void orient_editor::OnPointToLocation()
{
	((CButton*)GetDlgItem(IDC_POINT_TO_CHECKBOX))->SetCheck(TRUE);
	OnPointTo();
}

void orient_editor::OnCancel()
{
	theApp.record_window_data(&Object_wnd_data, this);
	CDialog::OnCancel();
}

void orient_editor::OnClose() 
{
	int z;

	if (query_modified()) {
		z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
		if (z == IDCANCEL)
			return;

		if (z == IDYES) {
			OnOK();
			return;
		}
	}

	CDialog::OnClose();
}