Scrolling layout

This commit is contained in:
IlyaShurupov 2024-10-17 16:46:49 +03:00
parent 03b2b5d038
commit 047d67c4fa
14 changed files with 288 additions and 35 deletions

View file

@ -27,8 +27,8 @@ namespace tp {
virtual void updateLayout(bool vertical) {}
virtual void pickRect(bool vertical) = 0;
virtual void clampRect() = 0;
virtual void pickRect(bool vertical) {}
virtual void clampRect() {}
[[nodiscard]] virtual RectF getAvailableChildArea() const;
public:
@ -59,7 +59,7 @@ namespace tp {
Widget* mWidget = nullptr;
protected:
Vec2<SizePolicy> mSizePolicy = { SizePolicy::Fixed, SizePolicy::Fixed };
Vec2<SizePolicy> mSizePolicy = { SizePolicy::Fixed, SizePolicy::Minimal };
Vec2F mMinSize = { 50, 50 };
Vec2F mMaxSize = { FLT_MAX / 2, FLT_MAX / 2 };
};

View file

@ -0,0 +1,22 @@
#pragma once
#include "Layout.hpp"
namespace tp {
class ScrollableBarWidget;
class ScrollableLayout : public WidgetLayout {
public:
explicit ScrollableLayout(Widget* widget) :
WidgetLayout(widget) {}
void updateLayout(bool vertical) override;
[[nodiscard]] RectF getAvailableChildArea() const override;
private:
void updateWidgetRects(const RectF& area, Widget* content, ScrollableBarWidget* scroller) const;
private:
halnf mScrollerSize = 15;
};
}

View file

@ -37,7 +37,7 @@ namespace tp {
RootWidget* mRootWidget = nullptr;
// debug
bool mDebug = true;
bool mDebug = false;
bool mDebugStopProcessing = false;
bool mDebugRedrawAlways = false;
bool mSlowMotion = false;

View file

@ -0,0 +1,57 @@
#pragma once
#include "Widget.hpp"
namespace tp {
// just the scrolling bar
class ScrollableBarWidget : public Widget {
public:
ScrollableBarWidget() = default;
[[nodiscard]] bool processesEvents() const override { return true; }
[[nodiscard]] bool propagateEventsToChildren() const override { return false; }
[[nodiscard]] bool needsNextFrame() const override { return mScrolling; }
void process(const EventHandler& events) override;
void draw(Canvas& canvas) override;
void updateSizeFactor(halnf factor); // holder-size divided by content-size
[[nodiscard]] bool getDirection() const { return mVertical; }
[[nodiscard]] halnf getPosFactor() const { return mPosFactor - mSizeFactor / 2; }
private:
[[nodiscard]] const RectF& getHandleRect() const;
void updateHandleRect();
void jumpTo(halnf);
void moveBy(halnf);
void clamp();
private: // state
bool mVertical = true;
bool mScrolling = false;
bool mHandleHovered = false;
Vec2F mStartPos = {};
halnf mPosFactor = 0.2f; // (center-of-holder - content-start) / content-size
halnf mSizeFactor = 0.3f;
private: // TODO : make params static
halnf mRounding = 5;
halnf mHandlePadding = 0;
RGBA mBGColor = { 0.1, 0.1, 0.1, 0.0 };
RGBA mHandleColor = { 0.2, 0.2, 0.2, 1 };
RGBA mHandleHoverColor = { 0.3, 0.3, 0.3, 1 };
RGBA mHandleSlideColor = { 0.5, 0.5, 0.5, 1 };
private: // cache
RectF mHandleRect;
};
class ScrollableWidget : public Widget {
public:
ScrollableWidget();
};
}