SDK/ScrollAreaWidget: Add EnableScrollbar

This commit is contained in:
Lynix 2019-07-07 16:28:09 +02:00
parent f5dc27ba03
commit 7da8945c72
3 changed files with 46 additions and 8 deletions

View File

@ -21,9 +21,15 @@ namespace Ndk
ScrollAreaWidget(ScrollAreaWidget&&) = default; ScrollAreaWidget(ScrollAreaWidget&&) = default;
~ScrollAreaWidget() = default; ~ScrollAreaWidget() = default;
void EnableScrollbar(bool enable);
inline float GetScrollHeight() const; inline float GetScrollHeight() const;
inline float GetScrollRatio() const; inline float GetScrollRatio() const;
inline bool HasScrollbar() const;
inline bool IsScrollbarEnabled() const;
inline bool IsScrollbarVisible() const;
inline void ScrollToHeight(float height); inline void ScrollToHeight(float height);
void ScrollToRatio(float ratio); void ScrollToRatio(float ratio);
@ -57,7 +63,8 @@ namespace Ndk
Nz::SpriteRef m_scrollbarSprite; Nz::SpriteRef m_scrollbarSprite;
Nz::Vector2i m_grabbedDelta; Nz::Vector2i m_grabbedDelta;
ScrollBarStatus m_scrollbarStatus; ScrollBarStatus m_scrollbarStatus;
bool m_isScrollBarVisible; bool m_isScrollbarEnabled;
bool m_hasScrollbar;
float m_scrollRatio; float m_scrollRatio;
}; };
} }

View File

@ -16,6 +16,21 @@ namespace Ndk
return m_scrollRatio; return m_scrollRatio;
} }
inline bool ScrollAreaWidget::HasScrollbar() const
{
return m_hasScrollbar;
}
inline bool ScrollAreaWidget::IsScrollbarEnabled() const
{
return m_isScrollbarEnabled;
}
inline bool ScrollAreaWidget::IsScrollbarVisible() const
{
return HasScrollbar() && IsScrollbarEnabled();
}
inline void ScrollAreaWidget::ScrollToHeight(float height) inline void ScrollAreaWidget::ScrollToHeight(float height)
{ {
float contentHeight = m_content->GetHeight(); float contentHeight = m_content->GetHeight();

View File

@ -18,6 +18,7 @@ namespace Ndk
BaseWidget(parent), BaseWidget(parent),
m_content(content), m_content(content),
m_scrollbarStatus(ScrollBarStatus::None), m_scrollbarStatus(ScrollBarStatus::None),
m_isScrollbarEnabled(true),
m_scrollRatio(0.f) m_scrollRatio(0.f)
{ {
m_content->SetParent(this); m_content->SetParent(this);
@ -42,6 +43,18 @@ namespace Ndk
Resize(m_content->GetSize()); Resize(m_content->GetSize());
} }
void ScrollAreaWidget::EnableScrollbar(bool enable)
{
if (m_isScrollbarEnabled != enable)
{
m_isScrollbarEnabled = enable;
bool isVisible = IsScrollbarVisible();
m_scrollbarEntity->Enable(isVisible);
m_scrollbarBackgroundEntity->Enable(isVisible);
}
}
void ScrollAreaWidget::ScrollToRatio(float ratio) void ScrollAreaWidget::ScrollToRatio(float ratio)
{ {
m_scrollRatio = Nz::Clamp(ratio, 0.f, 1.f); m_scrollRatio = Nz::Clamp(ratio, 0.f, 1.f);
@ -75,13 +88,16 @@ namespace Ndk
if (contentHeight > areaHeight) if (contentHeight > areaHeight)
{ {
m_isScrollBarVisible = true; m_hasScrollbar = true;
Nz::Vector2f contentSize(GetWidth() - scrollBarBackgroundWidth, contentHeight); Nz::Vector2f contentSize(GetWidth() - scrollBarBackgroundWidth, contentHeight);
m_content->Resize(contentSize); m_content->Resize(contentSize);
if (m_isScrollbarEnabled)
{
m_scrollbarEntity->Enable(); m_scrollbarEntity->Enable();
m_scrollbarBackgroundEntity->Enable(); m_scrollbarBackgroundEntity->Enable();
}
float scrollBarHeight = std::max(std::floor(areaHeight * (areaHeight / contentHeight)), 20.f); float scrollBarHeight = std::max(std::floor(areaHeight * (areaHeight / contentHeight)), 20.f);
@ -95,7 +111,7 @@ namespace Ndk
} }
else else
{ {
m_isScrollBarVisible = false; m_hasScrollbar = false;
m_content->Resize(GetSize()); m_content->Resize(GetSize());
@ -119,7 +135,7 @@ namespace Ndk
auto& scrollbarNode = m_scrollbarEntity->GetComponent<Ndk::NodeComponent>(); auto& scrollbarNode = m_scrollbarEntity->GetComponent<Ndk::NodeComponent>();
m_grabbedDelta.Set(x, y - scrollbarNode.GetPosition(Nz::CoordSys_Local).y); m_grabbedDelta.Set(x, int(y - scrollbarNode.GetPosition(Nz::CoordSys_Local).y));
} }
} }
@ -131,7 +147,7 @@ namespace Ndk
if (m_scrollbarStatus == ScrollBarStatus::Grabbed) if (m_scrollbarStatus == ScrollBarStatus::Grabbed)
{ {
Nz::Rectf scrollBarRect = GetScrollbarRect(); Nz::Rectf scrollBarRect = GetScrollbarRect();
UpdateScrollbarStatus((scrollBarRect.Contains(Nz::Vector2f(x, y))) ? ScrollBarStatus::Hovered : ScrollBarStatus::None); UpdateScrollbarStatus((scrollBarRect.Contains(Nz::Vector2f(float(x), float(y)))) ? ScrollBarStatus::Hovered : ScrollBarStatus::None);
} }
} }
@ -154,7 +170,7 @@ namespace Ndk
else else
{ {
Nz::Rectf scrollBarRect = GetScrollbarRect(); Nz::Rectf scrollBarRect = GetScrollbarRect();
UpdateScrollbarStatus((scrollBarRect.Contains(Nz::Vector2f(x, y))) ? ScrollBarStatus::Hovered : ScrollBarStatus::None); UpdateScrollbarStatus((scrollBarRect.Contains(Nz::Vector2f(float(x), float(y)))) ? ScrollBarStatus::Hovered : ScrollBarStatus::None);
} }
} }