00001 /* 00002 Copyright (C) 2001-2006, William Joseph. 00003 All Rights Reserved. 00004 00005 This file is part of GtkRadiant. 00006 00007 GtkRadiant is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 GtkRadiant is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with GtkRadiant; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #if !defined(INCLUDED_MODEL_H) 00023 #define INCLUDED_MODEL_H 00024 00025 #include "entitylib.h" 00026 #include "traverselib.h" 00027 #include "generic/callback.h" 00028 #include "stream/stringstream.h" 00029 #include "os/path.h" 00030 #include "moduleobserver.h" 00031 00032 class EModel: public ModuleObserver 00033 { 00034 ResourceReference m_resource; 00035 scene::Traversable& m_traverse; 00036 scene::Node* m_node; 00037 Callback m_modelChanged; 00038 00039 public: 00040 EModel (scene::Traversable& traversable, const Callback& modelChanged) : 00041 m_resource(""), m_traverse(traversable), m_node(0), m_modelChanged(modelChanged) 00042 { 00043 m_resource.attach(*this); 00044 } 00045 ~EModel () 00046 { 00047 m_resource.detach(*this); 00048 } 00049 00050 void realise () 00051 { 00052 m_resource.get()->load(); 00053 m_node = m_resource.get()->getNode(); 00054 if (m_node != 0) { 00055 m_traverse.insert(*m_node); 00056 } 00057 } 00058 void unrealise () 00059 { 00060 if (m_node != 0) { 00061 m_traverse.erase(*m_node); 00062 } 00063 } 00064 00065 void modelChanged (const std::string& value) 00066 { 00067 std::string cleaned = os::standardPath(value); 00068 m_resource.detach(*this); 00069 m_resource.setName(cleaned.c_str()); 00070 m_resource.attach(*this); 00071 m_modelChanged(); 00072 } 00073 typedef MemberCaller1<EModel, const std::string&, &EModel::modelChanged> ModelChangedCaller; 00074 00075 const std::string& getName () const 00076 { 00077 return m_resource.getName(); 00078 } 00079 scene::Node* getNode () const 00080 { 00081 return m_node; 00082 } 00083 }; 00084 00085 class SingletonModel 00086 { 00087 TraversableNode m_traverse; 00088 EModel m_model; 00089 public: 00090 SingletonModel () : 00091 m_model(m_traverse, Callback()) 00092 { 00093 } 00094 00095 void attach (scene::Traversable::Observer* observer) 00096 { 00097 m_traverse.attach(observer); 00098 } 00099 void detach (scene::Traversable::Observer* observer) 00100 { 00101 m_traverse.detach(observer); 00102 } 00103 00104 scene::Traversable& getTraversable () 00105 { 00106 return m_traverse; 00107 } 00108 00109 void modelChanged (const std::string& value) 00110 { 00111 m_model.modelChanged(value); 00112 } 00113 typedef MemberCaller1<SingletonModel, const std::string&, &SingletonModel::modelChanged> ModelChangedCaller; 00114 00115 scene::Node* getNode () const 00116 { 00117 return m_model.getNode(); 00118 } 00119 }; 00120 00121 #endif