Skip to content

Commit 57e5fd4

Browse files
committed
add event callbacks, update sources to not crash example anymore at runtime - now I have black screen. TODO: inspect what I have mistaken, I see some validation errors
1 parent c6a4586 commit 57e5fd4

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

61_UI/main.cpp

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,48 @@ using namespace asset;
2020
using namespace ui;
2121
using namespace video;
2222

23+
class CEventCallback : public ISimpleManagedSurface::ICallback
24+
{
25+
public:
26+
CEventCallback(nbl::core::smart_refctd_ptr<InputSystem>&& m_inputSystem, nbl::system::logger_opt_smart_ptr&& logger) : m_inputSystem(std::move(m_inputSystem)), m_logger(std::move(logger)) {}
27+
CEventCallback() {}
28+
29+
void setLogger(nbl::system::logger_opt_smart_ptr& logger)
30+
{
31+
m_logger = logger;
32+
}
33+
void setInputSystem(nbl::core::smart_refctd_ptr<InputSystem>&& m_inputSystem)
34+
{
35+
m_inputSystem = std::move(m_inputSystem);
36+
}
37+
private:
38+
39+
void onMouseConnected_impl(nbl::core::smart_refctd_ptr<nbl::ui::IMouseEventChannel>&& mch) override
40+
{
41+
m_logger.log("A mouse %p has been connected", nbl::system::ILogger::ELL_INFO, mch.get());
42+
m_inputSystem.get()->add(m_inputSystem.get()->m_mouse, std::move(mch));
43+
}
44+
void onMouseDisconnected_impl(nbl::ui::IMouseEventChannel* mch) override
45+
{
46+
m_logger.log("A mouse %p has been disconnected", nbl::system::ILogger::ELL_INFO, mch);
47+
m_inputSystem.get()->remove(m_inputSystem.get()->m_mouse, mch);
48+
}
49+
void onKeyboardConnected_impl(nbl::core::smart_refctd_ptr<nbl::ui::IKeyboardEventChannel>&& kbch) override
50+
{
51+
m_logger.log("A keyboard %p has been connected", nbl::system::ILogger::ELL_INFO, kbch.get());
52+
m_inputSystem.get()->add(m_inputSystem.get()->m_keyboard, std::move(kbch));
53+
}
54+
void onKeyboardDisconnected_impl(nbl::ui::IKeyboardEventChannel* kbch) override
55+
{
56+
m_logger.log("A keyboard %p has been disconnected", nbl::system::ILogger::ELL_INFO, kbch);
57+
m_inputSystem.get()->remove(m_inputSystem.get()->m_keyboard, kbch);
58+
}
59+
60+
private:
61+
nbl::core::smart_refctd_ptr<InputSystem> m_inputSystem = nullptr;
62+
nbl::system::logger_opt_smart_ptr m_logger = nullptr;
63+
};
64+
2365
class UISampleApp final : public examples::SimpleWindowedApplication
2466
{
2567
using device_base_t = examples::SimpleWindowedApplication;
@@ -39,14 +81,16 @@ class UISampleApp final : public examples::SimpleWindowedApplication
3981
if (!m_surface)
4082
{
4183
{
84+
auto windowCallback = core::make_smart_refctd_ptr<CEventCallback>(smart_refctd_ptr(m_inputSystem), smart_refctd_ptr(m_logger));
4285
IWindow::SCreationParams params = {};
4386
params.callback = core::make_smart_refctd_ptr<nbl::video::ISimpleManagedSurface::ICallback>();
44-
params.width = 256;
45-
params.height = 256;
87+
params.width = WIN_W;
88+
params.height = WIN_H;
4689
params.x = 32;
4790
params.y = 32;
4891
params.flags = ui::IWindow::ECF_HIDDEN | IWindow::ECF_BORDERLESS | IWindow::ECF_RESIZABLE;
4992
params.windowCaption = "UISampleApp";
93+
params.callback = windowCallback;
5094
const_cast<std::remove_const_t<decltype(m_window)>&>(m_window) = m_winMgr->createWindow(std::move(params));
5195
}
5296

@@ -62,6 +106,8 @@ class UISampleApp final : public examples::SimpleWindowedApplication
62106

63107
inline bool onAppInitialized(smart_refctd_ptr<ISystem>&& system) override
64108
{
109+
m_inputSystem = make_smart_refctd_ptr<InputSystem>(logger_opt_smart_ptr(smart_refctd_ptr(m_logger)));
110+
65111
if (!device_base_t::onAppInitialized(smart_refctd_ptr(system)))
66112
return false;
67113

@@ -99,7 +145,9 @@ class UISampleApp final : public examples::SimpleWindowedApplication
99145
};
100146

101147
auto scResources = std::make_unique<CDefaultSwapchainFramebuffers>(m_device.get(), swapchainParams.surfaceFormat.format, dependencies);
102-
if (!scResources->getRenderpass())
148+
auto* renderpass = scResources->getRenderpass();
149+
150+
if (!renderpass)
103151
return logFail("Failed to create Renderpass!");
104152

105153
auto gQueue = getGraphicsQueue();
@@ -123,7 +171,7 @@ class UISampleApp final : public examples::SimpleWindowedApplication
123171
return logFail("Couldn't create Command Buffer!");
124172
}
125173

126-
ui = core::make_smart_refctd_ptr<nbl::ext::imgui::UI>(smart_refctd_ptr(m_device), (int)m_maxFramesInFlight, scResources->getRenderpass(), nullptr, smart_refctd_ptr(m_window));
174+
ui = core::make_smart_refctd_ptr<nbl::ext::imgui::UI>(smart_refctd_ptr(m_device), (int)m_maxFramesInFlight, renderpass, nullptr, smart_refctd_ptr(m_window));
127175
ui->Register([this]() -> void
128176
{
129177
ui->BeginWindow("Test window");
@@ -135,6 +183,10 @@ class UISampleApp final : public examples::SimpleWindowedApplication
135183
}
136184
);
137185

186+
m_winMgr->setWindowSize(m_window.get(), WIN_W, WIN_H);
187+
m_surface->recreateSwapchain();
188+
m_winMgr->show(m_window.get());
189+
138190
return true;
139191
}
140192

