cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

ImGuiFileBrowser.h (5045B)


      1#ifndef IMGUIFILEBROWSER_H
      2#define IMGUIFILEBROWSER_H
      3
      4#include <string>
      5#include <vector>
      6#include "../imgui/imgui.h"
      7
      8namespace imgui_addons
      9{
     10    class ImGuiFileBrowser
     11    {
     12        public:
     13            ImGuiFileBrowser();
     14            ~ImGuiFileBrowser();
     15
     16            enum class DialogMode
     17            {
     18                SELECT, //Select Directory Mode
     19                OPEN,   //Open File mode
     20                SAVE    //Save File mode.
     21            };
     22
     23            /* Use this to show an open file dialog. The function takes label for the window,
     24             * the size, a DialogMode enum value defining in which mode the dialog should operate and optionally the extensions that are valid for opening.
     25             * Note that the select directory mode doesn't need any extensions.
     26             */
     27            bool showFileDialog(const std::string& label, const DialogMode mode, const ImVec2& sz_xy = ImVec2(0,0), const std::string& valid_types = "*.*", bool* is_open = 0);
     28
     29            /* Store the opened/saved file name or dir name (incase of selectDirectoryDialog) and the absolute path to the selection
     30             * Should only be accessed when above functions return true else may contain garbage.
     31             */
     32            std::string selected_fn;
     33            std::string selected_path;
     34            std::string ext;    // Store the saved file extension
     35
     36
     37        private:
     38            struct Info
     39            {
     40                Info(std::string name, bool is_hidden) : name(name), is_hidden(is_hidden)
     41                {
     42                }
     43                std::string name;
     44                bool is_hidden;
     45            };
     46
     47            //Enum used as bit flags.
     48            enum FilterMode
     49            {
     50                FilterMode_Files = 0x01,
     51                FilterMode_Dirs = 0x02
     52            };
     53
     54            //Helper Functions
     55            static std::string wStringToString(const wchar_t* wchar_arr);
     56            static bool alphaSortComparator(const Info& a, const Info& b);
     57            ImVec2 getButtonSize(std::string button_text);
     58
     59            /* Helper Functions that render secondary modals
     60             * and help in validating file extensions and for filtering, parsing top navigation bar.
     61             */
     62            void setValidExtTypes(const std::string& valid_types_string);
     63            bool validateFile();
     64            void showErrorModal();
     65            void showInvalidFileModal();
     66            bool showReplaceFileModal();
     67            void showHelpMarker(std::string desc);
     68            void parsePathTabs(std::string str);
     69            void filterFiles(int filter_mode);
     70
     71            /* Core Functions that render the 4 different regions making up
     72             * a simple file dialog
     73             */
     74            bool renderNavAndSearchBarRegion();
     75            bool renderFileListRegion();
     76            bool renderInputTextAndExtRegion();
     77            bool renderButtonsAndCheckboxRegion();
     78            bool renderInputComboBox();
     79            void renderExtBox();
     80
     81            /* Core Functions that handle navigation and
     82             * reading directories/files
     83             */
     84            bool readDIR(std::string path);
     85            bool onNavigationButtonClick(int idx);
     86            bool onDirClick(int idx);
     87
     88            // Functions that reset state and/or clear file list when reading new directory
     89            void clearFileList();
     90            void closeDialog();
     91
     92            #if defined (WIN32) || defined (_WIN32) || defined (__WIN32)
     93            bool loadWindowsDrives(); // Helper Function for Windows to load Drive Letters.
     94            #endif
     95
     96            #if defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__)
     97            void initCurrentPath();   // Helper function for UNIX based system to load Absolute path using realpath
     98            #endif
     99
    100            ImVec2 min_size, max_size, input_combobox_pos, input_combobox_sz;
    101            DialogMode dialog_mode;
    102            int filter_mode, col_items_limit, selected_idx, selected_ext_idx;
    103            float col_width, ext_box_width;
    104            bool show_hidden, show_inputbar_combobox, is_dir, is_appearing, filter_dirty, validate_file;
    105            char input_fn[256];
    106            bool* is_open_;
    107
    108            std::vector<std::string> valid_exts;
    109            std::vector<std::string> current_dirlist;
    110            std::vector<Info> subdirs;
    111            std::vector<Info> subfiles;
    112            std::string current_path, error_msg, error_title, invfile_modal_id, repfile_modal_id;
    113
    114            ImGuiTextFilter filter;
    115            std::string valid_types;
    116            std::vector<Info> filtered_dirs; // Note: We don't need to call delete. It's just for storing filtered items from subdirs and subfiles so we don't use PassFilter every frame.
    117            std::vector<Info> filtered_files;
    118            std::vector< std::reference_wrapper<std::string> > inputcb_filter_files;
    119    };
    120}
    121
    122
    123#endif // IMGUIFILEBROWSER_H