Sdk/Widgets: Properly fixes padding

This commit is contained in:
Lynix 2017-01-18 23:06:31 +01:00
parent 5752792565
commit c1dfc5c4b8
7 changed files with 44 additions and 34 deletions

View File

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

View File

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

View File

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

View File

@ -119,12 +119,7 @@ namespace Ndk
m_canvas->NotifyWidgetUpdate(m_canvasIndex);
if (m_backgroundEntity)
{
NodeComponent& node = m_backgroundEntity->GetComponent<NodeComponent>();
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()

View File

@ -18,9 +18,6 @@ namespace Ndk
{
if (m_backgroundEntity)
{
NodeComponent& node = m_backgroundEntity->GetComponent<NodeComponent>();
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);
}
}

View File

@ -40,12 +40,14 @@ namespace Ndk
{
BaseWidget::Layout();
Nz::Vector2f origin = GetContentOrigin();
const Nz::Vector2f& contentSize = GetContentSize();
m_gradientEntity->GetComponent<NodeComponent>().SetPosition(origin);
m_gradientSprite->SetSize(contentSize);
Nz::Boxf textBox = m_textEntity->GetComponent<GraphicsComponent>().GetBoundingVolume().aabb;
m_textEntity->GetComponent<NodeComponent>().SetPosition(contentSize.x / 2 - textBox.width / 2, contentSize.y / 2 - textBox.height / 2);
m_textEntity->GetComponent<NodeComponent>().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)

View File

@ -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<Ndk::NodeComponent>().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<NodeComponent>().SetPosition(contentOrigin.x + position, contentOrigin.y + lineInfo.bounds.y);
}
}