guiImageList.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "console/consoleTypes.h"
  24. #include "console/console.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "gui/editor/guiImageList.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CONOBJECT(GuiImageList);
  29. ConsoleDocClass( GuiImageList,
  30. "@brief GUI control which displays a list of images.\n\n"
  31. "Used to be a part of an old editor system for previous Torque systems. "
  32. "Doesn't appear to be used anymore, will most likely be deprecated.\n\n"
  33. "@ingroup GuiCore\n"
  34. "@internal");
  35. GuiImageList::GuiImageList()
  36. {
  37. VECTOR_SET_ASSOCIATION(mTextures);
  38. mTextures.clear();
  39. mUniqueId = 0;
  40. }
  41. U32 GuiImageList::Insert( const char* texturePath, GFXTextureProfile *Type )
  42. {
  43. TextureEntry *t = new TextureEntry;
  44. t->TexturePath = StringTable->insert(texturePath);
  45. if ( *t->TexturePath )
  46. {
  47. t->Handle = GFXTexHandle(t->TexturePath, Type, avar("%s() - t->Handle (line %d)", __FUNCTION__, __LINE__));
  48. if ( t->Handle )
  49. {
  50. t->id = ++mUniqueId;
  51. mTextures.push_back( t );
  52. return t->id;
  53. }
  54. }
  55. // Free Texture Entry.
  56. delete t;
  57. // Return Failure.
  58. return -1;
  59. }
  60. bool GuiImageList::Clear()
  61. {
  62. while ( mTextures.size() )
  63. FreeTextureEntry( mTextures[0] );
  64. mTextures.clear();
  65. mUniqueId = 0;
  66. return true;
  67. }
  68. bool GuiImageList::FreeTextureEntry( U32 Index )
  69. {
  70. U32 Id = IndexFromId( Index );
  71. if ( Id != -1 )
  72. return FreeTextureEntry( mTextures[ Id ] );
  73. else
  74. return false;
  75. }
  76. bool GuiImageList::FreeTextureEntry( PTextureEntry Entry )
  77. {
  78. if ( ! Entry )
  79. return false;
  80. U32 id = IndexFromId( Entry->id );
  81. delete Entry;
  82. mTextures.erase ( id );
  83. return true;
  84. }
  85. U32 GuiImageList::IndexFromId ( U32 Id )
  86. {
  87. if ( !mTextures.size() ) return -1;
  88. Vector<PTextureEntry>::iterator i = mTextures.begin();
  89. U32 j = 0;
  90. for ( ; i != mTextures.end(); i++ )
  91. {
  92. if ( i )
  93. {
  94. if ( (*i)->id == Id )
  95. return j;
  96. j++;
  97. }
  98. }
  99. return -1;
  100. }
  101. U32 GuiImageList::IndexFromPath ( const char* Path )
  102. {
  103. if ( !mTextures.size() ) return -1;
  104. Vector<PTextureEntry>::iterator i = mTextures.begin();
  105. for ( ; i != mTextures.end(); i++ )
  106. {
  107. if ( dStricmp( Path, (*i)->TexturePath ) == 0 )
  108. return (*i)->id;
  109. }
  110. return -1;
  111. }
  112. void GuiImageList::initPersistFields()
  113. {
  114. Parent::initPersistFields();
  115. }
  116. DefineEngineMethod( GuiImageList, getImage, const char*, (int index),,
  117. "@brief Get a path to the texture at the specified index.\n\n"
  118. "@param index Index of the image in the list.\n"
  119. "@tsexample\n"
  120. "// Define the image index/n"
  121. "%index = \"5\";\n\n"
  122. "// Request the image path location from the control.\n"
  123. "%imagePath = %thisGuiImageList.getImage(%index);\n"
  124. "@endtsexample\n\n"
  125. "@return File path to the image map for the specified index.\n\n"
  126. "@see SimObject")
  127. {
  128. return object->GetTexturePath(index);
  129. }
  130. DefineEngineMethod(GuiImageList, clear, bool, (),,
  131. "@brief Clears the imagelist\n\n"
  132. "@tsexample\n"
  133. "// Inform the GuiImageList control to clear itself.\n"
  134. "%isFinished = %thisGuiImageList.clear();\n"
  135. "@endtsexample\n\n"
  136. "@return Returns true when finished.\n\n"
  137. "@see SimObject")
  138. {
  139. return object->Clear();
  140. }
  141. DefineEngineMethod( GuiImageList, count, S32, (),,
  142. "@brief Gets the number of images in the list.\n\n"
  143. "@tsexample\n"
  144. "// Request the number of images from the GuiImageList control.\n"
  145. "%imageCount = %thisGuiImageList.count();\n"
  146. "@endtsexample\n\n"
  147. "@return Number of images in the control.\n\n"
  148. "@see SimObject")
  149. {
  150. return object->Count();
  151. }
  152. DefineEngineMethod( GuiImageList, remove, bool, (S32 index),,
  153. "@brief Removes an image from the list by index.\n\n"
  154. "@param index Image index to remove.\n"
  155. "@tsexample\n"
  156. "// Define the image index.\n"
  157. "%imageIndex = \"4\";\n\n"
  158. "// Inform the GuiImageList control to remove the image at the defined index.\n"
  159. "%wasSuccessful = %thisGuiImageList.remove(%imageIndex);\n"
  160. "@endtsexample\n\n"
  161. "@return True if the operation was successful, false if it was not.\n\n"
  162. "@see SimObject")
  163. {
  164. return object->FreeTextureEntry( index );
  165. }
  166. DefineEngineMethod( GuiImageList, getIndex, S32, (const char* imagePath),,
  167. "@brief Retrieves the imageindex of a specified texture in the list.\n\n"
  168. "@param imagePath Imagemap including filepath of image to search for\n"
  169. "@tsexample\n"
  170. "// Define the imagemap to search for\n"
  171. "%imagePath = \"./game/client/data/images/thisImage\";\n\n"
  172. "// Request the index entry for the defined imagemap\n"
  173. "%imageIndex = %thisGuiImageList.getIndex(%imagePath);\n"
  174. "@endtsexample\n\n"
  175. "@return Index of the imagemap matching the defined image path.\n\n"
  176. "@see SimObject")
  177. {
  178. return object->IndexFromPath( imagePath );
  179. }
  180. DefineEngineMethod(GuiImageList, insert, S32, (const char* imagePath),,
  181. "@brief Insert an image into imagelist- returns the image index or -1 for failure.\n\n"
  182. "@param imagePath Imagemap, with path, to add to the list.\n"
  183. "@tsexample\n"
  184. "// Define the imagemap to add to the list\n"
  185. "%imagePath = \"./game/client/data/images/thisImage\";\n\n"
  186. "// Request the GuiImageList control to add the defined image to its list.\n"
  187. "%imageIndex = %thisGuiImageList.insert(%imagePath);\n"
  188. "@endtsexample\n\n"
  189. "@return The index of the newly inserted imagemap, or -1 if the insertion failed.\n\n"
  190. "@see SimObject")
  191. {
  192. return object->Insert( imagePath );
  193. }
  194. GFXTexHandle GuiImageList::GetTextureHandle( U32 Index )
  195. {
  196. U32 ItemIndex = IndexFromId(Index);
  197. if ( ItemIndex != -1 )
  198. return mTextures[ItemIndex]->Handle;
  199. else
  200. return NULL;
  201. }
  202. GFXTexHandle GuiImageList::GetTextureHandle( const char* TexturePath )
  203. {
  204. Vector<PTextureEntry>::iterator i = mTextures.begin();
  205. for ( ; i != mTextures.end(); i++ )
  206. {
  207. if ( dStricmp( TexturePath, (*i)->TexturePath ) == 0 )
  208. return (*i)->Handle;
  209. }
  210. return NULL;
  211. }
  212. const char *GuiImageList::GetTexturePath( U32 Index )
  213. {
  214. U32 ItemIndex = IndexFromId(Index);
  215. if ( ItemIndex != -1 )
  216. return mTextures[ItemIndex]->TexturePath;
  217. else
  218. return "";
  219. }