Layouts (#189)
* Layout WIP * Widgets/BoxLayout: Fix layout algorithm * Widgets/BoxLayout: Fix box layout algorithm for good * SDK/Widgets: Remove padding * Sdk/Widgets: Make use of minimum/preferred size * Sdk/TextAreaWidget: Add Minimum/PreferredSize to TextArea * Sdk/Widgets: Add height/width variants of get/set fixed, maximum, minimum and preferred size methods * Sdk/BoxLayout: Remove useless code * Sdk/TextAreaWidget: Fix compilation * Widgets/TextAreaWidget: Fix cursor position
This commit is contained in:
122
SDK/src/NDK/Widgets/BoxLayout.cpp
Normal file
122
SDK/src/NDK/Widgets/BoxLayout.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NDK/Widgets/BoxLayout.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <Nazara/Core/MemoryHelper.hpp>
|
||||
#include <cassert>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
void BoxLayout::Layout()
|
||||
{
|
||||
std::size_t axis1, axis2;
|
||||
|
||||
switch (m_orientation)
|
||||
{
|
||||
case BoxLayoutOrientation_Horizontal:
|
||||
axis1 = 0; //< x
|
||||
axis2 = 1; //< y
|
||||
break;
|
||||
|
||||
case BoxLayoutOrientation_Vertical:
|
||||
axis1 = 1; //< y
|
||||
axis2 = 0; //< x
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
m_childInfos.clear();
|
||||
|
||||
// Handle size
|
||||
ForEachWidgetChild([&](BaseWidget* child)
|
||||
{
|
||||
if (!child->IsVisible())
|
||||
return;
|
||||
|
||||
m_childInfos.emplace_back();
|
||||
auto& info = m_childInfos.back();
|
||||
info.isConstrained = false;
|
||||
info.maximumSize = child->GetMaximumSize()[axis1];
|
||||
info.minimumSize = child->GetMinimumSize()[axis1];
|
||||
info.size = info.minimumSize;
|
||||
info.widget = child;
|
||||
});
|
||||
|
||||
Nz::Vector2f layoutSize = GetSize();
|
||||
|
||||
float availableSpace = layoutSize[axis1] - m_spacing * (m_childInfos.size() - 1);
|
||||
float remainingSize = availableSpace;
|
||||
for (auto& info : m_childInfos)
|
||||
remainingSize -= info.minimumSize;
|
||||
|
||||
// Okay this algorithm is FAR from perfect but I couldn't figure a way other than this one
|
||||
std::size_t unconstrainedChildCount = m_childInfos.size();
|
||||
|
||||
bool hasUnconstrainedChilds = false;
|
||||
for (std::size_t i = 0; i < m_childInfos.size(); ++i)
|
||||
{
|
||||
if (remainingSize <= 0.0001f)
|
||||
break;
|
||||
|
||||
float evenSize = remainingSize / unconstrainedChildCount;
|
||||
|
||||
for (auto& info : m_childInfos)
|
||||
{
|
||||
if (info.isConstrained)
|
||||
continue;
|
||||
|
||||
float previousSize = info.size;
|
||||
|
||||
info.size += evenSize;
|
||||
if (info.size > info.maximumSize)
|
||||
{
|
||||
unconstrainedChildCount--;
|
||||
|
||||
evenSize += (info.size - info.maximumSize) / unconstrainedChildCount;
|
||||
info.isConstrained = true;
|
||||
info.size = info.maximumSize;
|
||||
}
|
||||
else
|
||||
hasUnconstrainedChilds = true;
|
||||
|
||||
remainingSize -= info.size - previousSize;
|
||||
}
|
||||
|
||||
if (!hasUnconstrainedChilds)
|
||||
break;
|
||||
}
|
||||
|
||||
float spacing = m_spacing + remainingSize / (m_childInfos.size() - 1);
|
||||
|
||||
for (auto& info : m_childInfos)
|
||||
{
|
||||
Nz::Vector2f newSize = info.widget->GetSize();
|
||||
newSize[axis1] = info.size;
|
||||
|
||||
info.widget->Resize(newSize);
|
||||
}
|
||||
|
||||
// Handle position
|
||||
float cursor = 0.f;
|
||||
bool first = true;
|
||||
for (auto& info : m_childInfos)
|
||||
{
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
cursor += spacing;
|
||||
|
||||
Nz::Vector2f position = Nz::Vector2f(0.f, 0.f);
|
||||
position[axis1] = cursor;
|
||||
|
||||
info.widget->SetPosition(position);
|
||||
|
||||
cursor += info.size;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -30,13 +30,13 @@ namespace Ndk
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner_RightBottom, m_cornerColor);
|
||||
m_gradientSprite->SetMaterial(Nz::Material::New("Basic2D"));
|
||||
|
||||
m_gradientEntity = CreateEntity(false);
|
||||
m_gradientEntity = CreateEntity();
|
||||
m_gradientEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_gradientEntity->AddComponent<GraphicsComponent>().Attach(m_gradientSprite);
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite, 1);
|
||||
|
||||
@@ -73,22 +73,15 @@ namespace Ndk
|
||||
return s_pressCornerColor;
|
||||
}
|
||||
|
||||
void ButtonWidget::ResizeToContent()
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
|
||||
void ButtonWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
m_gradientSprite->SetSize(GetSize());
|
||||
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
const Nz::Vector2f& contentSize = GetContentSize();
|
||||
Nz::Vector2f size = GetSize();
|
||||
m_gradientSprite->SetSize(size);
|
||||
|
||||
Nz::Boxf textBox = m_textEntity->GetComponent<GraphicsComponent>().GetAABB();
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(origin.x + contentSize.x / 2 - textBox.width / 2, origin.y + contentSize.y / 2 - textBox.height / 2);
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(size.x / 2.f - textBox.width / 2.f, size.y / 2.f - textBox.height / 2.f);
|
||||
}
|
||||
|
||||
void ButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button button)
|
||||
|
||||
@@ -28,19 +28,19 @@ namespace Ndk
|
||||
m_checkboxContentSprite = Nz::Sprite::New(Nz::Material::New("Translucent2D"));
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_checkboxBorderEntity = CreateEntity(false);
|
||||
m_checkboxBorderEntity = CreateEntity();
|
||||
m_checkboxBorderEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_checkboxBorderEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxBorderSprite);
|
||||
|
||||
m_checkboxBackgroundEntity = CreateEntity(false);
|
||||
m_checkboxBackgroundEntity = CreateEntity();
|
||||
m_checkboxBackgroundEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_checkboxBackgroundEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxBackgroundSprite, 1);
|
||||
|
||||
m_checkboxContentEntity = CreateEntity(true);
|
||||
m_checkboxContentEntity = CreateEntity();
|
||||
m_checkboxContentEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_checkboxContentEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxContentSprite, 2);
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
|
||||
@@ -108,20 +108,11 @@ namespace Ndk
|
||||
return m_state;
|
||||
}
|
||||
|
||||
void CheckboxWidget::ResizeToContent()
|
||||
{
|
||||
Nz::Vector3f textSize = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
Nz::Vector2f checkboxSize = GetCheckboxSize();
|
||||
|
||||
Nz::Vector2f finalSize { checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin) + textSize.x, std::max(textSize.y, checkboxSize.y) };
|
||||
SetContentSize(finalSize);
|
||||
}
|
||||
|
||||
void CheckboxWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
Nz::Vector2f origin = Nz::Vector2f(0.f);
|
||||
Nz::Vector2f checkboxSize = GetCheckboxSize();
|
||||
Nz::Vector2f borderSize = GetCheckboxBorderSize();
|
||||
|
||||
@@ -178,4 +169,14 @@ namespace Ndk
|
||||
m_checkboxContentSprite->SetTexture(Nz::TextureRef {});
|
||||
}
|
||||
}
|
||||
|
||||
void CheckboxWidget::UpdateSize()
|
||||
{
|
||||
Nz::Vector3f textSize = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
Nz::Vector2f checkboxSize = GetCheckboxSize();
|
||||
|
||||
Nz::Vector2f finalSize{ checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin) + textSize.x, std::max(textSize.y, checkboxSize.y) };
|
||||
SetMinimumSize(finalSize);
|
||||
SetPreferredSize(finalSize);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Ndk
|
||||
ImageWidget::ImageWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent)
|
||||
{
|
||||
m_entity = CreateEntity(true);
|
||||
m_entity = CreateEntity();
|
||||
m_entity->AddComponent<NodeComponent>();
|
||||
auto& gfx = m_entity->AddComponent<GraphicsComponent>();
|
||||
|
||||
@@ -19,19 +19,10 @@ namespace Ndk
|
||||
gfx.Attach(m_sprite);
|
||||
}
|
||||
|
||||
void ImageWidget::ResizeToContent()
|
||||
{
|
||||
Nz::Vector3ui textureSize = m_sprite->GetMaterial()->GetDiffuseMap()->GetSize();
|
||||
SetSize({ static_cast<float>(textureSize.x), static_cast<float>(textureSize.y) });
|
||||
}
|
||||
|
||||
void ImageWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
Nz::Vector2f contentSize = GetContentSize();
|
||||
|
||||
m_entity->GetComponent<NodeComponent>().SetPosition(origin);
|
||||
m_sprite->SetSize(contentSize);
|
||||
m_sprite->SetSize(GetSize());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Ndk
|
||||
{
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
|
||||
@@ -23,12 +23,5 @@ namespace Ndk
|
||||
void LabelWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(GetContentOrigin());
|
||||
}
|
||||
|
||||
void LabelWidget::ResizeToContent()
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ namespace Ndk
|
||||
SetBarColor(s_barColor, s_barCornerColor);
|
||||
|
||||
|
||||
m_borderEntity = CreateEntity(false);
|
||||
m_borderEntity = CreateEntity();
|
||||
m_borderEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_borderEntity->AddComponent<GraphicsComponent>().Attach(m_borderSprite);
|
||||
|
||||
m_barEntity = CreateEntity(true);
|
||||
m_barEntity = CreateEntity();
|
||||
m_barEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
GraphicsComponent& graphics = m_barEntity->AddComponent<GraphicsComponent>();
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Ndk
|
||||
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
@@ -76,8 +76,8 @@ namespace Ndk
|
||||
|
||||
void ProgressBarWidget::Layout()
|
||||
{
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
Nz::Vector2f size = GetContentSize();
|
||||
Nz::Vector2f origin = Nz::Vector2f(0.f);
|
||||
Nz::Vector2f size = GetSize();
|
||||
Nz::Vector2f progressBarSize = size;
|
||||
|
||||
if (IsTextEnabled())
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <NDK/Widgets/TextAreaWidget.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
#include <Nazara/Utility/Font.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
|
||||
@@ -20,19 +21,23 @@ namespace Ndk
|
||||
m_readOnly(false),
|
||||
m_tabEnabled(false)
|
||||
{
|
||||
m_cursorEntity = CreateEntity(true);
|
||||
m_cursorEntity = CreateEntity();
|
||||
m_cursorEntity->AddComponent<GraphicsComponent>();
|
||||
m_cursorEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_cursorEntity->GetComponent<NodeComponent>().SetPosition(5.f, 3.f);
|
||||
m_cursorEntity->Enable(false);
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(5.f, 3.f);
|
||||
|
||||
SetCursor(Nz::SystemCursor_Text);
|
||||
SetCharacterSize(GetCharacterSize()); //< Actualize minimum / preferred size
|
||||
|
||||
EnableBackground(true);
|
||||
Layout();
|
||||
}
|
||||
|
||||
@@ -140,9 +145,25 @@ namespace Ndk
|
||||
return Nz::Vector2ui::Zero();
|
||||
}
|
||||
|
||||
void TextAreaWidget::ResizeToContent()
|
||||
void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
m_drawer.SetCharacterSize(characterSize);
|
||||
|
||||
std::size_t fontCount = m_drawer.GetFontCount();
|
||||
unsigned int lineHeight = 0;
|
||||
int spaceAdvance = 0;
|
||||
for (std::size_t i = 0; i < fontCount; ++i)
|
||||
{
|
||||
Nz::Font* font = m_drawer.GetFont(i);
|
||||
|
||||
const Nz::Font::SizeInfo& sizeInfo = font->GetSizeInfo(characterSize);
|
||||
lineHeight = std::max(lineHeight, sizeInfo.lineHeight);
|
||||
spaceAdvance = std::max(spaceAdvance, sizeInfo.spaceAdvance);
|
||||
}
|
||||
|
||||
Nz::Vector2f size = { float(spaceAdvance), float(lineHeight) + 5.f };
|
||||
SetMinimumSize(size);
|
||||
SetPreferredSize({ size.x * 6.f, size.y });
|
||||
}
|
||||
|
||||
void TextAreaWidget::Write(const Nz::String& text, std::size_t glyphPosition)
|
||||
@@ -165,8 +186,6 @@ namespace Ndk
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(GetContentOrigin());
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
|
||||
@@ -403,8 +422,7 @@ namespace Ndk
|
||||
{
|
||||
SetFocus();
|
||||
|
||||
const Padding& padding = GetPadding();
|
||||
Nz::Vector2ui hoveredGlyph = GetHoveredGlyph(float(x - padding.left), float(y - padding.top));
|
||||
Nz::Vector2ui hoveredGlyph = GetHoveredGlyph(float(x) - 5.f, float(y) - 5.f);
|
||||
|
||||
// Shift extends selection
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::LShift) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::RShift))
|
||||
@@ -434,10 +452,7 @@ namespace Ndk
|
||||
void TextAreaWidget::OnMouseMoved(int x, int y, int deltaX, int deltaY)
|
||||
{
|
||||
if (m_isMouseButtonDown)
|
||||
{
|
||||
const Padding& padding = GetPadding();
|
||||
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x - padding.left), float(y - padding.top)));
|
||||
}
|
||||
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x) - 5.f, float(y) - 3.f));
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
|
||||
@@ -514,8 +529,6 @@ namespace Ndk
|
||||
if (m_readOnly)
|
||||
return;
|
||||
|
||||
m_cursorEntity->GetComponent<NodeComponent>().SetPosition(GetContentOrigin());
|
||||
|
||||
std::size_t selectionLineCount = m_cursorPositionEnd.y - m_cursorPositionBegin.y + 1;
|
||||
std::size_t oldSpriteCount = m_cursorSprites.size();
|
||||
if (m_cursorSprites.size() != selectionLineCount)
|
||||
|
||||
Reference in New Issue
Block a user