SnapFile.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "SnapFile.h"
  2. using namespace std;
  3. void SF_makeList( const string & indexName, vector<string> & dirList )
  4. {
  5. // Stream
  6. ifstream myStream( indexName.c_str() );
  7. // Failure
  8. if ( !myStream )
  9. {
  10. cout << "SF_makeList can't read " << indexName << endl;
  11. return;
  12. }
  13. // Read
  14. string line;
  15. while( getline( myStream, line ) )
  16. {
  17. dirList.push_back( line );
  18. }
  19. // End
  20. myStream.close();
  21. }
  22. void SF_filter( vector<string> & dirList, string ext, bool autoRemove )
  23. {
  24. // Setup
  25. unsigned int offset( ext.length() );
  26. unsigned int curLeng;
  27. vector<string> done;
  28. // Filtering
  29. while( !dirList.empty() )
  30. {
  31. curLeng = dirList.back().length();
  32. if ( dirList.back()[ curLeng - offset - 1] == '.'
  33. && dirList.back().compare( curLeng - offset, offset, ext ) == 0 )
  34. {
  35. done.push_back( dirList.back() );
  36. if ( autoRemove )
  37. done.back().erase( curLeng - offset - 1, offset + 1 );
  38. }
  39. dirList.pop_back();
  40. }
  41. // Fill dirList again
  42. while( !done.empty() )
  43. {
  44. dirList.push_back( done.back() );
  45. done.pop_back();
  46. }
  47. }
  48. void SF_choose( const string & indexName, string & fileChosen, SDL_Surface* tgt, const SF_Graphic cfg )
  49. {
  50. // Get file list
  51. vector<string> dirList;
  52. SF_makeList( indexName, dirList );
  53. if ( dirList.empty() )
  54. {
  55. cout << "SF_choose : no file found in " << indexName << "." << endl;
  56. return;
  57. }
  58. // Graphic init
  59. bool mustDestroy;
  60. SDL_Surface* screen( tgt );
  61. mustDestroy = SF_fixWindow( screen, cfg );
  62. // Mecanism
  63. unsigned int select(0); // Element selected
  64. unsigned int top(0); // First element display (scroll)
  65. const unsigned int range( (screen->h - 32) / 9 );
  66. // Loop
  67. bool done(false);
  68. SDL_Event event;
  69. string msg("Jovian");
  70. while (!done)
  71. {
  72. // [4.1] Gestion évènements
  73. SDL_WaitEvent(&event);
  74. switch (event.type)
  75. {
  76. case SDL_QUIT:
  77. done = true;
  78. break;
  79. case SDL_KEYDOWN:
  80. switch( event.key.keysym.sym )
  81. {
  82. case SDLK_ESCAPE :
  83. done = true;
  84. break;
  85. case SDLK_SPACE:
  86. case SDLK_RETURN:
  87. done = true;
  88. fileChosen = dirList[ select ];
  89. break;
  90. case SDLK_RIGHT :
  91. case SDLK_DOWN :
  92. if ( select < dirList.size() - 1 )
  93. select ++;
  94. break;
  95. case SDLK_LEFT :
  96. case SDLK_UP :
  97. if ( select > 0 )
  98. select --;
  99. break;
  100. default :
  101. break;
  102. }
  103. break;
  104. /*case SDL_MOUSEMOTION:
  105. mouse.x = event.motion.x ;
  106. mouse.y = event.motion.y ;
  107. break;*/
  108. default:
  109. break;
  110. }
  111. // Draw
  112. SDL_FillRect(screen, 0, cfg.color_back );
  113. SF_enhanceWindow( screen, cfg );
  114. for ( unsigned int k( top ); k < dirList.size() && k < top + range; k++ )
  115. {
  116. if ( k == select )
  117. stringColor( screen, 24, 32 + k*9, (">" + dirList[k]).c_str(), cfg.color_select );
  118. else
  119. stringColor( screen, 32, 32 + k*9, dirList[k].c_str(), cfg.color_line );
  120. }
  121. SDL_Flip(screen);
  122. }
  123. // End
  124. if ( mustDestroy )
  125. {
  126. SDL_FreeSurface( screen );
  127. screen = nullptr;
  128. }
  129. return;
  130. }
  131. void SF_keyName( string & fileName, SDL_Surface* tgt, const SF_Graphic cfg )
  132. {
  133. // Graphic init
  134. bool mustDestroy;
  135. SDL_Surface* screen( tgt );
  136. mustDestroy = SF_fixWindow( screen, cfg );
  137. // Input init
  138. SDL_EnableUNICODE( 1 );
  139. // Location
  140. const unsigned int y( screen->h / 2 - 4 );
  141. unsigned int x;
  142. // Loop
  143. bool done(false);
  144. SDL_Event event;
  145. while (!done)
  146. {
  147. // [4.1] Gestion évènements
  148. SDL_WaitEvent(&event);
  149. switch (event.type)
  150. {
  151. case SDL_QUIT:
  152. done = true;
  153. break;
  154. case SDL_KEYDOWN:
  155. switch( event.key.keysym.sym )
  156. {
  157. case SDLK_ESCAPE :
  158. done = true;
  159. fileName = "";
  160. break;
  161. case SDLK_RETURN :
  162. done = true;
  163. break;
  164. case SDLK_BACKSPACE :
  165. if ( fileName.size() > 0 )
  166. fileName.erase( fileName.size() - 1 );
  167. break;
  168. case SDLK_RIGHT :
  169. case SDLK_DOWN :
  170. break;
  171. case SDLK_LEFT :
  172. case SDLK_UP :
  173. break;
  174. default :
  175. if( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
  176. {
  177. fileName += (char)event.key.keysym.unicode;
  178. }
  179. break;
  180. }
  181. break;
  182. /*case SDL_MOUSEMOTION:
  183. mouse.x = event.motion.x ;
  184. mouse.y = event.motion.y ;
  185. break;*/
  186. default:
  187. break;
  188. }
  189. // Draw
  190. SDL_FillRect(screen, 0, cfg.color_back );
  191. SF_enhanceWindow( screen, cfg );
  192. x = screen->w / 2 - 4*fileName.length();
  193. stringColor( screen, x, y, fileName.c_str(), cfg.color_select );
  194. SDL_Flip(screen);
  195. }
  196. // End
  197. if ( mustDestroy )
  198. {
  199. SDL_FreeSurface( screen );
  200. screen = nullptr;
  201. }
  202. return;
  203. }
  204. void SF_addAskedFile( const string & indexName, string & fileName, SDL_Surface* tgt, const SF_Graphic cfg )
  205. {
  206. // Ask
  207. SF_keyName( fileName, tgt, cfg );
  208. // Add
  209. SF_addFile( indexName, fileName );
  210. }
  211. void SF_addFile( const string & indexName, const string & fileName )
  212. {
  213. // Check occurence
  214. vector<string> dirList;
  215. SF_makeList( indexName, dirList );
  216. for ( auto it( dirList.begin() ); it != dirList.end(); it ++ )
  217. {
  218. if ( *it == fileName )
  219. return;
  220. }
  221. dirList.clear();
  222. // Stream
  223. ofstream myStream( indexName.c_str(), ios::app );
  224. // Failure
  225. if ( !myStream )
  226. {
  227. cout << "SF_makeList can't write " << indexName << endl;
  228. return;
  229. }
  230. // Write
  231. myStream << fileName << endl;
  232. // End
  233. myStream.close();
  234. }
  235. bool SF_fixWindow( SDL_Surface* & screen, const SF_Graphic cfg )
  236. {
  237. // Surface already set
  238. if ( screen )
  239. return false;
  240. // Graphic init
  241. screen = SDL_SetVideoMode(cfg.w, cfg.h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
  242. if ( !screen )
  243. {
  244. cout << "SF_fixWindow : graphic error : " << SDL_GetError() << endl;
  245. return false;
  246. }
  247. SDL_WM_SetCaption( cfg.name.c_str(), 0);
  248. // Created
  249. return true;
  250. }
  251. void SF_enhanceWindow( SDL_Surface* screen, const SF_Graphic & cfg )
  252. {
  253. unsigned int line_offset( 55 + 8 * cfg.name.length() );
  254. stringColor( screen, 50, 6, cfg.name.c_str(), cfg.color_line );
  255. #define SF_BORD 10
  256. lineColor( screen, SF_BORD, SF_BORD, 45, SF_BORD, cfg.color_line );
  257. lineColor( screen, line_offset, SF_BORD, screen->w - SF_BORD, SF_BORD, cfg.color_line );
  258. lineColor( screen, screen->w - SF_BORD, SF_BORD, screen->w - SF_BORD, screen->h - SF_BORD, cfg.color_line );
  259. lineColor( screen, screen->w - SF_BORD, screen->h - SF_BORD, SF_BORD, screen->h - SF_BORD, cfg.color_line );
  260. lineColor( screen, SF_BORD, screen->h - SF_BORD, SF_BORD, SF_BORD, cfg.color_line );
  261. }