2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined( INCLUDED_ANGLES_H )
23 #define INCLUDED_ANGLES_H
27 #include "math/quaternion.h"
28 #include "generic/callback.h"
33 const Vector3 ANGLESKEY_IDENTITY = Vector3(0, 0, 0);
35 inline void default_angles(Vector3 &angles)
37 angles = ANGLESKEY_IDENTITY;
40 inline void normalise_angles(Vector3 &angles)
42 angles[0] = static_cast<float>( float_mod(angles[0], 360));
43 angles[1] = static_cast<float>( float_mod(angles[1], 360));
44 angles[2] = static_cast<float>( float_mod(angles[2], 360));
47 inline void read_angle(Vector3 &angles, const char *value)
49 if (!string_parse_float(value, angles[2])) {
50 default_angles(angles);
54 normalise_angles(angles);
58 inline void read_angles(Vector3 &angles, const char *value)
60 if (!string_parse_vector3(value, angles)) {
61 default_angles(angles);
63 angles = Vector3(angles[2], angles[0], angles[1]);
64 normalise_angles(angles);
68 inline void write_angles(const Vector3 &angles, Entity *entity)
73 entity->setKeyValue("angle", "");
74 entity->setKeyValue("angles", "");
78 if (angles[0] == 0 && angles[1] == 0) {
79 entity->setKeyValue("angles", "");
80 write_angle(angles[2], entity);
82 sprintf(value, "%f %f %f", angles[1], angles[2], angles[0]);
83 entity->setKeyValue("angle", "");
84 entity->setKeyValue("angles", value);
89 inline Vector3 angles_rotated(const Vector3 &angles, const Quaternion &rotation)
91 return matrix4_get_rotation_euler_xyz_degrees(
92 matrix4_multiplied_by_matrix4(
93 matrix4_rotation_for_euler_xyz_degrees(angles),
94 matrix4_rotation_for_quaternion_quantised(rotation)
100 Callback<void()> m_anglesChanged;
105 AnglesKey(const Callback<void()> &anglesChanged)
106 : m_anglesChanged(anglesChanged), m_angles(ANGLESKEY_IDENTITY)
110 void angleChanged(const char *value)
112 read_angle(m_angles, value);
116 typedef MemberCaller<AnglesKey, void(const char *), &AnglesKey::angleChanged> AngleChangedCaller;
118 void anglesChanged(const char *value)
120 read_angles(m_angles, value);
124 typedef MemberCaller<AnglesKey, void(const char *), &AnglesKey::anglesChanged> AnglesChangedCaller;
126 void write(Entity *entity) const
128 write_angles(m_angles, entity);