Skip to content

Commit

Permalink
Update seq view hand drag
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed May 28, 2024
1 parent ad0ecce commit 05c2b45
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 29 deletions.
35 changes: 19 additions & 16 deletions src/ui/component/sequencer/SeqTimeRuler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ SeqTimeRuler::SeqTimeRuler(
const ScaleFunc& scaleFunc,
const WheelFunc& wheelFunc,
const WheelAltFunc& wheelAltFunc,
const SetPosFunc& setPosFunc)
const DragStartFunc& dragStartFunc,
const DragProcessFunc& dragProcessFunc,
const DragEndFunc& dragEndFunc)
: scrollFunc(scrollFunc), scaleFunc(scaleFunc),
wheelFunc(wheelFunc), wheelAltFunc(wheelAltFunc),
setPosFunc(setPosFunc) {
dragStartFunc(dragStartFunc), dragProcessFunc(dragProcessFunc), dragEndFunc(dragEndFunc) {
/** Look And Feel */
this->setLookAndFeel(
LookAndFeelFactory::getInstance()->forTimeRuler());
Expand Down Expand Up @@ -314,7 +316,7 @@ void SeqTimeRuler::mouseDown(const juce::MouseEvent& event) {
case Tools::Type::Hand:
/** Move View Area */
this->viewMoving = true;
this->moveStartPos = this->pos;
this->dragStartFunc();
break;
case Tools::Type::Pencil: {
/** Add Label */
Expand Down Expand Up @@ -384,16 +386,18 @@ void SeqTimeRuler::mouseDown(const juce::MouseEvent& event) {
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);
if (!this->viewMoving) {
double delta = 0;
if (xPos > this->getWidth()) {
delta = xPos - this->getWidth();
}
else if (xPos < 0) {
delta = xPos;
}

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

/** Size */
Expand All @@ -413,7 +417,7 @@ void SeqTimeRuler::mouseDrag(const juce::MouseEvent& event) {
/** Move View */
else if (this->viewMoving) {
int distance = event.getDistanceFromDragStartX();
this->setPosFunc(this->moveStartPos - distance);
this->dragProcessFunc(distance, 0, true, false);
}
}
}
Expand Down Expand Up @@ -476,8 +480,7 @@ void SeqTimeRuler::mouseUp(const juce::MouseEvent& event) {

/** Move View */
else if (this->viewMoving) {
this->viewMoving = false;
this->moveStartPos = 0;
this->dragEndFunc();
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/ui/component/sequencer/SeqTimeRuler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ class SeqTimeRuler final
using ScaleFunc = std::function<void(double, double, double)>;
using WheelFunc = std::function<void(float, bool)>;
using WheelAltFunc = std::function<void(double, double, float, bool)>;
using SetPosFunc = std::function<void(double)>;
using DragStartFunc = std::function<void(void)>;
using DragProcessFunc = std::function<void(int, int, bool, bool)>;
using DragEndFunc = std::function<void(void)>;
SeqTimeRuler(const ScrollFunc& scrollFunc,
const ScaleFunc& scaleFunc,
const WheelFunc& wheelFunc,
const WheelAltFunc& wheelAltFunc,
const SetPosFunc& setPosFunc);
const DragStartFunc& dragStartFunc,
const DragProcessFunc& dragProcessFunc,
const DragEndFunc& dragEndFunc);

void updateTempoLabel();
void updateBlock(int track, int index);
Expand Down Expand Up @@ -45,7 +49,9 @@ class SeqTimeRuler final
const ScaleFunc scaleFunc;
const WheelFunc wheelFunc;
const WheelAltFunc wheelAltFunc;
const SetPosFunc setPosFunc;
const DragStartFunc dragStartFunc;
const DragProcessFunc dragProcessFunc;
const DragEndFunc dragEndFunc;

double pos = 0, itemSize = 0;
double secStart = 0, secEnd = 0;
Expand All @@ -67,7 +73,6 @@ class SeqTimeRuler final
float labelDragPos = 0;

bool viewMoving = false;
double moveStartPos = 0;

/** Line List, Min Interval */
const std::tuple<juce::Array<LineItem>, double> createRulerLine(double pos, double itemSize) const;
Expand Down
8 changes: 6 additions & 2 deletions src/ui/component/sequencer/SeqTrackComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include "../../../audioCore/AC_API.h"
#include <IconManager.h>

SeqTrackComponent::SeqTrackComponent() {
SeqTrackComponent::SeqTrackComponent(
const DragStartFunc& dragStartFunc,
const DragProcessFunc& dragProcessFunc,
const DragEndFunc& dragEndFunc) {
/** Look And Feel */
this->setLookAndFeel(
LookAndFeelFactory::getInstance()->forSeqTrack());
Expand Down Expand Up @@ -103,7 +106,8 @@ SeqTrackComponent::SeqTrackComponent() {
this->addChildComponent(this->levelMeter.get());

/** Content */
this->content = std::make_unique<SeqTrackContentViewer>();
this->content = std::make_unique<SeqTrackContentViewer>(
dragStartFunc, dragProcessFunc, dragEndFunc);
this->addAndMakeVisible(this->content.get());
}

Expand Down
8 changes: 7 additions & 1 deletion src/ui/component/sequencer/SeqTrackComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ class SeqTrackComponent final
public juce::DragAndDropTarget,
public juce::SettableTooltipClient {
public:
SeqTrackComponent();
using DragStartFunc = std::function<void(void)>;
using DragProcessFunc = std::function<void(int, int, bool, bool)>;
using DragEndFunc = std::function<void(void)>;
SeqTrackComponent(
const DragStartFunc& dragStartFunc,
const DragProcessFunc& dragProcessFunc,
const DragEndFunc& dragEndFunc);

void update(int index);
void updateBlock(int blockIndex);
Expand Down
46 changes: 45 additions & 1 deletion src/ui/component/sequencer/SeqTrackContentViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ void DataImageUpdateTimer::timerCallback() {
}
}

SeqTrackContentViewer::SeqTrackContentViewer() {
SeqTrackContentViewer::SeqTrackContentViewer(
const DragStartFunc& dragStartFunc,
const DragProcessFunc& dragProcessFunc,
const DragEndFunc& dragEndFunc)
: dragStartFunc(dragStartFunc), dragProcessFunc(dragProcessFunc),
dragEndFunc(dragEndFunc) {
/** Look And Feel */
this->setLookAndFeel(
LookAndFeelFactory::getInstance()->forSeqBlock());
Expand Down Expand Up @@ -402,6 +407,45 @@ void SeqTrackContentViewer::mouseMove(const juce::MouseEvent& event) {
this->setMouseCursor(juce::MouseCursor::NormalCursor);
}

void SeqTrackContentViewer::mouseDrag(const juce::MouseEvent& event) {
if (event.mods.isLeftButtonDown()) {
/** Move View */
if (this->viewMoving) {
int distanceX = event.getDistanceFromDragStartX();
int distanceY = event.getDistanceFromDragStartY();
this->dragProcessFunc(distanceX, distanceY, true, true);
}
}
}

void SeqTrackContentViewer::mouseDown(const juce::MouseEvent& event) {
/** Get Tool Type */
auto tool = Tools::getInstance()->getType();

if (event.mods.isLeftButtonDown()) {
/** Check Tool Type */
switch (tool) {
case Tools::Type::Hand:
/** Move View Area */
this->viewMoving = true;
this->dragStartFunc();
break;
}
}
else if (event.mods.isRightButtonDown()) {
/** TODO */
}
}

void SeqTrackContentViewer::mouseUp(const juce::MouseEvent& event) {
if (event.mods.isLeftButtonDown()) {
/** Move View */
if (this->viewMoving) {
this->dragEndFunc();
}
}
}

void SeqTrackContentViewer::updateBlockInternal(int blockIndex) {
if (auto temp = this->blockTemp[blockIndex]) {
std::tie(temp->startTime, temp->endTime, temp->offset)
Expand Down
17 changes: 16 additions & 1 deletion src/ui/component/sequencer/SeqTrackContentViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

class SeqTrackContentViewer final : public juce::Component {
public:
SeqTrackContentViewer();
using DragStartFunc = std::function<void(void)>;
using DragProcessFunc = std::function<void(int, int, bool, bool)>;
using DragEndFunc = std::function<void(void)>;
SeqTrackContentViewer(
const DragStartFunc& dragStartFunc,
const DragProcessFunc& dragProcessFunc,
const DragEndFunc& dragEndFunc);

void setCompressed(bool isCompressed);

Expand All @@ -18,8 +24,15 @@ class SeqTrackContentViewer final : public juce::Component {
void paint(juce::Graphics& g) override;

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

private:
const DragStartFunc dragStartFunc;
const DragProcessFunc dragProcessFunc;
const DragEndFunc dragEndFunc;

bool compressed = false;
int index = -1;
double pos = 0, itemSize = 0;
Expand All @@ -42,6 +55,8 @@ class SeqTrackContentViewer final : public juce::Component {

std::unique_ptr<juce::Timer> blockImageUpdateTimer = nullptr;

bool viewMoving = false;

void updateBlockInternal(int blockIndex);
void setAudioPointTempInternal(const juce::Array<juce::MemoryBlock>& temp);
void updateMIDINoteTempInternal();
Expand Down
59 changes: 55 additions & 4 deletions src/ui/component/sequencer/SeqView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,21 @@ SeqView::SeqView()
comp->mouseWheelOutsideWithAlt(centerNum, thumbPer, deltaY, reversed);
}
},
[comp = ScrollerBase::SafePointer(this->hScroller.get())]
(double pos) {
[comp = ScrollerBase::SafePointer(this)] {
if (comp) {
comp->processAreaDragStart();
}
},
[comp = ScrollerBase::SafePointer(this)]
(int distanceX, int distanceY, bool moveX, bool moveY) {
if (comp) {
comp->setPos(pos);
comp->processAreaDragTo(
distanceX, distanceY, moveX, moveY);
}
},
[comp = ScrollerBase::SafePointer(this)] {
if (comp) {
comp->processAreaDragEnd();
}
});
this->addAndMakeVisible(this->ruler.get());
Expand Down Expand Up @@ -461,7 +472,24 @@ void SeqView::update(int index) {
}
else {
for (int i = currentSize; i < newSize; i++) {
auto track = std::make_unique<SeqTrackComponent>();
auto track = std::make_unique<SeqTrackComponent>(
[comp = ScrollerBase::SafePointer(this)] {
if (comp) {
comp->processAreaDragStart();
}
},
[comp = ScrollerBase::SafePointer(this)]
(int distanceX, int distanceY, bool moveX, bool moveY) {
if (comp) {
comp->processAreaDragTo(
distanceX, distanceY, moveX, moveY);
}
},
[comp = ScrollerBase::SafePointer(this)] {
if (comp) {
comp->processAreaDragEnd();
}
});
this->trackList->add(std::move(track));
}
}
Expand Down Expand Up @@ -744,3 +772,26 @@ void SeqView::updateGridTemp() {
g.fillRect(lineRect);
}
}

void SeqView::processAreaDragStart() {
this->viewMoving = true;
this->moveStartPosX = this->hScroller->getViewPos();
this->moveStartPosY = this->vScroller->getViewPos();
}

void SeqView::processAreaDragTo(
int distanceX, int distanceY, bool moveX, bool moveY) {
if (this->viewMoving) {
if (moveX) {
this->hScroller->setPos(this->moveStartPosX - distanceX);
}
if (moveY) {
this->vScroller->setPos(this->moveStartPosY - distanceY);
}
}
}

void SeqView::processAreaDragEnd() {
this->viewMoving = false;
this->moveStartPosX = this->moveStartPosY = 0;
}
7 changes: 7 additions & 0 deletions src/ui/component/sequencer/SeqView.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class SeqView final

juce::String emptyNoticeStr;

bool viewMoving = false;
double moveStartPosX = 0, moveStartPosY = 0;

int getViewWidth() const;
double getTimeLength() const;
std::tuple<double, double> getTimeWidthLimit() const;
Expand All @@ -110,5 +113,9 @@ class SeqView final

void updateGridTemp();

void processAreaDragStart();
void processAreaDragTo(int distanceX, int distanceY, bool moveX = true, bool moveY = true);
void processAreaDragEnd();

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SeqView)
};

0 comments on commit 05c2b45

Please sign in to comment.