editorIconRegistry.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/worldEditor/editorIconRegistry.h"
  24. #include "console/engineAPI.h"
  25. #include "console/simBase.h"
  26. #include "gui/core/guiTypes.h"
  27. EditorIconRegistry gEditorIcons;
  28. ConsoleDoc(
  29. "@class EditorIconRegistry\n"
  30. "@brief This class is used to find the correct icon file path for different SimObject class types.\n\n"
  31. "It is typically used by the editors, not intended for actual game development\n\n"
  32. "@internal"
  33. );
  34. IMPLEMENT_STATIC_CLASS(EditorIconRegistry,, "");
  35. EditorIconRegistry::EditorIconRegistry()
  36. {
  37. }
  38. EditorIconRegistry::~EditorIconRegistry()
  39. {
  40. clear();
  41. }
  42. void EditorIconRegistry::loadFromPath( const String &path, bool overwrite )
  43. {
  44. AbstractClassRep *classRep = AbstractClassRep::getClassList();
  45. while ( classRep )
  46. {
  47. String iconFile = String::ToString( "%s%s", path.c_str(), classRep->getClassName() );
  48. add( classRep->getClassName(), iconFile.c_str(), overwrite );
  49. classRep = classRep->getNextClass();
  50. }
  51. String defaultIconFile = path + "default";
  52. mDefaultIcon.set( defaultIconFile,
  53. &GFXDefaultGUIProfile,
  54. avar("%s() - mIcons[] (line %d)",
  55. __FUNCTION__, __LINE__) );
  56. }
  57. void EditorIconRegistry::add( const String &className, const String &imageFile, bool overwrite )
  58. {
  59. // First see if we can load the image.
  60. GFXTexHandle icon( imageFile, &GFXDefaultGUIProfile,
  61. avar("%s() - mIcons[] (line %d)", __FUNCTION__, __LINE__) );
  62. if ( icon.isNull() )
  63. return;
  64. // Look it up in the map.
  65. StringNoCase key( className );
  66. IconMap::Iterator iter = mIcons.find( key );
  67. if ( iter != mIcons.end() )
  68. {
  69. if ( !overwrite )
  70. return;
  71. iter->value = icon;
  72. }
  73. else
  74. mIcons.insertUnique( key, icon );
  75. }
  76. GFXTexHandle EditorIconRegistry::findIcon( AbstractClassRep *classRep )
  77. {
  78. while ( classRep )
  79. {
  80. StringNoCase key( classRep->getClassName() );
  81. IconMap::Iterator icon = mIcons.find( key );
  82. if ( icon != mIcons.end() && icon->value.isValid() )
  83. return icon->value;
  84. classRep = classRep->getParentClass();
  85. }
  86. return mDefaultIcon;
  87. }
  88. GFXTexHandle EditorIconRegistry::findIcon( const SimObject *object )
  89. {
  90. if( object == NULL )
  91. return mDefaultIcon;
  92. AbstractClassRep *classRep = object->getClassRep();
  93. return findIcon( classRep );
  94. }
  95. GFXTexHandle EditorIconRegistry::findIcon( const char *className )
  96. {
  97. // On the chance we have this className already in the map,
  98. // check there first because its a lot faster...
  99. StringNoCase key( className );
  100. IconMap::Iterator icon = mIcons.find( key );
  101. if ( icon != mIcons.end() && icon->value.isValid() )
  102. return icon->value;
  103. // Well, we could still have an icon for a parent class,
  104. // so find the AbstractClassRep for the className.
  105. //
  106. // Unfortunately the only way to do this is looping through
  107. // the AbstractClassRep linked list.
  108. bool found = false;
  109. AbstractClassRep* pClassRep = AbstractClassRep::getClassList();
  110. while ( pClassRep )
  111. {
  112. if ( key.equal( pClassRep->getClassName(), String::NoCase ) )
  113. {
  114. found = true;
  115. break;
  116. }
  117. pClassRep = pClassRep->getNextClass();
  118. }
  119. if ( !found )
  120. {
  121. Con::errorf( "EditorIconRegistry::findIcon, passed className %s was not an AbstractClassRep!", key.c_str() );
  122. return mDefaultIcon;
  123. }
  124. // Now do a find by AbstractClassRep recursively up the class tree...
  125. return findIcon( pClassRep );
  126. }
  127. bool EditorIconRegistry::hasIconNoRecurse( const SimObject *object )
  128. {
  129. AbstractClassRep *classRep = object->getClassRep();
  130. StringNoCase key( classRep->getClassName() );
  131. IconMap::Iterator icon = mIcons.find( key );
  132. return icon != mIcons.end();
  133. }
  134. void EditorIconRegistry::clear()
  135. {
  136. mIcons.clear();
  137. mDefaultIcon.free();
  138. }
  139. DefineEngineStaticMethod( EditorIconRegistry, add, void, (String className, String imageFile, bool overwrite), (true),
  140. "@internal")
  141. {
  142. gEditorIcons.add( className, imageFile, overwrite );
  143. }
  144. DefineEngineStaticMethod( EditorIconRegistry, loadFromPath, void, (String imagePath, bool overwrite), (true),
  145. "@internal")
  146. {
  147. gEditorIcons.loadFromPath( imagePath, overwrite );
  148. }
  149. DefineEngineStaticMethod( EditorIconRegistry, clear, void, (),,
  150. "@internal")
  151. {
  152. gEditorIcons.clear();
  153. }
  154. DefineEngineStaticMethod( EditorIconRegistry, findIconByClassName, const char*, (String className),,
  155. "@brief Returns the file path to the icon file if found."
  156. "@internal")
  157. {
  158. GFXTexHandle icon = gEditorIcons.findIcon( className );
  159. if ( icon.isNull() )
  160. return NULL;
  161. return icon->mPath;
  162. }
  163. DefineEngineStaticMethod( EditorIconRegistry, findIconBySimObject, const char*, (SimObject* obj),,
  164. "Returns the file path to the icon file if found."
  165. "@internal")
  166. {
  167. if ( !obj )
  168. {
  169. Con::warnf( "EditorIconRegistry::findIcon, parameter was not a SimObject!");
  170. return NULL;
  171. }
  172. GFXTexHandle icon = gEditorIcons.findIcon( obj );
  173. if ( icon.isNull() )
  174. return NULL;
  175. return icon->mPath;
  176. }