Skip to content

Commit

Permalink
build: Exclude CImGui, ImPlot, ImNodes and ColorTextEditor by default
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Sep 13, 2024
1 parent ba2e5a5 commit e7daaf9
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 34 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(FENESTRA_APPLICATION_PLUGIN_DIRECTORY "" CACHE STRING
set(FENESTRA_STRICT_WARNINGS ON CACHE BOOL "Enable strict warnings")
set(FENESTRA_DEFAULT_INSTALL_TARGETS ON CACHE BOOL "Enable default install targets")
set(FENESTRA_MACOS_CREATE_BUNDLE OFF CACHE BOOL "Create macOS bundle")
set(FENESTRA_BUNDLED_IMGUI_LIBRARIES "" CACHE STRING "ImGui libraries to use")


set(FENESTRA_BASE_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}")
Expand Down
54 changes: 35 additions & 19 deletions gui/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@
#include <opengl_support.h>

#include <fenestra/ui/imgui_extensions.h>
#include <implot.h>
#include <implot_internal.h>
#include <imnodes.h>
#include <imnodes_internal.h>

#if defined(IMGUI_LIBRARY_IMPLOT)
#include <implot.h>
#include <implot_internal.h>
#endif

#if defined(IMGUI_LIBRARY_IMNODES)
#include <imnodes.h>
#include <imnodes_internal.h>
#endif

#include <wolv/utils/string.hpp>

Expand Down Expand Up @@ -875,20 +881,37 @@ namespace fene {

// Initialize ImGui and all other ImGui extensions
GImGui = ImGui::CreateContext(fonts);
GImPlot = ImPlot::CreateContext();
GImNodes = ImNodes::CreateContext();
#if defined(IMGUI_LIBRARY_IMPLOT)
GImPlot = ImPlot::CreateContext();
#endif
#if defined(IMGUI_LIBRARY_IMNODES)
GImNodes = ImNodes::CreateContext();
#endif

ImGuiIO &io = ImGui::GetIO();
ImGuiStyle &style = ImGui::GetStyle();

io.UserData = &m_imguiCustomData;

ImGui::StyleColorsDark();
ImPlot::StyleColorsDark();
ImNodes::StyleColorsDark();
#if defined(IMGUI_LIBRARY_IMPLOT)
ImPlot::StyleColorsDark();
#endif
#if defined(IMGUI_LIBRARY_IMNODES)
ImNodes::StyleColorsDark();
ImNodes::GetStyle().Flags = ImNodesStyleFlags_NodeOutline | ImNodesStyleFlags_GridLines;

ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkCreationOnSnap);

// Allow ImNodes links to always be detached without holding down any button
{
static bool always = true;
ImNodes::GetIO().LinkDetachWithModifierClick.Modifier = &always;
}
#endif
ImGuiExt::StyleCustomColorsDark();

ImNodes::GetStyle().Flags = ImNodesStyleFlags_NodeOutline | ImNodesStyleFlags_GridLines;

io.ConfigFlags |= ImGuiConfigFlags_DockingEnable | ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigWindowsMoveFromTitleBarOnly = true;
Expand All @@ -898,15 +921,6 @@ namespace fene {

io.ConfigViewportsNoTaskBarIcon = false;

ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkDetachWithDragClick);
ImNodes::PushAttributeFlag(ImNodesAttributeFlags_EnableLinkCreationOnSnap);

// Allow ImNodes links to always be detached without holding down any button
{
static bool always = true;
ImNodes::GetIO().LinkDetachWithModifierClick.Modifier = &always;
}

const auto scale = float(FenestraManager::System::getGlobalScale());
style.ScaleAllSizes(scale);
io.DisplayFramebufferScale = ImVec2(scale, scale);
Expand Down Expand Up @@ -986,7 +1000,9 @@ namespace fene {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL3_Shutdown();

ImPlot::DestroyContext();
#if defined(IMGUI_LIBRARY_IMPLOT)
ImPlot::DestroyContext();
#endif
ImGui::DestroyContext();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/libfenestra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ target_link_libraries(libfenestra PUBLIC
nlohmann_json
fmt::fmt
nfd::nfd
imgui_imgui imgui_imnodes imgui_implot imgui_color_text_editor imgui_cimgui imgui_custom
imgui::imgui imgui::backend ${FENESTRA_BUNDLED_IMGUI_LIBRARIES}
lunasvg
trace
)
Expand Down
25 changes: 21 additions & 4 deletions lib/libfenestra/source/ui/imgui_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include <imgui.h>
#include <imgui_internal.h>
#include <implot.h>
#include <implot_internal.h>
#include <cimgui.h>
#include <opengl_support.h>

#undef IMGUI_DEFINE_MATH_OPERATORS
Expand Down Expand Up @@ -946,7 +943,27 @@ namespace ImGuiExt {

auto textSize = ImGui::CalcTextSize(drawString.c_str());

ImPlot::AddTextCentered(ImGui::GetWindowDrawList(), ImGui::GetCursorScreenPos() + availableSpace / 2 - ImVec2(0, textSize.y / 2), ImGui::GetColorU32(ImGuiCol_Text), drawString.c_str());
{
auto textBegin = drawString.c_str();
auto drawList = ImGui::GetWindowDrawList();
auto topCenter = ImGui::GetCursorScreenPos() + availableSpace / 2 - ImVec2(0, textSize.y / 2);
ImU32 textColor = ImGui::GetColorU32(ImGuiCol_Text);

float textHeight = ImGui::GetTextLineHeight();
const char* textEnd = ImGui::FindRenderedTextEnd(drawString.c_str(), nullptr);
ImVec2 textSize;
float y = 0;

while (const char* newLinePos = (const char*)memchr(textBegin, '\n', textEnd - textBegin)) {
textSize = ImGui::CalcTextSize(textBegin, newLinePos, true);
drawList->AddText(ImVec2(topCenter.x - textSize.x * 0.5F, topCenter.y + y), textColor, textBegin, newLinePos);
textBegin = newLinePos + 1;
y += textHeight;
}

textSize = ImGui::CalcTextSize(textBegin, textEnd, true);
drawList->AddText(ImVec2(topCenter.x - textSize.x * 0.5F, topCenter.y + y), textColor, textBegin, textEnd);
}
}

bool InputTextIcon(const char *label, const char *icon, std::string &buffer, ImGuiInputTextFlags flags) {
Expand Down
2 changes: 1 addition & 1 deletion lib/third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(third_party)
set(MESSAGES_SUPPRESSED ON)
set(BUILD_SHARED_LIBS OFF)

add_external_library(NAME imgui TARGET imgui_all NO_SYSTEM)
add_external_library(NAME imgui TARGET imgui::all NO_SYSTEM)
add_external_library(NAME jthread TARGET jthread)
add_external_library(NAME nlohmann_json TARGET nlohmann_json)
add_external_library(NAME xdgpp TARGET xdgpp)
Expand Down
7 changes: 5 additions & 2 deletions lib/third_party/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ project(imgui)

set(CMAKE_CXX_STANDARD 17)

add_library(imgui_all_includes INTERFACE)
add_library(imgui_all INTERFACE)
add_library(imgui::all ALIAS imgui_all)

add_library(imgui_all_includes INTERFACE)
add_library(imgui::all_includes ALIAS imgui_all_includes)

add_subdirectory(imgui)
add_subdirectory(cimgui)
Expand All @@ -13,4 +16,4 @@ add_subdirectory(imnodes)
add_subdirectory(custom)
add_subdirectory(ColorTextEditor)

target_link_libraries(imgui_all INTERFACE imgui imgui_cimgui imgui_implot imgui_imnodes imgui_custom imgui_color_text_editor)
target_link_libraries(imgui_all INTERFACE imgui::imgui imgui::cimgui imgui::implot imgui::imnodes imgui::backend imgui::color_text_editor)
2 changes: 2 additions & 0 deletions lib/third_party/imgui/ColorTextEditor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)
add_library(imgui_color_text_editor OBJECT
source/TextEditor.cpp
)
add_library(imgui::color_text_editor ALIAS imgui_color_text_editor)

target_include_directories(imgui_color_text_editor PUBLIC
include
)

target_link_libraries(imgui_color_text_editor PRIVATE imgui_includes)
target_compile_definitions(imgui_color_text_editor PUBLIC IMGUI_LIBRARY_COLOR_TEXT_EDITOR=1)
endif()

target_include_directories(imgui_all_includes INTERFACE include)
Expand Down
2 changes: 2 additions & 0 deletions lib/third_party/imgui/cimgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)
add_library(imgui_cimgui OBJECT
source/cimgui.cpp
)
add_library(imgui::cimgui ALIAS imgui_cimgui)

target_include_directories(imgui_cimgui PUBLIC
include
)

target_link_libraries(imgui_cimgui PRIVATE imgui_includes)
target_compile_definitions(imgui_cimgui PUBLIC IMGUI_LIBRARY_CIMGUI=1)
add_dependencies(fenestra_all imgui_cimgui)
endif()

15 changes: 8 additions & 7 deletions lib/third_party/imgui/custom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
cmake_minimum_required(VERSION 3.16)
# https://github.com/ocornut/imgui with custom modifications made to the OpenGL 3 and SDL3 backends
project(imgui_custom)
project(imgui_backend)

set(CMAKE_CXX_STANDARD 17)

if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)
add_library(imgui_custom OBJECT
add_library(imgui_backend OBJECT
source/imgui_impl_opengl3.cpp
source/imgui_impl_sdl3.cpp
)
add_library(imgui::backend ALIAS imgui_backend)

target_include_directories(imgui_custom PUBLIC
target_include_directories(imgui_backend PUBLIC
include
)

target_link_libraries(imgui_custom PRIVATE imgui_includes)
target_link_libraries(imgui_backend PRIVATE imgui_includes)

find_package(OpenGL REQUIRED)

target_include_directories(imgui_custom PUBLIC ${OpenGL_INCLUDE_DIRS})
target_link_directories(imgui_custom PUBLIC ${OpenGL_LIBRARY_DIRS})
target_link_libraries(imgui_custom PUBLIC SDL3::SDL3 ${OPENGL_LIBRARIES})
target_include_directories(imgui_backend PUBLIC ${OpenGL_INCLUDE_DIRS})
target_link_directories(imgui_backend PUBLIC ${OpenGL_LIBRARY_DIRS})
target_link_libraries(imgui_backend PUBLIC SDL3::SDL3 ${OPENGL_LIBRARIES})
endif()

target_include_directories(imgui_all_includes INTERFACE include)
2 changes: 2 additions & 0 deletions lib/third_party/imgui/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)

source/misc/freetype/imgui_freetype.cpp
)
add_library(imgui::imgui ALIAS imgui_imgui)

