Skip to content

Commit

Permalink
Add control commands
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Jan 9, 2024
1 parent f220bb6 commit 8ee4619
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/translates/zh-CN/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ countries: cn

"Playing" = "播放中"
"Recording" = "录制中"
"Ready" = "就绪"
"Ready" = "就绪"

"Play/Pause" = "播放/暂停"
16 changes: 16 additions & 0 deletions app/translates/zh-CN/menu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ countries: cn
"Undo" = "撤销"
"Redo" = "重做"

"Play" = "播放"
"Stop" = "停止"
"Record" = "录制"
"Rewind" = "倒回"

"Create an empty project." = "建立一个空项目。"
"Open a project from disk." = "从本地磁盘打开项目。"
"Save current project to disk." = "保存当前项目到本地磁盘。"
Expand All @@ -33,6 +38,13 @@ countries: cn
"Undo the last action." = "撤销最后一个操作。"
"Redo the last undo action." = "重做最后一个撤销的操作。"

"Play or pause." = "播放或暂停。"
"Stop playing." = "停止播放。"
"Record the source." = "录制源。"
"Back to start position." = "回到开始位置。"



"Close Editor" = "关闭编辑器"

"Copy" = "复制"
Expand All @@ -45,6 +57,8 @@ countries: cn
"Load Layout" = "加载布局"
"Save Layout" = "保存布局"

"Follow" = "跟随"

"Startup Config" = "启动配置"
"Function Config" = "功能配置"
"Audio Config" = "音频配置"
Expand Down Expand Up @@ -86,6 +100,8 @@ countries: cn
"Show audio debugger component." = "显示音频调试器组件。"
"Show MIDI debugger component." = "显示MIDI调试器组件。"

"Follow playing position in editor." = "在编辑器中跟随播放位置。"

"Open startup config page." = "打开启动配置页面。"
"Open the function config page." = "打开功能配置页面。"
"Open the audio config page." = "打开音频配置界面。"
Expand Down
9 changes: 8 additions & 1 deletion src/audioCore/misc/PlayPosition.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "PlayPosition.h"
#include "PlayPosition.h"

#include <chrono>
#include "../uiCallback/UICallback.h"
#include "../Utils.h"

