-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from fallahn/golf-1.16
Golf 1.16.2
- Loading branch information
Showing
45 changed files
with
5,541 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2019-2022 Zhuang Guan | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <crogine/Config.hpp> | ||
|
||
#include <filesystem> | ||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
#include <set> | ||
|
||
|
||
#ifndef IMGUI_VERSION | ||
# error "include imgui.h before this header" | ||
#endif | ||
|
||
using ImGuiFileBrowserFlags = int; | ||
|
||
enum ImGuiFileBrowserFlags_ | ||
{ | ||
ImGuiFileBrowserFlags_SelectDirectory = 1 << 0, // select directory instead of regular file | ||
ImGuiFileBrowserFlags_EnterNewFilename = 1 << 1, // allow user to enter new filename when selecting regular file | ||
ImGuiFileBrowserFlags_NoModal = 1 << 2, // file browsing window is modal by default. specify this to use a popup window | ||
ImGuiFileBrowserFlags_NoTitleBar = 1 << 3, // hide window title bar | ||
ImGuiFileBrowserFlags_NoStatusBar = 1 << 4, // hide status bar at the bottom of browsing window | ||
ImGuiFileBrowserFlags_CloseOnEsc = 1 << 5, // close file browser when pressing 'ESC' | ||
ImGuiFileBrowserFlags_CreateNewDir = 1 << 6, // allow user to create new directory | ||
ImGuiFileBrowserFlags_MultipleSelection = 1 << 7, // allow user to select multiple files. this will hide ImGuiFileBrowserFlags_EnterNewFilename | ||
ImGuiFileBrowserFlags_HideRegularFiles = 1 << 8, // hide regular files when ImGuiFileBrowserFlags_SelectDirectory is enabled | ||
ImGuiFileBrowserFlags_ConfirmOnEnter = 1 << 9, // confirm selection when pressing 'ENTER' | ||
}; | ||
|
||
namespace ImGui | ||
{ | ||
/*! | ||
Usage: Create an instance of the the FileBrowser object, usually at State scope. | ||
ImGuiFileBrowserFlags can be passed to ctor to configure behaviour such as multiple | ||
file selection or opening a directory. | ||
Then: | ||
\code | ||
//configure properties | ||
m_fileBrowser.SetTitle("My Browser"); | ||
m_fileBrowser.SetTypeFileters({".png"}); | ||
registerWindow([]() | ||
{ | ||
if(ImGui::Begin("My Window")) | ||
{ | ||
if(ImGui::Button("Open Image")) | ||
{ | ||
m_fileBrowser.Open(); | ||
} | ||
m_fileBrowser.Display(); //updates internal render list | ||
if(m_fileBrowser.HasSelected()) | ||
{ | ||
const std::string path = m_fileBrowser.GetSelected().string(); //note actually returns std::filesystem::path - may be more useful | ||
m_fileBrowser.ClearSelected(); | ||
//do stuff with file path | ||
} | ||
} | ||
ImGui::End(); | ||
}); | ||
\endcode | ||
*/ | ||
class CRO_EXPORT_API FileBrowser | ||
{ | ||
public: | ||
|
||
// pwd is set to current working directory by default | ||
explicit FileBrowser(ImGuiFileBrowserFlags flags = 0); | ||
|
||
FileBrowser(const FileBrowser ©From); | ||
|
||
FileBrowser &operator=(const FileBrowser ©From); | ||
|
||
// set the window position (in pixels) | ||
// default is centered | ||
void SetWindowPos(int posx, int posy) noexcept; | ||
|
||
// set the window size (in pixels) | ||
// default is (700, 450) | ||
void SetWindowSize(int width, int height) noexcept; | ||
|
||
// set the window title text | ||
void SetTitle(std::string title); | ||
|
||
// open the browsing window | ||
void Open(); | ||
|
||
// close the browsing window | ||
void Close(); | ||
|
||
// the browsing window is opened or not | ||
bool IsOpened() const noexcept; | ||
|
||
// display the browsing window if opened | ||
void Display(); | ||
|
||
// returns true when there is a selected filename | ||
bool HasSelected() const noexcept; | ||
|
||
// set current browsing directory | ||
bool SetPwd(const std::filesystem::path &pwd = | ||
std::filesystem::current_path()); | ||
|
||
// get current browsing directory | ||
const std::filesystem::path &GetPwd() const noexcept; | ||
|
||
// returns selected filename. make sense only when HasSelected returns true | ||
// when ImGuiFileBrowserFlags_MultipleSelection is enabled, only one of | ||
// selected filename will be returned | ||
std::filesystem::path GetSelected() const; | ||
|
||
// returns all selected filenames. | ||
// when ImGuiFileBrowserFlags_MultipleSelection is enabled, use this | ||
// instead of GetSelected | ||
std::vector<std::filesystem::path> GetMultiSelected() const; | ||
|
||
// set selected filename to empty | ||
void ClearSelected(); | ||
|
||
// (optional) set file type filters. eg. { ".h", ".cpp", ".hpp" } | ||
// ".*" matches any file types | ||
void SetTypeFilters(const std::vector<std::string> &typeFilters); | ||
|
||
// set currently applied type filter | ||
// default value is 0 (the first type filter) | ||
void SetCurrentTypeFilterIndex(int index); | ||
|
||
// when ImGuiFileBrowserFlags_EnterNewFilename is set | ||
// this function will pre-fill the input dialog with a filename. | ||
void SetInputName(std::string_view input); | ||
|
||
private: | ||
|
||
template <class Functor> | ||
struct ScopeGuard | ||
{ | ||
ScopeGuard(Functor&& t) : func(std::move(t)) { } | ||
|
||
~ScopeGuard() { func(); } | ||
|
||
private: | ||
|
||
Functor func; | ||
}; | ||
|
||
struct FileRecord | ||
{ | ||
bool isDir = false; | ||
std::filesystem::path name; | ||
std::string showName; | ||
std::filesystem::path extension; | ||
}; | ||
|
||
static std::string ToLower(const std::string &s); | ||
|
||
void UpdateFileRecords(); | ||
|
||
void SetPwdUncatched(const std::filesystem::path &pwd); | ||
|
||
bool IsExtensionMatched(const std::filesystem::path &extension) const; | ||
|
||
void ClearRangeSelectionState(); | ||
|
||
#ifdef _WIN32 | ||
static std::uint32_t GetDrivesBitMask(); | ||
#endif | ||
|
||
// for c++17 compatibility | ||
|
||
#if defined(__cpp_lib_char8_t) | ||
static std::string u8StrToStr(std::u8string s); | ||
#endif | ||
static std::string u8StrToStr(std::string s); | ||
|
||
int width_; | ||
int height_; | ||
int posX_; | ||
int posY_; | ||
ImGuiFileBrowserFlags flags_; | ||
|
||
std::string title_; | ||
std::string openLabel_; | ||
|
||
bool openFlag_; | ||
bool closeFlag_; | ||
bool isOpened_; | ||
bool ok_; | ||
bool posIsSet_; | ||
|
||
std::string statusStr_; | ||
|
||
std::vector<std::string> typeFilters_; | ||
unsigned int typeFilterIndex_; | ||
bool hasAllFilter_; | ||
|
||
std::filesystem::path pwd_; | ||
std::set<std::filesystem::path> selectedFilenames_; | ||
unsigned int rangeSelectionStart_; // enable range selection when shift is pressed | ||
|
||
std::vector<FileRecord> fileRecords_; | ||
|
||
// IMPROVE: truncate when selectedFilename_.length() > inputNameBuf_.size() - 1 | ||
static constexpr size_t INPUT_NAME_BUF_SIZE = 512; | ||
std::unique_ptr<std::array<char, INPUT_NAME_BUF_SIZE>> inputNameBuf_; | ||
|
||
std::string openNewDirLabel_; | ||
std::unique_ptr<std::array<char, INPUT_NAME_BUF_SIZE>> newDirNameBuf_; | ||
|
||
#ifdef _WIN32 | ||
uint32_t drives_; | ||
#endif | ||
}; | ||
} // namespace ImGui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.