target_include_directories(imgui_imgui PUBLIC
include
Expand All @@ -27,6 +28,7 @@ if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)
target_include_directories(imgui_imgui PUBLIC ${FREETYPE_INCLUDE_DIRS})
target_link_directories(imgui_imgui PUBLIC ${FREETYPE_LIBRARY_DIRS})
target_link_libraries(imgui_imgui PUBLIC ${FREETYPE_LIBRARIES})
target_compile_definitions(imgui_imgui PUBLIC IMGUI_LIBRARY_IMGUI=1)
endif()

add_library(imgui_includes INTERFACE)
Expand Down
2 changes: 2 additions & 0 deletions lib/third_party/imgui/imnodes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)
add_library(imgui_imnodes OBJECT
source/imnodes.cpp
)
add_library(imgui::imnodes ALIAS imgui_imnodes)

target_include_directories(imgui_imnodes PUBLIC
include
)

target_link_libraries(imgui_imnodes PRIVATE imgui_includes)
target_compile_definitions(imgui_imnodes PUBLIC IMGUI_LIBRARY_IMNODES=1)
endif()

target_include_directories(imgui_all_includes INTERFACE include)
2 changes: 2 additions & 0 deletions lib/third_party/imgui/implot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ if (NOT FENESTRA_EXTERNAL_PLUGIN_BUILD)
source/implot_items.cpp
source/implot_demo.cpp
)
add_library(imgui::implot ALIAS imgui_implot)

target_include_directories(imgui_implot PUBLIC
include
)

target_link_libraries(imgui_implot PRIVATE imgui_includes)
target_compile_definitions(imgui_implot PUBLIC IMGUI_LIBRARY_IMPLOT=1)
endif()

target_include_directories(imgui_all_includes INTERFACE include)

0 comments on commit e7daaf9

Please sign in to comment.