Widgets: Rework event dispatching

This commit is contained in:
SirLynix
2022-07-20 13:36:21 +02:00
committed by Jérôme Leclercq
parent 05c78da22a
commit 0fcf24f336
14 changed files with 175 additions and 76 deletions

View File

@@ -394,11 +394,7 @@ namespace Nz
return false;
}
void AbstractTextAreaWidget::OnKeyReleased(const WindowEvent::KeyEvent& /*key*/)
{
}
void AbstractTextAreaWidget::OnMouseButtonDoublePress(int x, int y, Mouse::Button button)
bool AbstractTextAreaWidget::OnMouseButtonDoublePress(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -412,10 +408,13 @@ namespace Nz
HandleWordSelection(hoveredGlyph);
m_isMouseButtonDown = true;
return true;
}
return false;
}
void AbstractTextAreaWidget::OnMouseButtonPress(int x, int y, Mouse::Button button)
bool AbstractTextAreaWidget::OnMouseButtonPress(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -433,16 +432,24 @@ namespace Nz
}
m_isMouseButtonDown = true;
return true;
}
return false;
}
void AbstractTextAreaWidget::OnMouseButtonRelease(int, int, Mouse::Button button)
bool AbstractTextAreaWidget::OnMouseButtonRelease(int, int, Mouse::Button button)
{
if (button == Mouse::Left)
{
m_isMouseButtonDown = false;
return true;
}
return false;
}
void AbstractTextAreaWidget::OnMouseButtonTriplePress(int x, int y, Mouse::Button button)
bool AbstractTextAreaWidget::OnMouseButtonTriplePress(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -456,7 +463,10 @@ namespace Nz
SetSelection(Vector2ui(0, hoveredGlyph.y), Vector2ui(std::numeric_limits<unsigned int>::max(), hoveredGlyph.y));
m_isMouseButtonDown = true;
return true;
}
return false;
}
void AbstractTextAreaWidget::OnMouseEnter()
@@ -465,10 +475,15 @@ namespace Nz
m_isMouseButtonDown = false;
}
void AbstractTextAreaWidget::OnMouseMoved(int x, int y, int /*deltaX*/, int /*deltaY*/)
bool AbstractTextAreaWidget::OnMouseMoved(int x, int y, int /*deltaX*/, int /*deltaY*/)
{
if (m_isMouseButtonDown)
{
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x), float(y)));
return true;
}
return false;
}
void AbstractTextAreaWidget::OnRenderLayerUpdated(int baseRenderLayer)
@@ -478,18 +493,19 @@ namespace Nz
cursor.sprite->UpdateRenderLayer(baseRenderLayer + 1);
}
void AbstractTextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
bool AbstractTextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
{
if (m_readOnly)
return;
return false;
if (Unicode::GetCategory(character) == Unicode::Category_Other_Control || (m_characterFilter && !m_characterFilter(character)))
return;
return false;
if (HasSelection())
EraseSelection();
Write(FromUtf32String(std::u32string_view(&character, 1)));
return true;
}
void AbstractTextAreaWidget::RefreshCursor()

View File

