| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_MODELSKINKEY_H ) |
| 23 |
#define INCLUDED_MODELSKINKEY_H |
| 24 |
|
| 25 |
#include "modelskin.h" |
| 26 |
|
| 27 |
#include "os/path.h" |
| 28 |
#include "stream/stringstream.h" |
| 29 |
#include "moduleobserver.h" |
| 30 |
#include "entitylib.h" |
| 31 |
#include "traverselib.h" |
| 32 |
|
| 33 |
inline void parseTextureName( CopiedString& name, const char* token ){ |
| 34 |
StringOutputStream cleaned( 256 ); |
| 35 |
cleaned << PathCleaned( token ); |
| 36 |
name = StringRange( cleaned.c_str(), path_get_filename_base_end( cleaned.c_str() ) ); |
| 37 |
} |
| 38 |
|
| 39 |
class ModelSkinKey : public ModuleObserver |
| 40 |
{ |
| 41 |
CopiedString m_name; |
| 42 |
ModelSkin* m_skin; |
| 43 |
Callback<void()> m_skinChangedCallback; |
| 44 |
|
| 45 |
ModelSkinKey( const ModelSkinKey& ); |
| 46 |
ModelSkinKey operator=( const ModelSkinKey& ); |
| 47 |
|
| 48 |
void construct(){ |
| 49 |
m_skin = &GlobalModelSkinCache().capture( m_name.c_str() ); |
| 50 |
m_skin->attach( *this ); |
| 51 |
} |
| 52 |
void destroy(){ |
| 53 |
m_skin->detach( *this ); |
| 54 |
GlobalModelSkinCache().release( m_name.c_str() ); |
| 55 |
} |
| 56 |
|
| 57 |
public: |
| 58 |
ModelSkinKey( const Callback<void()>& skinChangedCallback ) : m_skinChangedCallback( skinChangedCallback ){ |
| 59 |
construct(); |
| 60 |
} |
| 61 |
~ModelSkinKey(){ |
| 62 |
destroy(); |
| 63 |
} |
| 64 |
ModelSkin& get() const { |
| 65 |
return *m_skin; |
| 66 |
} |
| 67 |
void skinChanged( const char* value ){ |
| 68 |
destroy(); |
| 69 |
parseTextureName( m_name, value ); |
| 70 |
construct(); |
| 71 |
} |
| 72 |
typedef MemberCaller<ModelSkinKey, void(const char*), &ModelSkinKey::skinChanged> SkinChangedCaller; |
| 73 |
|
| 74 |
void realise(){ |
| 75 |
m_skinChangedCallback(); |
| 76 |
} |
| 77 |
void unrealise(){ |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
class InstanceSkinChanged : public scene::Instantiable::Visitor |
| 82 |
{ |
| 83 |
public: |
| 84 |
void visit( scene::Instance& instance ) const { |
| 85 |
|
| 86 |
SkinnedModel* skinned = InstanceTypeCast<SkinnedModel>::cast( instance ); |
| 87 |
if ( skinned != 0 ) { |
| 88 |
skinned->skinChanged(); |
| 89 |
} |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
inline void Node_modelSkinChanged( scene::Node& node ){ |
| 94 |
scene::Instantiable* instantiable = Node_getInstantiable( node ); |
| 95 |
ASSERT_NOTNULL( instantiable ); |
| 96 |
instantiable->forEachInstance( InstanceSkinChanged() ); |
| 97 |
} |
| 98 |
|
| 99 |
#endif |