Skip to content

Commit d83f956

Browse files
improve ui::IInputEventChannel and ui::IWindow a bit
1 parent 41df49e commit d83f956

File tree

5 files changed

+62
-25
lines changed

5 files changed

+62
-25
lines changed

include/nbl/ui/IInputEventChannel.h

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ template <typename EventType>
4343
class IEventChannelBase : public IInputEventChannel
4444
{
4545
protected:
46+
inline virtual ~IEventChannelBase() = default;
47+
inline explicit IEventChannelBase(size_t _circular_buffer_capacity) :
48+
m_bgEventBuf(_circular_buffer_capacity), m_frontEventBuf(_circular_buffer_capacity)
49+
{
50+
}
51+
4652
using cb_t = core::CConstantRuntimeSizedCircularBuffer<EventType>;
4753
using iterator_t = typename cb_t::iterator;
4854

@@ -61,6 +67,11 @@ class IEventChannelBase : public IInputEventChannel
6167
return std::unique_lock<std::mutex>(m_bgEventBufMtx);
6268
}
6369

70+
inline bool empty() const override final
71+
{
72+
return m_bgEventBuf.size() == 0ull;
73+
}
74+
6475
// Use this within OS-specific impl (Windows callback/XNextEvent loop thread/etc...)
6576
inline void pushIntoBackground(EventType&& ev)
6677
{
@@ -75,12 +86,34 @@ class IEventChannelBase : public IInputEventChannel
7586
downloadFromBackgroundIntoFront();
7687
return range_t(m_frontEventBuf.begin(), m_frontEventBuf.end());
7788
}
78-
79-
inline virtual ~IEventChannelBase() = default;
80-
inline explicit IEventChannelBase(size_t _circular_buffer_capacity) :
81-
m_bgEventBuf(_circular_buffer_capacity), m_frontEventBuf(_circular_buffer_capacity)
89+
90+
template<typename F, class ChannelType> requires std::is_base_of_v<IEventChannelBase<EventType>,ChannelType>
91+
class CChannelConsumer : public core::IReferenceCounted
8292
{
83-
}
93+
public:
94+
CChannelConsumer(F&& process, core::smart_refctd_ptr<ChannelType>&& channel)
95+
: m_process(std::move(process)), m_channel(std::move(channel)) {}
96+
97+
inline void operator()()
98+
{
99+
auto events = m_channel->getEvents();
100+
const auto frontBufferCapacity = m_channel->getFrontBufferCapacity();
101+
if (events.size()>consumedCounter+frontBufferCapacity)
102+
{
103+
m_process.overflow(events.size()-consumedCounter,m_channel.get());
104+
consumedCounter = events.size()-frontBufferCapacity;
105+
}
106+
m_process(range_t(events.begin()+consumedCounter,events.end()),m_channel.get());
107+
consumedCounter = events.size();
108+
}
109+
110+
const auto* getChannel() const {return m_channel.get();}
111+
112+
protected:
113+
F m_process;
114+
core::smart_refctd_ptr<ChannelType> m_channel;
115+
uint64_t consumedCounter = 0ull;
116+
};
84117

85118
private:
86119
inline void downloadFromBackgroundIntoFront()
@@ -93,12 +126,6 @@ class IEventChannelBase : public IInputEventChannel
93126
m_frontEventBuf.push_back(std::move(ev));
94127
}
95128
}
96-
97-
public:
98-
inline bool empty() const override final
99-
{
100-
return m_bgEventBuf.size() == 0ull;
101-
}
102129
};
103130
}
104131

@@ -119,6 +146,9 @@ class NBL_API2 IMouseEventChannel : public impl::IEventChannelBase<SMouseEvent>
119146
{
120147
return ET_MOUSE;
121148
}
149+
150+
template<typename F>
151+
using CChannelConsumer = base_t::CChannelConsumer<F,IMouseEventChannel>;
122152
};
123153

124154
// TODO left/right shift/ctrl/alt kb flags
@@ -139,6 +169,9 @@ class NBL_API2 IKeyboardEventChannel : public impl::IEventChannelBase<SKeyboardE
139169
{
140170
return ET_KEYBOARD;
141171
}
172+
173+
template<typename F>
174+
using CChannelConsumer = base_t::CChannelConsumer<F,IKeyboardEventChannel>;
142175
};
143176

144177
}

include/nbl/ui/IWindow.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class IWindow : public core::IReferenceCounted
161161
NBL_API2 virtual void onGainedKeyboardFocus_impl() {}
162162
NBL_API2 virtual void onLostKeyboardFocus_impl() {}
163163

