Skip to content

Commit cb388cc

Browse files
Rewrote UI System as an extension under namespace nbl::ext::imgui. I have a linking issue that fixed it with a trick I saw in Blur. I'm going to ask for real solution and check what I have done wrong.
1 parent 61b4788 commit cb388cc

File tree

11 files changed

+433
-372
lines changed

11 files changed

+433
-372
lines changed

3rdparty/CMakeLists.txt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,23 @@ add_library(spirv_cross OBJECT
266266
)
267267
target_compile_definitions(spirv_cross PUBLIC SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS)
268268

269-
270-
add_library(imgui
271-
"imgui/imconfig.h"
272-
"imgui/imgui_demo.cpp"
273-
"imgui/imgui_draw.cpp"
274-
"imgui/imgui_internal.h"
275-
"imgui/imgui_tables.cpp"
276-
"imgui/imgui_widgets.cpp"
277-
"imgui/imgui.cpp"
278-
"imgui/imgui.h"
279-
"imgui/imgui_stdlib.cpp"
280-
"imgui/imgui_stdlib.h"
281-
"imgui/imstb_rectpack.h"
282-
"imgui/imstb_textedit.h"
283-
"imgui/imstb_truetype.h"
284-
)
269+
if(NBL_BUILD_IMGUI)
270+
add_library(imgui
271+
"imgui/imconfig.h"
272+
"imgui/imgui_demo.cpp"
273+
"imgui/imgui_draw.cpp"
274+
"imgui/imgui_internal.h"
275+
"imgui/imgui_tables.cpp"
276+
"imgui/imgui_widgets.cpp"
277+
"imgui/imgui.cpp"
278+
"imgui/imgui.h"
279+
"imgui/imgui_stdlib.cpp"
280+
"imgui/imgui_stdlib.h"
281+
"imgui/imstb_rectpack.h"
282+
"imgui/imstb_textedit.h"
283+
"imgui/imstb_truetype.h"
284+
)
285+
endif()
285286

