guiImageList.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "graphics/dgl.h"
  26. #include "gui/editor/guiImageList.h"
  27. GuiImageList::GuiImageList()
  28. {
  29. VECTOR_SET_ASSOCIATION(mTextures);
  30. mTextures.clear();
  31. mUniqueId = 0;
  32. }
  33. U32 GuiImageList::Insert( const char* texturePath , TextureHandle::TextureHandleType type )
  34. {
  35. // Sanity!
  36. AssertISV( type != TextureHandle::InvalidTexture, "Invalid texture type." );
  37. TextureEntry *t = new TextureEntry;
  38. if ( ! t ) return -1;
  39. t->TexturePath = StringTable->insert(texturePath);
  40. if ( *t->TexturePath )
  41. {
  42. t->Handle = TextureHandle(t->TexturePath, type);
  43. if ( t->Handle )
  44. {
  45. t->Object = (TextureObject *) t->Handle;
  46. if(t->Object == NULL)
  47. {
  48. return -1;
  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. ConsoleMethod(GuiImageList, getImage,const char *, 3, 3, "(int index) Get a path to the texture at the specified index\n"
  117. "@param index The index of the desired image\n"
  118. "@return The ID of the image or -1 on failure")
  119. {
  120. return object->GetTexturePath(dAtoi(argv[2]));
  121. }
  122. ConsoleMethod(GuiImageList, clear,bool, 2, 2, "() Clears the imagelist\n"
  123. "@return Returns true on success, and false otherwise")
  124. {
  125. return object->Clear();
  126. }
  127. ConsoleMethod(GuiImageList, count,S32, 2, 2, "Gets the number of images in the list\n"
  128. "@return Return number of images as an integer")
  129. {
  130. return object->Count();
  131. }
  132. ConsoleMethod(GuiImageList, remove, bool, 3,3, "(index) Removes an image from the list by index\n"
  133. "@param index The index of the image to remove\n"
  134. "@return Returns true on success, false otherwise")
  135. {
  136. return object->FreeTextureEntry( dAtoi(argv[2] ) );
  137. }
  138. ConsoleMethod(GuiImageList, getIndex, S32, 3,3, "(char* path) Retrieves the imageindex of a specified texture in the list\n"
  139. "@param path Thge path of the image file to retrieve the indexs of\n"
  140. "@return The index of the image as an integer or -1 for failure")
  141. {
  142. return object->IndexFromPath( argv[2] );
  143. }
  144. ConsoleMethod(GuiImageList, insert, S32, 3, 3, "(image path) Insert an image into imagelist\n"
  145. "@param path The path of the image file\n"
  146. "@return returns the image index or -1 for failure")
  147. {
  148. return object->Insert( argv[2] );
  149. }
  150. TextureObject *GuiImageList::GetTextureObject( U32 Index )
  151. {
  152. U32 ItemIndex = IndexFromId(Index);
  153. if ( ItemIndex != -1 )
  154. return mTextures[ItemIndex]->Object;
  155. else
  156. return NULL;
  157. }
  158. TextureObject *GuiImageList::GetTextureObject( const char* TexturePath )
  159. {
  160. Vector<PTextureEntry>::iterator i = mTextures.begin();
  161. for ( ; i != mTextures.end(); i++ )
  162. {
  163. if ( dStricmp( TexturePath, (*i)->TexturePath ) == 0 )
  164. return (*i)->Object;
  165. }
  166. return NULL;
  167. }
  168. TextureHandle GuiImageList::GetTextureHandle( U32 Index )
  169. {
  170. U32 ItemIndex = IndexFromId(Index);
  171. if ( ItemIndex != -1 )
  172. return mTextures[ItemIndex]->Handle;
  173. else
  174. return NULL;
  175. }
  176. TextureHandle GuiImageList::GetTextureHandle( const char* TexturePath )
  177. {
  178. Vector<PTextureEntry>::iterator i = mTextures.begin();
  179. for ( ; i != mTextures.end(); i++ )
  180. {
  181. if ( dStricmp( TexturePath, (*i)->TexturePath ) == 0 )
  182. return (*i)->Handle;
  183. }
  184. return NULL;
  185. }
  186. const char *GuiImageList::GetTexturePath( U32 Index )
  187. {
  188. U32 ItemIndex = IndexFromId(Index);
  189. if ( ItemIndex != -1 )
  190. return mTextures[ItemIndex]->TexturePath;
  191. else
  192. return "";
  193. }
  194. // Used to derive from GuiControl, but no visual representation was needed
  195. //void GuiImageList::onRender(Point2I offset, const RectI &updateRect)
  196. //{
  197. // return;
  198. //
  199. // //make sure we have a parent
  200. // GuiCanvas *Canvas = getRoot();
  201. // if (! Canvas)
  202. // return;
  203. //
  204. // U32 IconSize = 32;
  205. //
  206. // RectI IconRect;
  207. //
  208. // IconRect.point = offset;
  209. //
  210. // IconRect.extent.x = 32;
  211. // IconRect.extent.y = 32;
  212. //
  213. // //debugging, checking insertion by rendering textures in list
  214. // if( mTextures.size() )
  215. // {
  216. // Point2I TexPoint = offset;
  217. //
  218. // Vector<PTextureEntry>::iterator i = mTextures.begin();
  219. //
  220. // ColorI color;
  221. //
  222. // dglGetBitmapModulation(&color);
  223. //
  224. // dglClearBitmapModulation();
  225. //
  226. // for ( ; i != mTextures.end(); i++ )
  227. // {
  228. // dglDrawBitmap((*i)->Object,TexPoint);
  229. // TexPoint.y += (*i)->Object->getBitmapHeight();
  230. // }
  231. //
  232. // dglSetBitmapModulation(color);
  233. // }
  234. //}
  235. IMPLEMENT_CONOBJECT(GuiImageList);