scale.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_SCALE_H)
  18. #define INCLUDED_SCALE_H
  19. #include "ientity.h"
  20. #include "math/matrix.h"
  21. #include "generic/callback.h"
  22. #include "stringio.h"
  23. const Vector3 SCALEKEY_IDENTITY = Vector3(1, 1, 1);
  24. inline void default_scale(Vector3& scale)
  25. {
  26. scale = SCALEKEY_IDENTITY;
  27. }
  28. inline void read_scale(Vector3& scalevec, const char* value)
  29. {
  30. float scale;
  31. if(!string_parse_float(value, scale)
  32. || scale == 0)
  33. {
  34. default_scale(scalevec);
  35. }
  36. else
  37. {
  38. scalevec = Vector3(scale, scale, scale);
  39. }
  40. }
  41. inline void read_scalevec(Vector3& scale, const char* value)
  42. {
  43. if(!string_parse_vector3(value, scale)
  44. || scale[0] == 0
  45. || scale[1] == 0
  46. || scale[2] == 0)
  47. default_scale(scale);
  48. }
  49. inline void write_scale(const Vector3& scale, Entity* entity)
  50. {
  51. if(scale[0] == 1 && scale[1] == 1 && scale[2] == 1)
  52. {
  53. entity->setKeyValue("modelscale", "");
  54. entity->setKeyValue("modelscale_vec", "");
  55. }
  56. else
  57. {
  58. char value[64];
  59. if(scale[0] == scale[1] && scale[0] == scale[2])
  60. {
  61. sprintf(value, "%g", scale[0]);
  62. entity->setKeyValue("modelscale_vec", "");
  63. entity->setKeyValue("modelscale", value);
  64. }
  65. else
  66. {
  67. sprintf(value, "%g %g %g", scale[0], scale[1], scale[2]);
  68. entity->setKeyValue("modelscale", "");
  69. entity->setKeyValue("modelscale_vec", value);
  70. }
  71. }
  72. }
  73. inline Vector3 scale_scaled(const Vector3& scale, const Vector3& scaling)
  74. {
  75. return matrix4_get_scale_vec3(
  76. matrix4_multiplied_by_matrix4(
  77. matrix4_scale_for_vec3(scale),
  78. matrix4_scale_for_vec3(scaling)
  79. )
  80. );
  81. }
  82. class ScaleKey
  83. {
  84. Callback m_scaleChanged;
  85. public:
  86. Vector3 m_scale;
  87. ScaleKey(const Callback& scaleChanged)
  88. : m_scaleChanged(scaleChanged), m_scale(SCALEKEY_IDENTITY)
  89. {
  90. }
  91. void uniformScaleChanged(const char* value)
  92. {
  93. read_scale(m_scale, value);
  94. m_scaleChanged();
  95. }
  96. typedef MemberCaller1<ScaleKey, const char*, &ScaleKey::uniformScaleChanged> UniformScaleChangedCaller;
  97. void scaleChanged(const char* value)
  98. {
  99. read_scalevec(m_scale, value);
  100. m_scaleChanged();
  101. }
  102. typedef MemberCaller1<ScaleKey, const char*, &ScaleKey::scaleChanged> ScaleChangedCaller;
  103. void write(Entity* entity) const
  104. {
  105. write_scale(m_scale, entity);
  106. }
  107. };
  108. #endif