* 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:
Jérôme Leclercq
2018-09-11 21:03:44 +02:00
committed by GitHub
parent d99ae411c6
commit 53aa9ea170
29 changed files with 470 additions and 162 deletions

View File

@@ -46,44 +46,61 @@ namespace Ndk
void EnableBackground(bool enable);
template<typename F> void ForEachWidgetChild(F iterator);
template<typename F> void ForEachWidgetChild(F iterator) const;
//virtual BaseWidget* Clone() const = 0;
inline const Nz::Color& GetBackgroundColor() const;
inline Canvas* GetCanvas();
inline Nz::SystemCursor GetCursor() const;
inline const Padding& GetPadding() const;
inline Nz::Vector2f GetContentOrigin() const;
inline const Nz::Vector2f& GetContentSize() const;
inline float GetHeight() const;
inline float GetMaximumHeight() const;
inline Nz::Vector2f GetMaximumSize() const;
inline float GetMaximumWidth() const;
inline float GetMinimumHeight() const;
inline Nz::Vector2f GetMinimumSize() const;
inline float GetMinimumWidth() const;
inline float GetPreferredHeight() const;
inline Nz::Vector2f GetPreferredSize() const;
inline float GetPreferredWidth() const;
inline Nz::Vector2f GetSize() const;
inline float GetWidth() const;
inline std::size_t GetWidgetChildCount() const;
bool HasFocus() const;
inline bool IsVisible() const;
virtual void ResizeToContent() = 0;
void Resize(const Nz::Vector2f& size);
void SetBackgroundColor(const Nz::Color& color);
void SetCursor(Nz::SystemCursor systemCursor);
inline void SetContentSize(const Nz::Vector2f& size);
void SetFocus();
inline void SetPadding(float left, float top, float right, float bottom);
void SetSize(const Nz::Vector2f& size);
inline void SetFixedHeight(float fixedHeight);
inline void SetFixedSize(const Nz::Vector2f& fixedSize);
inline void SetFixedWidth(float fixedWidth);
inline void SetMaximumHeight(float maximumHeight);
inline void SetMaximumSize(const Nz::Vector2f& maximumSize);
inline void SetMaximumWidth(float maximumWidth);
inline void SetMinimumHeight(float minimumHeight);
inline void SetMinimumSize(const Nz::Vector2f& minimumSize);
inline void SetMinimumWidth(float minimumWidth);
void Show(bool show = true);
BaseWidget& operator=(const BaseWidget&) = delete;
BaseWidget& operator=(BaseWidget&&) = delete;
struct Padding
{
float left;
float top;
float right;
float bottom;
};
protected:
const EntityHandle& CreateEntity(bool isContentEntity);
const EntityHandle& CreateEntity();
void DestroyEntity(Entity* entity);
virtual void Layout();
@@ -102,10 +119,12 @@ namespace Ndk
virtual void OnParentResized(const Nz::Vector2f& newSize);
virtual void OnTextEntered(char32_t character, bool repeated);
inline void SetPreferredSize(const Nz::Vector2f& preferredSize);
private:
inline BaseWidget();
inline void DestroyChild(BaseWidget* widget);
void DestroyChild(BaseWidget* widget);
void DestroyChildren();
inline bool IsRegisteredToCanvas() const;
inline void NotifyParentResized(const Nz::Vector2f& newSize);
@@ -117,7 +136,6 @@ namespace Ndk
struct WidgetEntity
{
EntityOwner handle;
bool isContent;
};
static constexpr std::size_t InvalidCanvasIndex = std::numeric_limits<std::size_t>::max();
@@ -127,12 +145,14 @@ namespace Ndk
std::vector<std::unique_ptr<BaseWidget>> m_children;
Canvas* m_canvas;
EntityOwner m_backgroundEntity;
Padding m_padding;
WorldHandle m_world;
Nz::Color m_backgroundColor;
Nz::SpriteRef m_backgroundSprite;
Nz::SystemCursor m_cursor;
Nz::Vector2f m_contentSize;
Nz::Vector2f m_maximumSize;
Nz::Vector2f m_minimumSize;
Nz::Vector2f m_preferredSize;
Nz::Vector2f m_size;
BaseWidget* m_widgetParent;
bool m_visible;
};

