From 7da8945c72e80fb1f0a82ecaeeac792af1fc4044 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 7 Jul 2019 16:28:09 +0200 Subject: [PATCH] SDK/ScrollAreaWidget: Add EnableScrollbar --- SDK/include/NDK/Widgets/ScrollAreaWidget.hpp | 9 +++++- SDK/include/NDK/Widgets/ScrollAreaWidget.inl | 15 ++++++++++ SDK/src/NDK/Widgets/ScrollAreaWidget.cpp | 30 +++++++++++++++----- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/SDK/include/NDK/Widgets/ScrollAreaWidget.hpp b/SDK/include/NDK/Widgets/ScrollAreaWidget.hpp index 5b7106b1f..17ada16ef 100644 --- a/SDK/include/NDK/Widgets/ScrollAreaWidget.hpp +++ b/SDK/include/NDK/Widgets/ScrollAreaWidget.hpp @@ -21,9 +21,15 @@ namespace Ndk ScrollAreaWidget(ScrollAreaWidget&&) = default; ~ScrollAreaWidget() = default; + void EnableScrollbar(bool enable); + inline float GetScrollHeight() const; inline float GetScrollRatio() const; + inline bool HasScrollbar() const; + inline bool IsScrollbarEnabled() const; + inline bool IsScrollbarVisible() const; + inline void ScrollToHeight(float height); void ScrollToRatio(float ratio); @@ -57,7 +63,8 @@ namespace Ndk Nz::SpriteRef m_scrollbarSprite; Nz::Vector2i m_grabbedDelta; ScrollBarStatus m_scrollbarStatus; - bool m_isScrollBarVisible; + bool m_isScrollbarEnabled; + bool m_hasScrollbar; float m_scrollRatio; }; } diff --git a/SDK/include/NDK/Widgets/ScrollAreaWidget.inl b/SDK/include/NDK/Widgets/ScrollAreaWidget.inl index c98230180..89bea6d47 100644 --- a/SDK/include/NDK/Widgets/ScrollAreaWidget.inl +++ b/SDK/include/NDK/Widgets/ScrollAreaWidget.inl @@ -16,6 +16,21 @@ namespace Ndk 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) { float contentHeight = m_content->GetHeight(); diff --git a/SDK/src/NDK/Widgets/ScrollAreaWidget.cpp b/SDK/src/NDK/Widgets/ScrollAreaWidget.cpp index 4f5ca3f6b..b7ec0d020 100644 --- a/SDK/src/NDK/Widgets/ScrollAreaWidget.cpp +++ b/SDK/src/NDK/Widgets/ScrollAreaWidget.cpp @@ -18,6 +18,7 @@ namespace Ndk BaseWidget(parent), m_content(content), m_scrollbarStatus(ScrollBarStatus::None), + m_isScrollbarEnabled(true), m_scrollRatio(0.f) { m_content->SetParent(this); @@ -42,6 +43,18 @@ namespace Ndk 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) { m_scrollRatio = Nz::Clamp(ratio, 0.f, 1.f); @@ -75,13 +88,16 @@ namespace Ndk if (contentHeight > areaHeight) { - m_isScrollBarVisible = true; + m_hasScrollbar = true; Nz::Vector2f contentSize(GetWidth() - scrollBarBackgroundWidth, contentHeight); m_content->Resize(contentSize); - m_scrollbarEntity->Enable(); - m_scrollbarBackgroundEntity->Enable(); + if (m_isScrollbarEnabled) + { + m_scrollbarEntity->Enable(); + m_scrollbarBackgroundEntity->Enable(); + } float scrollBarHeight = std::max(std::floor(areaHeight * (areaHeight / contentHeight)), 20.f); @@ -95,7 +111,7 @@ namespace Ndk } else { - m_isScrollBarVisible = false; + m_hasScrollbar = false; m_content->Resize(GetSize()); @@ -119,7 +135,7 @@ namespace Ndk auto& scrollbarNode = m_scrollbarEntity->GetComponent(); - 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) { 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 { 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); } }