diff --git a/.github/workflows/build-wasm.yml b/.github/workflows/build-wasm.yml index e60607e..8ce9a79 100644 --- a/.github/workflows/build-wasm.yml +++ b/.github/workflows/build-wasm.yml @@ -25,33 +25,27 @@ jobs: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest - container: - image: jspanchu/vtk-wasm-target:latest - options: -v ${{ github.workspace }}:/project/src steps: - - name: Checkout repository and submodules + - name: Checkout uses: actions/checkout@v3 with: submodules: recursive - - name: Configure - run: | - emcmake cmake -GNinja -S /project/src -B /project/build -DCMAKE_BUILD_TYPE=Release -DVTK_DIR=$VTK_DIR - name: Build - run: - cmake --build /project/build + run: | + npm install + npm run build-wasm - name: Prepare artifacts run: | - cd /project/build - mkdir /web_content - mv imgui_vtk_app.js /web_content/ - mv imgui_vtk_app.wasm /web_content/ - mv index.html /web_content/ + mkdir web_content + mv build-emscripten/imgui_vtk_app.js web_content/ + mv build-emscripten/imgui_vtk_app.wasm web_content/ + mv build-emscripten/index.html web_content/ - name: Setup Pages uses: actions/configure-pages@v3 - name: Upload artifact uses: actions/upload-pages-artifact@v1 with: - path: '/web_content/' + path: './web_content/' - name: Deploy to GitHub Pages id: deployment - uses: actions/deploy-pages@v1 + uses: actions/deploy-pages@v1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 24bec29..de287db 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,11 @@ # Build dirs out/ .vs/ -*.json +compile_commands.json .cache build -build* \ No newline at end of file +build* + +# node +node_modules/ +package-lock.json \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ef0436..4f9e755 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,220 +1,28 @@ +# SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti +# SPDX-License-Identifier: BSD-3-Clause + cmake_minimum_required (VERSION 3.6) project (vtkDearImGuiInjector) include (GenerateExportHeader) -find_package(ParaView - QUIET -) -if (NOT ParaView_FOUND) - find_package(VTK) - if (NOT VTK_FOUND) - message(FATAL_ERROR "Cannot find VTK installation") - return () - endif() -endif() - -message(STATUS "VTK_VERSION: ${VTK_VERSION}") - -# For an adobe spectrum look. -option(USE_ADOBE_SPECTRUM_STYLE "Style ImGui with Adobe spectrum look" ON) - -if (USE_ADOBE_SPECTRUM_STYLE) - set(_IMGUI_DIR "adobe_imgui_spectrum") - list(APPEND _proj_definitions ADOBE_IMGUI_SPECTRUM) -elseif(IMGUI_DIR) # From cli -DIMGUI_DIR - set(_IMGUI_DIR IMGUI_DIR) -else() - set(_IMGUI_DIR "imgui") -endif() - -# sources and headers -file(GLOB IMGUI_SOURCES ${_IMGUI_DIR}/*.cpp) -file(GLOB IMGUI_HEADERS ${_IMGUI_DIR}/*.h) -list(APPEND _proj_headers - ${IMGUI_HEADERS} - "${_IMGUI_DIR}/backends/imgui_impl_opengl3.h" - "include/vtkDearImGuiInjector.h" -) -list(APPEND _proj_sources - ${IMGUI_SOURCES} - "${_IMGUI_DIR}/backends/imgui_impl_opengl3.cpp" - "src/vtkDearImGuiInjector.cxx" -) - -if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten") - list(APPEND _proj_definitions IMGUI_IMPL_OPENGL_ES3) -endif() - -# library target -add_library(${CMAKE_PROJECT_NAME} STATIC ${_proj_sources}) -target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC ${_proj_definitions}) -target_include_directories(${CMAKE_PROJECT_NAME} PUBLIC - $ - $ - $ - $ - $ +find_package(VTK + COMPONENTS + FiltersSources + InteractionStyle + InteractionWidgets + RenderingOpenGL2 + RenderingUI ) -target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC ${VTK_LIBRARIES}) -generate_export_header(${CMAKE_PROJECT_NAME}) - -# Install targets -install(TARGETS ${CMAKE_PROJECT_NAME} - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) -install(FILES ${CMAKE_PROJECT_NAME}.h ${PROJECT_BINARY_DIR}/${CMAKE_PROJECT_NAME}_export.h DESTINATION include) -install(FILES ${_proj_headers} DESTINATION include) - -# ----------------------------------------------------------------------------- -# Configure application -# ----------------------------------------------------------------------------- -if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten") - message("Skipping WebAssembly example: This needs to run inside an Emscripten build environment") - - add_executable(test_imgui_vtk "src/test_imgui_vtk.cxx") - - target_link_libraries(test_imgui_vtk PRIVATE ${CMAKE_PROJECT_NAME}) - vtk_module_autoinit( - TARGETS test_imgui_vtk - MODULES ${VTK_LIBRARIES} - ) +if (NOT VTK_FOUND) + message("Skipping example: ${VTK_NOT_FOUND_MESSAGE}") return () endif () +message(STATUS "VTK_VERSION: ${VTK_VERSION}") -# ----------------------------------------------------------------------------- -# WebAssembly build options -# ----------------------------------------------------------------------------- - -set(emscripten_options) -list(APPEND emscripten_options - "--bind" - "-g3" - "SHELL:-s EXPORT_NAME=vtkApp" - "SHELL:-s ALLOW_MEMORY_GROWTH=1" - "SHELL:-s DEMANGLE_SUPPORT=1" - "SHELL:-s EMULATE_FUNCTION_POINTER_CASTS=0" - "SHELL:-s ERROR_ON_UNDEFINED_SYMBOLS=0" - "SHELL:-s MODULARIZE=1" - "SHELL:-s USE_PTHREADS=0" - "SHELL:-s WASM=1" -) - -# ----------------------------------------------------------------------------- -# Build options -# ----------------------------------------------------------------------------- - -set(OPTIMIZE "SMALLEST_WITH_CLOSURE" CACHE STRING "Emscripten optimization") -set_property(CACHE OPTIMIZE PROPERTY - STRINGS - NO_OPTIMIZATION # -O0 - LITTLE # -O1 - MORE # -O2 - BEST # -O3 - SMALL # -Os - SMALLEST # -Oz - SMALLEST_WITH_CLOSURE # -Oz --closure 1 -) - -if(OPTIMIZE STREQUAL "NO_OPTIMIZATION") - # imgui_vtk.js 659K - # imgui_vtk.wasm 4.9M - # time => 4 minutes 3 seconds - list(APPEND emscripten_options - "-Oz" - ) -elseif(OPTIMIZE STREQUAL "LITTLE") - # imgui_vtk.js 529K - # imgui_vtk.wasm 5.9M - list(APPEND emscripten_options - "-O1" - ) -elseif(OPTIMIZE STREQUAL "MORE") - # imgui_vtk.js 529K - # imgui_vtk.wasm 5.3M - list(APPEND emscripten_options - "-O2" - ) -elseif(OPTIMIZE STREQUAL "BEST") - # imgui_vtk.js 529K - # imgui_vtk.wasm 4.9M - # time => 4 minutes 7 seconds - list(APPEND emscripten_options - "-O3" - ) -elseif(OPTIMIZE STREQUAL "SMALL") - # imgui_vtk.js 529K - # imgui_vtk.wasm 4.9M - list(APPEND emscripten_options - "-Os" - ) -elseif(OPTIMIZE STREQUAL "SMALLEST") - # imgui_vtk.js 659K - # imgui_vtk.wasm 4.9M - list(APPEND emscripten_options - "-Oz" - ) -elseif(OPTIMIZE STREQUAL "SMALLEST_WITH_CLOSURE") - # imgui_vtk.js 659K - # imgui_vtk.wasm 4.9M - list(APPEND emscripten_options - "-Oz" - "SHELL:--closure 1" - ) -endif() - -# ----------------------------------------------------------------------------- -# Compile example code -# ----------------------------------------------------------------------------- - -add_executable(imgui_vtk_app "src/test_imgui_vtk.cxx") -target_link_libraries(imgui_vtk_app PRIVATE ${CMAKE_PROJECT_NAME} ${VTK_LIBRARIES}) - -target_link_libraries(imgui_vtk_app - PRIVATE - VTK::CommonColor - VTK::CommonCore - VTK::CommonDataModel - VTK::FiltersCore - VTK::FiltersSources - VTK::InteractionStyle - VTK::InteractionWidgets - VTK::RenderingCore - VTK::RenderingFreeType - VTK::RenderingOpenGL2 -) - -target_compile_options(imgui_vtk_app - PUBLIC - ${emscripten_options} -) - -target_link_options(imgui_vtk_app - PUBLIC - ${emscripten_options} -) - -# ----------------------------------------------------------------------------- -# VTK modules initialization -# ----------------------------------------------------------------------------- - -vtk_module_autoinit( - TARGETS imgui_vtk_app - MODULES ${VTK_LIBRARIES} -) - -# ----------------------------------------------------------------------------- -# Copy HTML to build directory -# ----------------------------------------------------------------------------- - -add_custom_command( - TARGET imgui_vtk_app - POST_BUILD - COMMAND - ${CMAKE_COMMAND} -E copy_if_different - "${CMAKE_CURRENT_SOURCE_DIR}/index.html" - $ -) +# For an adobe spectrum look. +option(USE_ADOBE_SPECTRUM_STYLE "Style ImGui with Adobe spectrum look" ON) +add_subdirectory(src) +add_subdirectory(sample) diff --git a/LICENSE b/LICENSE index ce2ddaa..1703b33 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 jaswantp +Copyright 2023 Jaswant Panchumarti Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/index.html b/index.html index afb76eb..3a52462 100644 --- a/index.html +++ b/index.html @@ -1,39 +1,45 @@ + + + - - - - - - - - - + + + + + + + + + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..67b92c2 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "vtk-dearimgui-injector", + "version": "0.0.0-semantically-released", + "scripts": { + "build-wasm": "npm run build-wasm:release", + "build-wasm:release": "npx itk-wasm -i kitware/vtk-wasm -b build-emscripten -s . build -- Release -DDEBUGINFO=PROFILE", + "build-wasm:debug": "npx itk-wasm -i kitware/vtk-wasm -b build-emscripten -s . build -- Debug -DDEBUGINFO=DEBUG_NATIVE -DOPTIMIZE=NO_OPTIMIZATION" + }, + "keywords": [ + "vtk", + "wasm", + "emscripten", + "opengl" + ], + "author": "Jaswant Panchumarti", + "license": "MIT", + "devDependencies": { + "itk-wasm": "^1.0.0-b.83" + } +} \ No newline at end of file diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt new file mode 100644 index 0000000..7bde928 --- /dev/null +++ b/sample/CMakeLists.txt @@ -0,0 +1,172 @@ +# ----------------------------------------------------------------------------- +# Configure application +# ----------------------------------------------------------------------------- + +if (NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten") + message("Skipping WebAssembly example: This needs to run inside an Emscripten build environment") + add_executable(main + "main.cxx" + "OverlayUI.cxx") + target_include_directories(main PRIVATE + $ + $) + target_link_libraries(main PRIVATE ${CMAKE_PROJECT_NAME}) + vtk_module_autoinit( + TARGETS main + MODULES ${VTK_LIBRARIES}) + # Install targets + install(TARGETS main + RUNTIME DESTINATION bin) + install(FILES "include/Karla-Regular.hpp" "include/OverlayUI.h" DESTINATION include) + return () +endif () + + +# ----------------------------------------------------------------------------- +# WebAssembly build options +# ----------------------------------------------------------------------------- +set(DEBUGINFO "NONE" CACHE STRING "Type of debug info") +set_property(CACHE DEBUGINFO PROPERTY + STRINGS + NONE # -g0 + READABLE_JS # -g1 + PROFILE # -g2 + DEBUG_NATIVE # -g3 +) +set(OPTIMIZE "BEST" CACHE STRING "Emscripten optimization") +set_property(CACHE OPTIMIZE PROPERTY + STRINGS + NO_OPTIMIZATION # -O0 + LITTLE # -O1 + MORE # -O2 + BEST # -O3 + SMALL # -Os + SMALLEST # -Oz + SMALLEST_WITH_CLOSURE # -Oz --closure 1 +) + +# ----------------------------------------------------------------------------- +# Compile example code +# ----------------------------------------------------------------------------- + +add_executable(imgui_vtk_app + "main.cxx" + "OverlayUI.cxx") +target_include_directories(imgui_vtk_app PRIVATE + $) +target_link_libraries(imgui_vtk_app PRIVATE ${CMAKE_PROJECT_NAME}) + +set(emscripten_link_options) +set(emscripten_compile_options) +list(APPEND emscripten_link_options + "-lembind" + "-sMODULARIZE=1" + "-sEXPORT_NAME=vtkApp" + "-sENVIRONMENT=web" + "-sALLOW_MEMORY_GROWTH=1" + "-sSINGLE_FILE=1" + "-sMAXIMUM_MEMORY=4GB" +) + +set(emscripten_debug_options) +if(DEBUGINFO STREQUAL "NONE") + list(APPEND emscripten_debug_options + "-g0" + ) +elseif(DEBUGINFO STREQUAL "READABLE_JS") + list(APPEND emscripten_debug_options + "-g1" + ) + list(APPEND emscripten_link_options + "-sDEMANGLE_SUPPORT=1" + ) +elseif(DEBUGINFO STREQUAL "PROFILE") + list(APPEND emscripten_debug_options + "-g2" + ) + list(APPEND emscripten_link_options + "-sDEMANGLE_SUPPORT=1" + ) +elseif(DEBUGINFO STREQUAL "DEBUG_NATIVE") + list(APPEND emscripten_debug_options + "-g3" + "-fsanitize=address" + ) + list(APPEND emscripten_link_options + "-sASSERTIONS=1" + "-sDEMANGLE_SUPPORT=1" + ) +endif() + +# ----------------------------------------------------------------------------- +# Build options +# ----------------------------------------------------------------------------- +set(emscripten_optimizations) +if(OPTIMIZE STREQUAL "NO_OPTIMIZATION") + list(APPEND emscripten_optimizations + "-O0" + ) +elseif(OPTIMIZE STREQUAL "LITTLE") + list(APPEND emscripten_optimizations + "-O1" + ) +elseif(OPTIMIZE STREQUAL "MORE") + list(APPEND emscripten_optimizations + "-O2" + ) +elseif(OPTIMIZE STREQUAL "BEST") + list(APPEND emscripten_optimizations + "-O3" + ) +elseif(OPTIMIZE STREQUAL "SMALL") + list(APPEND emscripten_optimizations + "-Os" + ) +elseif(OPTIMIZE STREQUAL "SMALLEST") + list(APPEND emscripten_optimizations + "-Oz" + ) +elseif(OPTIMIZE STREQUAL "SMALLEST_WITH_CLOSURE") + list(APPEND emscripten_optimizations + "-Oz" + ) + list(APPEND emscripten_link_options + "--closure=1" + ) +endif() + +target_compile_options(imgui_vtk_app + PUBLIC + ${emscripten_compile_options} + ${emscripten_optimizations} + ${emscripten_debug_options} +) + +target_link_options(imgui_vtk_app + PUBLIC + ${emscripten_link_options} + ${emscripten_optimizations} + ${emscripten_debug_options} +) +# ----------------------------------------------------------------------------- +# VTK modules initialization +# ----------------------------------------------------------------------------- + +vtk_module_autoinit( + TARGETS imgui_vtk_app + MODULES ${VTK_LIBRARIES} +) + +# ----------------------------------------------------------------------------- +# Copy HTML to build directory +# ----------------------------------------------------------------------------- + +add_custom_command( + TARGET imgui_vtk_app + POST_BUILD + COMMAND + ${CMAKE_COMMAND} -E copy_if_different + "${PROJECT_SOURCE_DIR}/index.html" + $ +) + diff --git a/sample/OverlayUI.cxx b/sample/OverlayUI.cxx new file mode 100644 index 0000000..0def995 --- /dev/null +++ b/sample/OverlayUI.cxx @@ -0,0 +1,132 @@ +// SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti +// SPDX-License-Identifier: BSD-3-Clause + +#include "OverlayUI.h" +#include "vtkDearImGuiInjector.h" + +#include +#include +#include +#include +#include + +#ifdef ADOBE_IMGUI_SPECTRUM +#include "Karla-Regular.hpp" +#include "imgui_spectrum.h" +#endif +#include "imgui.h" // to draw custom UI + +namespace +{ +//------------------------------------------------------------------------------ +void HelpMarker(const char* desc) +{ + ImGui::TextDisabled("(?)"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted(desc); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } +} +} + +//------------------------------------------------------------------------------ +void OverlayUI::setup(vtkObject* caller, unsigned long, void*, void* callData) +{ + vtkDearImGuiInjector* overlay_ = reinterpret_cast(caller); + if (!callData) + { + return; + } + bool imguiInitStatus = *(reinterpret_cast(callData)); + if (imguiInitStatus) + { + auto io = ImGui::GetIO(); +#ifndef ADOBE_IMGUI_SPECTRUM + io.Fonts->AddFontFromMemoryCompressedBase85TTF(Karla_Regular_compressed_data_base85, 16); + io.Fonts->AddFontDefault(); +#else + ImGui::Spectrum::LoadFont(18.0f); + ImGui::Spectrum::StyleColorsSpectrum(); +#endif + auto& style = ImGui::GetStyle(); + style.ChildRounding = 8; + style.FrameRounding = 8; + style.GrabRounding = 8; + style.PopupRounding = 8; + style.ScrollbarRounding = 8; + style.TabRounding = 8; + style.WindowRounding = 8; + style.FrameBorderSize = 1.f; + } + else + { + vtkErrorWithObjectMacro( + overlay_, "Failed to setup overlay UI because ImGUI failed to initialize!"); + } +} + +//------------------------------------------------------------------------------ +void OverlayUI::draw(vtkObject* caller, unsigned long, void*, void* callData) +{ + vtkDearImGuiInjector* overlay_ = reinterpret_cast(caller); + + ImGui::SetNextWindowBgAlpha(0.5); + ImGui::SetNextWindowPos(ImVec2(5, 25), ImGuiCond_Once); + ImGui::SetNextWindowSize(ImVec2(450, 550), ImGuiCond_Once); + ImGui::Begin("VTK"); + if (ImGui::CollapsingHeader("vtkRenderWindow", ImGuiTreeNodeFlags_DefaultOpen)) + { + auto rw = overlay_->Interactor->GetRenderWindow(); + ImGui::Text("MTime: %ld", rw->GetMTime()); + ImGui::Text("Name: %s", rw->GetClassName()); + if (ImGui::TreeNode("Capabilities")) + { + ImGui::TextWrapped("OpenGL: %s", rw->ReportCapabilities()); + ImGui::TreePop(); + } + } + if (ImGui::CollapsingHeader("vtkRenderWindowInteractor", ImGuiTreeNodeFlags_DefaultOpen)) + { + auto& iren = overlay_->Interactor; + ImGui::Text("MTime: %ld", iren->GetMTime()); + ImGui::Text("Name: %s", iren->GetClassName()); + if (ImGui::TreeNode("Style")) + { + auto styleBase = iren->GetInteractorStyle(); + vtkInteractorObserver* iStyle = nullptr; + if (styleBase->IsA("vtkInteractorStyleSwitchBase")) + { + iStyle = vtkInteractorStyleSwitch::SafeDownCast(styleBase)->GetCurrentStyle(); + } + else + { + iStyle = styleBase; + } + ImGui::Text("MTime: %ld", iStyle->GetMTime()); + ImGui::Text("Name: %s", iStyle->GetClassName()); + ImGui::TreePop(); + } + if (ImGui::TreeNode("Mouse")) + { + int* xy = iren->GetEventPosition(); + ImGui::Text("X: %d", xy[0]); + ImGui::Text("Y: %d", xy[1]); + ImGui::TreePop(); + } + if (ImGui::TreeNode("Keyboard")) + { + ImGui::Text("KeySym: %s", iren->GetKeySym()); + ImGui::SameLine(); + HelpMarker("VTK does not flush KeySym per frame."); + ImGui::Text("KeyCode: %c", iren->GetKeyCode()); + ImGui::Text("Mods: %s %s %s", (iren->GetAltKey() ? "ALT" : " "), + (iren->GetControlKey() ? "CTRL" : " "), (iren->GetShiftKey() ? "SHIFT" : " ")); + ImGui::TreePop(); + } + } + ImGui::End(); +} diff --git a/src/test_imgui_vtk.cxx b/sample/include/Karla-Regular.hpp similarity index 72% rename from src/test_imgui_vtk.cxx rename to sample/include/Karla-Regular.hpp index b8dd3e5..5fc1419 100644 --- a/src/test_imgui_vtk.cxx +++ b/sample/include/Karla-Regular.hpp @@ -1,98 +1,9 @@ -#include -#include - -#include "vtkDearImGuiInjector.h" - -#include "vtkActor.h" -#include "vtkCallbackCommand.h" -#include "vtkCameraOrientationWidget.h" -#include "vtkCameraOrientationRepresentation.h" -#include "vtkCameraOrientationWidget.h" -#include "vtkConeSource.h" -#include "vtkInteractorStyle.h" -#include "vtkInteractorStyleSwitch.h" -#include "vtkNew.h" -#include "vtkPolyData.h" -#include "vtkPolyDataMapper.h" -#include "vtkRenderer.h" -#include "vtkRenderWindow.h" -#include "vtkRenderWindowInteractor.h" - -#ifdef ADOBE_IMGUI_SPECTRUM -#include "imgui_spectrum.h" -#endif -#include "imgui.h" // to draw custom UI -#include "vtkOpenGLRenderWindow.h" // needed to check if opengl is supported. - -// Listens to vtkDearImGuiInjector::ImGuiSetupEvent -static void SetupUI(vtkDearImGuiInjector*); -// Listens to vtkDearImGuiInjector::ImGuiDrawEvent -static void DrawUI(vtkDearImGuiInjector*); -static void HelpMarker(const char* desc); - -//------------------------------------------------------------------------------ -// Main -//------------------------------------------------------------------------------ - -int main(int argc, char* argv[]) -{ - // Create a renderer, render window, and interactor - vtkNew renderer; - vtkNew renderWindow; - vtkNew iren; - renderWindow->SetMultiSamples(8); - renderWindow->AddRenderer(renderer); - iren->SetRenderWindow(renderWindow); - - // Create pipeline - vtkNew coneSource; - coneSource->Update(); - - vtkNew mapper; - mapper->SetInputConnection(coneSource->GetOutputPort()); - - vtkNew actor; - actor->SetMapper(mapper); - - // Add the actors to the scene - renderer->AddActor(actor); - - // Start rendering app - renderer->SetBackground(0.2, 0.3, 0.4); - renderWindow->Render(); - - /// Change to your code begins here. /// - // Initialize an overlay with DearImgui elements. - vtkNew dearImGuiOverlay; - // 💉 the overlay. - dearImGuiOverlay->Inject(iren); - // These functions add callbacks to ImGuiSetupEvent and ImGuiDrawEvents. - SetupUI(dearImGuiOverlay); - // You can draw custom user interface elements using ImGui:: namespace. - DrawUI(dearImGuiOverlay); - /// Change to your code ends here. /// - - vtkNew camManipulator; - camManipulator->SetParentRenderer(renderer); - camManipulator->On(); - auto rep = vtkCameraOrientationRepresentation::SafeDownCast(camManipulator->GetRepresentation()); - rep->AnchorToLowerRight(); - - // Start event loop - renderWindow->SetSize(1920, 1000); - vtkInteractorStyleSwitch::SafeDownCast(iren->GetInteractorStyle())->SetCurrentStyleToTrackballCamera(); - iren->EnableRenderOff(); - iren->Start(); - - return 0; -} - +#pragma once //------------------------------------------------------------------------------ // Custom extra bloat to look good and show some VTK information //------------------------------------------------------------------------------ // File: 'Karla-Regular.ttf' (16848 bytes) // Exported using binary_to_compressed_c.cpp -#ifndef ADOBE_IMGUI_SPECTRUM static const char Karla_Regular_compressed_data_base85[15900 + 1] = "7])#######5Z9sf'/###I),##bw9hLQXH##j$1S:'>pgLZvaAd668X%QUQl;*31Qg3R9s/N@4',:a`T9n&)Seo$,p8ZW%q/" "^6se=K9C[HP>JeE';G##Uu/>8i1i86 uiSetup; - auto uiSetupFunction = - [](vtkObject* caller, long unsigned int vtkNotUsed(eventId), void* clientData, void* callData) - { - vtkDearImGuiInjector* overlay_ = reinterpret_cast(caller); - if (!callData) - { - return; - } - bool imguiInitStatus = *(reinterpret_cast(callData)); - if (imguiInitStatus) - { - auto io = ImGui::GetIO(); -#ifndef ADOBE_IMGUI_SPECTRUM - io.Fonts->AddFontFromMemoryCompressedBase85TTF(Karla_Regular_compressed_data_base85, 16); - io.Fonts->AddFontDefault(); -#else - ImGui::Spectrum::LoadFont(18.0f); - ImGui::Spectrum::StyleColorsSpectrum(); -#endif - auto& style = ImGui::GetStyle(); - style.ChildRounding = 8; - style.FrameRounding = 8; - style.GrabRounding = 8; - style.PopupRounding = 8; - style.ScrollbarRounding = 8; - style.TabRounding = 8; - style.WindowRounding = 8; - style.FrameBorderSize = 1.f; - } - }; - uiSetup->SetCallback(uiSetupFunction); - overlay->AddObserver(vtkDearImGuiInjector::ImGuiSetupEvent, uiSetup); -} - -static void DrawUI(vtkDearImGuiInjector* overlay) -{ - vtkNew uiDraw; - auto uiDrawFunction = [](vtkObject* caller, long unsigned int vtkNotUsed(eventId), - void* clientData, void* vtkNotUsed(callData)) - { - vtkDearImGuiInjector* overlay_ = reinterpret_cast(caller); - - ImGui::SetNextWindowBgAlpha(0.5); - ImGui::SetNextWindowPos(ImVec2(5, 25), ImGuiCond_Once); - ImGui::SetNextWindowSize(ImVec2(450, 550), ImGuiCond_Once); - ImGui::Begin("VTK"); - if (ImGui::CollapsingHeader("vtkRenderWindow", ImGuiTreeNodeFlags_DefaultOpen)) - { - auto rw = overlay_->Interactor->GetRenderWindow(); - ImGui::Text("MTime: %ld", rw->GetMTime()); - ImGui::Text("Name: %s", rw->GetClassName()); - if (ImGui::TreeNode("Capabilities")) - { - ImGui::TextWrapped("OpenGL: %s", rw->ReportCapabilities()); - ImGui::TreePop(); - } - } - if (ImGui::CollapsingHeader("vtkRenderWindowInteractor", ImGuiTreeNodeFlags_DefaultOpen)) - { - auto& iren = overlay_->Interactor; - ImGui::Text("MTime: %ld", iren->GetMTime()); - ImGui::Text("Name: %s", iren->GetClassName()); - if (ImGui::TreeNode("Style")) - { - auto styleBase = iren->GetInteractorStyle(); - vtkInteractorObserver* iStyle = nullptr; - if (styleBase->IsA("vtkInteractorStyleSwitchBase")) - { - iStyle = vtkInteractorStyleSwitch::SafeDownCast(styleBase)->GetCurrentStyle(); - } - else - { - iStyle = styleBase; - } - ImGui::Text("MTime: %ld", iStyle->GetMTime()); - ImGui::Text("Name: %s", iStyle->GetClassName()); - ImGui::TreePop(); - } - if (ImGui::TreeNode("Mouse")) - { - int* xy = iren->GetEventPosition(); - ImGui::Text("X: %d", xy[0]); - ImGui::Text("Y: %d", xy[1]); - ImGui::TreePop(); - } - if (ImGui::TreeNode("Keyboard")) - { - ImGui::Text("KeySym: %s", iren->GetKeySym()); - ImGui::SameLine(); - HelpMarker("VTK does not flush KeySym per frame."); - ImGui::Text("KeyCode: %c", iren->GetKeyCode()); - ImGui::Text("Mods: %s %s %s", (iren->GetAltKey() ? "ALT" : " "), - (iren->GetControlKey() ? "CTRL" : " "), (iren->GetShiftKey() ? "SHIFT" : " ")); - ImGui::TreePop(); - } - } - ImGui::End(); - }; - uiDraw->SetCallback(uiDrawFunction); - overlay->AddObserver(vtkDearImGuiInjector::ImGuiDrawEvent, uiDraw); -} - -static void HelpMarker(const char* desc) -{ - ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered()) - { - ImGui::BeginTooltip(); - ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); - ImGui::TextUnformatted(desc); - ImGui::PopTextWrapPos(); - ImGui::EndTooltip(); - } -} diff --git a/sample/include/OverlayUI.h b/sample/include/OverlayUI.h new file mode 100644 index 0000000..0129b67 --- /dev/null +++ b/sample/include/OverlayUI.h @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti +// SPDX-License-Identifier: BSD-3-Clause + +#pragma once + +class vtkObject; + +class OverlayUI +{ +public: + static void setup(vtkObject* caller, unsigned long eventId, void* clientData, void* callData); + static void draw(vtkObject* caller, unsigned long eventId, void* clientData, void* callData); +}; \ No newline at end of file diff --git a/sample/main.cxx b/sample/main.cxx new file mode 100644 index 0000000..958f9ba --- /dev/null +++ b/sample/main.cxx @@ -0,0 +1,81 @@ +// SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti +// SPDX-License-Identifier: BSD-3-Clause + +#include +#include + +#include "OverlayUI.h" +#include "vtkDearImGuiInjector.h" + +#include "vtkActor.h" +#include "vtkCallbackCommand.h" +#include "vtkCameraOrientationRepresentation.h" +#include "vtkCameraOrientationWidget.h" +#include "vtkConeSource.h" +#include "vtkInteractorStyle.h" +#include "vtkInteractorStyleSwitch.h" +#include "vtkNew.h" +#include "vtkPolyData.h" +#include "vtkPolyDataMapper.h" +#include "vtkRenderWindow.h" +#include "vtkRenderWindowInteractor.h" +#include "vtkRenderer.h" + +//------------------------------------------------------------------------------ +int main(int argc, char* argv[]) +{ + // Create a renderer, render window, and interactor + vtkNew renderer; + vtkNew renderWindow; + vtkNew iren; + renderWindow->SetMultiSamples(8); + renderWindow->AddRenderer(renderer); + iren->SetRenderWindow(renderWindow); + + // Create pipeline + vtkNew coneSource; + coneSource->Update(); + + vtkNew mapper; + mapper->SetInputConnection(coneSource->GetOutputPort()); + + vtkNew actor; + actor->SetMapper(mapper); + + // Add the actors to the scene + renderer->AddActor(actor); + + // Start rendering app + renderer->SetBackground(0.2, 0.3, 0.4); + renderWindow->Render(); + + /// Change to your code begins here. /// + // Initialize an overlay with DearImgui elements. + vtkNew dearImGuiOverlay; + dearImGuiOverlay->DebugOn(); + // 💉 the overlay. + dearImGuiOverlay->Inject(iren); + // Listens to vtkDearImGuiInjector::ImGuiSetupEvent + vtkNew uiSetupCmd; + uiSetupCmd->SetCallback(OverlayUI::setup); + dearImGuiOverlay->AddObserver(vtkDearImGuiInjector::ImGuiSetupEvent, uiSetupCmd); + // Listens to vtkDearImGuiInjector::ImGuiDrawEvent + vtkNew uiDrawCmd; + uiDrawCmd->SetCallback(OverlayUI::draw); + dearImGuiOverlay->AddObserver(vtkDearImGuiInjector::ImGuiDrawEvent, uiDrawCmd); + // You can draw custom user interface elements using ImGui:: namespace. + /// Change to your code ends here. /// + + vtkNew camManipulator; + camManipulator->SetParentRenderer(renderer); + camManipulator->On(); + auto rep = vtkCameraOrientationRepresentation::SafeDownCast(camManipulator->GetRepresentation()); + rep->AnchorToLowerRight(); + + // Start event loop + renderWindow->SetSize(1920, 1000); + iren->EnableRenderOff(); + iren->Start(); + + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..8d51ec1 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,47 @@ +if (USE_ADOBE_SPECTRUM_STYLE) + set(IMGUI_DIR "${PROJECT_SOURCE_DIR}/adobe_imgui_spectrum") + list(APPEND lib_definitions ADOBE_IMGUI_SPECTRUM) +elseif(IMGUI_DIR) # From cli -DIMGUI_DIR + set(IMGUI_DIR IMGUI_DIR) +else() + set(IMGUI_DIR "${PROJECT_SOURCE_DIR}/imgui") +endif() + +# sources and headers +file(GLOB IMGUI_SOURCES ${IMGUI_DIR}/*.cpp) +file(GLOB IMGUI_HEADERS ${IMGUI_DIR}/*.h) +list(APPEND lib_headers + ${IMGUI_HEADERS} + "${IMGUI_DIR}/backends/imgui_impl_opengl3.h" + "vtkDearImGuiInjector.h" +) +list(APPEND lib_sources + ${IMGUI_SOURCES} + "${IMGUI_DIR}/backends/imgui_impl_opengl3.cpp" + "vtkDearImGuiInjector.cxx" +) + +if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten") + list(APPEND lib_definitions IMGUI_IMPL_OPENGL_ES3) +endif() + +# library target +add_library(vtkDearImGuiInjector STATIC ${lib_sources}) +target_compile_definitions(vtkDearImGuiInjector PUBLIC ${lib_definitions}) +target_include_directories(vtkDearImGuiInjector PUBLIC + $ + $ + $ + $ + $ +) +target_link_libraries(vtkDearImGuiInjector PUBLIC ${VTK_LIBRARIES}) +generate_export_header(vtkDearImGuiInjector) + +# Install targets +install(TARGETS vtkDearImGuiInjector + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib) +install(FILES vtkDearImGuiInjector.h ${CMAKE_CURRENT_BINARY_DIR}/vtkDearImGuiInjector_export.h DESTINATION include) +install(FILES ${lib_headers} DESTINATION include) diff --git a/src/vtkDearImGuiInjector.cxx b/src/vtkDearImGuiInjector.cxx index 5819423..4217fce 100644 --- a/src/vtkDearImGuiInjector.cxx +++ b/src/vtkDearImGuiInjector.cxx @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti +// SPDX-License-Identifier: BSD-3-Clause + #include #include #include @@ -18,7 +21,7 @@ #define IMGUI_IMPL_OPENGL_ES3 #endif -#include "backends/imgui_impl_opengl3.h" +#include "imgui_impl_opengl3.h" #include "imgui.h" #ifdef __EMSCRIPTEN__ @@ -26,7 +29,6 @@ #include #endif - #ifdef USES_X11 #include #elif defined(USES_SDL2) @@ -72,6 +74,7 @@ const std::unordered_map imguiToVtkCursors( { ImGuiMouseCursor_NotAllowed, VTK_CURSOR_DEFAULT } }); } +//------------------------------------------------------------------------------ vtkDearImGuiInjector::vtkDearImGuiInjector() { // Start DearImGui @@ -79,6 +82,7 @@ vtkDearImGuiInjector::vtkDearImGuiInjector() ImGui::CreateContext(); } +//------------------------------------------------------------------------------ vtkDearImGuiInjector::~vtkDearImGuiInjector() { // Destroy DearImGUi @@ -88,6 +92,7 @@ vtkDearImGuiInjector::~vtkDearImGuiInjector() } } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::Inject(vtkRenderWindowInteractor* interactor) { if (this->FinishedSetup) @@ -103,18 +108,19 @@ void vtkDearImGuiInjector::Inject(vtkRenderWindowInteractor* interactor) } // intercept interactor - this->EventCallbackCommand->SetClientData(this); - this->EventCallbackCommand->SetCallback(&vtkDearImGuiInjector::DispatchEv); - interactor->AddObserver(vtkCommand::StartEvent, this, &vtkDearImGuiInjector::PumpEv); + this->EventInterceptor->SetClientData(this); + this->EventInterceptor->SetCallback(&vtkDearImGuiInjector::InterceptEvent); + this->Interactor->AddObserver(vtkCommand::StartEvent, this, &vtkDearImGuiInjector::PumpEvents); // intercept renderer renWin->AddObserver(vtkCommand::StartEvent, this, &vtkDearImGuiInjector::BeginDearImGuiOverlay); renWin->AddObserver(vtkCommand::RenderEvent, this, &vtkDearImGuiInjector::RenderDearImGuiOverlay); // Safely exit when vtk app exits - interactor->AddObserver(vtkCommand::ExitEvent, this, &vtkDearImGuiInjector::TearDown); + this->Interactor->AddObserver(vtkCommand::ExitEvent, this, &vtkDearImGuiInjector::TearDown); } +//------------------------------------------------------------------------------ bool vtkDearImGuiInjector::SetUp(vtkRenderWindow* renWin) { if (renWin->GetNeverRendered()) @@ -215,6 +221,7 @@ bool vtkDearImGuiInjector::SetUp(vtkRenderWindow* renWin) return status; } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::TearDown(vtkObject* caller, unsigned long eid, void* callData) { auto interactor = vtkRenderWindowInteractor::SafeDownCast(caller); @@ -227,6 +234,7 @@ void vtkDearImGuiInjector::TearDown(vtkObject* caller, unsigned long eid, void* vtkDebugMacro(<< "tear down"); } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::BeginDearImGuiOverlay( vtkObject* caller, unsigned long eid, void* callData) { @@ -318,6 +326,7 @@ void vtkDearImGuiInjector::BeginDearImGuiOverlay( vtkDebugMacro(<< "new frame end"); } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::RenderDearImGuiOverlay( vtkObject* caller, unsigned long eid, void* callData) { @@ -335,6 +344,7 @@ void vtkDearImGuiInjector::RenderDearImGuiOverlay( } } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::InstallEventCallback(vtkRenderWindowInteractor* interactor) { auto iObserver = interactor->GetInteractorStyle(); @@ -348,79 +358,81 @@ void vtkDearImGuiInjector::InstallEventCallback(vtkRenderWindowInteractor* inter return; } - this->currentIStyle = nullptr; + this->CurrentIStyle = nullptr; if (styleBase->IsA("vtkInteractorStyleSwitchBase")) { - this->currentIStyle = vtkInteractorStyleSwitch::SafeDownCast(styleBase)->GetCurrentStyle(); + this->CurrentIStyle = vtkInteractorStyleSwitch::SafeDownCast(styleBase)->GetCurrentStyle(); } else { - this->currentIStyle = styleBase; + this->CurrentIStyle = styleBase; } - this->currentIStyle->AddObserver(vtkCommand::EnterEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::EnterEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::LeaveEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::LeaveEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::MouseMoveEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::MouseMoveEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::LeftButtonPressEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::LeftButtonPressEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::LeftButtonReleaseEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::LeftButtonReleaseEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::LeftButtonDoubleClickEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::LeftButtonDoubleClickEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MiddleButtonPressEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MiddleButtonPressEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MiddleButtonReleaseEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MiddleButtonReleaseEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MiddleButtonDoubleClickEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MiddleButtonDoubleClickEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::RightButtonPressEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::RightButtonPressEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::RightButtonReleaseEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::RightButtonReleaseEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::RightButtonDoubleClickEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::RightButtonDoubleClickEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MouseWheelForwardEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MouseWheelForwardEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MouseWheelBackwardEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MouseWheelBackwardEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MouseWheelLeftEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MouseWheelLeftEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver( - vtkCommand::MouseWheelRightEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver( + vtkCommand::MouseWheelRightEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::ExposeEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::ExposeEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::ConfigureEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::ConfigureEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::TimerEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::TimerEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::KeyPressEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::KeyPressEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::KeyReleaseEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::KeyReleaseEvent, this->EventInterceptor, 1.0); - this->currentIStyle->AddObserver(vtkCommand::CharEvent, this->EventCallbackCommand, 1.0); + this->CurrentIStyle->AddObserver(vtkCommand::CharEvent, this->EventInterceptor, 1.0); } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::UninstallEventCallback() { - this->currentIStyle->RemoveObserver(this->EventCallbackCommand); + this->CurrentIStyle->RemoveObserver(this->EventInterceptor); } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::UpdateMousePosAndButtons(vtkRenderWindowInteractor* interactor) { // Update buttons @@ -443,6 +455,7 @@ void vtkDearImGuiInjector::UpdateMousePosAndButtons(vtkRenderWindowInteractor* i } } +//------------------------------------------------------------------------------ void vtkDearImGuiInjector::UpdateMouseCursor(vtkRenderWindow* renWin) { ImGuiIO& io = ImGui::GetIO(); @@ -466,6 +479,7 @@ void vtkDearImGuiInjector::UpdateMouseCursor(vtkRenderWindow* renWin) namespace { +//------------------------------------------------------------------------------ void mainLoopCallback(void* arg) { vtkDearImGuiInjector* self = static_cast(arg); @@ -490,9 +504,10 @@ EM_BOOL resizeCallback(int eventType, const EmscriptenUiEvent* e, void* userData } -void vtkDearImGuiInjector::PumpEv(vtkObject* caller, unsigned long eid, void* callData) +//------------------------------------------------------------------------------ +void vtkDearImGuiInjector::PumpEvents(vtkObject* caller, unsigned long eid, void* callData) { - vtkDebugMacro(<< "PumpEv"); + vtkDebugMacro(<< "PumpEvents"); auto interactor = vtkRenderWindowInteractor::SafeDownCast(caller); interactor->Enable(); interactor->Initialize(); @@ -500,7 +515,7 @@ void vtkDearImGuiInjector::PumpEv(vtkObject* caller, unsigned long eid, void* ca #ifdef __EMSCRIPTEN__ emscripten_set_resize_callback( EMSCRIPTEN_EVENT_TARGET_WINDOW, reinterpret_cast(interactor), 1, resizeCallback); - emscripten_set_main_loop_arg(&mainLoopCallback, (void*)this, 0, 1); + emscripten_set_main_loop_arg(&mainLoopCallback, (void*)this, 0, vtkRenderWindowInteractor::InteractorManagesTheEventLoop); #else while (!interactor->GetDone()) { @@ -509,7 +524,8 @@ void vtkDearImGuiInjector::PumpEv(vtkObject* caller, unsigned long eid, void* ca #endif } -void vtkDearImGuiInjector::DispatchEv( +//------------------------------------------------------------------------------ +void vtkDearImGuiInjector::InterceptEvent( vtkObject* caller, unsigned long eid, void* clientData, void* callData) { // auto interactor = vtkRenderWindowInteractor::SafeDownCast(caller); diff --git a/include/vtkDearImGuiInjector.h b/src/vtkDearImGuiInjector.h similarity index 81% rename from include/vtkDearImGuiInjector.h rename to src/vtkDearImGuiInjector.h index 8f4c911..d388f30 100644 --- a/include/vtkDearImGuiInjector.h +++ b/src/vtkDearImGuiInjector.h @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: Copyright 2023 Jaswant Panchumarti +// SPDX-License-Identifier: BSD-3-Clause + #pragma once #include @@ -12,15 +15,14 @@ #if __has_include() && WIN32 #define USES_WIN32 1 #undef USES_X11 -// #warning "Unsupported platform! Keyboard mapping not setup" #endif -#if __has_include() && EMSCRIPTEN +#if __has_include() && EMSCRIPTEN #define USES_SDL2 1 #undef USES_X11 #undef USES_WIN32 #endif #if !defined(USES_X11) && !defined(USES_WIN32) && !defined(USES_SDL2) -#warning "Unsupported platform! Keyboard mapping not setup" +#warning "Keycodes may not be recognized!" #endif class vtkRenderWindow; @@ -63,14 +65,14 @@ class VTKDEARIMGUIINJECTOR_EXPORT vtkDearImGuiInjector : public vtkObject void UpdateMouseCursor(vtkRenderWindow* renWin); // Run the event loop. - void PumpEv(vtkObject* caller, unsigned long eid, void* callData); + void PumpEvents(vtkObject* caller, unsigned long eid, void* callData); - // routes events: - // VTK[X,Win32,Cocoa]Interactor >>>> DearImGui >>>> VTK[...]InteractorStyle - static void DispatchEv(vtkObject* caller, unsigned long eid, void* clientData, void* callData); + // route an event through Dear ImGUI. + // VTK[X,Win32,Cocoa]Interactor ---> DearImGui ---> VTK[...]InteractorStyle + static void InterceptEvent(vtkObject* caller, unsigned long eid, void* clientData, void* callData); - vtkNew EventCallbackCommand; - vtkWeakPointer currentIStyle; + vtkNew EventInterceptor; + vtkWeakPointer CurrentIStyle; double Time = 0; bool MouseJustPressed[3] = { false, false, false };