diff --git a/SDK/include/NDK/BaseWidget.hpp b/SDK/include/NDK/BaseWidget.hpp index 2bdf0dd81..6cfea3a8c 100644 --- a/SDK/include/NDK/BaseWidget.hpp +++ b/SDK/include/NDK/BaseWidget.hpp @@ -46,6 +46,7 @@ namespace Ndk inline const Nz::Color& GetBackgroundColor() const; inline Canvas* GetCanvas(); inline const Padding& GetPadding() const; + inline Nz::Vector2f GetContentOrigin() const; inline const Nz::Vector2f& GetContentSize() const; inline Nz::Vector2f GetSize() const; diff --git a/SDK/include/NDK/BaseWidget.inl b/SDK/include/NDK/BaseWidget.inl index 041abe9bd..8ad0521a2 100644 --- a/SDK/include/NDK/BaseWidget.inl +++ b/SDK/include/NDK/BaseWidget.inl @@ -60,6 +60,11 @@ namespace Ndk return m_padding; } + inline Nz::Vector2f BaseWidget::GetContentOrigin() const + { + return { m_padding.left, m_padding.top }; + } + inline const Nz::Vector2f& BaseWidget::GetContentSize() const { return m_contentSize; diff --git a/SDK/include/NDK/Widgets/TextAreaWidget.hpp b/SDK/include/NDK/Widgets/TextAreaWidget.hpp index 2b0774ec8..311a1a840 100644 --- a/SDK/include/NDK/Widgets/TextAreaWidget.hpp +++ b/SDK/include/NDK/Widgets/TextAreaWidget.hpp @@ -57,7 +57,7 @@ namespace Ndk TextAreaWidget& operator=(TextAreaWidget&&) = default; private: - void RefreshCursor(); + void Layout() override; void OnKeyPressed(const Nz::WindowEvent::KeyEvent& key) override; void OnKeyReleased(const Nz::WindowEvent::KeyEvent& key) override; @@ -67,6 +67,8 @@ namespace Ndk void OnMouseExit() override; void OnTextEntered(char32_t character, bool repeated) override; + void RefreshCursor(); + EntityHandle m_cursorEntity; EntityHandle m_textEntity; Nz::SimpleTextDrawer m_drawer; diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp index 69d60965e..bdd3b93bb 100644 --- a/SDK/src/NDK/BaseWidget.cpp +++ b/SDK/src/NDK/BaseWidget.cpp @@ -119,12 +119,7 @@ namespace Ndk m_canvas->NotifyWidgetUpdate(m_canvasIndex); if (m_backgroundEntity) - { - NodeComponent& node = m_backgroundEntity->GetComponent(); - node.SetPosition(-m_padding.left, -m_padding.top); - m_backgroundSprite->SetSize(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom); - } } void BaseWidget::InvalidateNode() diff --git a/SDK/src/NDK/Canvas.cpp b/SDK/src/NDK/Canvas.cpp index b13b7ef6a..2aac8846c 100644 --- a/SDK/src/NDK/Canvas.cpp +++ b/SDK/src/NDK/Canvas.cpp @@ -18,9 +18,6 @@ namespace Ndk { if (m_backgroundEntity) { - NodeComponent& node = m_backgroundEntity->GetComponent(); - node.SetPosition(-m_padding.left, -m_padding.top); - m_backgroundSprite->SetSize(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom); } } diff --git a/SDK/src/NDK/Widgets/ButtonWidget.cpp b/SDK/src/NDK/Widgets/ButtonWidget.cpp index 25ac74c69..88ae0bf35 100644 --- a/SDK/src/NDK/Widgets/ButtonWidget.cpp +++ b/SDK/src/NDK/Widgets/ButtonWidget.cpp @@ -40,12 +40,14 @@ namespace Ndk { BaseWidget::Layout(); + Nz::Vector2f origin = GetContentOrigin(); const Nz::Vector2f& contentSize = GetContentSize(); + m_gradientEntity->GetComponent().SetPosition(origin); m_gradientSprite->SetSize(contentSize); Nz::Boxf textBox = m_textEntity->GetComponent().GetBoundingVolume().aabb; - m_textEntity->GetComponent().SetPosition(contentSize.x / 2 - textBox.width / 2, contentSize.y / 2 - textBox.height / 2); + m_textEntity->GetComponent().SetPosition(origin.x + contentSize.x / 2 - textBox.width / 2, origin.y + contentSize.y / 2 - textBox.height / 2); } void ButtonWidget::OnMouseButtonRelease(int /*x*/, int /*y*/, Nz::Mouse::Button button) diff --git a/SDK/src/NDK/Widgets/TextAreaWidget.cpp b/SDK/src/NDK/Widgets/TextAreaWidget.cpp index 0c9b7b3d5..e239efac7 100644 --- a/SDK/src/NDK/Widgets/TextAreaWidget.cpp +++ b/SDK/src/NDK/Widgets/TextAreaWidget.cpp @@ -94,33 +94,10 @@ namespace Ndk } } - void TextAreaWidget::RefreshCursor() { - std::size_t lineCount = m_drawer.GetLineCount(); - std::size_t line = 0U; - for (std::size_t i = line + 1; i < lineCount; ++i) - { - if (m_drawer.GetLine(i).glyphIndex > m_cursorPosition) - break; + BaseWidget::Layout(); - line = i; - } - const auto& lineInfo = m_drawer.GetLine(line); - - std::size_t glyphCount = m_drawer.GetGlyphCount(); - float position; - if (glyphCount > 0 && lineInfo.glyphIndex < m_cursorPosition) - { - const auto& glyph = m_drawer.GetGlyph(std::min(m_cursorPosition, glyphCount - 1)); - position = glyph.bounds.x; - if (m_cursorPosition >= glyphCount) - position += glyph.bounds.width; - } - else - position = 0.f; - - m_cursorEntity->GetComponent().SetPosition(position, lineInfo.bounds.y); } void TextAreaWidget::OnKeyPressed(const Nz::WindowEvent::KeyEvent& key) @@ -222,4 +199,35 @@ namespace Ndk } } } + + void TextAreaWidget::RefreshCursor() + { + std::size_t lineCount = m_drawer.GetLineCount(); + std::size_t line = 0U; + for (std::size_t i = line + 1; i < lineCount; ++i) + { + if (m_drawer.GetLine(i).glyphIndex > m_cursorPosition) + break; + + line = i; + } + + const auto& lineInfo = m_drawer.GetLine(line); + + std::size_t glyphCount = m_drawer.GetGlyphCount(); + float position; + if (glyphCount > 0 && lineInfo.glyphIndex < m_cursorPosition) + { + const auto& glyph = m_drawer.GetGlyph(std::min(m_cursorPosition, glyphCount - 1)); + position = glyph.bounds.x; + if (m_cursorPosition >= glyphCount) + position += glyph.bounds.width; + } + else + position = 0.f; + + Nz::Vector2f contentOrigin = GetContentOrigin(); + + m_cursorEntity->GetComponent().SetPosition(contentOrigin.x + position, contentOrigin.y + lineInfo.bounds.y); + } }