Skip to content

Commit

Permalink
Fix closing of editors on window close
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj-Yadav committed Jun 9, 2024
1 parent 0418a39 commit 4cae3c0
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 188 deletions.
69 changes: 21 additions & 48 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,78 +1,51 @@
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(ffmpeg_node_editor LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS
ON
CACHE INTERNAL "")

find_package(Backward CONFIG REQUIRED)
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(reproc CONFIG REQUIRED)
find_package(reproc++ CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

# Import non vcpkg stuff
add_subdirectory(./third_party)

add_executable(
ffmpeg_node_editor
src/backend.cpp
src/backend_opengl.cpp
src/backend_win32_d3d12.cpp
add_library(
core STATIC
src/ffmpeg/filter_graph.cpp
src/ffmpeg/profile.cpp
src/ffmpeg/runner.cpp
src/file_utils.cpp
src/imgui_extras.cpp
src/main.cpp
src/node_editor.cpp
src/pref.cpp
src/string_utils.cpp)

target_include_directories(ffmpeg_node_editor
PUBLIC "${CMAKE_SOURCE_DIR}/include")

target_include_directories(core PUBLIC "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(
ffmpeg_node_editor
PRIVATE ${OPENGL_gl_LIBRARY}
Backward::Interface
IconFontCppHeaders
glfw
imgui
nlohmann_json::nlohmann_json
reproc
reproc++
spdlog::spdlog
tinyfiledialogs)

if(MSVC AND FALSE)
target_compile_definitions(ffmpeg_node_editor
PUBLIC FFMPEG_NODE_EDITOR_USE_WIN32_D3D12)
target_link_libraries(ffmpeg_node_editor PRIVATE d3d12.lib d3dcompiler.lib
dxgi.lib)
else()
target_compile_definitions(ffmpeg_node_editor
PUBLIC FFMPEG_NODE_EDITOR_USE_GLFW_OPENGL)
endif()
core
PUBLIC imgui
spdlog::spdlog
tinyfiledialogs
nlohmann_json::nlohmann_json
IconFontCppHeaders
reproc
reproc++)

# Main Executable
add_executable(ffmpeg_node_editor src/backend.cpp src/backend_glfw_opengl.cpp
src/backend_win32_d3d12.cpp src/main.cpp)
target_link_libraries(ffmpeg_node_editor PRIVATE core Backward::Interface)

# Setup Test, coverage and benchmarks
find_package(GTest CONFIG REQUIRED)

set(TEST_SRCS
src/ffmpeg/runner_test.cpp src/ffmpeg/runner.cpp src/imgui_extras_test.cpp
src/imgui_extras.cpp src/string_utils.cpp src/util_test.cpp)
add_executable(tests src/ffmpeg/runner_test.cpp src/imgui_extras_test.cpp
src/util_test.cpp)

add_executable(tests ${TEST_SRCS})
target_include_directories(tests PUBLIC "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(
tests
PRIVATE GTest::gtest
GTest::gtest_main
imgui
IconFontCppHeaders
reproc
reproc++
spdlog::spdlog)
target_link_libraries(tests PRIVATE GTest::gtest GTest::gtest_main core)
94 changes: 7 additions & 87 deletions include/imgui_extras.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

#include <string>

#include "IconsFontAwesome6.h"
#include "file_utils.hpp"
#include "util.hpp"

namespace ImGui {

inline ImVec2 GetItemRectPoint(float wx = 0, float wy = 0) {
Expand Down Expand Up @@ -41,83 +37,13 @@ namespace ImGui {
return ColorConvertU32ToFloat4(ColorConvertHexToU32(hex));
}

inline bool InputFont(
const char* label, std::string& str, float width = -1) {
#ifdef _WIN32
PushItemWidth(std::max(width - GetFrameHeight(), 0.0f));
defer w([&]() { PopItemWidth(); });
if (InputText(label, &str)) { return true; }
Spring(0, 0);
if (FontButton(ICON_FA_FONT)) {
auto path = selectFont();
if (path.has_value()) {
str = path.value().string();
return true;
}
}
return false;
#else
return InputText(label, &str);
#endif
}
bool InputFont(const char* label, std::string& str, float width = -1);

inline bool InputFile(
const char* label, std::string& str, float width = -1) {
PushItemWidth(std::max(width - GetFrameHeight(), 0.0f));
defer w([&]() { PopItemWidth(); });
if (InputText(label, &str)) { return true; }
Spring(0, 0);
if (FontButton(ICON_FA_FOLDER_OPEN)) {
auto path = openFile();
if (path.has_value()) {
str = path->string();
return true;
}
}
return false;
}
bool InputFile(const char* label, std::string& str, float width = -1);

inline bool InputColor(
const char* label, std::string& str, float width = -1) {
PushItemWidth(std::max(width - GetFrameHeight(), 0.0f));
defer w([&]() { PopItemWidth(); });
if (InputText(label, &str)) { return true; }
Spring(0, 0);
auto color = ColorConvertHexToFloat4(str);
if (ColorEdit4(
"##col", &color.x,
ImGuiColorEditFlags_AlphaPreviewHalf |
ImGuiColorEditFlags_AlphaBar |
ImGuiColorEditFlags_NoOptions)) {
str = ColorConvertFloat4ToHex(color);
return true;
}
return false;
}
bool InputColor(const char* label, std::string& str, float width = -1);

inline bool InputCheckbox(
const char* label, std::string& str, float width = -1) {
PushItemWidth(std::max(width - GetFrameHeight(), 0.0f));
defer w([&]() { PopItemWidth(); });
const int TRUE = 1, FALSE = 0, MAYBE = 2;
int v = FALSE;
if (str == "1") {
v = TRUE | MAYBE;
} else if (str != "0") {
v = MAYBE;
}
if (InputText(label, &str)) { return true; }
Spring(0, 0);
if (CheckboxFlags("##b", &v, TRUE | MAYBE)) {
if (v == FALSE) {
str = "0";
} else {
str = "1";
}
return true;
}
return false;
}
bool InputCheckbox(const char* label, std::string& str, float width = -1);

inline int UnsavedDocumentFlag(
bool unsaved, int flag = ImGuiWindowFlags_None) {
Expand All @@ -132,14 +58,8 @@ namespace ImGui {
DISCARD_CHANGES
};

inline UnsavedDocumentAction UnsavedDocumentClose(
UnsavedDocumentAction UnsavedDocumentClose(
bool unsaved, bool open, std::string const& title,
std::string const& text) {
if (open || !unsaved) { return UnsavedDocumentAction::NO_OP; }
auto result = showActionDialog(title, text);
if (result == 0) { return UnsavedDocumentAction::CANCEL_CLOSE; }
if (result == 1) { return UnsavedDocumentAction::SAVE_CHANGES; }
if (result == 2) { return UnsavedDocumentAction::DISCARD_CHANGES; }
return UnsavedDocumentAction::NO_OP;
}
std::string const& text);

} // namespace ImGui
11 changes: 9 additions & 2 deletions include/node_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ class NodeEditor {
std::string name;
std::filesystem::path path;

bool isOpen = true;

public:
NodeEditor(const Profile& p, std::string n);
[[nodiscard]] std::string getName() const;
[[nodiscard]] const std::filesystem::path& getPath() const { return path; };
[[nodiscard]] bool save();
void setPath(std::filesystem::path& p) { path = p; };
std::pair<bool, bool> draw(const Preference& pref);
void draw(const Preference& pref, bool& focused);

[[nodiscard]] bool isClosed() const { return !isOpen; }
void close();

[[nodiscard]] bool hasChanges() const { return g.changed(); }
bool save();
bool load(const std::filesystem::path& path);
};
9 changes: 7 additions & 2 deletions include/pref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,18 @@ struct Preference {
std::filesystem::path font;
int fontSize;
std::string player;
bool show = false;
bool unsaved = false;

bool isOpen = false;

Preference();

void setOptions() const;
void draw();

void close();

[[nodiscard]] bool hasChanges() const { return unsaved; }
bool load();
bool save();
bool draw();
};
2 changes: 1 addition & 1 deletion src/backend_opengl.cpp → src/backend_glfw_opengl.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef FFMPEG_NODE_EDITOR_USE_GLFW_OPENGL
#ifdef IMGUI_USE_GLFW_OPENGL
#include <GLFW/glfw3.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
Expand Down
9 changes: 2 additions & 7 deletions src/backend_win32_d3d12.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef FFMPEG_NODE_EDITOR_USE_WIN32_D3D12
#ifdef IMGUI_USE_WIN32_D3D12
#include <array>

#include "backend.hpp"
Expand Down Expand Up @@ -79,7 +79,7 @@ namespace Window {
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

bool InitWindow(ImGuiConfigFlags flags, const Preference& pref) {
ImGui_ImplWin32_EnableDpiAwareness();
// ImGui_ImplWin32_EnableDpiAwareness();
wc = {
sizeof(wc),
CS_OWNDC,
Expand Down Expand Up @@ -496,11 +496,6 @@ namespace Window {
CreateRenderTarget();
}
return 0;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) ==
SC_KEYMENU) // Disable ALT application menu
return 0;
break;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
Expand Down
5 changes: 3 additions & 2 deletions src/ffmpeg/filter_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,9 @@ FilterGraphError FilterGraph::play(const Preference& pref, const NodeId& id) {
fmt::format("[s{}]", socketId.val);
outputSocketNames[socketId.val] =
fmt::format("[s{}]", socketId.val);
fmt::format_to(
std::back_inserter(buff), inputSocketNames[socketId.val]);
buff += inputSocketNames[socketId.val];
// fmt::format_to(
// std::back_inserter(buff), inputSocketNames[socketId.val]);
});
if (!isInput) { fmt::format_to(std::back_inserter(buff), ";"); }
},
Expand Down
2 changes: 1 addition & 1 deletion src/ffmpeg/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ Profile GetProfile() {

nlohmann::json json = profile.filters;
std::ofstream o(path.appDir / "filters.json", std::ios_base::binary);
o << std::setw(4) << json;
o << json.dump(1, '\t');
}

profile.filters.push_back(
Expand Down
4 changes: 2 additions & 2 deletions src/ffmpeg/runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int Runner::lineScanner(
return reproc::run(args, options).first;
}

SPDLOG_INFO("args: {}", args);
SPDLOG_DEBUG("args: {}", args);

auto readLambda = [&](auto stream, auto bytes, auto n) {
if (readStdErr && stream != reproc::stream::err) {
Expand Down Expand Up @@ -163,7 +163,7 @@ std::pair<int, std::string> Runner::play(

auto tempString = tempPath.string();

std::vector<std::string_view> player_args;
std::vector<std::string_view> player_args{"cmd", "/C", "start"};
for (auto& elem : str::split(player, '\n')) {
if (elem == "%f") {
player_args.emplace_back(tempString);
Expand Down
Loading

0 comments on commit 4cae3c0

Please sign in to comment.