mirror of
https://github.com/raysan5/raygui.git
synced 2026-02-02 12:19:17 -05:00
Review gui_file_dialog with proposed improvements
Proposed improvements in: https://github.com/raysan5/raylib/issues/1101
This commit is contained in:
@ -40,7 +40,7 @@ int main()
|
|||||||
SetExitKey(0);
|
SetExitKey(0);
|
||||||
|
|
||||||
// Custom file dialog
|
// Custom file dialog
|
||||||
GuiFileDialogState fileDialogState = InitGuiFileDialog();
|
GuiFileDialogState fileDialogState = InitGuiFileDialog(420, 310, GetWorkingDirectory(), false);
|
||||||
|
|
||||||
bool exitWindow = false;
|
bool exitWindow = false;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* FileDialog v1.0.0 - Modal file dialog to open/save files
|
* FileDialog v1.1 - Modal file dialog to open/save files
|
||||||
*
|
*
|
||||||
* MODULE USAGE:
|
* MODULE USAGE:
|
||||||
* #define GUI_FILE_DIALOG_IMPLEMENTATION
|
* #define GUI_FILE_DIALOG_IMPLEMENTATION
|
||||||
@ -16,13 +16,24 @@
|
|||||||
* - DirectoryExists()
|
* - DirectoryExists()
|
||||||
* - FileExists()
|
* - FileExists()
|
||||||
*
|
*
|
||||||
* LICENSE: Propietary License
|
* LICENSE: zlib/libpng
|
||||||
*
|
*
|
||||||
* Copyright (c) 2019 raylib technologies (@raylibtech). All Rights Reserved.
|
* Copyright (c) 2019-2020 Ramon Santamaria (@raysan5)
|
||||||
*
|
*
|
||||||
* Unauthorized copying of this file, via any medium is strictly prohibited
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
||||||
* This project is proprietary and confidential unless the owner allows
|
* will the authors be held liable for any damages arising from the use of this software.
|
||||||
* usage in any other form by expresely written permission.
|
*
|
||||||
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
||||||
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||||
|
*
|
||||||
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
||||||
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
||||||
|
* in the product documentation would be appreciated but is not required.
|
||||||
|
*
|
||||||
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
||||||
|
* as being the original software.
|
||||||
|
*
|
||||||
|
* 3. This notice may not be removed or altered from any source distribution.
|
||||||
*
|
*
|
||||||
**********************************************************************************************/
|
**********************************************************************************************/
|
||||||
|
|
||||||
@ -37,6 +48,7 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
|
Vector2 size;
|
||||||
|
|
||||||
bool fileDialogActive;
|
bool fileDialogActive;
|
||||||
|
|
||||||
@ -52,6 +64,7 @@ typedef struct {
|
|||||||
bool SelectFilePressed;
|
bool SelectFilePressed;
|
||||||
bool CancelFilePressed;
|
bool CancelFilePressed;
|
||||||
int fileTypeActive;
|
int fileTypeActive;
|
||||||
|
int itemFocused;
|
||||||
|
|
||||||
// Custom state variables (depend on development software)
|
// Custom state variables (depend on development software)
|
||||||
// NOTE: This variables should be added manually if required
|
// NOTE: This variables should be added manually if required
|
||||||
@ -89,7 +102,7 @@ extern "C" { // Prevents name mangling of functions
|
|||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Declaration
|
// Module Functions Declaration
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
GuiFileDialogState InitGuiFileDialog(void);
|
GuiFileDialogState InitGuiFileDialog(int width, int height, const char *initPath, bool active);
|
||||||
void GuiFileDialog(GuiFileDialogState *state);
|
void GuiFileDialog(GuiFileDialogState *state);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -128,12 +141,15 @@ typedef struct FileInfo {
|
|||||||
int type;
|
int type;
|
||||||
int icon;
|
int icon;
|
||||||
} FileInfo;
|
} FileInfo;
|
||||||
|
#else
|
||||||
|
// Filename only
|
||||||
|
typedef char *FileInfo;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Global Variables Definition
|
// Global Variables Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
char **dirFilesIcon = NULL;
|
FileInfo *dirFilesIcon = NULL;
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Internal Module Functions Definition
|
// Internal Module Functions Definition
|
||||||
@ -143,19 +159,22 @@ static char **ReadDirectoryFiles(const char *dir, int *filesCount, char *filterE
|
|||||||
|
|
||||||
#if defined(USE_CUSTOM_LISTVIEW_FILEINFO)
|
#if defined(USE_CUSTOM_LISTVIEW_FILEINFO)
|
||||||
// List View control for files info with extended parameters
|
// List View control for files info with extended parameters
|
||||||
static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *focus, int *scrollIndex, int active)
|
static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *focus, int *scrollIndex, int active);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Definition
|
// Module Functions Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
GuiFileDialogState InitGuiFileDialog(void)
|
GuiFileDialogState InitGuiFileDialog(int width, int height, const char *initPath, bool active)
|
||||||
{
|
{
|
||||||
GuiFileDialogState state = { 0 };
|
GuiFileDialogState state = { 0 };
|
||||||
|
|
||||||
state.position = (Vector2){ GetScreenWidth()/2 - 480/2, GetScreenHeight()/2 - 305/2 };
|
// Default dialog size is 440x310
|
||||||
|
state.size.x = width == -1 ? 440 : width;
|
||||||
|
state.size.y = height == -1 ? 310 : height;
|
||||||
|
state.position = (Vector2){ GetScreenWidth()/2 - state.size.x/2, GetScreenHeight()/2 - state.size.y/2 };
|
||||||
|
|
||||||
state.fileDialogActive = false;
|
state.fileDialogActive = active;
|
||||||
state.dirPathEditMode = false;
|
state.dirPathEditMode = false;
|
||||||
|
|
||||||
state.filesListActive = -1;
|
state.filesListActive = -1;
|
||||||
@ -169,58 +188,91 @@ GuiFileDialogState InitGuiFileDialog(void)
|
|||||||
|
|
||||||
state.fileTypeActive = 0;
|
state.fileTypeActive = 0;
|
||||||
|
|
||||||
|
strcpy(state.fileNameText, "\0");
|
||||||
|
|
||||||
// Custom variables initialization
|
// Custom variables initialization
|
||||||
strcpy(state.dirPathText, GetWorkingDirectory());
|
if (initPath && DirectoryExists(initPath))
|
||||||
|
{
|
||||||
|
strcpy(state.dirPathText, initPath);
|
||||||
|
}
|
||||||
|
else if (initPath && FileExists(initPath))
|
||||||
|
{
|
||||||
|
strcpy(state.dirPathText, GetDirectoryPath(initPath));
|
||||||
|
strcpy(state.fileNameText, GetFileName(initPath));
|
||||||
|
}
|
||||||
|
else strcpy(state.dirPathText, GetWorkingDirectory());
|
||||||
|
|
||||||
strcpy(state.dirPathTextCopy, state.dirPathText);
|
strcpy(state.dirPathTextCopy, state.dirPathText);
|
||||||
|
strcpy(state.fileNameTextCopy, state.fileNameText);
|
||||||
|
|
||||||
strcpy(state.filterExt, "all");
|
strcpy(state.filterExt, "all");
|
||||||
|
|
||||||
state.dirFilesCount = 0;
|
state.dirFilesCount = 0;
|
||||||
state.dirFiles = NULL; // NOTE: Loaded lazily on window active
|
state.dirFiles = NULL; // NOTE: Loaded lazily on window active
|
||||||
|
|
||||||
strcpy(state.fileNameText, "\0");
|
|
||||||
strcpy(state.fileNameTextCopy, state.fileNameText);
|
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read files in new path
|
||||||
|
static void FD_RELOAD_DIRPATH(GuiFileDialogState *state)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < state->dirFilesCount; i++) RL_FREE(state->dirFiles[i]);
|
||||||
|
RL_FREE(state->dirFiles);
|
||||||
|
|
||||||
|
state->dirFiles = ReadDirectoryFiles(state->dirPathText, &state->dirFilesCount, state->filterExt);
|
||||||
|
state->itemFocused = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update and draw file dialog
|
||||||
void GuiFileDialog(GuiFileDialogState *state)
|
void GuiFileDialog(GuiFileDialogState *state)
|
||||||
{
|
{
|
||||||
if (state->fileDialogActive)
|
if (state->fileDialogActive)
|
||||||
{
|
{
|
||||||
|
const int winWidth = state->size.x;
|
||||||
|
const int winHeight = state->size.y;
|
||||||
|
|
||||||
// Load dirFilesIcon and state->dirFiles lazily on windows open
|
// Load dirFilesIcon and state->dirFiles lazily on windows open
|
||||||
// NOTE: they are automatically unloaded at fileDialog closing
|
// NOTE: they are automatically unloaded at fileDialog closing
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
if (dirFilesIcon == NULL)
|
if (dirFilesIcon == NULL)
|
||||||
{
|
{
|
||||||
dirFilesIcon = (char **)RL_MALLOC(MAX_DIRECTORY_FILES*sizeof(char *)); // Max files to read
|
dirFilesIcon = (FileInfo *)RL_MALLOC(MAX_DIRECTORY_FILES*sizeof(FileInfo)); // Max files to read
|
||||||
for (int i = 0; i < MAX_DIRECTORY_FILES; i++) dirFilesIcon[i] = (char *)calloc(MAX_DIR_PATH_LENGTH, 1); // Max file name length
|
for (int i = 0; i < MAX_DIRECTORY_FILES; i++) dirFilesIcon[i] = (char *)calloc(MAX_DIR_PATH_LENGTH, 1); // Max file name length
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state->dirFiles == NULL) state->dirFiles = ReadDirectoryFiles(state->dirPathText, &state->dirFilesCount, state->filterExt);
|
if (state->dirFiles == NULL)
|
||||||
|
{
|
||||||
|
state->dirFiles = ReadDirectoryFiles(state->dirPathText, &state->dirFilesCount, state->filterExt);
|
||||||
|
|
||||||
|
for(int f = 0; f < state->dirFilesCount; f++)
|
||||||
|
{
|
||||||
|
if (strcmp(state->fileNameText, state->dirFiles[f]) == 0)
|
||||||
|
{
|
||||||
|
if (state->filesListActive != f) state->filesListScrollIndex = state->filesListActive = f; // make it active and visible only on first call
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
|
|
||||||
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)), 0.85f));
|
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)), 0.85f));
|
||||||
state->fileDialogActive = !GuiWindowBox((Rectangle){ state->position.x + 0, state->position.y + 0, 480, 310 }, "#198#Select File Dialog");
|
state->fileDialogActive = !GuiWindowBox((Rectangle){ state->position.x + 0, state->position.y + 0, winWidth, winHeight }, "#198#LuaJIT | Select File Dialog");
|
||||||
|
|
||||||
if (GuiButton((Rectangle){ state->position.x + 430, state->position.y + 35, 40, 25 }, "< .."))
|
if (GuiButton((Rectangle){ state->position.x + winWidth - 50, state->position.y + 35, 40, 25 }, "< .."))// || IsKeyReleased(KEY_DPAD_Y))
|
||||||
{
|
{
|
||||||
// Move dir path one level up
|
// Move dir path one level up
|
||||||
strcpy(state->dirPathText, GetPrevDirectoryPath(state->dirPathText));
|
strcpy(state->dirPathText, GetPrevDirectoryPath(state->dirPathText));
|
||||||
|
|
||||||
// RL_FREE previous dirFiles (reloaded by ReadDirectoryFiles())
|
// RL_FREE previous dirFiles (reloaded by ReadDirectoryFiles())
|
||||||
for (int i = 0; i < state->dirFilesCount; i++) RL_FREE(state->dirFiles[i]);
|
FD_RELOAD_DIRPATH(state);
|
||||||
RL_FREE(state->dirFiles);
|
|
||||||
|
|
||||||
// Read files in the new path
|
|
||||||
state->dirFiles = ReadDirectoryFiles(state->dirPathText, &state->dirFilesCount, state->filterExt);
|
|
||||||
|
|
||||||
state->filesListActive = -1;
|
state->filesListActive = -1;
|
||||||
strcpy(state->fileNameText, "\0");
|
strcpy(state->fileNameText, "\0");
|
||||||
strcpy(state->fileNameTextCopy, state->fileNameText);
|
strcpy(state->fileNameTextCopy, state->fileNameText);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GuiTextBox((Rectangle){ state->position.x + 10, state->position.y + 35, 410, 25 }, state->dirPathText, 256, state->dirPathEditMode))
|
if (GuiTextBox((Rectangle){ state->position.x + 10, state->position.y + 35, winWidth - 65, 25 }, state->dirPathText, 256, state->dirPathEditMode))
|
||||||
{
|
{
|
||||||
if (state->dirPathEditMode)
|
if (state->dirPathEditMode)
|
||||||
{
|
{
|
||||||
@ -228,11 +280,7 @@ void GuiFileDialog(GuiFileDialogState *state)
|
|||||||
if (DirectoryExists(state->dirPathText))
|
if (DirectoryExists(state->dirPathText))
|
||||||
{
|
{
|
||||||
// RL_FREE previous dirFiles (reloaded by ReadDirectoryFiles())
|
// RL_FREE previous dirFiles (reloaded by ReadDirectoryFiles())
|
||||||
for (int i = 0; i < state->dirFilesCount; i++) RL_FREE(state->dirFiles[i]);
|
FD_RELOAD_DIRPATH(state);
|
||||||
RL_FREE(state->dirFiles);
|
|
||||||
|
|
||||||
// Read files in new path
|
|
||||||
state->dirFiles = ReadDirectoryFiles(state->dirPathText, &state->dirFilesCount, state->filterExt);
|
|
||||||
|
|
||||||
strcpy(state->dirPathTextCopy, state->dirPathText);
|
strcpy(state->dirPathTextCopy, state->dirPathText);
|
||||||
}
|
}
|
||||||
@ -248,28 +296,29 @@ void GuiFileDialog(GuiFileDialogState *state)
|
|||||||
GuiSetStyle(LISTVIEW, LIST_ITEMS_HEIGHT, 24);
|
GuiSetStyle(LISTVIEW, LIST_ITEMS_HEIGHT, 24);
|
||||||
|
|
||||||
// TODO: ListViewElements should be aligned left
|
// TODO: ListViewElements should be aligned left
|
||||||
state->filesListActive = GuiListViewEx((Rectangle){ state->position.x + 10, state->position.y + 70, 460, 164 }, (const char **)dirFilesIcon, state->dirFilesCount, NULL, &state->filesListScrollIndex, state->filesListActive);
|
# if defined(USE_CUSTOM_LISTVIEW_FILEINFO)
|
||||||
|
FileInfo fileInfo;
|
||||||
|
state->filesListActive = GuiListViewFiles((Rectangle){ state->position.x + 10, state->position.y + 70, winWidth - 20, winHeight - 135 }, fileInfo, state->dirFilesCount, &state->itemFocused, &state->filesListScrollIndex, state->filesListActive);
|
||||||
|
# else
|
||||||
|
state->filesListActive = GuiListViewEx((Rectangle){ state->position.x + 10, state->position.y + 70, winWidth - 20, winHeight - 135 }, (const char**)dirFilesIcon, state->dirFilesCount, &state->itemFocused, &state->filesListScrollIndex, state->filesListActive);
|
||||||
|
# endif
|
||||||
GuiSetStyle(LISTVIEW, TEXT_ALIGNMENT, prevTextAlignment);
|
GuiSetStyle(LISTVIEW, TEXT_ALIGNMENT, prevTextAlignment);
|
||||||
GuiSetStyle(LISTVIEW, LIST_ITEMS_HEIGHT, prevElementsHeight);
|
GuiSetStyle(LISTVIEW, LIST_ITEMS_HEIGHT, prevElementsHeight);
|
||||||
|
|
||||||
if ((state->filesListActive >= 0) && (state->filesListActive != state->prevFilesListActive))
|
if ((state->filesListActive >= 0) && (state->filesListActive != state->prevFilesListActive))
|
||||||
|
//&& (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_DPAD_A)))
|
||||||
{
|
{
|
||||||
strcpy(state->fileNameText, state->dirFiles[state->filesListActive]);
|
strcpy(state->fileNameText, state->dirFiles[state->filesListActive]);
|
||||||
|
|
||||||
if (DirectoryExists(TextFormat("%s\\%s", state->dirPathText, state->fileNameText)))
|
if (DirectoryExists(TextFormat("%s/%s", state->dirPathText, state->fileNameText)))
|
||||||
{
|
{
|
||||||
if (TextIsEqual(state->fileNameText, "..")) strcpy(state->dirPathText, GetPrevDirectoryPath(state->dirPathText));
|
if (TextIsEqual(state->fileNameText, "..")) strcpy(state->dirPathText, GetPrevDirectoryPath(state->dirPathText));
|
||||||
else strcpy(state->dirPathText, TextFormat("%s\\%s", state->dirPathText, state->fileNameText));
|
else strcpy(state->dirPathText, TextFormat("%s/%s", strcmp(state->dirPathText, "/")==0 ? "" : state->dirPathText, state->fileNameText));
|
||||||
|
|
||||||
strcpy(state->dirPathTextCopy, state->dirPathText);
|
strcpy(state->dirPathTextCopy, state->dirPathText);
|
||||||
|
|
||||||
// RL_FREE previous dirFiles (reloaded by ReadDirectoryFiles())
|
// RL_FREE previous dirFiles (reloaded by ReadDirectoryFiles())
|
||||||
for (int i = 0; i < state->dirFilesCount; i++) RL_FREE(state->dirFiles[i]);
|
FD_RELOAD_DIRPATH(state);
|
||||||
RL_FREE(state->dirFiles);
|
|
||||||
|
|
||||||
// Read files in new path
|
|
||||||
state->dirFiles = ReadDirectoryFiles(state->dirPathText, &state->dirFilesCount, state->filterExt);
|
|
||||||
|
|
||||||
strcpy(state->dirPathTextCopy, state->dirPathText);
|
strcpy(state->dirPathTextCopy, state->dirPathText);
|
||||||
|
|
||||||
@ -281,11 +330,11 @@ void GuiFileDialog(GuiFileDialogState *state)
|
|||||||
state->prevFilesListActive = state->filesListActive;
|
state->prevFilesListActive = state->filesListActive;
|
||||||
}
|
}
|
||||||
|
|
||||||
GuiLabel((Rectangle){ state->position.x + 10, state->position.y + 245, 68, 25 }, "File name:");
|
GuiLabel((Rectangle){ state->position.x + 10, state->position.y + winHeight - 60, 68, 25 }, "File name:");
|
||||||
|
|
||||||
if (GuiTextBox((Rectangle){ state->position.x + 75, state->position.y + 245, 275, 25 }, state->fileNameText, 128, state->fileNameEditMode))
|
if (GuiTextBox((Rectangle){ state->position.x + 75, state->position.y + winHeight - 60, winWidth - 200, 25 }, state->fileNameText, 128, state->fileNameEditMode))
|
||||||
{
|
{
|
||||||
if (state->fileNameText)
|
if (*state->fileNameText)
|
||||||
{
|
{
|
||||||
// Verify if a valid filename has been introduced
|
// Verify if a valid filename has been introduced
|
||||||
if (FileExists(TextFormat("%s/%s", state->dirPathText, state->fileNameText)))
|
if (FileExists(TextFormat("%s/%s", state->dirPathText, state->fileNameText)))
|
||||||
@ -310,14 +359,22 @@ void GuiFileDialog(GuiFileDialogState *state)
|
|||||||
state->fileNameEditMode = !state->fileNameEditMode;
|
state->fileNameEditMode = !state->fileNameEditMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
state->fileTypeActive = GuiComboBox((Rectangle){ state->position.x + 75, state->position.y + 275, 275, 25 }, "All files", state->fileTypeActive);
|
state->fileTypeActive = GuiComboBox((Rectangle){ state->position.x + 75, state->position.y + winHeight - 30, winWidth - 200, 25 }, "All files", state->fileTypeActive);
|
||||||
GuiLabel((Rectangle){ state->position.x + 10, state->position.y + 275, 68, 25 }, "File filter:");
|
GuiLabel((Rectangle){ state->position.x + 10, state->position.y + winHeight - 30, 68, 25 }, "File filter:");
|
||||||
|
|
||||||
state->SelectFilePressed = GuiButton((Rectangle){ state->position.x + 360, state->position.y + 245, 110, 25 }, "Select");
|
state->SelectFilePressed = GuiButton((Rectangle){ state->position.x + winWidth - 120, state->position.y + winHeight - 60, 110,
|
||||||
|
#ifdef PLATFORM_DESKTOP
|
||||||
|
25
|
||||||
|
#else
|
||||||
|
25+30
|
||||||
|
#endif
|
||||||
|
}, "Select");// || IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_DPAD_A);
|
||||||
|
|
||||||
if (state->SelectFilePressed) state->fileDialogActive = false;
|
if (state->SelectFilePressed) state->fileDialogActive = false;
|
||||||
|
|
||||||
if (GuiButton((Rectangle){ state->position.x + 360, state->position.y + 275, 110, 25 }, "Cancel")) state->fileDialogActive = false;
|
#ifdef PLATFORM_DESKTOP
|
||||||
|
if (GuiButton((Rectangle){ state->position.x + winWidth - 120, state->position.y + winHeight - 30, 110, 25 }, "Quit")) state->fileDialogActive = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
// File dialog has been closed!
|
// File dialog has been closed!
|
||||||
if (!state->fileDialogActive)
|
if (!state->fileDialogActive)
|
||||||
@ -339,6 +396,19 @@ void GuiFileDialog(GuiFileDialogState *state)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read all filenames from directory (supported file types)
|
// Read all filenames from directory (supported file types)
|
||||||
|
static inline int _file_comp(const char *d1, const char *d2, const char *dir)
|
||||||
|
{
|
||||||
|
const bool b1 = DirectoryExists(TextFormat("%s/%s", dir, d1));
|
||||||
|
const bool b2 = DirectoryExists(TextFormat("%s/%s", dir, d2));
|
||||||
|
|
||||||
|
if (b1 && !b2) return -1;
|
||||||
|
if (!b1 && b2) return 1;
|
||||||
|
|
||||||
|
if (!FileExists(TextFormat("%s/%s", dir, d1))) return 1;
|
||||||
|
if (!FileExists(TextFormat("%s/%s", dir, d2))) return -1;
|
||||||
|
|
||||||
|
return strcmp(d1, d2);
|
||||||
|
}
|
||||||
static char **ReadDirectoryFiles(const char *dir, int *filesCount, char *filterExt)
|
static char **ReadDirectoryFiles(const char *dir, int *filesCount, char *filterExt)
|
||||||
{
|
{
|
||||||
int validFilesCount = 0;
|
int validFilesCount = 0;
|
||||||
@ -352,6 +422,39 @@ static char **ReadDirectoryFiles(const char *dir, int *filesCount, char *filterE
|
|||||||
int dirFilesCount = 0;
|
int dirFilesCount = 0;
|
||||||
char **files = GetDirectoryFiles(dir, &dirFilesCount);
|
char **files = GetDirectoryFiles(dir, &dirFilesCount);
|
||||||
|
|
||||||
|
// Sort files and directories: dir by name + files by name
|
||||||
|
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort#C
|
||||||
|
if (dirFilesCount > 1)
|
||||||
|
{
|
||||||
|
const int MAX = 64;
|
||||||
|
unsigned int left = 0, stack[MAX], pos = 0, seed = rand(), len = dirFilesCount;
|
||||||
|
|
||||||
|
for ( ; ; )
|
||||||
|
{
|
||||||
|
for (; left+1 < len; len++) /* sort left to len-1 */
|
||||||
|
{
|
||||||
|
if (pos == MAX) len = stack[pos = 0]; /* stack overflow, reset */
|
||||||
|
char *pivot = files[left+seed%(len-left)]; /* pick random pivot */
|
||||||
|
seed = seed*69069+1; /* next pseudorandom number */
|
||||||
|
stack[pos++] = len; /* sort right part later */
|
||||||
|
|
||||||
|
for (unsigned int right = left-1; ; ) /* inner loop: partitioning */
|
||||||
|
{
|
||||||
|
while (_file_comp(files[++right], pivot, dir) < 0);/* look for greater element */
|
||||||
|
while (_file_comp(pivot, files[--len], dir) < 0); /* look for smaller element */
|
||||||
|
if (right >= len) break; /* partition point found? */
|
||||||
|
char *temp = files[right];
|
||||||
|
files[right] = files[len]; /* the only swap */
|
||||||
|
files[len] = temp;
|
||||||
|
} /* partitioned, continue left part */
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos == 0) break; /* stack empty? */
|
||||||
|
left = len; /* left to right is sorted */
|
||||||
|
len = stack[--pos]; /* get next range to sort */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (TextIsEqual(extensions[0], "all")) filterExtensions = false;
|
if (TextIsEqual(extensions[0], "all")) filterExtensions = false;
|
||||||
|
|
||||||
for (int i = 0; (i < dirFilesCount) && (validFilesCount < MAX_DIRECTORY_FILES); i++)
|
for (int i = 0; (i < dirFilesCount) && (validFilesCount < MAX_DIRECTORY_FILES); i++)
|
||||||
@ -360,10 +463,10 @@ static char **ReadDirectoryFiles(const char *dir, int *filesCount, char *filterE
|
|||||||
|
|
||||||
if (!filterExtensions)
|
if (!filterExtensions)
|
||||||
{
|
{
|
||||||
strcpy(validFiles[validFilesCount], files[i]);
|
strncpy(validFiles[validFilesCount], files[i], MAX_DIR_PATH_LENGTH);
|
||||||
|
|
||||||
// Only filter files by extensions, directories should be available
|
// Only filter files by extensions, directories should be available
|
||||||
if (DirectoryExists(TextFormat("%s\\%s", dir, files[i]))) strcpy(dirFilesIcon[validFilesCount], TextFormat("#%i#%s", 1, files[i]));
|
if (DirectoryExists(TextFormat("%s/%s", dir, files[i]))) strcpy(dirFilesIcon[validFilesCount], TextFormat("#%i#%s", 1, files[i]));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO: Assign custom filetype icons depending on file extension (image, audio, text, video, models...)
|
// TODO: Assign custom filetype icons depending on file extension (image, audio, text, video, models...)
|
||||||
@ -393,8 +496,6 @@ static char **ReadDirectoryFiles(const char *dir, int *filesCount, char *filterE
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Sort files and directories: dir by name + files by name
|
|
||||||
|
|
||||||
ClearDirectoryFiles();
|
ClearDirectoryFiles();
|
||||||
|
|
||||||
*filesCount = validFilesCount;
|
*filesCount = validFilesCount;
|
||||||
@ -411,18 +512,18 @@ static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *f
|
|||||||
|
|
||||||
// Check if we need a scroll bar
|
// Check if we need a scroll bar
|
||||||
bool useScrollBar = false;
|
bool useScrollBar = false;
|
||||||
if ((GuiGetStyle(LISTVIEW, ELEMENTS_HEIGHT) + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING))*count > bounds.height) useScrollBar = true;
|
if ((GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING))*count > bounds.height) useScrollBar = true;
|
||||||
|
|
||||||
// Define base item rectangle [0]
|
// Define base item rectangle [0]
|
||||||
Rectangle itemBounds = { 0 };
|
Rectangle itemBounds = { 0 };
|
||||||
itemBounds.x = bounds.x + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING);
|
itemBounds.x = bounds.x + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING);
|
||||||
itemBounds.y = bounds.y + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING) + GuiGetStyle(DEFAULT, BORDER_WIDTH);
|
itemBounds.y = bounds.y + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING) + GuiGetStyle(DEFAULT, BORDER_WIDTH);
|
||||||
itemBounds.width = bounds.width - 2*GuiGetStyle(LISTVIEW, ELEMENTS_PADDING) - GuiGetStyle(DEFAULT, BORDER_WIDTH);
|
itemBounds.width = bounds.width - 2*GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING) - GuiGetStyle(DEFAULT, BORDER_WIDTH);
|
||||||
itemBounds.height = GuiGetStyle(LISTVIEW, ELEMENTS_HEIGHT);
|
itemBounds.height = GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT);
|
||||||
if (useScrollBar) itemBounds.width -= GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH);
|
if (useScrollBar) itemBounds.width -= GuiGetStyle(LISTVIEW, SCROLLBAR_WIDTH);
|
||||||
|
|
||||||
// Get items on the list
|
// Get items on the list
|
||||||
int visibleItems = bounds.height/(GuiGetStyle(LISTVIEW, ELEMENTS_HEIGHT) + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING));
|
int visibleItems = bounds.height/(GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING));
|
||||||
if (visibleItems > count) visibleItems = count;
|
if (visibleItems > count) visibleItems = count;
|
||||||
|
|
||||||
int startIndex = (scrollIndex == NULL)? 0 : *scrollIndex;
|
int startIndex = (scrollIndex == NULL)? 0 : *scrollIndex;
|
||||||
@ -451,7 +552,7 @@ static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *f
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update item rectangle y position for next item
|
// Update item rectangle y position for next item
|
||||||
itemBounds.y += (GuiGetStyle(LISTVIEW, ELEMENTS_HEIGHT) + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING));
|
itemBounds.y += (GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useScrollBar)
|
if (useScrollBar)
|
||||||
@ -469,7 +570,7 @@ static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *f
|
|||||||
else itemFocused = -1;
|
else itemFocused = -1;
|
||||||
|
|
||||||
// Reset item rectangle y to [0]
|
// Reset item rectangle y to [0]
|
||||||
itemBounds.y = bounds.y + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING) + GuiGetStyle(DEFAULT, BORDER_WIDTH);
|
itemBounds.y = bounds.y + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING) + GuiGetStyle(DEFAULT, BORDER_WIDTH);
|
||||||
}
|
}
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
|
|
||||||
@ -521,7 +622,7 @@ static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *f
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update item rectangle y position for next item
|
// Update item rectangle y position for next item
|
||||||
itemBounds.y += (GuiGetStyle(LISTVIEW, ELEMENTS_HEIGHT) + GuiGetStyle(LISTVIEW, ELEMENTS_PADDING));
|
itemBounds.y += (GuiGetStyle(LISTVIEW, LIST_ITEMS_HEIGHT) + GuiGetStyle(LISTVIEW, LIST_ITEMS_PADDING));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useScrollBar)
|
if (useScrollBar)
|
||||||
@ -536,15 +637,15 @@ static int GuiListViewFiles(Rectangle bounds, FileInfo *files, int count, int *f
|
|||||||
float percentVisible = (float)(endIndex - startIndex)/count;
|
float percentVisible = (float)(endIndex - startIndex)/count;
|
||||||
float sliderSize = bounds.height*percentVisible;
|
float sliderSize = bounds.height*percentVisible;
|
||||||
|
|
||||||
int prevSliderSize = GuiGetStyle(SCROLLBAR, SLIDER_SIZE); // Save default slider size
|
int prevSliderSize = GuiGetStyle(SCROLLBAR, SLIDER_WIDTH); // Save default slider size
|
||||||
int prevScrollSpeed = GuiGetStyle(SCROLLBAR, SCROLL_SPEED); // Save default scroll speed
|
int prevScrollSpeed = GuiGetStyle(SCROLLBAR, SCROLL_SPEED); // Save default scroll speed
|
||||||
GuiSetStyle(SCROLLBAR, SLIDER_SIZE, sliderSize); // Change slider size
|
GuiSetStyle(SCROLLBAR, SLIDER_WIDTH, sliderSize); // Change slider size
|
||||||
GuiSetStyle(SCROLLBAR, SCROLL_SPEED, count - visibleItems); // Change scroll speed
|
GuiSetStyle(SCROLLBAR, SCROLL_SPEED, count - visibleItems); // Change scroll speed
|
||||||
|
|
||||||
startIndex = GuiScrollBar(scrollBarBounds, startIndex, 0, count - visibleItems);
|
startIndex = GuiScrollBar(scrollBarBounds, startIndex, 0, count - visibleItems);
|
||||||
|
|
||||||
GuiSetStyle(SCROLLBAR, SCROLL_SPEED, prevScrollSpeed); // Reset scroll speed to default
|
GuiSetStyle(SCROLLBAR, SCROLL_SPEED, prevScrollSpeed); // Reset scroll speed to default
|
||||||
GuiSetStyle(SCROLLBAR, SLIDER_SIZE, prevSliderSize); // Reset slider size to default
|
GuiSetStyle(SCROLLBAR, SLIDER_WIDTH, prevSliderSize); // Reset slider size to default
|
||||||
}
|
}
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user