View File

@@ -13,11 +13,13 @@ namespace Ndk
m_canvas(nullptr),
m_backgroundColor(Nz::Color(230, 230, 230, 255)),
m_cursor(Nz::SystemCursor_Default),
m_contentSize(50.f, 50.f),
m_size(50.f, 50.f),
m_maximumSize(std::numeric_limits<float>::infinity()),
m_minimumSize(0.f),
m_preferredSize(-1),
m_widgetParent(nullptr),
m_visible(true)
{
SetPadding(5.f, 5.f, 5.f, 5.f);
}
template<typename T, typename... Args>
@@ -64,6 +66,20 @@ namespace Ndk
SetPosition(GetPosition(Nz::CoordSys_Local).x, (parentSize.y - mySize.y) / 2.f);
}
template<typename F>
inline void BaseWidget::ForEachWidgetChild(F iterator)
{
for (const auto& child : m_children)
iterator(child.get());
}
template<typename F>
inline void BaseWidget::ForEachWidgetChild(F iterator) const
{
for (const auto& child : m_children)
iterator(static_cast<const BaseWidget*>(child.get()));
}
inline const Nz::Color& BaseWidget::GetBackgroundColor() const
{
return m_backgroundColor;
@@ -79,24 +95,69 @@ namespace Ndk
return m_cursor;
}
inline const BaseWidget::Padding& BaseWidget::GetPadding() const
inline float BaseWidget::GetHeight() const
{
return m_padding;
return m_size.y;
}
inline Nz::Vector2f BaseWidget::GetContentOrigin() const
inline float BaseWidget::GetMaximumHeight() const
{
return { m_padding.left, m_padding.top };
return m_maximumSize.y;
}
inline const Nz::Vector2f& BaseWidget::GetContentSize() const
inline Nz::Vector2f BaseWidget::GetMaximumSize() const
{
return m_contentSize;
return m_maximumSize;
}
inline float BaseWidget::GetMaximumWidth() const
{
return m_maximumSize.x;
}
inline float BaseWidget::GetMinimumHeight() const
{
return m_minimumSize.y;
}
inline Nz::Vector2f BaseWidget::GetMinimumSize() const
{
return m_minimumSize;
}
inline float BaseWidget::GetMinimumWidth() const
{
return m_minimumSize.x;
}
inline float BaseWidget::GetPreferredHeight() const
{
return m_preferredSize.y;
}
inline Nz::Vector2f BaseWidget::GetPreferredSize() const
{
return m_preferredSize;
}
inline float BaseWidget::GetPreferredWidth() const
{
return m_preferredSize.x;
}
inline Nz::Vector2f BaseWidget::GetSize() const
{
return Nz::Vector2f(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom);
return Nz::Vector2f(GetWidth(), GetHeight());
}
inline float BaseWidget::GetWidth() const
{
return m_size.x;
}
inline std::size_t BaseWidget::GetWidgetChildCount() const
{
return m_children.size();
}
inline bool BaseWidget::IsVisible() const
@@ -104,22 +165,79 @@ namespace Ndk
return m_visible;
}
inline void BaseWidget::SetContentSize(const Nz::Vector2f& size)
inline void BaseWidget::SetFixedHeight(float fixedHeight)
{
NotifyParentResized(size);
m_contentSize = size;
Layout();
SetMaximumHeight(fixedHeight);
SetMinimumHeight(fixedHeight);
}
inline void BaseWidget::SetPadding(float left, float top, float right, float bottom)
inline void BaseWidget::SetFixedSize(const Nz::Vector2f& fixedSize)
{
m_padding.left = left;
m_padding.top = top;
m_padding.bottom = bottom;
m_padding.right = right;
SetMaximumSize(fixedSize);
SetMinimumSize(fixedSize);
}
Layout();
inline void BaseWidget::SetFixedWidth(float fixedWidth)
{
SetMaximumWidth(fixedWidth);
SetMinimumWidth(fixedWidth);
}
inline void BaseWidget::SetMaximumHeight(float maximumHeight)
{
Nz::Vector2f maximumSize = GetMaximumSize();
maximumSize.y = maximumHeight;
SetMaximumSize(maximumSize);
}
inline void BaseWidget::SetMaximumSize(const Nz::Vector2f& maximumSize)
{
m_maximumSize = maximumSize;
Nz::Vector2f size = GetSize();
if (size.x > m_maximumSize.x || size.y > m_maximumSize.y)
Resize(size); //< Will clamp automatically
}
inline void BaseWidget::SetMaximumWidth(float maximumWidth)
{
Nz::Vector2f maximumSize = GetMaximumSize();
maximumSize.x = maximumWidth;
SetMaximumSize(maximumSize);
}
inline void BaseWidget::SetMinimumHeight(float minimumHeight)
{
Nz::Vector2f minimumSize = GetMinimumSize();
minimumSize.y = minimumHeight;
SetMinimumSize(minimumSize);
}
inline void BaseWidget::SetMinimumSize(const Nz::Vector2f& minimumSize)
{
m_minimumSize = minimumSize;
Nz::Vector2f size = GetSize();
if (size.x < m_minimumSize.x || size.y < m_minimumSize.y)
Resize(size); //< Will clamp automatically
}
inline void BaseWidget::SetMinimumWidth(float minimumWidth)
{
Nz::Vector2f minimumSize = GetMinimumSize();
minimumSize.x = minimumWidth;
SetMinimumSize(minimumSize);
}
inline void BaseWidget::SetPreferredSize(const Nz::Vector2f& preferredSize)
{
m_preferredSize = preferredSize;
Resize(m_preferredSize);
}
inline bool BaseWidget::IsRegisteredToCanvas() const