@@ -276,38 +276,43 @@ namespace Nz
return false;
}
void BaseWidget::OnKeyReleased(const WindowEvent::KeyEvent& /*key*/)
bool BaseWidget::OnKeyReleased(const WindowEvent::KeyEvent& /*key*/)
{
return false;
}
void BaseWidget::OnMouseEnter()
{
}
void BaseWidget::OnMouseMoved(int /*x*/, int /*y*/, int /*deltaX*/, int /*deltaY*/)
bool BaseWidget::OnMouseMoved(int /*x*/, int /*y*/, int /*deltaX*/, int /*deltaY*/)
{
return false;
}
void BaseWidget::OnMouseButtonDoublePress(int x, int y, Mouse::Button button)
bool BaseWidget::OnMouseButtonDoublePress(int x, int y, Mouse::Button button)
{
return OnMouseButtonPress(x, y, button);
}
void BaseWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button /*button*/)
bool BaseWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button /*button*/)
{
return false;
}
void BaseWidget::OnMouseButtonRelease(int /*x*/, int /*y*/, Mouse::Button /*button*/)
bool BaseWidget::OnMouseButtonRelease(int /*x*/, int /*y*/, Mouse::Button /*button*/)
{
return false;
}
void BaseWidget::OnMouseButtonTriplePress(int x, int y, Mouse::Button button)
bool BaseWidget::OnMouseButtonTriplePress(int x, int y, Mouse::Button button)
{
return OnMouseButtonPress(x, y, button);
}
void BaseWidget::OnMouseWheelMoved(int /*x*/, int /*y*/, float /*delta*/)
bool BaseWidget::OnMouseWheelMoved(int /*x*/, int /*y*/, float /*delta*/)
{
return false;
}
void BaseWidget::OnMouseExit()
@@ -322,12 +327,14 @@ namespace Nz
{
}
void BaseWidget::OnTextEntered(char32_t /*character*/, bool /*repeated*/)
bool BaseWidget::OnTextEntered(char32_t /*character*/, bool /*repeated*/)
{
return false;
}
void BaseWidget::OnTextEdited(const std::array<char, 32>& /*characters*/, int /*length*/)
bool BaseWidget::OnTextEdited(const std::array<char, 32>& /*characters*/, int /*length*/)
{
return false;
}
void BaseWidget::ShowChildren(bool show)
@@ -360,6 +367,20 @@ namespace Nz
m_canvasIndex = m_canvas->RegisterWidget(this);
}
void BaseWidget::SetParent(BaseWidget* widget)
{
Canvas* oldCanvas = m_canvas;
Canvas* newCanvas = widget->GetCanvas();
// Changing a widget canvas is a problem because of the canvas entities
NazaraAssert(oldCanvas == newCanvas, "Transferring a widget between canvas is not yet supported");
Node::SetParent(widget);
m_widgetParent = widget;
Layout();
}
void BaseWidget::UnregisterFromCanvas()
{
if (IsRegisteredToCanvas())

View File

@@ -34,13 +34,18 @@ namespace Nz
m_style->Layout(GetSize());
}
void ButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
bool ButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
{
if (button == Mouse::Left)
{
m_style->OnPress();
return true;
}
return false;
}
void ButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
bool ButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -50,7 +55,11 @@ namespace Nz
// we don't want this to trigger the button, so double-check
if (IsInside(float(x), float(y)))
OnButtonTrigger(this);
return true;
}
return false;
}
void ButtonWidget::OnMouseEnter()

View File

