Преглед изворни кода

Import file browser project

Previously known as SnapFile.
DricomDragon пре 4 година
родитељ
комит
6dafffafe1
4 измењених фајлова са 527 додато и 0 уклоњено
  1. 10 0
      sdl1/fileBrowser/DataCenter/idx.txt
  2. 315 0
      sdl1/fileBrowser/SnapFile.cpp
  3. 70 0
      sdl1/fileBrowser/SnapFile.h
  4. 132 0
      sdl1/fileBrowser/test.cpp

+ 10 - 0
sdl1/fileBrowser/DataCenter/idx.txt

@@ -0,0 +1,10 @@
+Kestrel.spc
+Osprey.spc
+Cat.png
+Flower.png
+Torus.spc
+LaniusShip.spc
+read_me.txt
+OuestFrance.html
+RockShip.spc
+Stealth.spc

+ 315 - 0
sdl1/fileBrowser/SnapFile.cpp

@@ -0,0 +1,315 @@
+#include "SnapFile.h"
+
+using namespace std;
+
+void SF_makeList( const string & indexName, vector<string> & dirList )
+{
+	// Stream
+	ifstream myStream( indexName.c_str() );
+
+	// Failure
+	if ( !myStream )
+	{
+		cout << "SF_makeList can't read " << indexName << endl;
+		return;
+	}
+
+	// Read
+	string line;
+
+	while( getline( myStream, line ) )
+	{
+		dirList.push_back( line );
+	}
+
+	// End
+	myStream.close();
+}
+
+
+void SF_filter( vector<string> & dirList, string ext, bool autoRemove )
+{
+	// Setup
+	unsigned int offset( ext.length() );
+	unsigned int curLeng;
+	vector<string> done;
+
+	// Filtering
+	while( !dirList.empty() )
+	{
+		curLeng = dirList.back().length();
+
+		if ( dirList.back()[ curLeng - offset - 1] == '.'
+		     && dirList.back().compare( curLeng - offset, offset, ext ) == 0 )
+		{
+			done.push_back( dirList.back() );
+
+			if ( autoRemove )
+				done.back().erase( curLeng - offset - 1, offset + 1 );
+		}
+
+		dirList.pop_back();
+	}
+
+	// Fill dirList again
+	while( !done.empty() )
+	{
+		dirList.push_back( done.back() );
+
+		done.pop_back();
+	}
+}
+
+
+void SF_choose( const string & indexName, string & fileChosen, SDL_Surface* tgt, const SF_Graphic cfg )
+{
+	// Get file list
+	vector<string> dirList;
+	SF_makeList( indexName, dirList );
+
+	if ( dirList.empty() )
+	{
+		cout << "SF_choose : no file found in " << indexName << "." << endl;
+		return;
+	} 
+
+	// Graphic init
+    bool mustDestroy;
+    SDL_Surface* screen( tgt );
+    mustDestroy = SF_fixWindow( screen, cfg );
+
+    // Mecanism
+    unsigned int select(0); // Element selected
+    unsigned int top(0); // First element display (scroll)
+    const unsigned int range( (screen->h - 32) / 9 );
+
+    // Loop
+    bool done(false);
+    SDL_Event event;
+    string msg("Jovian");
+    while (!done)
+    {
+        // [4.1] Gestion évènements
+        SDL_WaitEvent(&event);
+        switch (event.type)
+        {
+        case SDL_QUIT:
+            done = true;
+            break;
+        case SDL_KEYDOWN:
+            switch( event.key.keysym.sym )
+            {
+            case SDLK_ESCAPE :
+                done = true;
+                break;
+            case SDLK_SPACE:
+            case SDLK_RETURN:
+            	done = true;
+            	fileChosen = dirList[ select ];
+            	break;
+            case SDLK_RIGHT :
+            case SDLK_DOWN :
+                if ( select < dirList.size() - 1 )
+                	select ++;
+                break;
+            case SDLK_LEFT :
+            case SDLK_UP :
+                if ( select > 0 )
+                	select --;
+                break;
+            default :
+                break;
+            }
+            break;
+        /*case SDL_MOUSEMOTION:
+            mouse.x = event.motion.x ;
+            mouse.y = event.motion.y ;
+            break;*/
+        default:
+            break;
+
+        }
+
+        // Draw
+        SDL_FillRect(screen, 0, cfg.color_back );
+
+        SF_enhanceWindow( screen, cfg );
+
+     	for ( unsigned int k( top ); k < dirList.size() && k < top + range; k++ )
+     	{
+     		if ( k == select )
+     			stringColor( screen, 24, 32 + k*9, (">" + dirList[k]).c_str(), cfg.color_select );
+     		else
+     			stringColor( screen, 32, 32 + k*9, dirList[k].c_str(), cfg.color_line );
+     	}
+
+        SDL_Flip(screen);
+
+    }
+
+    // End
+    if ( mustDestroy )
+    {
+        SDL_FreeSurface( screen );
+        screen = nullptr;
+    }
+
+    return;
+}
+
+
+void SF_keyName( string & fileName, SDL_Surface* tgt, const SF_Graphic cfg )
+{
+	// Graphic init
+    bool mustDestroy;
+    SDL_Surface* screen( tgt );
+    mustDestroy = SF_fixWindow( screen, cfg );
+
+    // Input init
+    SDL_EnableUNICODE( 1 );
+
+    // Location
+    const unsigned int y( screen->h / 2 - 4 );
+    unsigned int x;
+
+    // Loop
+    bool done(false);
+    SDL_Event event;
+    while (!done)
+    {
+        // [4.1] Gestion évènements
+        SDL_WaitEvent(&event);
+        switch (event.type)
+        {
+        case SDL_QUIT:
+            done = true;
+            break;
+        case SDL_KEYDOWN:
+            switch( event.key.keysym.sym )
+            {
+            case SDLK_ESCAPE :
+                done = true;
+                fileName = "";
+                break;
+            case SDLK_RETURN :
+            	done = true;
+            	break;
+            case SDLK_BACKSPACE :
+            	if ( fileName.size() > 0 )
+            		fileName.erase( fileName.size() - 1 );
+            	break;
+            case SDLK_RIGHT :
+            case SDLK_DOWN :
+                break;
+            case SDLK_LEFT :
+            case SDLK_UP :
+                break;
+            default :
+            	if( event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
+            	{
+                	fileName += (char)event.key.keysym.unicode;
+            	}
+                break;
+            }
+            break;
+        /*case SDL_MOUSEMOTION:
+            mouse.x = event.motion.x ;
+            mouse.y = event.motion.y ;
+            break;*/
+        default:
+            break;
+
+        }
+
+        // Draw
+        SDL_FillRect(screen, 0, cfg.color_back );
+
+        SF_enhanceWindow( screen, cfg );
+
+        x = screen->w / 2 - 4*fileName.length();
+     	stringColor( screen, x, y, fileName.c_str(), cfg.color_select );
+
+        SDL_Flip(screen);
+
+    }
+
+    // End
+    if ( mustDestroy )
+    {
+        SDL_FreeSurface( screen );
+        screen = nullptr;
+    }
+    
+    return;
+}
+
+void SF_addAskedFile( const string & indexName, string & fileName, SDL_Surface* tgt, const SF_Graphic cfg )
+{
+	// Ask
+    SF_keyName( fileName, tgt, cfg );
+
+    // Add
+    SF_addFile( indexName, fileName );
+}
+
+void SF_addFile( const string & indexName, const string & fileName )
+{
+    // Check occurence
+    vector<string> dirList;
+    SF_makeList( indexName, dirList );
+
+    for ( auto it( dirList.begin() ); it != dirList.end(); it ++ )
+    {
+        if ( *it == fileName )
+            return;
+    }
+
+    dirList.clear();
+
+    // Stream
+    ofstream myStream( indexName.c_str(), ios::app );
+
+    // Failure
+    if ( !myStream )
+    {
+        cout << "SF_makeList can't write " << indexName << endl;
+        return;
+    }
+
+    // Write
+    myStream << fileName << endl;
+
+    // End
+    myStream.close();
+}
+
+bool SF_fixWindow( SDL_Surface* & screen, const SF_Graphic cfg )
+{
+    // Surface already set
+    if ( screen )
+        return false;
+
+    // Graphic init
+    screen = SDL_SetVideoMode(cfg.w, cfg.h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF );
+    if ( !screen )
+    {
+        cout << "SF_fixWindow : graphic error : " << SDL_GetError() << endl;
+        return false;
+    }
+    SDL_WM_SetCaption( cfg.name.c_str(), 0);
+
+    // Created
+    return true;
+}
+
+void SF_enhanceWindow( SDL_Surface* screen, const SF_Graphic & cfg )
+{
+    unsigned int line_offset( 55 + 8 * cfg.name.length() );
+    stringColor( screen, 50, 6, cfg.name.c_str(), cfg.color_line );
+    #define SF_BORD 10
+    lineColor( screen, SF_BORD, SF_BORD, 45, SF_BORD, cfg.color_line );
+    lineColor( screen, line_offset, SF_BORD, screen->w - SF_BORD, SF_BORD, cfg.color_line );
+    lineColor( screen, screen->w - SF_BORD, SF_BORD, screen->w - SF_BORD, screen->h - SF_BORD, cfg.color_line );
+    lineColor( screen, screen->w - SF_BORD, screen->h - SF_BORD, SF_BORD, screen->h - SF_BORD, cfg.color_line );
+    lineColor( screen, SF_BORD, screen->h - SF_BORD, SF_BORD, SF_BORD, cfg.color_line );
+}

+ 70 - 0
sdl1/fileBrowser/SnapFile.h

@@ -0,0 +1,70 @@
+#ifndef SNAPFILE_H
+#define SNAPFILE_H
+
+// Description
+/*
+SnapFile is a library based on SDL and SDL_gfx built to manage an index.
+
+Author : Jovian Hersmeule
+Last modification : 14 May 2017
+
+> Why is it relevant to use index ?
+Index are commonly used to list files inside a folder. Using index is
+relevant because it doesn't rely on a specific exploitation system.
+Indeed, listing files in a folder recquires specific libraries which
+are different between Windows and Linux.
+
+> What can perform SnapFile ?
+SnapFile has many functions do update and read your index. Thanks to SDL, SnapFile
+can create windows in order to interact easily with a user, in order to type a file
+name or to choose a file from the index.
+*/
+
+// Basic includes
+#include <iostream>
+#include <vector>
+#include <fstream>
+
+// Graphic includes
+#include <SDL/SDL.h>
+#include <SDL/SDL_gfxPrimitives.h>
+
+// Graphic config
+struct SF_Graphic
+{
+	unsigned int h;
+	unsigned int w;
+	Uint32 color_line; // GFX : RGBA
+	Uint32 color_back; // SDL : ARGB
+	Uint32 color_select; // GFX : RGBA
+	std::string name;
+};
+
+// Standard graphic config
+#define SF_STDGRPH {300, 400, 0xffff00ff, 0xff000000, 0xffffffff, "SnapFile"}
+
+// Add files listed in the index given
+void SF_makeList( const std::string & indexName, std::vector<std::string> & dirList );
+
+// Keep files with desired extension, and remove them if wanted
+void SF_filter( std::vector<std::string> & dirList, std::string ext, bool autoRemove = false );
+
+// Use a window and return file chosen
+void SF_choose( const std::string & indexName, std::string & fileChosen, SDL_Surface* tgt = nullptr, const SF_Graphic cfg = SF_STDGRPH );
+
+// Use a window to write a name
+void SF_keyName( std::string & fileName, SDL_Surface* tgt = nullptr, const SF_Graphic cfg = SF_STDGRPH );
+
+// Use a window to add a file, modify arg "fileName"
+void SF_addAskedFile( const std::string & indexName, std::string & fileName, SDL_Surface* tgt = nullptr, const SF_Graphic cfg = SF_STDGRPH );
+
+// Add a file in a index, with occurence check
+void SF_addFile( const std::string & indexName, const std::string & fileName );
+
+// Create a window if needed, return true if creation
+bool SF_fixWindow( SDL_Surface* & tgt, const SF_Graphic cfg = SF_STDGRPH );
+
+// Add shape and esthetic
+void SF_enhanceWindow( SDL_Surface* screen, const SF_Graphic & cfg );
+
+#endif

+ 132 - 0
sdl1/fileBrowser/test.cpp

@@ -0,0 +1,132 @@
+#include "SnapFile.h"
+
+using namespace std;
+
+#define IDX "DataCenter/idx.txt"
+
+void testList();
+void testCompare();
+void testChoose();
+void testAdd();
+void testKeypad();
+
+int main(int argc, char const *argv[])
+{
+	cout << "Bienvenue dans le test de SnapFile." << endl;
+
+	if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
+    {
+        std::cout << "Impossible to init SDL: " << SDL_GetError() << std::endl;
+        return 1;
+    }
+    atexit(SDL_Quit);
+
+	//testList();
+	//testCompare();
+	testChoose();
+	//testAdd();
+	//testKeypad();
+
+	return 0;
+}
+
+void testList()
+{
+	// Data
+	vector<string> file_list;
+
+	string index( IDX );
+
+	// Test SF_makeList
+	SF_makeList( index, file_list );
+
+	cout << "Voici les petits fichiers indexés :" << endl;
+
+	for( auto iter( file_list.begin() ); iter != file_list.end(); iter ++ )
+	{
+		cout << "> " << *iter << endl;
+	}
+
+	// Test SF_filter
+	SF_filter( file_list, "spc", true);
+
+	cout << endl << "On garde les spc seulement :" << endl;
+
+	for( auto iter( file_list.begin() ); iter != file_list.end(); iter ++ )
+	{
+		cout << "> " << *iter << endl;
+	}
+}
+
+void testCompare()
+{
+	// Test comparaison
+	string motif;
+	string ref;
+	unsigned int pos, len;
+
+	cout << endl << "Rentrez un mot : ";
+	cin >> ref;
+
+	cout << endl << "Rentrez le motif : ";
+	cin >> motif;
+
+	cout << endl << "Rentrez la position : ";
+	cin >> pos;
+
+	cout << endl << "Rentrez la longueur : ";
+	cin >> len;
+
+	cout << endl << "Comparaison : " << ref.compare( pos, len, motif ) << endl;
+}
+
+void testChoose()
+{
+	// Test charge
+	string index( IDX );
+	string fileChosen("");
+	SF_choose( index, fileChosen );
+
+	if ( fileChosen == "" )
+		cout << endl << "Pas de fichier sélectionné." << endl;
+	else
+		cout << endl << "Vous avez sélectionné le fichier : " << fileChosen << endl;
+}
+
+void testAdd()
+{
+	// Data
+	string index( IDX );
+
+	// Voir
+	cout << endl << "Regarder les fichiers indexés !" << endl;
+	testList();
+
+	string chaine("Sans Nom");
+	// Ajouter en C++
+	/*cout << endl << "Inscrivez une nouvelle entrée pour l'index :" << endl << ">>>";
+	cin >> chaine;
+
+	SF_addFile( index, chaine );*/
+
+	// Ajouter en SDL
+	cout << endl << "Une petite fenere SDL, une !" << endl;
+	SF_addAskedFile( index, chaine );
+	cout << "Le fichier saisi s'appelle " << chaine << endl;
+
+
+	// Revoir
+	cout << endl << "Vérifiez les fichiers indexés !" << endl;
+	testList();
+}
+
+void testKeypad()
+{
+	// Test taper texte
+	string name("Sans Nom");
+	SF_keyName( name );
+	if ( name == "" )
+		cout << endl << "Pas de nom tapé." << endl;
+	else
+		cout << endl << "Nouveau nom : " << name << endl;
+}