GithubHelp home page GithubHelp logo

earsuit / imfiledialog Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 194 KB

Easy compile C++20 cross platform ImGui file browser with system theme style icon

License: MIT License

CMake 2.44% C++ 97.56%

imfiledialog's Introduction

ImFileDialog

Some improvements from dfranx/ImFileDialog based on my own needs, such as

  • render item icon follows the system theme
  • ask confirmation if file exits in save mode
  • replaced the SDL2 with glfw for the example
  • upgrade to C++20
  • remove the need of GTK-3 on Linux
  • better support on Windows, Linux, Macos
  • Allow use Gettext for translation, enable with compile flag USE_GETTEXT.

Dependencies

Compile example

Windows

Install dependencies

The example requires OpenGL, GLFW3 and GLEW, on Windows, these could be installed via vcpkg.

Build

  1. git submodule init && git submodule update
  2. mkdir build && cd build
  3. cmake -B . -S .. "-DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]\scripts/buildsystems/vcpkg.cmake"
  4. cmake --build .
  5. cd Debug
  6. ./ImFileDialogExample.exe

Linux

Install dependencies

sudo apt-get install libgl1-mesa-dev libglew-dev libglfw3-dev -y

Build

  1. git submodule init && git submodule update
  2. mkdir build && cd build
  3. cmake ..
  4. make -j
  5. ./ImFileDialogExample

MacOS

Install dependencies

brew install glew glfw

Build

  1. git submodule init && git submodule update
  2. mkdir build && cd build
  3. cmake ..
  4. make -j
  5. ./ImFileDialogExample

Usage

To use ImFileDialog in your project, just add ImFileDialog.h, ImFileDialog.cpp and StbImpl.cpp to it.

Please note if you already use stb_image library in your project, just exculde the StbImpl.cpp, otherwise you will have multiple definition of methods from the stb_image library.

According to https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1423r2.html, to make it compile with C++20, the simplest solution to upgrade to c++20 with char8_t, so add /Zc:char8_t- compiler flag to MSVC or -fno-char8_t if you are using Clang/g++.

And also add -ftemplate-depth=2048 compiler flag if you are using clang/g++, this is to constexpr std::array compile.

Here's an example on how to use ImFileDialog:

  1. You need to set the CreateTexture and DeleteTexture function
ifd::FileDialog::getInstance().createTexture = [](uint8_t* data, int w, int h, ifd::Format fmt) -> void* {
	GLuint tex;

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	
	if (fmt == ifd::Format::BGRA) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
	} else if (fmt == ifd::Format::RGBA) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
	} else {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	}

	glGenerateMipmap(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, 0);

	return (void*)tex;
};
ifd::FileDialog::getInstance().deleteTexture = [](void* tex) {
	GLuint texID = (GLuint)tex;
	glDeleteTextures(1, &texID);
};

Where ifd::Format is defined as:

enum class Format: char{
	BGRA,
	RGBA,
	RGB
};
  1. Open a file dialog on button press (just an example):
if (ImGui::Button("Open a texture"))
	ifd::FileDialog::getInstance().open("TextureOpenDialog", "Open a texture", "Image file (*.png;*.jpg;*.jpeg;*.bmp;*.tga){.png,.jpg,.jpeg,.bmp,.tga},.*");
  1. Render and check if done:
if (ifd::FileDialog::getInstance().isDone("TextureOpenDialog")) {
	if (ifd::FileDialog::getInstance().hasResult()) {
		std::string res = ifd::FileDialog::getInstance().getResult().u8string();
		printf("OPEN[%s]\n", res.c_str());
	}
	ifd::FileDialog::getInstance().close();
}

File filter syntax:

Name1 {.ext1,.ext2}, Name2 {.ext3,.ext4},.*

Screenshots

1. Table view:

Table view

2. Icon view:

Icon view

3. Zooming in:

Zooming in

4. Favorites:

Favorites

5. Image preview + threading (CTRL + scroll):

Table view

LICENSE

ImFileDialog is licensed under MIT license. See LICENSE for more details.

imfiledialog's People

Contributors

earsuit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

thanhgialai3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.