juce::Optional<juce::AudioPlayHead::PositionInfo> MovablePlayHead::getPosition() const {
Expand Down Expand Up @@ -30,6 +31,9 @@ void MovablePlayHead::transportPlay(bool shouldStartPlaying) {
if (!shouldStartPlaying) {
this->position.setIsRecording(false);
}

UICallbackAPI<bool>::invoke(
UICallbackType::PlayStateChanged, shouldStartPlaying);
}

void MovablePlayHead::transportRecord(bool shouldStartRecording) {
Expand All @@ -38,6 +42,9 @@ void MovablePlayHead::transportRecord(bool shouldStartRecording) {
if (shouldStartRecording) {
this->position.setIsPlaying(true);
}

UICallbackAPI<bool>::invoke(
UICallbackType::RecordStateChanged, shouldStartRecording);
}

void MovablePlayHead::transportRewind() {
Expand Down
2 changes: 2 additions & 0 deletions src/audioCore/uiCallback/UICallbackType.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

enum class UICallbackType : int {
ErrorAlert,
PlayStateChanged,
RecordStateChanged,

TypeMaxNum
};
12 changes: 12 additions & 0 deletions src/ui/component/ControllerComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ControllerComponent.h"
#include "../lookAndFeel/LookAndFeelFactory.h"
#include "../menuAndCommand/CommandManager.h"
#include "../menuAndCommand/CommandTypes.h"
#include "../Utils.h"
#include <IconManager.h>

Expand Down Expand Up @@ -48,13 +50,17 @@ ControllerComponent::ControllerComponent() {
this->pauseIcon.get(), nullptr, nullptr, nullptr);
this->playButton->setWantsKeyboardFocus(false);
this->playButton->setMouseCursor(juce::MouseCursor::PointingHandCursor);
this->playButton->setCommandToTrigger(
CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Play, true);
this->addAndMakeVisible(this->playButton.get());

this->stopButton = std::make_unique<juce::DrawableButton>(
TRANS("Stop"), juce::DrawableButton::ButtonStyle::ImageOnButtonBackgroundOriginalSize);
this->stopButton->setImages(this->stopIcon.get());
this->stopButton->setWantsKeyboardFocus(false);
this->stopButton->setMouseCursor(juce::MouseCursor::PointingHandCursor);
this->stopButton->setCommandToTrigger(
CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Stop, true);
this->addAndMakeVisible(this->stopButton.get());

this->recordButton = std::make_unique<juce::DrawableButton>(
Expand All @@ -64,13 +70,17 @@ ControllerComponent::ControllerComponent() {
this->recordOnIcon.get(), nullptr, nullptr, nullptr);
this->recordButton->setWantsKeyboardFocus(false);
this->recordButton->setMouseCursor(juce::MouseCursor::PointingHandCursor);
this->recordButton->setCommandToTrigger(
CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Record, true);
this->addAndMakeVisible(this->recordButton.get());

this->rewindButton = std::make_unique<juce::DrawableButton>(
TRANS("Rewind"), juce::DrawableButton::ButtonStyle::ImageOnButtonBackgroundOriginalSize);
this->rewindButton->setImages(this->rewindIcon.get());
this->rewindButton->setWantsKeyboardFocus(false);
this->rewindButton->setMouseCursor(juce::MouseCursor::PointingHandCursor);
this->rewindButton->setCommandToTrigger(
CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Rewind, true);
this->addAndMakeVisible(this->rewindButton.get());

this->followButton = std::make_unique<juce::DrawableButton>(
Expand All @@ -80,6 +90,8 @@ ControllerComponent::ControllerComponent() {
this->followOnIcon.get(), nullptr, nullptr, nullptr);
this->followButton->setWantsKeyboardFocus(false);
this->followButton->setMouseCursor(juce::MouseCursor::PointingHandCursor);
this->followButton->setCommandToTrigger(
CommandManager::getInstance(), (juce::CommandID)GUICommandType::Follow, true);
this->addAndMakeVisible(this->followButton.get());
}

Expand Down
7 changes: 6 additions & 1 deletion src/ui/dataModel/MainMenuModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ juce::PopupMenu MainMenuModel::createProject() const {
juce::PopupMenu MainMenuModel::createControl() const {
juce::PopupMenu menu;

/** TODO */
menu.addCommandItem(CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Play);
menu.addCommandItem(CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Stop);
menu.addCommandItem(CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Record);
menu.addCommandItem(CommandManager::getInstance(), (juce::CommandID)CoreCommandType::Rewind);
menu.addSeparator();
menu.addCommandItem(CommandManager::getInstance(), (juce::CommandID)GUICommandType::Follow);

return menu;
}
Expand Down
12 changes: 12 additions & 0 deletions src/ui/menuAndCommand/CommandManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
#include "CoreCommandTarget.h"
#include "GUICommandTarget.h"
#include "../Utils.h"
#include "../../audioCore/AC_API.h"

CommandManager::CommandManager() {
UICallbackAPI<bool>::set(UICallbackType::PlayStateChanged,
[this](bool) {
this->commandStatusChanged();
});
UICallbackAPI<bool>::set(UICallbackType::RecordStateChanged,
[this](bool) {
this->commandStatusChanged();
});
}

CommandManager::~CommandManager() {
this->stopListening();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/menuAndCommand/CommandManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CommandManager final
public juce::ChangeListener,
private juce::DeletedAtShutdown {
public:
CommandManager() = default;
CommandManager();
~CommandManager();

void init();
Expand Down
9 changes: 8 additions & 1 deletion src/ui/menuAndCommand/CommandTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ enum class CoreCommandType : int {
Render = 0x2200,

Undo = 0x2300,
Redo
Redo,

Play = 0x2400,
Stop,
Record,
Rewind
};

enum class GUICommandType : int {
Expand All @@ -38,6 +43,8 @@ enum class GUICommandType : int {
AudioDebugger,
MidiDebugger,

Follow = 0x3400,

StartupConfig = 0x3500,
FunctionConfig,
AudioConfig,
Expand Down
76 changes: 75 additions & 1 deletion src/ui/menuAndCommand/CoreCommandTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ void CoreCommandTarget::getAllCommands(
(juce::CommandID)(CoreCommandType::Render),

(juce::CommandID)(CoreCommandType::Undo),
(juce::CommandID)(CoreCommandType::Redo)
(juce::CommandID)(CoreCommandType::Redo),

(juce::CommandID)(CoreCommandType::Play),
(juce::CommandID)(CoreCommandType::Stop),
(juce::CommandID)(CoreCommandType::Record),
(juce::CommandID)(CoreCommandType::Rewind)
};
}

Expand Down Expand Up @@ -104,6 +109,26 @@ void CoreCommandTarget::getCommandInfo(
result.setActive(active);
break;
}

case CoreCommandType::Play:
result.setInfo(TRANS("Play"), TRANS("Play or pause."), TRANS("Control"), 0);
result.addDefaultKeypress(juce::KeyPress::spaceKey, juce::ModifierKeys::noModifiers);
result.setActive(true);
result.setTicked(this->checkForPlaying());
break;
case CoreCommandType::Stop:
result.setInfo(TRANS("Stop"), TRANS("Stop playing."), TRANS("Control"), 0);
result.setActive(this->checkForPlaying());
break;
case CoreCommandType::Record:
result.setInfo(TRANS("Record"), TRANS("Record the source."), TRANS("Control"), 0);
result.setActive(true);
result.setTicked(this->checkForRecording());
break;
case CoreCommandType::Rewind:
result.setInfo(TRANS("Rewind"), TRANS("Back to start position."), TRANS("Control"), 0);
result.setActive(true);
break;
}
}

Expand Down Expand Up @@ -141,6 +166,19 @@ bool CoreCommandTarget::perform(
case CoreCommandType::Redo:
this->redo();
return true;

case CoreCommandType::Play:
this->play();
return true;
case CoreCommandType::Stop:
this->stop();
return true;
case CoreCommandType::Record:
this->record();
return true;
case CoreCommandType::Rewind:
this->rewind();
return true;
}

return false;
Expand Down Expand Up @@ -380,6 +418,34 @@ void CoreCommandTarget::redo() const {
ActionDispatcher::getInstance()->performRedo();
}

void CoreCommandTarget::play() const {
bool isPlaying = this->checkForPlaying();

auto action = isPlaying
? std::unique_ptr<ActionBase>(new ActionPause)
: std::unique_ptr<ActionBase>(new ActionPlay);
ActionDispatcher::getInstance()->dispatch(std::move(action));
}

void CoreCommandTarget::stop() const {
auto action = std::unique_ptr<ActionBase>(new ActionStop);
ActionDispatcher::getInstance()->dispatch(std::move(action));
}

void CoreCommandTarget::record() const {
bool isRecording = this->checkForRecording();

auto action = isRecording
? std::unique_ptr<ActionBase>(new ActionStopRecord)
: std::unique_ptr<ActionBase>(new ActionStartRecord);
ActionDispatcher::getInstance()->dispatch(std::move(action));
}

void CoreCommandTarget::rewind() const {
auto action = std::unique_ptr<ActionBase>(new ActionRewind);
ActionDispatcher::getInstance()->dispatch(std::move(action));
}

bool CoreCommandTarget::checkForSave() const {
if (quickAPI::checkProjectSaved() && quickAPI::checkSourcesSaved()) {
return true;
Expand Down Expand Up @@ -502,6 +568,14 @@ const juce::String CoreCommandTarget::getRedoName() const {
return manager.getRedoDescription();
}

bool CoreCommandTarget::checkForPlaying() const {
return quickAPI::isPlaying();
}

bool CoreCommandTarget::checkForRecording() const {
return quickAPI::isRecording();
}

CoreCommandTarget* CoreCommandTarget::getInstance() {
return CoreCommandTarget::instance ? CoreCommandTarget::instance
: (CoreCommandTarget::instance = new CoreCommandTarget{});
Expand Down
8 changes: 8 additions & 0 deletions src/ui/menuAndCommand/CoreCommandTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class CoreCommandTarget final
void undo() const;
void redo() const;

void play() const;
void stop() const;
void record() const;
void rewind() const;

private:
bool checkForSave() const;
void selectForSource(const std::function<void(int)>& callback) const;
Expand All @@ -40,6 +45,9 @@ class CoreCommandTarget final
const juce::String getUndoName() const;
const juce::String getRedoName() const;

bool checkForPlaying() const;
bool checkForRecording() const;

std::unique_ptr<juce::ListBox> trackListBox = nullptr;
std::unique_ptr<juce::ListBoxModel> trackListBoxModel = nullptr;

Expand Down
21 changes: 21 additions & 0 deletions src/ui/menuAndCommand/GUICommandTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void GUICommandTarget::getAllCommands(
(juce::CommandID)(GUICommandType::AudioDebugger),
(juce::CommandID)(GUICommandType::MidiDebugger),

(juce::CommandID)(GUICommandType::Follow),

(juce::CommandID)(GUICommandType::StartupConfig),
(juce::CommandID)(GUICommandType::FunctionConfig),
(juce::CommandID)(GUICommandType::AudioConfig),
Expand Down Expand Up @@ -159,6 +161,12 @@ void GUICommandTarget::getCommandInfo(
result.setActive(true);
break;

case GUICommandType::Follow:
result.setInfo(TRANS("Follow"), TRANS("Follow playing position in editor."), TRANS("Control"), 0);
result.setTicked(this->checkFollow());
result.setActive(true);
break;

case GUICommandType::StartupConfig:
result.setInfo(TRANS("Startup Config"), TRANS("Open startup config page."), TRANS("Config"), 0);
result.setActive(true);
Expand Down Expand Up @@ -295,6 +303,10 @@ bool GUICommandTarget::perform(
this->changeOpened(CompManager::CompType::MidiDebugger);
return true;

case GUICommandType::Follow:
this->follow();
return true;

case GUICommandType::StartupConfig:
this->openConfig(0);
return true;
Expand Down Expand Up @@ -387,6 +399,10 @@ void GUICommandTarget::changeOpened(CompManager::CompType type) const {
}
}

void GUICommandTarget::follow() const {
/** TODO */
}

void GUICommandTarget::openConfig(int page) const {
auto window = new ConfigWindow;
window->setPage(page);
Expand Down Expand Up @@ -471,6 +487,11 @@ void GUICommandTarget::about() const {
window->enterModalState(true, nullptr, true);
}

bool GUICommandTarget::checkFollow() const {
/** TODO */
return false;
}

GUICommandTarget* GUICommandTarget::getInstance() {
return GUICommandTarget::instance ? GUICommandTarget::instance
: (GUICommandTarget::instance = new GUICommandTarget{});
Expand Down
Loading

0 comments on commit 8ee4619

Please sign in to comment.