2
0

custom_file_dialog.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************************
  2. *
  3. * raygui - custom file dialog to load image
  4. *
  5. * DEPENDENCIES:
  6. * raylib 4.0 - Windowing/input management and drawing.
  7. * raygui 3.0 - Immediate-mode GUI controls.
  8. *
  9. * COMPILATION (Windows - MinGW):
  10. * gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
  11. *
  12. * LICENSE: zlib/libpng
  13. *
  14. * Copyright (c) 2016-2024 Ramon Santamaria (@raysan5)
  15. *
  16. **********************************************************************************************/
  17. #include "raylib.h"
  18. #define RAYGUI_IMPLEMENTATION
  19. #include "../../src/raygui.h"
  20. #undef RAYGUI_IMPLEMENTATION // Avoid including raygui implementation again
  21. #define GUI_WINDOW_FILE_DIALOG_IMPLEMENTATION
  22. #include "gui_window_file_dialog.h"
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main()
  27. {
  28. // Initialization
  29. //---------------------------------------------------------------------------------------
  30. int screenWidth = 800;
  31. int screenHeight = 560;
  32. InitWindow(screenWidth, screenHeight, "raygui - custom modal dialog");
  33. SetExitKey(0);
  34. // Custom file dialog
  35. GuiWindowFileDialogState fileDialogState = InitGuiWindowFileDialog(GetWorkingDirectory());
  36. bool exitWindow = false;
  37. char fileNameToLoad[512] = { 0 };
  38. Texture texture = { 0 };
  39. SetTargetFPS(60);
  40. //--------------------------------------------------------------------------------------
  41. // Main game loop
  42. while (!exitWindow) // Detect window close button or ESC key
  43. {
  44. // Update
  45. //----------------------------------------------------------------------------------
  46. exitWindow = WindowShouldClose();
  47. if (fileDialogState.SelectFilePressed)
  48. {
  49. // Load image file (if supported extension)
  50. if (IsFileExtension(fileDialogState.fileNameText, ".png"))
  51. {
  52. strcpy(fileNameToLoad, TextFormat("%s" PATH_SEPERATOR "%s", fileDialogState.dirPathText, fileDialogState.fileNameText));
  53. UnloadTexture(texture);
  54. texture = LoadTexture(fileNameToLoad);
  55. }
  56. fileDialogState.SelectFilePressed = false;
  57. }
  58. //----------------------------------------------------------------------------------
  59. // Draw
  60. //----------------------------------------------------------------------------------
  61. BeginDrawing();
  62. ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));
  63. DrawTexture(texture, GetScreenWidth()/2 - texture.width/2, GetScreenHeight()/2 - texture.height/2 - 5, WHITE);
  64. DrawRectangleLines(GetScreenWidth()/2 - texture.width/2, GetScreenHeight()/2 - texture.height/2 - 5, texture.width, texture.height, BLACK);
  65. DrawText(fileNameToLoad, 208, GetScreenHeight() - 20, 10, GRAY);
  66. // raygui: controls drawing
  67. //----------------------------------------------------------------------------------
  68. if (fileDialogState.windowActive) GuiLock();
  69. if (GuiButton((Rectangle){ 20, 20, 140, 30 }, GuiIconText(ICON_FILE_OPEN, "Open Image"))) fileDialogState.windowActive = true;
  70. GuiUnlock();
  71. // GUI: Dialog Window
  72. //--------------------------------------------------------------------------------
  73. GuiWindowFileDialog(&fileDialogState);
  74. //--------------------------------------------------------------------------------
  75. //----------------------------------------------------------------------------------
  76. EndDrawing();
  77. //----------------------------------------------------------------------------------
  78. }
  79. // De-Initialization
  80. //--------------------------------------------------------------------------------------
  81. UnloadTexture(texture); // Unload texture
  82. CloseWindow(); // Close window and OpenGL context
  83. //--------------------------------------------------------------------------------------
  84. return 0;
  85. }