guiImageList.cpp 7.2 KB

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