286287
add_library(aesGladman OBJECT
287288
aesGladman/aes_ni.c
@@ -368,7 +369,6 @@ set(NBL_3RDPARTY_TARGETS
368369
SPIRV-Tools-static # SPIRV-Tools-shared in case of SHARED lib
369370
SPIRV-Tools-opt
370371
egl
371-
imgui
372372
)
373373
if (_NBL_COMPILE_WITH_OPEN_EXR_)
374374
list(APPEND NBL_3RDPARTY_TARGETS
@@ -391,6 +391,9 @@ endif()
391391
if (NBL_BUILD_MITSUBA_LOADER)
392392
list(APPEND NBL_3RDPARTY_TARGETS expat)
393393
endif()
394+
if (NBL_BUILD_IMGUI)
395+
list(APPEND NBL_3RDPARTY_TARGETS imgui)
396+
endif()
394397
if(ENABLE_HLSL)
395398
list(APPEND NBL_3RDPARTY_TARGETS HLSL)
396399
endif()

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ option(NBL_BUILD_EXAMPLES "Enable building examples" ON)
253253

254254
option(NBL_BUILD_TOOLS "Enable building tools (just convert2BAW as for now)" ON)
255255

256-
option(NBL_BUILD_MITSUBA_LOADER "Enable nbl::ext::MitsubaLoader?" ON)
256+
option(NBL_BUILD_MITSUBA_LOADER "Enable nbl::ext::MitsubaLoader?" OFF)
257+
258+
option(NBL_BUILD_IMGUI "Enable nbl::ext::ImGui?" ON)
257259

258260
if (NBL_COMPILE_WITH_CUDA)
259261
find_package(OPTIX)

examples_tests/61.UI/CMakeLists.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
if(NBL_BUILD_IMGUI)
2+
13
include(common RESULT_VARIABLE RES)
24
if(NOT RES)
35
message(FATAL_ERROR "common.cmake not found. Should be in {repo_root}/cmake directory")
46
endif()
57

6-
set(EXTRA_SOURCES "")
7-
list(APPEND EXTRA_SOURCES
8-
"UI_System.cpp"
9-
"UI_System.hpp"
8+
list(APPEND libs
9+
"imgui"
10+
"${NBL_EXT_IMGUI_LIB}"
1011
)
1112

12-
nbl_create_executable_project("${EXTRA_SOURCES}" "" "" "imgui")
13+
# I'll ask about why it is not working about me adding imGui.cpp manually
14+
nbl_create_executable_project("../../src/nbl/ext/ImGui/ImGui.cpp" "" "" "${libs}")
15+
16+
endif()

examples_tests/61.UI/UI_System.hpp

Lines changed: 0 additions & 110 deletions
This file was deleted.

examples_tests/61.UI/main.cpp

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
#define _NBL_STATIC_LIB_
66
#include <nabla.h>
77

8-
98
#include "../common/CommonAPI.h"
10-
11-
#include "UI_System.hpp"
9+
#include "nbl/ext/ImGui/ImGui.h"
10+
#include "nbl/ui/ICursorControl.h"
1211

1312
using namespace nbl;
1413
using namespace asset;
@@ -114,26 +113,24 @@ class UIApp : public ApplicationBase
114113

115114
nbl::video::IGPUObjectFromAssetConverter cpu2gpu;
116115

117-
// TOOD: Pass pipeline cache in future
118-
UI::Init(
116+
ui = core::make_smart_refctd_ptr<nbl::ext::imgui::UI>(
119117
logicalDevice,
120118
FRAMES_IN_FLIGHT,
121119
renderpass,
122120
nullptr,
123121
cpu2gpuParams,
124-
inputSystem,
125122
window
126123
);
127124

128-
UI::Register([]()->void{
129-
UI::BeginWindow("Test window");
130-
UI::SetNextItemWidth(100);
131-
UI::Text("Hi");
132-
UI::SetNextItemWidth(100);
133-
UI::Button("Button", []()->void {
125+
ui->Register([this]()->void{
126+
ui->BeginWindow("Test window");
127+
ui->SetNextItemWidth(100);
128+
ui->Text("Hi");
129+
ui->SetNextItemWidth(100);
130+
ui->Button("Button", []()->void {
134131
printf("Button pressed!\n");
135132
});
136-
UI::EndWindow();
133+
ui->EndWindow();
137134
});
138135

139136
logicalDevice->createCommandBuffers(commandPools[CommonAPI::InitOutput::EQT_GRAPHICS].get(),video::IGPUCommandBuffer::EL_PRIMARY,FRAMES_IN_FLIGHT,commandBuffers);
@@ -149,7 +146,6 @@ class UIApp : public ApplicationBase
149146
void onAppTerminated_impl() override
150147
{
151148
logicalDevice->waitIdle();
152-
UI::Shutdown();
153149
}
154150

155151
void workLoopBody() override
@@ -202,9 +198,10 @@ class UIApp : public ApplicationBase
202198

203199
commandBuffer->beginRenderPass(&beginInfo, nbl::asset::ESC_INLINE);
204200

201+
// TODO: Use real deltaTime instead
205202
float deltaTimeInSec = 0.1f;
206-
// TODO: Use deltaTime instead
207-
UI::Render(*commandBuffer, resourceIx);
203+
204+
ui->Render(*commandBuffer, resourceIx);
208205

209206
commandBuffer->endRenderPass();
210207
commandBuffer->end();
@@ -228,7 +225,36 @@ class UIApp : public ApplicationBase
228225
acquiredNextFBO
229226
);
230227

231-
UI::Update(deltaTimeInSec);
228+
CommonAPI::InputSystem::ChannelReader<IMouseEventChannel> mouse;
229+
230+
inputSystem->getDefaultMouse(&mouse);
231+
232+
static std::chrono::microseconds previousEventTimestamp {};
233+
234+
std::vector<SMouseEvent> validEvents {};
235+
236+
mouse.consumeEvents([&validEvents](const IMouseEventChannel::range_t& events) -> void {
237+
for (auto event : events)
238+
{
239+
if (event.timeStamp < previousEventTimestamp)
240+
{
241+
continue;
242+
}
243+
previousEventTimestamp = event.timeStamp;
244+
245+
validEvents.push_back(event);
246+
}
247+
});
248+
249+
auto const mousePosition = window->getCursorControl()->getPosition();
250+
251+
ui->Update(
252+
deltaTimeInSec,
253+
static_cast<float>(mousePosition.x),
254+
static_cast<float>(mousePosition.y),
255+
validEvents.size(),
256+
validEvents.data()
257+
);
232258
}
233259

234260
bool keepRunning() override
@@ -260,7 +286,9 @@ class UIApp : public ApplicationBase
260286
core::smart_refctd_ptr<video::IGPUFence> frameComplete[FRAMES_IN_FLIGHT] = { nullptr };
261287
core::smart_refctd_ptr<video::IGPUSemaphore> imageAcquire[FRAMES_IN_FLIGHT] = { nullptr };
262288
core::smart_refctd_ptr<video::IGPUSemaphore> renderFinished[FRAMES_IN_FLIGHT] = { nullptr };
263-
289+
290+
nbl::core::smart_refctd_ptr<nbl::ext::imgui::UI> ui {};
291+
264292
_NBL_STATIC_INLINE_CONSTEXPR uint64_t MAX_TIMEOUT = 99999999999999ull;
265293
uint32_t acquiredNextFBO = {};
266294
int32_t resourceIx = -1;

examples_tests/common/CommonAPI.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#pragma once
1+
#ifndef COMMON_API_H
2+
#define COMMON_API_H
23

34
#define _NBL_STATIC_LIB_
45
#include <nabla.h>
@@ -1854,3 +1855,4 @@ CommonAPI::main<app_class>(argc, argv);\
18541855
#endif
18551856
//***** Application framework macros ******
18561857

1858+
#endif

include/nbl/core/decl/smart_refctd_ptr.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ class smart_refctd_ptr
9898
inline I_REFERENCE_COUNTED& operator[](size_t idx) { return ptr[idx]; }
9999
inline const I_REFERENCE_COUNTED& operator[](size_t idx) const { return ptr[idx]; }
100100

101-
[[nodiscard]]
102-
bool isValid() const noexcept
103-
{
104-
return ptr != nullptr;
105-
}
106-
107101
inline explicit operator bool() const { return ptr; }
108102
inline bool operator!() const { return !ptr; }
109103

0 commit comments

Comments
 (0)