Sdk/Canvas: Add basic keyboard handler

This commit is contained in:
Lynix 2016-10-28 17:46:02 +02:00
parent 38da351e9b
commit 7df732b927
5 changed files with 36 additions and 1 deletions

View File

@ -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();

View File

@ -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,7 +48,8 @@ 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
{
BaseWidget* widget;
@ -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;
};
}

View File

@ -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;
}
}

View File

@ -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)
{
}
}

View File

@ -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);
}
}