]> git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/origin.h
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / entity / origin.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_ORIGIN_H )
23 #define INCLUDED_ORIGIN_H
24
25 #include "ientity.h"
26
27 #include "math/matrix.h"
28 #include "generic/callback.h"
29 #include "stringio.h"
30
31 const Vector3 ORIGINKEY_IDENTITY = Vector3(0, 0, 0);
32
33 inline void default_origin(Vector3 &origin)
34 {
35     origin = ORIGINKEY_IDENTITY;
36 }
37
38 inline void read_origin(Vector3 &origin, const char *value)
39 {
40     if (!string_parse_vector3(value, origin)) {
41         default_origin(origin);
42     }
43 }
44
45 inline void write_origin(const Vector3 &origin, Entity *entity, const char *key)
46 {
47     char value[64];
48     sprintf(value, "%f %f %f", origin[0], origin[1], origin[2]);
49     entity->setKeyValue(key, value);
50 }
51
52 inline Vector3 origin_translated(const Vector3 &origin, const Vector3 &translation)
53 {
54     return matrix4_get_translation_vec3(
55             matrix4_multiplied_by_matrix4(
56                     matrix4_translation_for_vec3(origin),
57                     matrix4_translation_for_vec3(translation)
58             )
59     );
60 }
61
62 inline Vector3 origin_snapped(const Vector3 &origin, float snap)
63 {
64     return vector3_snapped(origin, snap);
65 }
66
67 class OriginKey {
68     Callback<void()> m_originChanged;
69 public:
70     Vector3 m_origin;
71
72
73     OriginKey(const Callback<void()> &originChanged)
74             : m_originChanged(originChanged), m_origin(ORIGINKEY_IDENTITY)
75     {
76     }
77
78     void originChanged(const char *value)
79     {
80         read_origin(m_origin, value);
81         m_originChanged();
82     }
83
84     typedef MemberCaller<OriginKey, void(const char *), &OriginKey::originChanged> OriginChangedCaller;
85
86
87     void write(Entity *entity) const
88     {
89         write_origin(m_origin, entity, "origin");
90     }
91 };
92
93
94 #include "scenelib.h"
95
96 inline BrushDoom3 *Node_getBrushDoom3(scene::Node &node)
97 {
98     return NodeTypeCast<BrushDoom3>::cast(node);
99 }
100
101 inline void BrushDoom3_setDoom3GroupOrigin(scene::Node &node, const Vector3 &origin)
102 {
103     BrushDoom3 *brush = Node_getBrushDoom3(node);
104     if (brush != 0) {
105         brush->setDoom3GroupOrigin(origin);
106     }
107 }
108
109 class SetDoom3GroupOriginWalker : public scene::Traversable::Walker {
110     const Vector3 &m_origin;
111 public:
112     SetDoom3GroupOriginWalker(const Vector3 &origin) : m_origin(origin)
113     {
114     }
115
116     bool pre(scene::Node &node) const
117     {
118         BrushDoom3_setDoom3GroupOrigin(node, m_origin);
119         return true;
120     }
121 };
122
123 class Doom3GroupOrigin : public scene::Traversable::Observer {
124     scene::Traversable &m_set;
125     const Vector3 &m_origin;
126     bool m_enabled;
127
128 public:
129     Doom3GroupOrigin(scene::Traversable &set, const Vector3 &origin) : m_set(set), m_origin(origin), m_enabled(false)
130     {
131     }
132
133     void enable()
134     {
135         m_enabled = true;
136         originChanged();
137     }
138
139     void disable()
140     {
141         m_enabled = false;
142     }
143
144     void originChanged()
145     {
146         if (m_enabled) {
147             m_set.traverse(SetDoom3GroupOriginWalker(m_origin));
148         }
149     }
150
151     void insert(scene::Node &node)
152     {
153         if (m_enabled) {
154             BrushDoom3_setDoom3GroupOrigin(node, m_origin);
155         }
156     }
157
158     void erase(scene::Node &node)
159     {
160         if (m_enabled) {
161             BrushDoom3_setDoom3GroupOrigin(node, Vector3(0, 0, 0));
162         }
163     }
164 };
165
166
167 #endif