]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/model.h
Revert partially (auto) "reformat code! now the code is only ugly on the *inside*"
[xonotic/netradiant.git] / plugins / entity / model.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_MODEL_H )
23 #define INCLUDED_MODEL_H
24
25 #include "entitylib.h"
26 #include "traverselib.h"
27 #include "generic/callback.h"
28 #include "stream/stringstream.h"
29 #include "os/path.h"
30 #include "moduleobserver.h"
31
32 class EModel : public ModuleObserver
33 {
34 ResourceReference m_resource;
35 scene::Traversable& m_traverse;
36 scene::Node* m_node;
37 Callback<void()> m_modelChanged;
38
39 public:
40 EModel( scene::Traversable& traversable, const Callback<void()>& modelChanged )
41         : m_resource( "" ), m_traverse( traversable ), m_node( 0 ), m_modelChanged( modelChanged ){
42         m_resource.attach( *this );
43 }
44 ~EModel(){
45         m_resource.detach( *this );
46 }
47
48 void realise(){
49         m_resource.get()->load();
50         m_node = m_resource.get()->getNode();
51         if ( m_node != 0 ) {
52                 m_traverse.insert( *m_node );
53         }
54 }
55 void unrealise(){
56         if ( m_node != 0 ) {
57                 m_traverse.erase( *m_node );
58         }
59 }
60
61 void modelChanged( const char* value ){
62         StringOutputStream cleaned( string_length( value ) );
63         cleaned << PathCleaned( value );
64         m_resource.detach( *this );
65         m_resource.setName( cleaned.c_str() );
66         m_resource.attach( *this );
67         m_modelChanged();
68 }
69 typedef MemberCaller<EModel, void(const char*), &EModel::modelChanged> ModelChangedCaller;
70
71 const char* getName() const {
72         return m_resource.getName();
73 }
74 scene::Node* getNode() const {
75         return m_node;
76 }
77 };
78
79 class SingletonModel
80 {
81 TraversableNode m_traverse;
82 EModel m_model;
83 public:
84 SingletonModel()
85         : m_model( m_traverse, Callback<void()>() ){
86 }
87
88 void attach( scene::Traversable::Observer* observer ){
89         m_traverse.attach( observer );
90 }
91 void detach( scene::Traversable::Observer* observer ){
92         m_traverse.detach( observer );
93 }
94
95 scene::Traversable& getTraversable(){
96         return m_traverse;
97 }
98
99 void modelChanged( const char* value ){
100         m_model.modelChanged( value );
101 }
102 typedef MemberCaller<SingletonModel, void(const char*), &SingletonModel::modelChanged> ModelChangedCaller;
103
104 scene::Node* getNode() const {
105         return m_model.getNode();
106 }
107 };
108
109 #endif