164+
// TODO: change the signature of the disconnected calls to be `const T* const`
164165
NBL_API2 virtual void onMouseConnected_impl(core::smart_refctd_ptr<IMouseEventChannel>&& mch) {}
165166
NBL_API2 virtual void onMouseDisconnected_impl(IMouseEventChannel* mch) {}
166167
NBL_API2 virtual void onKeyboardConnected_impl(core::smart_refctd_ptr<IKeyboardEventChannel>&& kbch) {}
@@ -202,10 +203,11 @@ class IWindow : public core::IReferenceCounted
202203
core::smart_refctd_ptr<IEventCallback> callback;
203204
int32_t x, y;
204205
uint32_t width = 0u, height = 0u;
205-
E_CREATE_FLAGS flags = E_CREATE_FLAGS::ECF_NONE;
206+
core::bitflag<E_CREATE_FLAGS> flags = E_CREATE_FLAGS::ECF_NONE;
206207
uint32_t eventChannelCapacityLog2[IInputEventChannel::ET_COUNT];
207208
std::string windowCaption;
208209
};
210+
209211
protected:
210212
// TODO need to update constructors of all derived CWindow* classes
211213
inline IWindow(SCreationParams&& params) :
@@ -220,6 +222,8 @@ class IWindow : public core::IReferenceCounted
220222
core::bitflag<E_CREATE_FLAGS> m_flags = static_cast<E_CREATE_FLAGS>(0u);
221223
};
222224

225+
NBL_ENUM_ADD_BITWISE_OPERATORS(IWindow::E_CREATE_FLAGS);
226+
223227
}
224228

225229

src/nbl/ui/CWindowManagerWin32.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ IWindowManager::SDisplayInfo CWindowManagerWin32::getPrimaryDisplayInfo() const
2626
return info;
2727
}
2828

29-
static inline DWORD getWindowStyle(IWindow::E_CREATE_FLAGS flags)
29+
static inline DWORD getWindowStyle(const core::bitflag<IWindow::E_CREATE_FLAGS> flags)
3030
{
3131
DWORD style = WS_POPUP;
3232

33-
if ((flags & IWindow::ECF_FULLSCREEN) == 0)
33+
if (!flags.hasFlags(IWindow::ECF_FULLSCREEN))
3434
{
35-
if ((flags & IWindow::ECF_BORDERLESS) == 0)
35+
if (!flags.hasFlags(IWindow::ECF_BORDERLESS))
3636
{
3737
style |= WS_BORDER;
3838
style |= (WS_SYSMENU | WS_CAPTION);
@@ -41,32 +41,32 @@ static inline DWORD getWindowStyle(IWindow::E_CREATE_FLAGS flags)
4141
style |= WS_CLIPCHILDREN;
4242
style |= WS_CLIPSIBLINGS;
4343
}
44-
if (flags & IWindow::ECF_MINIMIZED)
44+
if (flags.hasFlags(IWindow::ECF_MINIMIZED))
4545
{
4646
style |= WS_MINIMIZE;
4747
}
48-
if (flags & IWindow::ECF_MAXIMIZED)
48+
if (flags.hasFlags(IWindow::ECF_MAXIMIZED))
4949
{
5050
style |= WS_MAXIMIZE;
5151
}
52-
if (flags & IWindow::ECF_ALWAYS_ON_TOP)
52+
if (flags.hasFlags(IWindow::ECF_ALWAYS_ON_TOP))
5353
{
5454
style |= WS_EX_TOPMOST;
5555
}
56-
if ((flags & IWindow::ECF_HIDDEN) == 0)
56+
if (!flags.hasFlags(IWindow::ECF_HIDDEN))
5757
{
5858
style |= WS_VISIBLE;
5959
}
6060
style |= WS_OVERLAPPEDWINDOW;
61-
if ((flags & IWindow::ECF_RESIZABLE) == 0)
61+
if (!flags.hasFlags(IWindow::ECF_RESIZABLE))
6262
{
6363
style &= ~WS_SIZEBOX;
6464
}
65-
if ((flags & IWindow::ECF_CAN_MAXIMIZE) == 0)
65+
if (!flags.hasFlags(IWindow::ECF_CAN_MAXIMIZE))
6666
{
6767
style &= ~WS_MAXIMIZEBOX;
6868
}
69-
if ((flags & IWindow::ECF_CAN_MINIMIZE) == 0)
69+
if (!flags.hasFlags(IWindow::ECF_CAN_MINIMIZE))
7070
{
7171
style &= ~WS_MINIMIZEBOX;
7272
}
@@ -186,7 +186,7 @@ void CWindowManagerWin32::SRequestParams_CreateWindow::operator()(core::StorageT
186186
);
187187

188188
//
189-
if ((flags&CWindowWin32::ECF_HIDDEN)==0)
189+
if (!flags.hasFlags(CWindowWin32::ECF_HIDDEN))
190190
ShowWindow(nativeWindow, SW_SHOWNORMAL);
191191
UpdateWindow(nativeWindow);
192192

src/nbl/ui/CWindowManagerWin32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class NBL_API2 CWindowManagerWin32 final : public IWindowManagerWin32, public IC
124124
std::string windowCaption;
125125
uint32_t width, height;
126126
int32_t x, y;
127-
IWindowWin32::E_CREATE_FLAGS flags;
127+
core::bitflag<IWindowWin32::E_CREATE_FLAGS> flags;
128128
};
129129
struct SRequestParams_DestroyWindow
130130
{

0 commit comments

Comments
 (0)