angle.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined(INCLUDED_ANGLE_H)
  18. #define INCLUDED_ANGLE_H
  19. #include "ientity.h"
  20. #include "math/quaternion.h"
  21. #include "generic/callback.h"
  22. #include "stringio.h"
  23. const float ANGLEKEY_IDENTITY = 0;
  24. inline void default_angle(float& angle)
  25. {
  26. angle = ANGLEKEY_IDENTITY;
  27. }
  28. inline void normalise_angle(float& angle)
  29. {
  30. angle = static_cast<float>(float_mod(angle, 360.0));
  31. }
  32. inline void read_angle(float& angle, const char* value)
  33. {
  34. if(!string_parse_float(value, angle))
  35. {
  36. angle = 0;
  37. }
  38. else
  39. {
  40. normalise_angle(angle);
  41. }
  42. }
  43. inline void write_angle(float angle, Entity* entity)
  44. {
  45. if(angle == 0)
  46. {
  47. entity->setKeyValue("angle", "");
  48. }
  49. else
  50. {
  51. char value[64];
  52. sprintf(value, "%g", angle);
  53. entity->setKeyValue("angle", value);
  54. }
  55. }
  56. class AngleKey
  57. {
  58. Callback m_angleChanged;
  59. public:
  60. float m_angle;
  61. AngleKey(const Callback& angleChanged)
  62. : m_angleChanged(angleChanged), m_angle(ANGLEKEY_IDENTITY)
  63. {
  64. }
  65. void angleChanged(const char* value)
  66. {
  67. read_angle(m_angle, value);
  68. m_angleChanged();
  69. }
  70. typedef MemberCaller1<AngleKey, const char*, &AngleKey::angleChanged> AngleChangedCaller;
  71. void write(Entity* entity) const
  72. {
  73. write_angle(m_angle, entity);
  74. }
  75. };
  76. inline float angle_rotated(float angle, const Quaternion& rotation)
  77. {
  78. return matrix4_get_rotation_euler_xyz_degrees(
  79. matrix4_multiplied_by_matrix4(
  80. matrix4_rotation_for_z_degrees(angle),
  81. matrix4_rotation_for_quaternion_quantised(rotation)
  82. )
  83. ).z();
  84. }
  85. #endif