View File

@@ -28,8 +28,6 @@ namespace Ndk
inline const WorldHandle& GetWorld() const;
void ResizeToContent() override;
Canvas& operator=(const Canvas&) = delete;
Canvas& operator=(Canvas&&) = delete;

View File

@@ -27,9 +27,6 @@ namespace Ndk
m_mouseMovedSlot.Connect(eventHandler.OnMouseMoved, this, &Canvas::OnEventMouseMoved);
m_mouseLeftSlot.Connect(eventHandler.OnMouseLeft, this, &Canvas::OnEventMouseLeft);
m_textEnteredSlot.Connect(eventHandler.OnTextEntered, this, &Canvas::OnEventTextEntered);
// Disable padding by default
SetPadding(0.f, 0.f, 0.f, 0.f);
}
inline Canvas::~Canvas()

View File

@@ -0,0 +1,48 @@
// 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
#pragma once
#ifndef NDK_WIDGETS_BOXLAYOUT_HPP
#define NDK_WIDGETS_BOXLAYOUT_HPP
#include <NDK/Prerequisites.hpp>
#include <NDK/BaseWidget.hpp>
#include <NDK/Widgets/Enums.hpp>
#include <vector>
namespace Ndk
{
class NDK_API BoxLayout : public BaseWidget
{
public:
inline BoxLayout(BaseWidget* parent, BoxLayoutOrientation orientation);
BoxLayout(const BoxLayout&) = delete;
BoxLayout(BoxLayout&&) = default;
~BoxLayout() = default;
void Layout() override;
BoxLayout& operator=(const BoxLayout&) = delete;
BoxLayout& operator=(BoxLayout&&) = default;
private:
struct ChildInfo
{
BaseWidget* widget;
bool isConstrained;
float maximumSize;
float minimumSize;
float size;
};
std::vector<ChildInfo> m_childInfos;
BoxLayoutOrientation m_orientation;
float m_spacing;
};
}
#include <NDK/Widgets/BoxLayout.inl>
#endif // NDK_WIDGETS_BOXLAYOUT_HPP

