]> git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/model.h
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     ResourceReference m_resource;
34     scene::Traversable &m_traverse;
35     scene::Node *m_node;
36     Callback<void()> m_modelChanged;
37
38 public:
39     EModel(scene::Traversable &traversable, const Callback<void()> &modelChanged)
40             : m_resource(""), m_traverse(traversable), m_node(0), m_modelChanged(modelChanged)
41     {
42         m_resource.attach(*this);
43     }
44
45     ~EModel()
46     {
47         m_resource.detach(*this);
48     }
49
50     void realise()
51     {
52         m_resource.get()->load();
53         m_node = m_resource.get()->getNode();
54         if (m_node != 0) {
55             m_traverse.insert(*m_node);
56         }
57     }
58
59     void unrealise()
60     {
61         if (m_node != 0) {
62             m_traverse.erase(*m_node);
63         }
64     }
65
66     void modelChanged(const char *value)
67     {
68         StringOutputStream cleaned(string_length(value));
69         cleaned << PathCleaned(value);
70         m_resource.detach(*this);
71         m_resource.setName(cleaned.c_str());
72         m_resource.attach(*this);
73         m_modelChanged();
74     }
75
76     typedef MemberCaller<EModel, void(const char *), &EModel::modelChanged> ModelChangedCaller;
77
78     const char *getName() const
79     {
80         return m_resource.getName();
81     }
82
83     scene::Node *getNode() const
84     {
85         return m_node;
86     }
87 };
88
89 class SingletonModel {
90     TraversableNode m_traverse;
91     EModel m_model;
92 public:
93     SingletonModel()
94             : m_model(m_traverse, Callback<void()>())
95     {
96     }
97
98     void attach(scene::Traversable::Observer *observer)
99     {
100         m_traverse.attach(observer);
101     }
102
103     void detach(scene::Traversable::Observer *observer)
104     {
105         m_traverse.detach(observer);
106     }
107
108     scene::Traversable &getTraversable()
109     {
110         return m_traverse;
111     }
112
113     void modelChanged(const char *value)
114     {
115         m_model.modelChanged(value);
116     }
117
118     typedef MemberCaller<SingletonModel, void(const char *), &SingletonModel::modelChanged> ModelChangedCaller;
119
120     scene::Node *getNode() const
121     {
122         return m_model.getNode();
123     }
124 };
125
126 #endif