Torque3D Documentation / _generateds / editorIconRegistry.cpp

editorIconRegistry.cpp

Engine/source/gui/worldEditor/editorIconRegistry.cpp

More...

Public Variables

The global registry of editor icons.

Public Functions

ConsoleDoc("@class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">EditorIconRegistry\n</a>" "@brief This class is used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/talgorithm_8h/#talgorithm_8h_1a113846f47aa4d2409fe12e783dcd69cf">find</a> the correct icon <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> path <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> different <a href="/coding/class/classsimobject/">SimObject</a> class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">types.\n\n</a>" "It is typically used by the editors, not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> actual game <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">development\n\n</a>" " @internal" )
ConsoleStaticMethod(EditorIconRegistry , add , void , 3 , 4 , "( String className, <a href="/coding/class/classstring/">String</a> imageFile [, bool overwrite = true] )" "@internal" )
ConsoleStaticMethod(EditorIconRegistry , clear , void , 1 , 1 , "" "@internal" )
ConsoleStaticMethod(EditorIconRegistry , findIconByClassName , const char * , 2 , 2 , "( String className )\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "Returns the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the icon <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a2594a51175f310ed96ad6cd7d6514878">if</a> found." "@internal" )
ConsoleStaticMethod(EditorIconRegistry , findIconBySimObject , const char * , 2 , 2 , "( SimObject )\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "Returns the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the icon <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a2594a51175f310ed96ad6cd7d6514878">if</a> found." "@internal" )
ConsoleStaticMethod(EditorIconRegistry , loadFromPath , void , 2 , 3 , "( String imagePath [, bool overwrite = true] )" "@internal" )

Detailed Description

Public Variables

EditorIconRegistry gEditorIcons 

The global registry of editor icons.

Public Functions

ConsoleDoc("@class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">EditorIconRegistry\n</a>" "@brief This class is used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/talgorithm_8h/#talgorithm_8h_1a113846f47aa4d2409fe12e783dcd69cf">find</a> the correct icon <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> path <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> different <a href="/coding/class/classsimobject/">SimObject</a> class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">types.\n\n</a>" "It is typically used by the editors, not intended <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> actual game <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">development\n\n</a>" " @internal" )

ConsoleStaticMethod(EditorIconRegistry , add , void , 3 , 4 , "( String className, <a href="/coding/class/classstring/">String</a> imageFile [, bool overwrite = true] )" "@internal" )

ConsoleStaticMethod(EditorIconRegistry , clear , void , 1 , 1 , "" "@internal" )

ConsoleStaticMethod(EditorIconRegistry , findIconByClassName , const char * , 2 , 2 , "( String className )\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "Returns the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the icon <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a2594a51175f310ed96ad6cd7d6514878">if</a> found." "@internal" )

ConsoleStaticMethod(EditorIconRegistry , findIconBySimObject , const char * , 2 , 2 , "( SimObject )\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "Returns the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> path <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the icon <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a2594a51175f310ed96ad6cd7d6514878">if</a> found." "@internal" )

ConsoleStaticMethod(EditorIconRegistry , loadFromPath , void , 2 , 3 , "( String imagePath [, bool overwrite = true] )" "@internal" )

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