]> git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/origin.h
Callback: remove fixed-arity wrappers
[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         origin = ORIGINKEY_IDENTITY;
35 }
36 inline void read_origin( Vector3& origin, const char* value ){
37         if ( !string_parse_vector3( value, origin ) ) {
38                 default_origin( origin );
39         }
40 }
41 inline void write_origin( const Vector3& origin, Entity* entity, const char* key ){
42         char value[64];
43         sprintf( value, "%f %f %f", origin[0], origin[1], origin[2] );
44         entity->setKeyValue( key, value );
45 }
46
47 inline Vector3 origin_translated( const Vector3& origin, const Vector3& translation ){
48         return matrix4_get_translation_vec3(
49                            matrix4_multiplied_by_matrix4(
50                                    matrix4_translation_for_vec3( origin ),
51                                    matrix4_translation_for_vec3( translation )
52                                    )
53                            );
54 }
55
56 inline Vector3 origin_snapped( const Vector3& origin, float snap ){
57         return vector3_snapped( origin, snap );
58 }
59
60 class OriginKey
61 {
62 Callback<void()> m_originChanged;
63 public:
64 Vector3 m_origin;
65
66
67 OriginKey( const Callback<void()>& originChanged )
68         : m_originChanged( originChanged ), m_origin( ORIGINKEY_IDENTITY ){
69 }
70
71 void originChanged( const char* value ){
72         read_origin( m_origin, value );
73         m_originChanged();
74 }
75 typedef MemberCaller<OriginKey, void(const char*), &OriginKey::originChanged> OriginChangedCaller;
76
77
78 void write( Entity* entity ) const {
79         write_origin( m_origin, entity, "origin" );
80 }
81 };
82
83
84 #include "scenelib.h"
85
86 inline BrushDoom3* Node_getBrushDoom3( scene::Node& node ){
87         return NodeTypeCast<BrushDoom3>::cast( node );
88 }
89
90 inline void BrushDoom3_setDoom3GroupOrigin( scene::Node& node, const Vector3& origin ){
91         BrushDoom3* brush = Node_getBrushDoom3( node );
92         if ( brush != 0 ) {
93                 brush->setDoom3GroupOrigin( origin );
94         }
95 }
96
97 class SetDoom3GroupOriginWalker : public scene::Traversable::Walker
98 {
99 const Vector3& m_origin;
100 public:
101 SetDoom3GroupOriginWalker( const Vector3& origin ) : m_origin( origin ){
102 }
103 bool pre( scene::Node& node ) const {
104         BrushDoom3_setDoom3GroupOrigin( node, m_origin );
105         return true;
106 }
107 };
108
109 class Doom3GroupOrigin : public scene::Traversable::Observer
110 {
111 scene::Traversable& m_set;
112 const Vector3& m_origin;
113 bool m_enabled;
114
115 public:
116 Doom3GroupOrigin( scene::Traversable& set, const Vector3& origin ) : m_set( set ), m_origin( origin ), m_enabled( false ){
117 }
118
119 void enable(){
120         m_enabled = true;
121         originChanged();
122 }
123 void disable(){
124         m_enabled = false;
125 }
126
127 void originChanged(){
128         if ( m_enabled ) {
129                 m_set.traverse( SetDoom3GroupOriginWalker( m_origin ) );
130         }
131 }
132
133 void insert( scene::Node& node ){
134         if ( m_enabled ) {
135                 BrushDoom3_setDoom3GroupOrigin( node, m_origin );
136         }
137 }
138 void erase( scene::Node& node ){
139         if ( m_enabled ) {
140                 BrushDoom3_setDoom3GroupOrigin( node, Vector3( 0, 0, 0 ) );
141         }
142 }
143 };
144
145
146 #endif