modelskinkey.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (C) 2001-2006, William Joseph.
  3. All Rights Reserved.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #if !defined(INCLUDED_MODELSKINKEY_H)
  18. #define INCLUDED_MODELSKINKEY_H
  19. #include "modelskin.h"
  20. #include "os/path.h"
  21. #include "stream/stringstream.h"
  22. #include "moduleobserver.h"
  23. #include "entitylib.h"
  24. #include "traverselib.h"
  25. inline void parseTextureName(CopiedString& name, const char* token)
  26. {
  27. StringOutputStream cleaned(256);
  28. cleaned << PathCleaned(token);
  29. name = CopiedString(cleaned.c_str(), path_get_filename_base_end(cleaned.c_str())); // remove extension
  30. }
  31. class ModelSkinKey : public ModuleObserver
  32. {
  33. CopiedString m_name;
  34. ModelSkin* m_skin;
  35. Callback m_skinChangedCallback;
  36. ModelSkinKey(const ModelSkinKey&);
  37. ModelSkinKey operator=(const ModelSkinKey&);
  38. void construct()
  39. {
  40. m_skin = &GlobalModelSkinCache().capture(m_name.c_str());
  41. m_skin->attach(*this);
  42. }
  43. void destroy()
  44. {
  45. m_skin->detach(*this);
  46. GlobalModelSkinCache().release(m_name.c_str());
  47. }
  48. public:
  49. ModelSkinKey(const Callback& skinChangedCallback) : m_skinChangedCallback(skinChangedCallback)
  50. {
  51. construct();
  52. }
  53. ~ModelSkinKey()
  54. {
  55. destroy();
  56. }
  57. ModelSkin& get() const
  58. {
  59. return *m_skin;
  60. }
  61. void skinChanged(const char* value)
  62. {
  63. destroy();
  64. parseTextureName(m_name, value);
  65. construct();
  66. }
  67. typedef MemberCaller1<ModelSkinKey, const char*, &ModelSkinKey::skinChanged> SkinChangedCaller;
  68. void realise()
  69. {
  70. m_skinChangedCallback();
  71. }
  72. void unrealise()
  73. {
  74. }
  75. };
  76. class InstanceSkinChanged : public scene::Instantiable::Visitor
  77. {
  78. public:
  79. void visit(scene::Instance& instance) const
  80. {
  81. //\todo don't do this for instances that are not children of the entity setting the skin
  82. SkinnedModel* skinned = InstanceTypeCast<SkinnedModel>::cast(instance);
  83. if(skinned != 0)
  84. {
  85. skinned->skinChanged();
  86. }
  87. }
  88. };
  89. inline void Node_modelSkinChanged(scene::Node& node)
  90. {
  91. scene::Instantiable* instantiable = Node_getInstantiable(node);
  92. ASSERT_NOTNULL(instantiable);
  93. instantiable->forEachInstance(InstanceSkinChanged());
  94. }
  95. #endif