@@ -243,24 +295,34 @@ class UISampleApp final : public examples::SimpleWindowedApplication
243295
}
244296

245297
static std::chrono::microseconds previousEventTimestamp{};
246-
std::vector<SMouseEvent> validEvents{};
247298

248-
mouse.consumeEvents([&validEvents](const IMouseEventChannel::range_t& events) -> void
299+
struct
300+
{
301+
std::vector<SMouseEvent> mouse{};
302+
} capturedEvents;
303+
304+
m_inputSystem->getDefaultMouse(&mouse);
305+
m_inputSystem->getDefaultKeyboard(&keyboard);
306+
307+
mouse.consumeEvents([&](const IMouseEventChannel::range_t& events) -> void
249308
{
250309
for (auto event : events)
251310
{
252311
if (event.timeStamp < previousEventTimestamp)
253312
continue;
254313

255314
previousEventTimestamp = event.timeStamp;
256-
257-
validEvents.push_back(event);
315+
capturedEvents.mouse.push_back(event);
258316
}
259-
});
317+
}, m_logger.get());
260318

261-
auto const mousePosition = m_window->getCursorControl()->getPosition();
319+
keyboard.consumeEvents([&](const IKeyboardEventChannel::range_t& events) -> void
320+
{
321+
// TOOD
322+
}, m_logger.get());
262323

263-
ui->Update(deltaTimeInSec, static_cast<float>(mousePosition.x), static_cast<float>(mousePosition.y), validEvents.size(), validEvents.data());
324+
const auto mousePosition = m_window->getCursorControl()->getPosition();
325+
ui->Update(deltaTimeInSec, static_cast<float>(mousePosition.x), static_cast<float>(mousePosition.y), capturedEvents.mouse.size(), capturedEvents.mouse.data());
264326
}
265327

266328
inline bool keepRunning() override

0 commit comments

Comments
 (0)