View File

@@ -0,0 +1,15 @@
// 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>
namespace Ndk
{
BoxLayout::BoxLayout(BaseWidget* parent, BoxLayoutOrientation orientation) :
BaseWidget(parent),
m_orientation(orientation),
m_spacing(5.f)
{
}
}

View File

@@ -28,10 +28,6 @@ namespace Ndk
ButtonWidget(ButtonWidget&&) = default;
~ButtonWidget() = default;
//virtual ButtonWidget* Clone() const = 0;
void ResizeToContent() override;
inline const Nz::Color& GetColor() const;
inline const Nz::Color& GetCornerColor() const;
inline const Nz::Color& GetHoverColor() const;

View File

@@ -93,6 +93,10 @@ namespace Ndk
{
m_textSprite->Update(drawer);
Nz::Vector2f textSize = Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths());
SetMinimumSize(textSize);
SetPreferredSize(textSize + Nz::Vector2f(20.f, 10.f));
Layout();
}
}

View File

@@ -53,7 +53,6 @@ namespace Ndk
void SetState(CheckboxState state);
inline void SetTextMargin(float margin);
void ResizeToContent() override;
inline void UpdateText(const Nz::AbstractTextDrawer& drawer);
@@ -68,6 +67,7 @@ namespace Ndk
void Layout() override;
void UpdateCheckbox();
void UpdateSize();
void OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button) override;
inline bool ContainsCheckbox(int x, int y) const;

View File

@@ -65,6 +65,7 @@ namespace Ndk
m_checkboxBackgroundSprite->SetSize(size - GetCheckboxBorderSize() * 2.f);
m_checkboxContentSprite->SetSize(GetCheckboxSize() - GetCheckboxBorderSize() * 2.f - Nz::Vector2f { 4.f, 4.f });
UpdateSize();
Layout();
}
@@ -77,6 +78,8 @@ namespace Ndk
inline void CheckboxWidget::UpdateText(const Nz::AbstractTextDrawer& drawer)
{
m_textSprite->Update(drawer);
UpdateSize();
Layout();
}

View File

@@ -9,6 +9,12 @@
namespace Ndk
{
enum BoxLayoutOrientation
{
BoxLayoutOrientation_Horizontal,
BoxLayoutOrientation_Vertical
};
enum CheckboxState
{
CheckboxState_Checked,

View File

@@ -26,14 +26,14 @@ namespace Ndk
//virtual ImageWidget* Clone() const = 0;
void ResizeToContent() override;
void ResizeToContent();
inline const Nz::Color& GetColor() const;
inline const Nz::TextureRef& GetTexture() const;
inline const Nz::Rectf& GetTextureCoords() const;
inline void SetColor(const Nz::Color& color);
inline void SetTexture(const Nz::TextureRef& texture, bool resizeToContent = true);
inline void SetTexture(const Nz::TextureRef& texture);
inline void SetTextureCoords(const Nz::Rectf& coords);
inline void SetTextureRect(const Nz::Rectui& rect);

View File

@@ -26,12 +26,13 @@ namespace Ndk
m_sprite->SetColor(color);
}
inline void ImageWidget::SetTexture(const Nz::TextureRef& texture, bool resizeToContent)
inline void ImageWidget::SetTexture(const Nz::TextureRef& texture)
{
m_sprite->SetTexture(texture, false);
if (resizeToContent)
ResizeToContent();
Nz::Vector2f textureSize = Nz::Vector2f(Nz::Vector2ui(m_sprite->GetMaterial()->GetDiffuseMap()->GetSize()));
SetMinimumSize(textureSize);
SetPreferredSize(textureSize);
}
inline void ImageWidget::SetTextureCoords(const Nz::Rectf& coords)

View File

@@ -26,10 +26,6 @@ namespace Ndk
LabelWidget(LabelWidget&&) = default;
~LabelWidget() = default;
//virtual LabelWidget* Clone() const = 0;
void ResizeToContent() override;
inline void UpdateText(const Nz::AbstractTextDrawer& drawer);
LabelWidget& operator=(const LabelWidget&) = delete;

View File

@@ -9,5 +9,8 @@ namespace Ndk
inline void LabelWidget::UpdateText(const Nz::AbstractTextDrawer& drawer)
{
m_textSprite->Update(drawer);
SetMinimumSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
}
}

