Sdk/Canvas: Add basic keyboard handler
This commit is contained in:
parent
38da351e9b
commit
7df732b927
|
|
@ -45,6 +45,8 @@ namespace Ndk
|
|||
inline const Nz::Vector2f& GetContentSize() const;
|
||||
inline Nz::Vector2f GetSize() const;
|
||||
|
||||
void GrabKeyboard();
|
||||
|
||||
virtual void ResizeToContent() = 0;
|
||||
|
||||
inline void SetContentSize(const Nz::Vector2f& size);
|
||||
|
|
@ -73,6 +75,7 @@ namespace Ndk
|
|||
virtual void OnMouseButtonPress(int x, int y, Nz::Mouse::Button button);
|
||||
virtual void OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button);
|
||||
virtual void OnMouseExit();
|
||||
virtual void OnTextEntered(char32_t character, bool repeated);
|
||||
|
||||
private:
|
||||
inline BaseWidget();
|
||||
|
|
|
|||
|
|
@ -36,7 +36,11 @@ namespace Ndk
|
|||
void Layout() override;
|
||||
|
||||
void NotifyWidgetUpdate(std::size_t index);
|
||||
|
||||
std::size_t RegisterWidget(BaseWidget* widget);
|
||||
|
||||
inline void SetKeyboardOwner(BaseWidget* widget);
|
||||
|
||||
void UnregisterWidget(std::size_t index);
|
||||
|
||||
private:
|
||||
|
|
@ -44,6 +48,7 @@ namespace Ndk
|
|||
void OnMouseButtonRelease(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::MouseButtonEvent& event);
|
||||
void OnMouseMoved(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::MouseMoveEvent& event);
|
||||
void OnMouseLeft(const Nz::EventHandler* eventHandler);
|
||||
void OnTextEntered(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::TextEvent& event);
|
||||
|
||||
struct WidgetBox
|
||||
{
|
||||
|
|
@ -55,9 +60,11 @@ namespace Ndk
|
|||
NazaraSlot(Nz::EventHandler, OnMouseButtonReleased, m_mouseButtonReleasedSlot);
|
||||
NazaraSlot(Nz::EventHandler, OnMouseMoved, m_mouseMovedSlot);
|
||||
NazaraSlot(Nz::EventHandler, OnMouseLeft, m_mouseLeftSlot);
|
||||
NazaraSlot(Nz::EventHandler, OnTextEntered, m_textEnteredSlot);
|
||||
|
||||
std::vector<WidgetBox> m_widgetBoxes;
|
||||
const WidgetBox* m_hoveredWidget;
|
||||
BaseWidget* m_keyboardOwner;
|
||||
WorldHandle m_world;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ namespace Ndk
|
|||
{
|
||||
inline Canvas::Canvas(WorldHandle world, Nz::EventHandler& eventHandler) :
|
||||
m_hoveredWidget(nullptr),
|
||||
m_keyboardOwner(nullptr),
|
||||
m_world(std::move(world))
|
||||
{
|
||||
m_canvas = this;
|
||||
|
|
@ -16,10 +17,16 @@ namespace Ndk
|
|||
m_mouseButtonReleasedSlot.Connect(eventHandler.OnMouseButtonReleased, this, &Canvas::OnMouseButtonRelease);
|
||||
m_mouseMovedSlot.Connect(eventHandler.OnMouseMoved, this, &Canvas::OnMouseMoved);
|
||||
m_mouseLeftSlot.Connect(eventHandler.OnMouseLeft, this, &Canvas::OnMouseLeft);
|
||||
m_textEnteredSlot.Connect(eventHandler.OnTextEntered, this, &Canvas::OnTextEntered);
|
||||
}
|
||||
|
||||
inline const WorldHandle& Canvas::GetWorld() const
|
||||
{
|
||||
return m_world;
|
||||
}
|
||||
|
||||
inline void Ndk::Canvas::SetKeyboardOwner(BaseWidget* widget)
|
||||
{
|
||||
m_keyboardOwner = widget;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,11 @@ namespace Ndk
|
|||
m_entities.erase(it);
|
||||
}
|
||||
|
||||
void BaseWidget::GrabKeyboard()
|
||||
{
|
||||
m_canvas->SetKeyboardOwner(this);
|
||||
}
|
||||
|
||||
void BaseWidget::Layout()
|
||||
{
|
||||
if (m_canvas)
|
||||
|
|
@ -112,4 +117,8 @@ namespace Ndk
|
|||
void BaseWidget::OnMouseExit()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnTextEntered(char32_t character, bool repeated)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -57,6 +57,9 @@ namespace Ndk
|
|||
if (m_hoveredWidget == &entry)
|
||||
m_hoveredWidget = nullptr;
|
||||
|
||||
if (m_keyboardOwner == entry.widget)
|
||||
m_keyboardOwner = nullptr;
|
||||
|
||||
entry = std::move(lastEntry);
|
||||
entry.widget->UpdateCanvasIndex(index);
|
||||
m_widgetBoxes.pop_back();
|
||||
|
|
@ -136,4 +139,10 @@ namespace Ndk
|
|||
m_hoveredWidget = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::OnTextEntered(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::TextEvent& event)
|
||||
{
|
||||
if (m_keyboardOwner)
|
||||
m_keyboardOwner->OnTextEntered(event.character, event.repeated);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue