Skip to content

Commit

Permalink
Update scroller item size
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Mar 9, 2024
1 parent 1d92d2c commit 3e34195
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 94 deletions.
12 changes: 6 additions & 6 deletions src/ui/component/MixerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ MixerView::MixerView()

/** Scroller */
this->hScroller = std::make_unique<Scroller>(false,
[this] { return this->getViewWidth(); },
[this] { return (double)(this->getViewWidth()); },
[this] { return (double)(this->getTrackNum()); },
[this] { return this->getTrackWidthLimit(); },
[this](int pos, int itemSize) { this->updatePos(pos, itemSize); },
[this](double pos, double itemSize) { this->updatePos(pos, itemSize); },
Scroller::PaintPreviewFunc{},
[this](juce::Graphics& g, int itemIndex,
int width, int height, bool vertical) {
Expand Down Expand Up @@ -194,13 +194,13 @@ int MixerView::getTrackNum() const {
return this->trackList.size();
}

std::tuple<int, int> MixerView::getTrackWidthLimit() const {
std::tuple<double, double> MixerView::getTrackWidthLimit() const {
auto screenSize = utils::getScreenSize(this);
return { (int)(screenSize.getWidth() * 0.04),
(int)(screenSize.getWidth() * 0.05) };
return { screenSize.getWidth() * 0.04,
screenSize.getWidth() * 0.05 };
}

void MixerView::updatePos(int pos, int itemSize) {
void MixerView::updatePos(double pos, double itemSize) {
int height = this->getHeight() - this->hScroller->getHeight();
for (int i = 0; i < this->trackList.size(); i++) {
juce::Rectangle<int> trackRect(
Expand Down
4 changes: 2 additions & 2 deletions src/ui/component/MixerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class MixerView final

int getViewWidth() const;
int getTrackNum() const;
std::tuple<int, int> getTrackWidthLimit() const;
std::tuple<double, double> getTrackWidthLimit() const;

void updatePos(int pos, int itemSize);
void updatePos(double pos, double itemSize);

void paintTrackPreview(juce::Graphics& g, int itemIndex,
int width, int height, bool vertical);
Expand Down
6 changes: 3 additions & 3 deletions src/ui/component/Scroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ Scroller::Scroller(bool vertical,
LookAndFeelFactory::getInstance()->forScroller());
}

int Scroller::createViewSize() {
double Scroller::createViewSize() {
return this->viewSizeCallback();
}

double Scroller::createItemNum() {
return this->itemNumCallback();
}

std::tuple<int, int> Scroller::createItemSizeLimit() {
std::tuple<double, double> Scroller::createItemSizeLimit() {
return this->itemSizeLimitCallback();
}

void Scroller::updatePos(int pos, int itemSize) {
void Scroller::updatePos(double pos, double itemSize) {
this->updatePosCallback(pos, itemSize);
}

Expand Down
12 changes: 6 additions & 6 deletions src/ui/component/Scroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

class Scroller : public ScrollerBase {
public:
using ViewSizeFunc = std::function<int(void)>;
using ViewSizeFunc = std::function<double(void)>;
using ItemNumFunc = std::function<double(void)>;
using ItemSizeLimitFunc = std::function<std::tuple<int, int>(void)>;
using UpdatePosFunc = std::function<void(int, int)>;
using ItemSizeLimitFunc = std::function<std::tuple<double, double>(void)>;
using UpdatePosFunc = std::function<void(double, double)>;
using PaintPreviewFunc = std::function<void(juce::Graphics&, bool)>;
using PaintItemPreviewFunc = std::function<void(juce::Graphics&, int, int, int, bool)>;

Expand All @@ -23,11 +23,11 @@ class Scroller : public ScrollerBase {
const PaintItemPreviewFunc& paintItemPreviewCallback = PaintItemPreviewFunc{});

protected:
int createViewSize() override;
double createViewSize() override;
double createItemNum() override;
std::tuple<int, int> createItemSizeLimit() override;
std::tuple<double, double> createItemSizeLimit() override;

void updatePos(int pos, int itemSize) override;
void updatePos(double pos, double itemSize) override;

void paintPreview(juce::Graphics& g, bool vertical) override;
void paintItemPreview(juce::Graphics& g, int itemIndex,
Expand Down
82 changes: 41 additions & 41 deletions src/ui/component/ScrollerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void ScrollerBase::update() {
this->setPos(this->viewPos);
}

void ScrollerBase::setPos(int pos) {
void ScrollerBase::setPos(double pos) {
/** Limit Pos */
pos = this->limitPos(pos);

Expand All @@ -104,7 +104,7 @@ void ScrollerBase::setPos(int pos) {
this->updatePos(pos, this->itemSize);
}

void ScrollerBase::setItemSize(int size) {
void ScrollerBase::setItemSize(double size) {
/** Limit Size */
size = this->limitItemSize(size);

Expand All @@ -118,31 +118,31 @@ void ScrollerBase::setItemSize(int size) {
this->updatePos(this->viewPos, size);
}

int ScrollerBase::getViewPos() const {
double ScrollerBase::getViewPos() const {
return this->viewPos;
}

int ScrollerBase::getViewSize() const {
double ScrollerBase::getViewSize() const {
return this->viewSize;
}

int ScrollerBase::getItemSize() const {
double ScrollerBase::getItemSize() const {
return this->itemSize;
}

double ScrollerBase::getItemNum() const {
return this->itemNum;
}

int ScrollerBase::getItemMinSize() const {
double ScrollerBase::getItemMinSize() const {
return this->itemMinSize;
}

int ScrollerBase::getItemMaxSize() const {
double ScrollerBase::getItemMaxSize() const {
return this->itemMaxSize;
}

int ScrollerBase::getActualTotalSize() const {
double ScrollerBase::getActualTotalSize() const {
return this->itemSize * this->itemNum;
}

Expand All @@ -165,9 +165,9 @@ void ScrollerBase::mouseDrag(const juce::MouseEvent& event) {
break;
case State::PressedThumb: {
auto [startPos, endPos] = this->getThumb();
int halfThumbSize = (endPos - startPos) * this->thumbPressedPer;
int newThumbStartPos = caredPos - halfThumbSize;
int newPos = (newThumbStartPos / (double)this->getTrackLength()) * this->getActualTotalSize();
double halfThumbSize = (endPos - startPos) * this->thumbPressedPer;
double newThumbStartPos = caredPos - halfThumbSize;
double newPos = (newThumbStartPos / (double)this->getTrackLength()) * this->getActualTotalSize();
this->setPos(newPos);
break;
}
Expand Down Expand Up @@ -199,9 +199,9 @@ void ScrollerBase::mouseDown(const juce::MouseEvent& event) {
}
else {
/** Press On Track */
int halfThumbSize = (endPos - startPos) / 2;
int newThumbStartPos = caredPos - halfThumbSize;
int newPos = (newThumbStartPos / (double)this->getTrackLength()) * this->getActualTotalSize();
double halfThumbSize = (endPos - startPos) / 2;
double newThumbStartPos = caredPos - halfThumbSize;
double newPos = (newThumbStartPos / (double)this->getTrackLength()) * this->getActualTotalSize();
this->setPos(newPos);
this->recordThumbPress(caredPos);
}
Expand All @@ -226,9 +226,9 @@ void ScrollerBase::mouseUp(const juce::MouseEvent& event) {
break;
case State::PressedThumb: {
auto [startPos, endPos] = this->getThumb();
int halfThumbSize = (endPos - startPos) * this->thumbPressedPer;
int newThumbStartPos = caredPos - halfThumbSize;
int newPos = (newThumbStartPos / (double)this->getTrackLength()) * this->getActualTotalSize();
double halfThumbSize = (endPos - startPos) * this->thumbPressedPer;
double newThumbStartPos = caredPos - halfThumbSize;
double newPos = (newThumbStartPos / (double)this->getTrackLength()) * this->getActualTotalSize();
this->setPos(newPos);
break;
}
Expand All @@ -251,7 +251,7 @@ void ScrollerBase::mouseWheelMove(const juce::MouseEvent& event,
return;
}

int totalSize = this->getActualTotalSize();
double totalSize = this->getActualTotalSize();
double delta = totalSize * wheel.deltaY
* (wheel.isReversed ? 1 : -1) * 0.15;

Expand Down Expand Up @@ -294,26 +294,26 @@ void ScrollerBase::mouseWheelMove(const juce::MouseEvent& event,
}
}

void ScrollerBase::recordThumbPress(int pos) {
void ScrollerBase::recordThumbPress(double pos) {
auto [startPos, endPos] = this->getThumb();
this->thumbPressedPer = (pos - startPos) / (double)(endPos - startPos);
}

void ScrollerBase::setStart(int start) {
void ScrollerBase::setStart(double start) {
/** Get Size */
double startPer = start / (double)this->getTrackLength();
double endPer = (this->viewPos + this->viewSize) / (double)this->getActualTotalSize();
if (startPer < 0.0) { startPer = 0.0; }
if (startPer > (endPer - DELTA)) { startPer = endPer - DELTA; }
double viewNum = (endPer - startPer) * this->itemNum;
int itemSize = this->limitItemSize(this->viewSize / viewNum);
double itemSize = this->limitItemSize(this->viewSize / viewNum);

/** Set Size */
this->itemSize = itemSize;

/** Get Pos */
int endPos = endPer * this->getActualTotalSize();
int pos = endPos - this->viewSize;
double endPos = endPer * this->getActualTotalSize();
double pos = endPos - this->viewSize;
if (pos < 0) { pos = 0; }

/** Set Pos */
Expand All @@ -326,20 +326,20 @@ void ScrollerBase::setStart(int start) {
this->updatePos(pos, itemSize);
}

void ScrollerBase::setEnd(int end) {
void ScrollerBase::setEnd(double end) {
/** Get Size */
double startPer = this->viewPos / (double)this->getActualTotalSize();
double endPer = end / (double)this->getTrackLength();
if (endPer > 1.0) { endPer = 1.0; }
if (endPer < (startPer + DELTA)) { endPer = startPer + DELTA; }
double viewNum = (endPer - startPer) * this->itemNum;
int itemSize = this->limitItemSize(this->viewSize / viewNum);
double itemSize = this->limitItemSize(this->viewSize / viewNum);

/** Set Size */
this->itemSize = itemSize;

/** Get Pos */
int pos = startPer * this->getActualTotalSize();
double pos = startPer * this->getActualTotalSize();
if (pos < 0) { pos = 0; }

/** Set Pos */
Expand All @@ -352,20 +352,20 @@ void ScrollerBase::setEnd(int end) {
this->updatePos(pos, itemSize);
}

int ScrollerBase::limitPos(int pos) const {
double ScrollerBase::limitPos(double pos) const {
pos = std::min(pos, this->getActualTotalSize() - this->viewSize);
pos = std::max(pos, 0);
pos = std::max(pos, 0.0);
return pos;
}

int ScrollerBase::limitItemSize(int size) const {
double ScrollerBase::limitItemSize(double size) const {
size = std::min(size, this->itemMaxSize);
size = std::max(size, this->itemMinSize);
return size;
}

double ScrollerBase::limitItemNum(double num) const {
return std::max(num, std::ceil(this->viewSize / (double)this->itemMinSize));
return std::max(num, this->viewSize / (double)this->itemMinSize);
}

int ScrollerBase::getJudgeSize() const {
Expand All @@ -381,11 +381,11 @@ int ScrollerBase::getCaredPos(const juce::Point<int>& pos) const {
return this->vertical ? pos.getY() : pos.getX();
}

std::tuple<int, int> ScrollerBase::getThumb() const {
int actualTotalSize = this->getActualTotalSize();
int trackLength = this->vertical ? this->getHeight() : this->getWidth();
int startPos = (this->viewPos / (double)actualTotalSize) * trackLength;
int endPos = ((this->viewPos + this->viewSize) / (double)actualTotalSize) * trackLength;
std::tuple<double, double> ScrollerBase::getThumb() const {
double actualTotalSize = this->getActualTotalSize();
double trackLength = this->vertical ? this->getHeight() : this->getWidth();
double startPos = (this->viewPos / (double)actualTotalSize) * trackLength;
double endPos = ((this->viewPos + this->viewSize) / (double)actualTotalSize) * trackLength;
return { startPos, endPos };
}

Expand All @@ -397,15 +397,15 @@ void ScrollerBase::resetState() {

void ScrollerBase::updateState(const juce::Point<int>& pos, bool pressed) {
/** Get Size */
int position = this->getCaredPos(pos);
double position = this->getCaredPos(pos);

auto [startPos, endPos] = this->getThumb();

int judgeSize = this->getJudgeSize();
int startJudgeS = startPos - judgeSize / 2;
int startJudgeE = startPos + std::min(judgeSize / 2, (endPos - startPos) / 2);
int endJudgeS = endPos - std::min(judgeSize / 2, (endPos - startPos) / 2);
int endJudgeE = endPos + judgeSize / 2;
double judgeSize = this->getJudgeSize();
double startJudgeS = startPos - judgeSize / 2;
double startJudgeE = startPos + std::min(judgeSize / 2, (endPos - startPos) / 2);
double endJudgeS = endPos - std::min(judgeSize / 2, (endPos - startPos) / 2);
double endJudgeE = endPos + judgeSize / 2;

/** Change State */
if (pressed) {
Expand Down
41 changes: 20 additions & 21 deletions src/ui/component/ScrollerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ class ScrollerBase : public juce::Component {
void paint(juce::Graphics& g) override;

void update();
void setPos(int pos);
void setItemSize(int size);
void setPos(double pos);
void setItemSize(double size);

int getViewPos() const;
int getViewSize() const;
int getItemSize() const;
double getViewPos() const;
double getViewSize() const;
double getItemSize() const;
double getItemNum() const;
int getItemMinSize() const;
int getItemMaxSize() const;
double getItemMinSize() const;
double getItemMaxSize() const;

int getActualTotalSize() const;
double getActualTotalSize() const;

void mouseMove(const juce::MouseEvent& event);
void mouseDrag(const juce::MouseEvent& event);
Expand All @@ -32,11 +32,11 @@ class ScrollerBase : public juce::Component {
const juce::MouseWheelDetails& wheel);

protected:
virtual int createViewSize() = 0;
virtual double createViewSize() = 0;
virtual double createItemNum() = 0;
virtual std::tuple<int, int> createItemSizeLimit() = 0;
virtual std::tuple<double, double> createItemSizeLimit() = 0;

virtual void updatePos(int pos, int itemSize) = 0;
virtual void updatePos(double pos, double itemSize) = 0;

virtual void paintPreview(juce::Graphics& g, bool vertical) {};
virtual void paintItemPreview(juce::Graphics& g, int itemIndex,
Expand All @@ -45,10 +45,9 @@ class ScrollerBase : public juce::Component {
private:
const bool vertical;

int viewPos = 0, viewSize = 0;
int itemSize = 0;
double itemNum = 0;
int itemMinSize = 0, itemMaxSize = 0;
double viewPos = 0, viewSize = 0;
double itemSize = 0, itemNum = 0;
double itemMinSize = 0, itemMaxSize = 0;

enum class State {
Normal, HoverThumb, HoverStart, HoverEnd,
Expand All @@ -57,19 +56,19 @@ class ScrollerBase : public juce::Component {
State state = State::Normal;

double thumbPressedPer = -1;
void recordThumbPress(int pos);
void recordThumbPress(double pos);

void setStart(int start);
void setEnd(int end);
void setStart(double start);
void setEnd(double end);

int limitPos(int pos) const;
int limitItemSize(int size) const;
double limitPos(double pos) const;
double limitItemSize(double size) const;
double limitItemNum(double num) const;

int getJudgeSize() const;
int getTrackLength() const;
int getCaredPos(const juce::Point<int>& pos) const;
std::tuple<int, int> getThumb() const;
std::tuple<double, double> getThumb() const;

void resetState();
void updateState(const juce::Point<int>& pos, bool pressed);
Expand Down
Loading

0 comments on commit 3e34195

Please sign in to comment.