View File

@@ -67,8 +67,6 @@ namespace Ndk
inline void SetTextMargin(float margin);
inline void SetTextColor(const Nz::Color& color);
inline void ResizeToContent() override {}
NazaraSignal(OnValueChanged, const ProgressBarWidget* /*progressBar*/);
private:

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Samy Bensaid
// Copyright (C) 2017 Samy Bensaid
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
@@ -148,9 +148,8 @@ namespace Ndk
{
if (IsTextEnabled())
{
Nz::Vector2f size = GetContentSize();
m_textSprite->Update(Nz::SimpleTextDrawer::Draw(Nz::String::Number(m_value).Append('%'),
static_cast<unsigned>(std::min(size.x, size.y) / 2.f), 0u, m_textColor));
Nz::Vector2f size = GetSize();
m_textSprite->Update(Nz::SimpleTextDrawer::Draw(Nz::String::Number(m_value).Append('%'), static_cast<unsigned int>(std::min(size.x, size.y) / 2.f), 0u, m_textColor));
}
}
}

View File

@@ -11,8 +11,8 @@
#include <Nazara/Utility/SimpleTextDrawer.hpp>
#include <NDK/BaseWidget.hpp>
#include <NDK/Widgets/Enums.hpp>
#include <vector>
#include <functional>
#include <vector>
namespace Ndk
{
@@ -40,7 +40,7 @@ namespace Ndk
void Erase(std::size_t firstGlyph, std::size_t lastGlyph);
void EraseSelection();
inline CharacterFilter GetCharacterFilter() const;
inline const CharacterFilter& GetCharacterFilter() const;
inline unsigned int GetCharacterSize() const;
inline const Nz::Vector2ui& GetCursorPosition() const;
inline Nz::Vector2ui GetCursorPosition(std::size_t glyphIndex) const;
@@ -61,10 +61,8 @@ namespace Ndk
inline void MoveCursor(int offset);
inline void MoveCursor(const Nz::Vector2i& offset);
void ResizeToContent() override;
inline void SetCharacterFilter(CharacterFilter filter);
inline void SetCharacterSize(unsigned int characterSize);
void SetCharacterSize(unsigned int characterSize);
inline void SetCursorPosition(std::size_t glyphIndex);
inline void SetCursorPosition(Nz::Vector2ui cursorPosition);
inline void SetEchoMode(EchoMode echoMode);
@@ -108,7 +106,7 @@ namespace Ndk
void RefreshCursor();
void UpdateDisplayText();
std::function<bool(char32_t)> m_characterFilter;
CharacterFilter m_characterFilter;
EchoMode m_echoMode;
EntityHandle m_cursorEntity;
EntityHandle m_textEntity;

View File

@@ -33,7 +33,7 @@ namespace Ndk
Erase(glyphPosition, glyphPosition + 1U);
}
inline TextAreaWidget::CharacterFilter TextAreaWidget::GetCharacterFilter() const
inline const TextAreaWidget::CharacterFilter& TextAreaWidget::GetCharacterFilter() const
{
return m_characterFilter;
}
@@ -162,12 +162,7 @@ namespace Ndk
inline void TextAreaWidget::SetCharacterFilter(CharacterFilter filter)
{
m_characterFilter = filter;
}
inline void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
{
m_drawer.SetCharacterSize(characterSize);
m_characterFilter = std::move(filter);
}
inline void TextAreaWidget::SetCursorPosition(std::size_t glyphIndex)