Skip to content

Commit

Permalink
Add scroller play pos cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Jul 25, 2024
1 parent e4807c7 commit adaa702
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/ui/component/base/Scroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ Scroller::Scroller(bool vertical,
const ItemSizeLimitFunc& itemSizeLimitCallback,
const UpdatePosFunc& updatePosCallback,
const PaintPreviewFunc& paintPreviewCallback,
const PaintItemPreviewFunc& paintItemPreviewCallback)
const PaintItemPreviewFunc& paintItemPreviewCallback,
const PlayPosFunc& playPosCallback)
: ScrollerBase(vertical),
viewSizeCallback(viewSizeCallback), itemNumCallback(itemNumCallback),
itemSizeLimitCallback(itemSizeLimitCallback), updatePosCallback(updatePosCallback),
paintPreviewCallback(paintPreviewCallback), paintItemPreviewCallback(paintItemPreviewCallback) {
paintPreviewCallback(paintPreviewCallback), paintItemPreviewCallback(paintItemPreviewCallback),
playPosCallback(playPosCallback) {
/** Look And Feel */
this->setLookAndFeel(
LookAndFeelFactory::getInstance()->forScroller());
Expand Down Expand Up @@ -47,3 +49,10 @@ void Scroller::paintItemPreview(juce::Graphics& g, int itemIndex,
g, itemIndex, width, height, vertical);
}
}

double Scroller::getPlayPos() {
if (this->playPosCallback) {
return this->playPosCallback();
}
return -1;
}
7 changes: 6 additions & 1 deletion src/ui/component/base/Scroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Scroller : public ScrollerBase {
using UpdatePosFunc = std::function<void(double, double)>;
using PaintPreviewFunc = std::function<void(juce::Graphics&, int, int, bool)>;
using PaintItemPreviewFunc = std::function<void(juce::Graphics&, int, int, int, bool)>;
using PlayPosFunc = std::function<double(void)>;

public:
Scroller() = delete;
Expand All @@ -20,7 +21,8 @@ class Scroller : public ScrollerBase {
const ItemSizeLimitFunc& itemSizeLimitCallback,
const UpdatePosFunc& updatePosCallback,
const PaintPreviewFunc& paintPreviewCallback = PaintPreviewFunc{},
const PaintItemPreviewFunc& paintItemPreviewCallback = PaintItemPreviewFunc{});
const PaintItemPreviewFunc& paintItemPreviewCallback = PaintItemPreviewFunc{},
const PlayPosFunc& playPosCallback = PlayPosFunc{});

protected:
double createViewSize() override;
Expand All @@ -33,13 +35,16 @@ class Scroller : public ScrollerBase {
void paintItemPreview(juce::Graphics& g, int itemIndex,
int width, int height, bool vertical) override;

double getPlayPos() override;

private:
const ViewSizeFunc viewSizeCallback;
const ItemNumFunc itemNumCallback;
const ItemSizeLimitFunc itemSizeLimitCallback;
const UpdatePosFunc updatePosCallback;
const PaintPreviewFunc paintPreviewCallback;
const PaintItemPreviewFunc paintItemPreviewCallback;
const PlayPosFunc playPosCallback;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Scroller)
};
40 changes: 40 additions & 0 deletions src/ui/component/base/ScrollerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ void ScrollerBase::paint(juce::Graphics& g) {
g.drawImageAt(*(this->backTemp.get()), 0, 0);
}

/** Play Pos */
if (this->showPos) {
/** Size */
auto screenSize = utils::getScreenSize(this);
int paddingWidth = screenSize.getWidth() * 0.0015;
int paddingHeight = screenSize.getHeight() * 0.0025;
float posWidth = screenSize.getWidth() * 0.001;
float posHeight = posWidth;

/** Colour */
auto& laf = this->getLookAndFeel();
juce::Colour posColor = laf.findColour(
juce::Label::ColourIds::outlineColourId);

/** Pos */
double pos = this->getPlayPos();
float posPer = pos / this->itemNum;
double trackLength = this->vertical ? this->getHeight() : this->getWidth();

juce::Rectangle<float> posLineRect(
this->vertical ? paddingWidth : posPer * trackLength - posWidth / 2,
this->vertical ? posPer * trackLength - posHeight / 2 : paddingHeight,
this->vertical ? this->getWidth() - paddingWidth * 2 : posWidth,
this->vertical ? posHeight : this->getHeight() - paddingHeight * 2);
g.setColour(posColor);
g.fillRect(posLineRect);
}

/** Front Temp */
if (this->frontTemp) {
g.drawImageAt(*(this->frontTemp.get()), 0, 0);
Expand Down Expand Up @@ -86,6 +114,18 @@ void ScrollerBase::setItemSize(double size) {
this->updatePos(this->viewPos, size);
}

void ScrollerBase::setShouldShowPlayPos(bool showPos) {
this->showPos = showPos;
this->repaint();
}

void ScrollerBase::updateLevelMeter() {
/** Repaint */
if (this->showPos) {
this->repaint();
}
}

double ScrollerBase::getViewPos() const {
return this->viewPos;
}
Expand Down
11 changes: 10 additions & 1 deletion src/ui/component/base/ScrollerBase.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <JuceHeader.h>
#include "../../misc/LevelMeterHub.h"

class ScrollerBase : public juce::Component {
class ScrollerBase : public juce::Component,
public LevelMeterHub::Target {
public:
ScrollerBase() = delete;
ScrollerBase(bool vertical);
Expand All @@ -14,6 +16,9 @@ class ScrollerBase : public juce::Component {
void update();
void setPos(double pos);
void setItemSize(double size);
void setShouldShowPlayPos(bool showPos);

void updateLevelMeter() override;

double getViewPos() const;
double getViewSize() const;
Expand Down Expand Up @@ -49,13 +54,17 @@ class ScrollerBase : public juce::Component {
virtual void paintItemPreview(juce::Graphics& g, int itemIndex,
int width, int height, bool vertical) {};

virtual double getPlayPos() { return -1; };

private:
const bool vertical;

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

bool showPos = false;

std::unique_ptr<juce::Image> backTemp = nullptr;
std::unique_ptr<juce::Image> frontTemp = nullptr;

Expand Down
8 changes: 7 additions & 1 deletion src/ui/component/sequencer/SeqView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ SeqView::SeqView()
[this](double pos, double itemSize) { this->updateHPos(pos, itemSize); },
[this](juce::Graphics& g, int width, int height, bool vertical) {
this->paintBlockPreview(g, width, height, vertical); },
Scroller::PaintItemPreviewFunc{});
Scroller::PaintItemPreviewFunc{},
[this] { return this->getPlayPos(); });
this->hScroller->setShouldShowPlayPos(true);
this->addAndMakeVisible(this->hScroller.get());

this->vScroller = std::make_unique<Scroller>(true,
Expand Down Expand Up @@ -871,6 +873,10 @@ std::tuple<double, double> SeqView::getTimeWidthLimit() const {
return { screenSize.getWidth() * 0.01, screenSize.getWidth() * 0.5 };
}

double SeqView::getPlayPos() const {
return quickAPI::getTimeInSecond();
}

void SeqView::updateHPos(double pos, double itemSize) {
/** Set Pos */
this->pos = pos;
Expand Down
1 change: 1 addition & 0 deletions src/ui/component/sequencer/SeqView.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class SeqView final
int getViewWidth() const;
double getTimeLength() const;
std::tuple<double, double> getTimeWidthLimit() const;
double getPlayPos() const;

void updateHPos(double pos, double itemSize);
void paintBlockPreview(juce::Graphics& g,
Expand Down
4 changes: 4 additions & 0 deletions src/ui/lookAndFeel/base/ScrollerLookAndFeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ ScrollerLookAndFeel::ScrollerLookAndFeel()
ColorMap::getInstance()->get("ThemeColorB4").withAlpha(0.7f));
this->setColour(juce::ScrollBar::ColourIds::trackColourId,
ColorMap::getInstance()->get("ThemeColorB3"));/** Outline */

/** Play Pos */
this->setColour(juce::Label::ColourIds::outlineColourId,
ColorMap::getInstance()->get("ThemeColorA2"));
}

0 comments on commit adaa702

Please sign in to comment.