raygui/examples/custom_file_dialog/custom_file_dialog.c

112 lines
4.2 KiB
C
Raw Normal View History

2019-08-11 12:02:43 +02:00
/*******************************************************************************************
*
* raygui - custom file dialog to load image
*
* DEPENDENCIES:
* raylib 4.0 - Windowing/input management and drawing.
* raygui 3.0 - Immediate-mode GUI controls.
2019-08-11 12:02:43 +02:00
*
* COMPILATION (Windows - MinGW):
* gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
*
* LICENSE: zlib/libpng
*
2021-12-31 20:06:32 +01:00
* Copyright (c) 2016-2022 Ramon Santamaria (@raysan5)
2019-08-11 12:02:43 +02:00
*
**********************************************************************************************/
#include "raylib.h"
#define RAYGUI_IMPLEMENTATION
#include "../../src/raygui.h"
#undef RAYGUI_IMPLEMENTATION // Avoid including raygui implementation again
#define GUI_FILE_DIALOG_IMPLEMENTATION
#include "gui_file_dialog.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main()
{
// Initialization
//---------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 560;
2019-08-11 12:02:43 +02:00
InitWindow(screenWidth, screenHeight, "raygui - custom modal dialog");
SetExitKey(0);
// Custom file dialog
GuiFileDialogState fileDialogState = InitGuiFileDialog(420, 310, GetWorkingDirectory(), false);
2019-08-11 12:02:43 +02:00
bool exitWindow = false;
char fileNameToLoad[512] = { 0 };
2019-08-11 12:02:43 +02:00
Texture texture = { 0 };
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!exitWindow) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
exitWindow = WindowShouldClose();
2019-08-11 12:02:43 +02:00
if (fileDialogState.SelectFilePressed)
{
// Load image file (if supported extension)
if (IsFileExtension(fileDialogState.fileNameText, ".png"))
{
strcpy(fileNameToLoad, TextFormat("%s/%s", fileDialogState.dirPathText, fileDialogState.fileNameText));
UnloadTexture(texture);
texture = LoadTexture(fileNameToLoad);
}
2019-08-11 12:02:43 +02:00
fileDialogState.SelectFilePressed = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
2019-08-11 12:02:43 +02:00
DrawTexture(texture, GetScreenWidth()/2 - texture.width/2, GetScreenHeight()/2 - texture.height/2 - 5, WHITE);
DrawRectangleLines(GetScreenWidth()/2 - texture.width/2, GetScreenHeight()/2 - texture.height/2 - 5, texture.width, texture.height, BLACK);
2019-08-11 12:02:43 +02:00
DrawText(fileNameToLoad, 208, GetScreenHeight() - 20, 10, GRAY);
2019-08-11 12:02:43 +02:00
// raygui: controls drawing
//----------------------------------------------------------------------------------
if (fileDialogState.fileDialogActive) GuiLock();
if (GuiButton((Rectangle){ 20, 20, 140, 30 }, GuiIconText(ICON_FILE_OPEN, "Open Image"))) fileDialogState.fileDialogActive = true;
2019-08-11 12:02:43 +02:00
GuiUnlock();
2019-08-11 12:02:43 +02:00
// GUI: Dialog Window
//--------------------------------------------------------------------------------
GuiFileDialog(&fileDialogState);
//--------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}