@@ -90,23 +90,40 @@ namespace Nz
m_widgetEntries.pop_back();
}
template<typename F>
void Canvas::DispatchEvent(std::size_t widgetIndex, F&& functor)
{
for (;;)
{
WidgetEntry& targetWidget = m_widgetEntries[widgetIndex];
if (functor(targetWidget))
return;
if (!targetWidget.widget->m_widgetParent)
return;
widgetIndex = targetWidget.widget->m_widgetParent->m_canvasIndex;
}
}
void Canvas::OnEventMouseButtonPressed(const EventHandler* /*eventHandler*/, const WindowEvent::MouseButtonEvent& event)
{
UpdateHoveredWidget(event.x, event.y);
if (std::size_t targetWidgetIndex = GetMouseEventTarget(); targetWidgetIndex != InvalidCanvasIndex)
{
WidgetEntry& targetWidget = m_widgetEntries[targetWidgetIndex];
DispatchEvent(targetWidgetIndex, [&](WidgetEntry& widgetEntry)
{
int x = static_cast<int>(std::round(event.x - widgetEntry.box.x));
int y = static_cast<int>(std::round(m_size.y - event.y - widgetEntry.box.y));
int x = static_cast<int>(std::round(event.x - targetWidget.box.x));
int y = static_cast<int>(std::round(m_size.y - event.y - targetWidget.box.y));
if (event.clickCount == 2)
targetWidget.widget->OnMouseButtonDoublePress(x, y, event.button);
else if (event.clickCount == 3)
targetWidget.widget->OnMouseButtonTriplePress(x, y, event.button);
else
targetWidget.widget->OnMouseButtonPress(x, y, event.button);
if (event.clickCount == 2)
return widgetEntry.widget->OnMouseButtonDoublePress(x, y, event.button);
else if (event.clickCount == 3)
return widgetEntry.widget->OnMouseButtonTriplePress(x, y, event.button);
else
return widgetEntry.widget->OnMouseButtonPress(x, y, event.button);
});
}
SetMouseOwner(m_hoveredWidget);
@@ -117,12 +134,13 @@ namespace Nz
{
if (std::size_t targetWidgetIndex = GetMouseEventTarget(); targetWidgetIndex != InvalidCanvasIndex)
{
WidgetEntry& targetWidget = m_widgetEntries[targetWidgetIndex];
DispatchEvent(targetWidgetIndex, [&](WidgetEntry& widgetEntry)
{
int x = static_cast<int>(std::round(event.x - widgetEntry.box.x));
int y = static_cast<int>(std::round(m_size.y - event.y - widgetEntry.box.y));
int x = static_cast<int>(std::round(event.x - targetWidget.box.x));
int y = static_cast<int>(std::round(m_size.y - event.y - targetWidget.box.y));
targetWidget.widget->OnMouseButtonRelease(x, y, event.button);
return widgetEntry.widget->OnMouseButtonRelease(x, y, event.button);
});
}
m_mouseOwnerButtons[event.button] = false;
@@ -187,12 +205,13 @@ namespace Nz
{
if (std::size_t targetWidgetIndex = GetMouseEventTarget(); targetWidgetIndex != InvalidCanvasIndex)
{
WidgetEntry& targetWidget = m_widgetEntries[targetWidgetIndex];
DispatchEvent(targetWidgetIndex, [&](WidgetEntry& widgetEntry)
{
int x = static_cast<int>(std::round(event.x - widgetEntry.box.x));
int y = static_cast<int>(std::round(m_size.y - event.y - widgetEntry.box.y));
int x = static_cast<int>(std::round(event.x - targetWidget.box.x));
int y = static_cast<int>(std::round(m_size.y - event.y - targetWidget.box.y));
targetWidget.widget->OnMouseWheelMoved(x, y, event.delta);
return widgetEntry.widget->OnMouseWheelMoved(x, y, event.delta);
});
}
}
@@ -278,19 +297,22 @@ namespace Nz
{
std::size_t bestEntry = InvalidCanvasIndex;
float bestEntryArea = std::numeric_limits<float>::infinity();
int bestEntryLayer = std::numeric_limits<int>::min();
Vector3f mousePos(float(x), m_size.y - float(y), 0.f);
for (std::size_t i = 0; i < m_widgetEntries.size(); ++i)
{
const Boxf& box = m_widgetEntries[i].box;
int layer = m_widgetEntries[i].widget->GetBaseRenderLayer();
if (box.Contains(mousePos))
{
float area = box.width * box.height;
if (area < bestEntryArea)
if (area < bestEntryArea && layer >= bestEntryLayer)
{
bestEntry = i;
bestEntryArea = area;
bestEntryLayer = layer;
}
}
}

View File

@@ -41,13 +41,18 @@ namespace Nz
m_style->Layout(GetSize());
}
void CheckboxWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
bool CheckboxWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
{
if (button == Mouse::Left)
{
m_style->OnPress();
return true;
}
return false;
}
void CheckboxWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
bool CheckboxWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -57,7 +62,11 @@ namespace Nz
// we don't want this to trigger the button, so double-check
if (IsInside(x, y))
SwitchToNextState();
return true;
}
return false;
}
void CheckboxWidget::OnMouseEnter()

View File

@@ -32,13 +32,18 @@ namespace Nz
m_style->Layout(GetSize());
}
void ImageButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
bool ImageButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
{
if (button == Mouse::Left)
{
m_style->OnPress();
return true;
}
return false;
}
void ImageButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
bool ImageButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -48,7 +53,11 @@ namespace Nz
// we don't want this to trigger the button, so double-check
if (IsInside(float(x), float(y)))
OnButtonTrigger(this);
return true;
}
return false;
}
void ImageButtonWidget::OnMouseEnter()

View File

@@ -23,7 +23,7 @@ namespace Nz
m_style->Layout(GetSize());
}
void ScrollbarButtonWidget::OnMouseButtonPress(int x, int y, Mouse::Button button)
bool ScrollbarButtonWidget::OnMouseButtonPress(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -31,10 +31,13 @@ namespace Nz
OnButtonGrabbed(this, x, y);
m_style->OnGrab();
return true;
}
return false;
}
void ScrollbarButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
bool ScrollbarButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
@@ -42,7 +45,10 @@ namespace Nz
OnButtonReleased(this);
m_style->OnRelease();
return true;
}
return false;
}
void ScrollbarButtonWidget::OnMouseEnter()
@@ -55,10 +61,15 @@ namespace Nz
m_style->OnHoverEnd();
}
void ScrollbarButtonWidget::OnMouseMoved(int x, int y, int /*deltaX*/, int /*deltaY*/)
bool ScrollbarButtonWidget::OnMouseMoved(int x, int y, int /*deltaX*/, int /*deltaY*/)
{
if (m_isGrabbed)
{
OnButtonMoved(this, x, y);
return true;
}
return false;
}
void ScrollbarButtonWidget::OnRenderLayerUpdated(int baseRenderLayer)