]> icculus.org git repositories - divverent/netradiant.git/blob - plugins/entity/angle.h
support misc_*model as misc_model in the radiant editor (way more convenient)
[divverent/netradiant.git] / plugins / entity / angle.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #if !defined(INCLUDED_ANGLE_H)
23 #define INCLUDED_ANGLE_H
24
25 #include "ientity.h"
26
27 #include "math/quaternion.h"
28 #include "generic/callback.h"
29 #include "stringio.h"
30
31 const float ANGLEKEY_IDENTITY = 0;
32
33 inline void default_angle(float& angle)
34 {
35   angle = ANGLEKEY_IDENTITY;
36 }
37 inline void normalise_angle(float& angle)
38 {
39   angle = static_cast<float>(float_mod(angle, 360.0));
40 }
41 inline void read_angle(float& angle, const char* value)
42 {
43   if(!string_parse_float(value, angle))
44   {
45     angle = 0;
46   }
47   else
48   {
49     normalise_angle(angle);
50   }
51 }
52 inline void write_angle(float angle, Entity* entity)
53 {
54   if(angle == 0)
55   {
56     entity->setKeyValue("angle", "");
57   }
58   else
59   {
60     char value[64];
61     sprintf(value, "%f", angle);
62     entity->setKeyValue("angle", value);
63   }
64 }
65
66 class AngleKey
67 {
68   Callback m_angleChanged;
69 public:
70   float m_angle;
71
72
73   AngleKey(const Callback& angleChanged)
74     : m_angleChanged(angleChanged), m_angle(ANGLEKEY_IDENTITY)
75   {
76   }
77
78   void angleChanged(const char* value)
79   {
80     read_angle(m_angle, value);
81     m_angleChanged();
82   }
83   typedef MemberCaller1<AngleKey, const char*, &AngleKey::angleChanged> AngleChangedCaller;
84
85   void write(Entity* entity) const
86   {
87     write_angle(m_angle, entity);
88   }
89 };
90
91 inline float angle_rotated(float angle, const Quaternion& rotation)
92 {
93   return matrix4_get_rotation_euler_xyz_degrees(
94     matrix4_multiplied_by_matrix4(
95       matrix4_rotation_for_z_degrees(angle),
96       matrix4_rotation_for_quaternion_quantised(rotation)
97     )
98   ).z();
99 }
100
101 #endif