Skip to content

Commit

Permalink
Update SeqTimeRuler cursor change
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Mar 23, 2024
1 parent 13aeadd commit b68f4d6
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 33 deletions.
5 changes: 5 additions & 0 deletions src/audioCore/AudioCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ void AudioCore::record(bool start) {
}

void AudioCore::setPositon(double pos) {
/** Limit Pos */
pos = std::max(0.0, pos);
pos = std::min(this->mainAudioGraph->getTailLengthSeconds(), pos);

/** Set Time */
this->playStartTime = pos;
this->mainAudioGraph->closeAllNote();
PlayPosition::getInstance()->setPositionInSeconds(pos);
Expand Down
4 changes: 4 additions & 0 deletions src/audioCore/quickAPI/QuickSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,8 @@ namespace quickAPI {
void clearEffectMIDICCListener(PluginHolder pointer) {
clearPluginMIDICCListener(pointer);
}

void setPlayPosition(double timeSec) {
AudioCore::getInstance()->setPositon(timeSec);
}
}
2 changes: 2 additions & 0 deletions src/audioCore/quickAPI/QuickSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ namespace quickAPI {
void setEffectMIDICCListener(PluginHolder pointer, const MIDICCListener& listener);
void clearInstrMIDICCListener(PluginHolder pointer);
void clearEffectMIDICCListener(PluginHolder pointer);

void setPlayPosition(double timeSec);
}
79 changes: 52 additions & 27 deletions src/ui/component/ScrollerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,50 @@ void ScrollerBase::mouseMove(const juce::MouseEvent& event) {
this->updateState(event.getPosition(), false);
}

void ScrollerBase::scroll(double delta) {
/** Get Total Size */
double totalSize = this->getActualTotalSize();

/** Get Start Pos */
auto [startPos, endPos] = this->getThumb();
double startPer = startPos / (double)this->getTrackLength();
double endPer = endPos / (double)this->getTrackLength();

/** Set Pos */
double trueStartPos = startPer * totalSize;
this->setPos(trueStartPos + delta);
}

void ScrollerBase::scale(double centerPer, double thumbPer, double delta) {
/** Get Total Size */
double totalSize = this->getActualTotalSize();

/** Get Start Pos */
auto [startPos, endPos] = this->getThumb();
double startPer = startPos / (double)this->getTrackLength();
double endPer = endPos / (double)this->getTrackLength();

/** Get New Per */
double deltaPer = delta / totalSize;
double deltaL = deltaPer * thumbPer;
double deltaR = deltaPer - deltaL;
double newStartPer = startPer + deltaL;
double newEndPer = endPer - deltaR;
newEndPer = std::max(newStartPer, newEndPer);

/** Get Item Size */
double itemSize = this->viewSize / ((newEndPer - newStartPer) * this->itemNum);
itemSize = this->limitItemSize(itemSize);

/** Get Start Pos */
double newPer = (this->viewSize / itemSize) / this->itemNum;
newStartPer = centerPer - newPer * thumbPer;

/** Set Pos */
this->setItemSize(itemSize);
this->setPos(newStartPer * this->getActualTotalSize());
}

void ScrollerBase::mouseDrag(const juce::MouseEvent& event) {
if (event.mods.isLeftButtonDown()) {
auto pos = event.getPosition();
Expand Down Expand Up @@ -254,16 +298,15 @@ void ScrollerBase::mouseWheelMove(const juce::MouseEvent& event,
}

/** Get Wheel Delta */
double totalSize = this->getActualTotalSize();
double delta = (1.0 + ((this->itemSize - this->itemMinSize) / (this->itemMaxSize - this->itemMinSize)))
* wheel.deltaY * (wheel.isReversed ? 1 : -1) * 100.0;

/** Get Thumb Per */
auto [startPos, endPos] = this->getThumb();
double startPer = startPos / (double)this->getTrackLength();
double endPer = endPos / (double)this->getTrackLength();

if (event.mods == juce::ModifierKeys::altModifier) {
/** Get Thumb Per */
auto [startPos, endPos] = this->getThumb();
double startPer = startPos / (double)this->getTrackLength();
double endPer = endPos / (double)this->getTrackLength();

/** Get Scale Center */
auto pos = event.getPosition();
auto caredPos = this->getCaredPos(pos);
Expand All @@ -278,30 +321,12 @@ void ScrollerBase::mouseWheelMove(const juce::MouseEvent& event,
centerPer = endPer;
}

/** Get New Per */
double deltaPer = delta / totalSize;
double deltaL = deltaPer * thumbPer;
double deltaR = deltaPer - deltaL;
double newStartPer = startPer + deltaL;
double newEndPer = endPer - deltaR;
newEndPer = std::max(newStartPer, newEndPer);

/** Get Item Size */
double itemSize = this->viewSize / ((newEndPer - newStartPer) * this->itemNum);
itemSize = this->limitItemSize(itemSize);

/** Get Start Pos */
double newPer = (this->viewSize / itemSize) / this->itemNum;
newStartPer = centerPer - newPer * thumbPer;

/** Set Pos */
this->setItemSize(itemSize);
this->setPos(newStartPer * this->getActualTotalSize());
/** Scale */
this->scale(centerPer, thumbPer, delta);
}
else {
/** Set Pos */
double trueStartPos = startPer * totalSize;
this->setPos(trueStartPos + delta);
this->scroll(delta);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/ui/component/ScrollerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class ScrollerBase : public juce::Component {

double getActualTotalSize() const;

void scroll(double delta);
void scale(double centerPer, double thumbPer, double delta);

void mouseMove(const juce::MouseEvent& event);
void mouseDrag(const juce::MouseEvent& event);
void mouseDown(const juce::MouseEvent& event);
Expand Down
61 changes: 57 additions & 4 deletions src/ui/component/SeqTimeRuler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include "../Utils.h"
#include "../../audioCore/AC_API.h"

SeqTimeRuler::SeqTimeRuler() {
SeqTimeRuler::SeqTimeRuler(
const ScrollFunc& scrollFunc,
const ScaleFunc& scaleFunc)
: scrollFunc(scrollFunc), scaleFunc(scaleFunc) {
/** Look And Feel */
this->setLookAndFeel(
LookAndFeelFactory::getInstance()->forTimeRuler());
Expand All @@ -13,10 +16,16 @@ SeqTimeRuler::SeqTimeRuler() {
juce::Image::ARGB, 1, 1, false);
}

void SeqTimeRuler::updateBlock(int /*track*/, int /*index*/) {
/** Get Total Length */
this->totalLengthSec = quickAPI::getTotalLength();
}

void SeqTimeRuler::updateHPos(double pos, double itemSize) {
/** Set Pos */
this->pos = pos;
this->itemSize = itemSize;
std::tie(this->secStart, this->secEnd) = this->getViewArea(pos, itemSize);

/** Update Line Temp */
std::tie(this->lineTemp, this->minInterval) = this->createRulerLine(pos, itemSize);
Expand Down Expand Up @@ -108,8 +117,9 @@ void SeqTimeRuler::updateLevelMeter() {

void SeqTimeRuler::resized() {
/** Update Line Temp */
std::tie(this->secStart, this->secEnd) = this->getViewArea(this->pos, this->itemSize);
std::tie(this->lineTemp, this->minInterval) = this->createRulerLine(pos, itemSize);

/** Update Ruler Temp */
int width = this->getWidth(), height = this->getHeight();
width = std::max(width, 1);
Expand All @@ -134,8 +144,7 @@ void SeqTimeRuler::paint(juce::Graphics& g) {
g.drawImageAt(*(this->rulerTemp.get()), 0, 0);

/** Cursor */
auto [secStart, secEnd] = this->getViewArea(this->pos, this->itemSize);
float cursorPosX = ((this->playPosSec - secStart) / (secEnd - secStart)) * this->getWidth();
float cursorPosX = ((this->playPosSec - this->secStart) / (this->secEnd - this->secStart)) * this->getWidth();
juce::Rectangle<float> cursorRect(
cursorPosX - cursorThickness / 2, 0,
cursorThickness, this->getHeight());
Expand All @@ -144,6 +153,50 @@ void SeqTimeRuler::paint(juce::Graphics& g) {
g.fillRect(cursorRect);
}

void SeqTimeRuler::mouseDown(const juce::MouseEvent& event) {
/** Play Position Changed */
if (event.mods.isLeftButtonDown()) {
double per = event.position.getX() / (double)this->getWidth();
double timeSec = this->secStart + (this->secEnd - this->secStart) * per;

quickAPI::setPlayPosition(timeSec);
}
}

void SeqTimeRuler::mouseDrag(const juce::MouseEvent& event) {
/** Auto Scroll */
float xPos = event.position.getX();
double delta = 0;
if (xPos > this->getWidth()) {
delta = xPos - this->getWidth();
}
else if (xPos < 0) {
delta = xPos;
}

if (delta != 0) {
this->scrollFunc(delta / 4);
}

/** Play Position Changed */
if (event.mods.isLeftButtonDown()) {
double per = xPos / (double)this->getWidth();
double timeSec = this->secStart + (this->secEnd - this->secStart) * per;

quickAPI::setPlayPosition(timeSec);
}
}

void SeqTimeRuler::mouseUp(const juce::MouseEvent& event) {
/** Play Position Changed */
/*if (event.mods.isLeftButtonDown()) {
double per = event.position.getX() / (double)this->getWidth();
double timeSec = this->secStart + (this->secEnd - this->secStart) * per;
quickAPI::setPlayPosition(timeSec);
}*/
}

std::tuple<double, double> SeqTimeRuler::getViewArea(
double pos, double itemSize) const {
double secStart = pos / itemSize;
Expand Down
15 changes: 14 additions & 1 deletion src/ui/component/SeqTimeRuler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,39 @@ class SeqTimeRuler final
: public juce::Component,
public LevelMeterHub::Target {
public:
SeqTimeRuler();
using ScrollFunc = std::function<void(double)>;
using ScaleFunc = std::function<void(double, double, double)>;
SeqTimeRuler(const ScrollFunc& scrollFunc,
const ScaleFunc& scaleFunc);

void updateBlock(int track, int index);
void updateHPos(double pos, double itemSize);
void updateRulerTemp();
void updateLevelMeter() override;

void resized() override;
void paint(juce::Graphics& g) override;

void mouseDown(const juce::MouseEvent& event) override;
void mouseDrag(const juce::MouseEvent& event) override;
void mouseUp(const juce::MouseEvent& event) override;

/** Place, IsBar, barId */
using LineItem = std::tuple<double, bool, int>;
std::tuple<double, double> getViewArea(double pos, double itemSize) const;

private:
const ScrollFunc scrollFunc;
const ScaleFunc scaleFunc;

double pos = 0, itemSize = 0;
double secStart = 0, secEnd = 0;
juce::Array<LineItem> lineTemp;
double minInterval = 0;
std::unique_ptr<juce::Image> rulerTemp = nullptr;

double playPosSec = 0;
double totalLengthSec = 0;

/** Line List, Min Interval */
const std::tuple<juce::Array<LineItem>, double> createRulerLine(double pos, double itemSize) const;
Expand Down
17 changes: 16 additions & 1 deletion src/ui/component/SeqView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,19 @@ SeqView::SeqView()
this->addAndMakeVisible(this->vScroller.get());

/** Time Ruler */
this->ruler = std::make_unique<SeqTimeRuler>();
this->ruler = std::make_unique<SeqTimeRuler>(
[comp = ScrollerBase::SafePointer(this->hScroller.get())]
(double delta) {
if (comp) {
comp->scroll(delta);
}
},
[comp = ScrollerBase::SafePointer(this->hScroller.get())]
(double centerPer, double thumbPer, double delta) {
if (comp) {
comp->scale(centerPer, thumbPer, delta);
}
});
this->addAndMakeVisible(this->ruler.get());

/** Update Callback */
Expand Down Expand Up @@ -226,6 +238,9 @@ void SeqView::update(int index) {
}

void SeqView::updateBlock(int track, int index) {
/** Update Time Ruler */
this->ruler->updateBlock(track, index);

/** Update Tracks */
this->trackList->updateBlock(track, index);

Expand Down

0 comments on commit b68f4d6

Please sign in to comment.