vmap/plugins/entity/angle.h

99 lines
2.2 KiB
C
Raw Normal View History

2020-11-17 11:16:16 +00:00
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if !defined( INCLUDED_ANGLE_H )
#define INCLUDED_ANGLE_H
#include "ientity.h"
#include "math/quaternion.h"
#include "generic/callback.h"
#include "stringio.h"
const float ANGLEKEY_IDENTITY = 0;
inline void default_angle(float &angle)
{
2021-08-04 20:23:18 +00:00
angle = ANGLEKEY_IDENTITY;
2020-11-17 11:16:16 +00:00
}
inline void normalise_angle(float &angle)
{
2021-08-04 20:23:18 +00:00
angle = static_cast<float>( float_mod(angle, 360.0));
2020-11-17 11:16:16 +00:00
}
inline void read_angle(float &angle, const char *value)
{
2021-08-04 20:23:18 +00:00
if (!string_parse_float(value, angle)) {
angle = 0;
} else {
normalise_angle(angle);
}
2020-11-17 11:16:16 +00:00
}
inline void write_angle(float angle, Entity *entity)
{
2021-08-04 20:23:18 +00:00
if (angle == 0) {
entity->setKeyValue("angle", "");
} else {
char value[64];
sprintf(value, "%f", angle);
entity->setKeyValue("angle", value);
}
2020-11-17 11:16:16 +00:00
}
class AngleKey {
2021-08-04 20:23:18 +00:00
Callback<void()> m_angleChanged;
2020-11-17 11:16:16 +00:00
public:
2021-08-04 20:23:18 +00:00
float m_angle;
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
AngleKey(const Callback<void()> &angleChanged)
: m_angleChanged(angleChanged), m_angle(ANGLEKEY_IDENTITY)
{
}
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
void angleChanged(const char *value)
{
read_angle(m_angle, value);
m_angleChanged();
}
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
typedef MemberCaller<AngleKey, void (const char *), &AngleKey::angleChanged> AngleChangedCaller;
2020-11-17 11:16:16 +00:00
2021-08-04 20:23:18 +00:00
void write(Entity *entity) const
{
write_angle(m_angle, entity);
}
2020-11-17 11:16:16 +00:00
};
inline float angle_rotated(float angle, const Quaternion &rotation)
{
2021-08-04 20:23:18 +00:00
return matrix4_get_rotation_euler_xyz_degrees(
matrix4_multiplied_by_matrix4(
matrix4_rotation_for_z_degrees(angle),
matrix4_rotation_for_quaternion_quantised(rotation)
)
).z();
2020-11-17 11:16:16 +00:00
}
#endif