Update widgets module. Raster example initial.

This commit is contained in:
IlyaShurupov 2024-04-12 15:27:40 +03:00
parent 9e339defd6
commit 9cfae3fe3e
48 changed files with 1169 additions and 530 deletions

View file

@ -8,17 +8,7 @@ namespace tp {
template <typename Events, typename Canvas>
class ScrollBarWidget : public Widget<Events, Canvas> {
public:
ScrollBarWidget() {
this->createConfig("ScrollBar");
this->addColor("Default", "Base");
this->addColor("Handle", "Accent");
this->addColor("Hovered", "Interaction");
this->addColor("Scrolling", "Action");
this->addValue("Padding", "Padding");
this->addValue("HandleSize", 20.f);
this->addValue("MinSize", 20.f);
this->addValue("Rounding", "Rounding");
}
ScrollBarWidget() = default;
// takes whole area
void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override {
@ -79,28 +69,22 @@ namespace tp {
if (mSizeFraction > 1.f) return;
// if (!areaParent.isOverlap(getHandle())) return;
tp::RGBA col = this->getColor("Handle");
tp::RGBA col = mHandleColor;
if (mIsScrolling) {
col = this->getColor("Scrolling");
col = mScrollingColor;
} else if (mHovered) {
col = this->getColor("Hovered");
col = mHoveredColor;
}
canvas.rect(area, this->getColor("Default"), this->getValue("Rounding"));
canvas.rect(area, mDefaultColor, mRounding);
canvas.rect(getHandleHandle(), col, this->getValue("Rounding"));
canvas.rect(getHandleHandle(), col, mRounding);
}
RectF getHandleHandle() const {
auto minSize = this->getValue("MinSize");
if (mIsScrolling) {
minSize = this->getValue("MinSize") * 2;
} else if (mHovered) {
minSize = this->getValue("MinSize") * 2;
}
auto area = getHandle();
auto sliderSize = tp::clamp(area.w * mSizeFraction, minSize, area.w);
auto sliderSize = tp::clamp(area.w * mSizeFraction, mMinSize * 2, area.w);
auto diffSize = sliderSize - area.w * mSizeFraction;
return { area.x, area.y + (area.w - diffSize) * mPositionFraction, area.z, sliderSize };
}
@ -109,14 +93,28 @@ namespace tp {
if (mSizeFraction > 1.f) {
return this->mArea;
}
return { this->mArea.x, this->mArea.y, this->mArea.z - this->getValue("HandleSize"), this->mArea.w };
return { this->mArea.x, this->mArea.y, this->mArea.z - mHandleSize, this->mArea.w };
}
RectF getHandle() const {
return { this->mArea.x + this->mArea.z - this->getValue("HandleSize") + this->getValue("Padding"),
this->mArea.y + this->getValue("Padding"),
this->getValue("HandleSize") - this->getValue("Padding") * 2,
this->mArea.w - this->getValue("Padding") * 2 };
return { this->mArea.x + this->mArea.z - mHandleSize + mPadding,
this->mArea.y + mPadding,
mHandleSize - mPadding * 2,
this->mArea.w - mPadding * 2 };
}
public:
void updateConfigCache(WidgetManager& wm) override {
wm.setActiveId("Scrollbar");
mDefaultColor = wm.getColor("Default", "Base");
mHandleColor = wm.getColor("Handle", "Accent");
mHoveredColor = wm.getColor("Hovered", "Interaction");
mScrollingColor = wm.getColor("Scrolling", "Action");
mPadding = wm.getNumber("Padding", "Padding");
mHandleSize = wm.getNumber("HandleSize", 20.f);
mMinSize = wm.getNumber("MinSize", 20.f);
mRounding = wm.getNumber("Rounding", "Rounding");
}
public:
@ -126,29 +124,29 @@ namespace tp {
halnf mSizeFraction = 1.f;
halnf mPositionFraction = 0.f;
bool mHovered = false;
RGBA mDefaultColor;
RGBA mHandleColor;
RGBA mHoveredColor;
RGBA mScrollingColor;
halnf mPadding = 0;
halnf mHandleSize = 10;
halnf mMinSize = 10;
halnf mRounding = 10;
};
template <typename Events, typename Canvas>
class ScrollableWindow : public Widget<Events, Canvas> {
public:
ScrollableWindow() {
this->createConfig("ScrollableWindow");
this->addColor("Default", "Base");
this->addValue("Padding", "Padding");
}
ScrollableWindow() = default;
~ScrollableWindow() = default;
// takes whole area
void proc(const Events& events, const tp::RectF& areaParent, const tp::RectF& aArea) override {
this->mArea = aArea;
this->mVisible = areaParent.isOverlap(aArea);
if (!this->mVisible) return;
void procBody(const Events& events) override {
updateContents();
updateContentSize();
const auto padding = this->getValue("Padding");
const auto padding = mPadding;
mScroller.mSizeFraction = this->mArea.w / mContentSize;
mScroller.proc(events, this->mArea, this->mArea);
@ -168,9 +166,7 @@ namespace tp {
}
}
void draw(Canvas& canvas) override {
if (!this->mVisible) return;
void drawBody(Canvas& canvas) override {
mScroller.draw(canvas);
canvas.pushClamp(this->mArea);
@ -180,16 +176,29 @@ namespace tp {
canvas.popClamp();
}
public:
void updateConfigCache(WidgetManager& wm) override {
wm.setActiveId("ScrollableWidget");
mDefaultColor = wm.getColor("Default", "Base");
mPadding = wm.getNumber("Padding", "Padding");
mScroller.updateConfigCache(wm);
for (auto item : mContents) {
item->updateConfigCache(wm);
}
}
private:
void updateContents() {
if (mContents.size()) {
const auto padding = this->getValue("Padding");
const halnf offset = mContents.first()->mArea.y + padding;
const halnf offset = mContents.first()->mArea.y + mPadding;
halnf start = 0;
for (auto widget : mContents) {
widget->mArea.y = start;
start += widget->mArea.w + padding;
start += widget->mArea.w + mPadding;
}
for (auto widget : mContents) {
@ -201,17 +210,15 @@ namespace tp {
void updateContentSize() {
mContentSize = 0;
if (mContents.size()) {
const auto padding = this->getValue("Padding");
mContentSize = mContents.last()->mArea.y - mContents.first()->mArea.y;
mContentSize += mContents.last()->mArea.w;
mContentSize += 2 * padding;
mContentSize += 2 * mPadding;
}
}
void setOffset(const halnf offset) {
if (!mContents.size()) return;
const auto padding = this->getValue("Padding");
auto newOffset = offset - mContents.first()->mArea.y + padding;
auto newOffset = offset - mContents.first()->mArea.y + mPadding;
for (auto widget : mContents) {
widget->mArea.y += newOffset;
}
@ -219,7 +226,11 @@ namespace tp {
public:
halnf mContentSize = 0;
Buffer<Widget<Events, Canvas>*> mContents;
ScrollBarWidget<Events, Canvas> mScroller;
RGBA mDefaultColor;
halnf mPadding = 0;
};
}