From f061fe8a8c5f7f5e4fd9cba17698267e45931e4a Mon Sep 17 00:00:00 2001 From: apoorv569 Date: Sun, 29 Aug 2021 19:45:03 +0530 Subject: [PATCH] Add custom event to send loop points to MainFrame. --- meson.build | 1 + src/MainFrame.cpp | 47 +++++++++++++++++++---- src/MainFrame.hpp | 4 ++ src/SH_Event.cpp | 56 +++++++++++++++++++++++++++ src/SH_Event.hpp | 87 ++++++++++++++++++++++++++++++++++++++++++ src/WaveformViewer.cpp | 52 +++++++++++++++++++------ src/WaveformViewer.hpp | 24 +++++------- 7 files changed, 238 insertions(+), 33 deletions(-) create mode 100644 src/SH_Event.cpp create mode 100644 src/SH_Event.hpp diff --git a/meson.build b/meson.build index 52f0b5d..bae49b3 100755 --- a/meson.build +++ b/meson.build @@ -58,6 +58,7 @@ src = [ 'src/Serialize.cpp', 'src/Tags.cpp', 'src/WaveformViewer.cpp', + 'src/SH_Event.cpp', ] diff --git a/src/MainFrame.cpp b/src/MainFrame.cpp index 1ec0801..5c842de 100644 --- a/src/MainFrame.cpp +++ b/src/MainFrame.cpp @@ -379,7 +379,8 @@ MainFrame::MainFrame() // Intializing wxTimer m_Timer = new wxTimer(this); - m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_StatusBar, *m_Library, *m_MediaCtrl, *m_Timer, *m_InfoBar, m_ConfigFilepath, m_DatabaseFilepath); + m_TopWaveformPanel = new WaveformViewer(this, m_TopPanel, *m_Library, *m_MediaCtrl, *m_Timer, *m_InfoBar, + m_ConfigFilepath, m_DatabaseFilepath); // Binding events. Bind(wxEVT_MENU, &MainFrame::OnSelectAddFile, this, MN_AddFile); @@ -433,6 +434,9 @@ MainFrame::MainFrame() Bind(wxEVT_BUTTON, &MainFrame::OnClickAddHive, this, BC_HiveAdd); Bind(wxEVT_BUTTON, &MainFrame::OnClickRemoveHive, this, BC_HiveRemove); + Bind(SampleHive::SH_EVT_LOOP_POINTS_UPDATED, &MainFrame::OnRecieveLoopPoints, this); + Bind(SampleHive::SH_EVT_STATUSBAR_MESSAGE_UPDATED, &MainFrame::OnRecieveStatusBarStatus, this); + // Adding widgets to their sizers m_MainSizer->Add(m_MainPanel, 1, wxALL | wxEXPAND, 0); @@ -553,8 +557,7 @@ void MainFrame::OnClickSettings(wxCommandEvent& event) } if (settings->IsWaveformColourChanged()) { - m_TopWaveformPanel->SetBitmapDirty(true); - m_TopWaveformPanel->Refresh(); + m_TopWaveformPanel->ResetDC(); } break; case wxID_CANCEL: @@ -945,8 +948,7 @@ void MainFrame::OnClickPlay(wxCommandEvent& event) PushStatusText(wxString::Format(_("Now playing: %s"), selection), 1); // Update waveform bitmap - m_TopWaveformPanel->SetBitmapDirty(true); - m_TopWaveformPanel->Refresh(); + m_TopWaveformPanel->ResetDC(); m_MediaCtrl->Play(); @@ -1097,8 +1099,7 @@ void MainFrame::OnClickLibrary(wxDataViewEvent& event) } // Update the waveform bitmap - m_TopWaveformPanel->SetBitmapDirty(true); - m_TopWaveformPanel->Refresh(); + m_TopWaveformPanel->ResetDC(); wxString selection = m_Library->GetTextValue(selected_row, 1); @@ -2942,4 +2943,36 @@ void MainFrame::OnEnterLoopPoints(wxCommandEvent& event) wxLogDebug("Loop point text changed"); } +void MainFrame::OnRecieveLoopPoints(SampleHive::SH_LoopPointsEvent& event) +{ + wxLogDebug("%s called and recieved loop points", __FUNCTION__); + + std::pair loop_points = event.GetLoopPoints(); + + wxLongLong loopA = loop_points.first; + wxLongLong loopB = loop_points.second; + + int loopA_min = static_cast((loopA / 60000).GetValue()); + int loopA_sec = static_cast(((loopA % 60000) / 1000).GetValue()); + int loopB_min = static_cast((loopB / 60000).GetValue()); + int loopB_sec = static_cast(((loopB % 60000) / 1000).GetValue()); + + wxLogDebug(wxString::Format("LoopA: %2i:%02i, LoopB: %2i:%02i", + loopA_min, loopA_sec, loopB_min, loopB_sec)); + + m_LoopPointAText->SetValue(wxString::Format("%2i:%02i", loopA_min, loopA_sec)); + m_LoopPointBText->SetValue(wxString::Format("%2i:%02i", loopB_min, loopB_sec)); + + // TODO Looping the selected section + + wxLogDebug("%s Event processed successfully..", __FUNCTION__); +} + +void MainFrame::OnRecieveStatusBarStatus(SampleHive::SH_SetStatusBarMessageEvent& event) +{ + std::pair status = event.GetMessageAndSection(); + + m_StatusBar->PushStatusText(status.first, status.second); +} + MainFrame::~MainFrame(){} diff --git a/src/MainFrame.hpp b/src/MainFrame.hpp index fb63dcb..335db69 100644 --- a/src/MainFrame.hpp +++ b/src/MainFrame.hpp @@ -56,6 +56,7 @@ #include #include "WaveformViewer.hpp" +#include "SH_Event.hpp" struct FileInfo { @@ -252,6 +253,9 @@ class MainFrame : public wxFrame void AddSamples(wxArrayString& files); void OnAutoImportDir(const wxString& pathToDirectory); + void OnRecieveLoopPoints(SampleHive::SH_LoopPointsEvent& event); + void OnRecieveStatusBarStatus(SampleHive::SH_SetStatusBarMessageEvent& event); + // ------------------------------------------------------------------- void LoadDatabase(); void RefreshDatabase(); diff --git a/src/SH_Event.cpp b/src/SH_Event.cpp new file mode 100644 index 0000000..cd1a749 --- /dev/null +++ b/src/SH_Event.cpp @@ -0,0 +1,56 @@ +#include "SH_Event.hpp" + +namespace SampleHive +{ + SH_LoopPointsEvent::SH_LoopPointsEvent(wxEventType eventType, int winId) + : wxCommandEvent(eventType, winId) + { + + } + + SH_LoopPointsEvent::~SH_LoopPointsEvent() + { + + } + + wxDEFINE_EVENT(SH_EVT_LOOP_POINTS_UPDATED, SH_LoopPointsEvent); + + // SH_AddSampleEvent::SH_AddSampleEvent(wxEventType eventType, int winId) + // : wxCommandEvent(eventType, winId) + // { + + // } + + // SH_AddSampleEvent::~SH_AddSampleEvent() + // { + + // } + + // wxDEFINE_EVENT(SH_EVT_STATUS_ADD_SAMPLE, SH_AddSampleEvent); + + // SH_MediaEvent::SH_MediaEvent(wxEventType eventType, int winId) + // : wxCommandEvent(eventType, winId) + // { + + // } + + // SH_MediaEvent::~SH_MediaEvent() + // { + + // } + + // wxDEFINE_EVENT(SH_EVT_MEDIA_STATUS_UPDATED, SH_MediaEvent); + + SH_SetStatusBarMessageEvent::SH_SetStatusBarMessageEvent(wxEventType eventType, int winId) + : wxCommandEvent(eventType, winId) + { + + } + + SH_SetStatusBarMessageEvent::~SH_SetStatusBarMessageEvent() + { + + } + + wxDEFINE_EVENT(SH_EVT_STATUSBAR_MESSAGE_UPDATED, SH_SetStatusBarMessageEvent); +} diff --git a/src/SH_Event.hpp b/src/SH_Event.hpp new file mode 100644 index 0000000..c7915ad --- /dev/null +++ b/src/SH_Event.hpp @@ -0,0 +1,87 @@ +#pragma once + +#include + +// #include "wx/arrstr.h" +#include "wx/event.h" + +namespace SampleHive +{ + class SH_LoopPointsEvent : public wxCommandEvent + { + public: + SH_LoopPointsEvent(wxEventType eventType, int winId); + ~SH_LoopPointsEvent(); + + public: + virtual wxEvent* Clone() const { return new SH_LoopPointsEvent(*this); } + + public: + std::pair GetLoopPoints() const { return { m_LoopA, m_LoopB }; }; + void SetLoopPoints(std::pair loopPoints) + { m_LoopA = loopPoints.first; m_LoopB = loopPoints.second; }; + + private: + double m_LoopA, m_LoopB; + }; + + wxDECLARE_EVENT(SH_EVT_LOOP_POINTS_UPDATED, SH_LoopPointsEvent); + + // class SH_AddSampleEvent : public wxCommandEvent + // { + // public: + // SH_AddSampleEvent(wxEventType eventType, int winId); + // ~SH_AddSampleEvent(); + + // public: + // virtual wxEvent* Clone() const { return new SH_AddSampleEvent(*this); } + + // public: + // wxArrayString GetArrayString() const { return m_Files; }; + // void SetArrayString(const wxArrayString& files) { m_Files = files; }; + + // private: + // wxArrayString m_Files; + // }; + + // wxDECLARE_EVENT(SH_EVT_STATUS_ADD_SAMPLE, SH_AddSampleEvent); + + // class SH_MediaEvent : public wxCommandEvent + // { + // public: + // SH_MediaEvent(wxEventType eventType, int winId); + // ~SH_MediaEvent(); + + // public: + // virtual wxEvent* Clone() const { return new SH_MediaEvent(*this); } + + // public: + // void SetPath(const wxString& path) { m_Path = path; } + // wxString GetPath() const { return m_Path; } + + // private: + // wxString m_Path; + // }; + + // wxDECLARE_EVENT(SH_EVT_MEDIA_STATUS_UPDATED, SH_MediaEvent); + + class SH_SetStatusBarMessageEvent : public wxCommandEvent + { + public: + SH_SetStatusBarMessageEvent(wxEventType eventType, int winId); + ~SH_SetStatusBarMessageEvent(); + + public: + virtual wxEvent* Clone() const { return new SH_SetStatusBarMessageEvent(*this); } + + public: + std::pair GetMessageAndSection() const { return {m_Msg, m_Section }; } + void SetMessageAndSection(std::pair status) { m_Msg = status.first; m_Section = status.second; } + + private: + wxString m_Msg; + int m_Section; + }; + + wxDECLARE_EVENT(SH_EVT_STATUSBAR_MESSAGE_UPDATED, SH_SetStatusBarMessageEvent); +} diff --git a/src/WaveformViewer.cpp b/src/WaveformViewer.cpp index 2aaec24..cee2b2b 100644 --- a/src/WaveformViewer.cpp +++ b/src/WaveformViewer.cpp @@ -36,12 +36,14 @@ #include "SettingsDialog.hpp" #include "Serialize.hpp" #include "Tags.hpp" +#include "SH_Event.hpp" -WaveformViewer::WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxStatusBar& statusbar, wxDataViewListCtrl& library, wxMediaCtrl& mediaCtrl, - wxTimer& timer, wxInfoBar& infoBar, const std::string& configFilepath, const std::string& databaseFilepath) +WaveformViewer::WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library, + wxMediaCtrl& mediaCtrl, wxTimer& timer, wxInfoBar& infoBar, + const std::string& configFilepath, const std::string& databaseFilepath) : wxPanel(window, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxNO_BORDER | wxFULL_REPAINT_ON_RESIZE), - m_ParentFrame(parentFrame), m_Window(window), m_Library(library), m_InfoBar(infoBar), m_MediaCtrl(mediaCtrl), m_Timer(timer), - m_StatusBar(statusbar), m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath) + m_ParentFrame(parentFrame), m_Window(window), m_Library(library), m_InfoBar(infoBar), m_MediaCtrl(mediaCtrl), + m_Timer(timer), m_ConfigFilepath(configFilepath), m_DatabaseFilepath(databaseFilepath) { this->SetDoubleBuffered(true); @@ -84,6 +86,7 @@ void WaveformViewer::OnPaint(wxPaintEvent& event) RenderPlayhead(dc); + // Draw selection range if (bSelectRange) { wxRect rect(m_CurrentPoint, m_AnchorPoint); @@ -93,6 +96,7 @@ void WaveformViewer::OnPaint(wxPaintEvent& event) dc.DrawRectangle(rect); } + // Draw selected area if (!bSelectRange && bDrawSelectedArea && !bBitmapDirty) { dc.SetPen(wxPen(wxColour(200, 200, 200, 255), 4, wxPENSTYLE_SOLID)); @@ -100,6 +104,7 @@ void WaveformViewer::OnPaint(wxPaintEvent& event) dc.DrawRectangle(wxRect(m_AnchorPoint.x, -2, m_CurrentPoint.x - m_AnchorPoint.x, this->GetSize().GetHeight() + 5)); bAreaSelected = true; + SendLoopPoints(); } else bAreaSelected = false; @@ -410,19 +415,33 @@ void WaveformViewer::OnMouseLeftButtonUp(wxMouseEvent& event) SetCursor(wxCURSOR_ARROW); m_MediaCtrl.Seek(seek_to, wxFromStart); - m_StatusBar.PushStatusText(wxString::Format(_("Now playing: %s"), selected), 1); + SendStatusBarStatus(wxString::Format(_("Now playing: %s"), selected), 1); m_MediaCtrl.Play(); } } -WaveformViewer::LoopPoints WaveformViewer::GetLoopPoints() +void WaveformViewer::ResetDC() { + bBitmapDirty = true; + bSelectRange = false; + bDrawSelectedArea = false; + + Refresh(); +} + +void WaveformViewer::SendLoopPoints() +{ + wxLogDebug("%s Called", __FUNCTION__); + + SampleHive::SH_LoopPointsEvent event(SampleHive::SH_EVT_LOOP_POINTS_UPDATED, this->GetId()); + event.SetEventObject(this); + Database db(m_InfoBar); int selected_row = m_Library.GetSelectedRow(); if (selected_row < 0) - return { 0.0f, 0.0f }; + return; wxString selected = m_Library.GetTextValue(selected_row, 1); std::string path = db.GetSamplePathByFilename(m_DatabaseFilepath, selected.BeforeLast('.').ToStdString()); @@ -431,15 +450,26 @@ WaveformViewer::LoopPoints WaveformViewer::GetLoopPoints() int length = tags.GetAudioInfo().length; - // double position = m_MediaCtrl.Tell(); - int panel_width = this->GetSize().GetWidth(); - // double line_pos = panel_width * (position / length); int a = m_AnchorPoint.x, b = m_CurrentPoint.x; double loopA = ((double)a / panel_width) * length; double loopB = ((double)b / panel_width) * length; - return { loopA, loopB }; + event.SetLoopPoints({ loopA, loopB }); + + HandleWindowEvent(event); + + wxLogDebug("%s processed event, sending loop points..", __FUNCTION__); +} + +void WaveformViewer::SendStatusBarStatus(const wxString& msg, int section) +{ + SampleHive::SH_SetStatusBarMessageEvent event(SampleHive::SH_EVT_STATUSBAR_MESSAGE_UPDATED, this->GetId()); + event.SetEventObject(this); + + event.SetMessageAndSection({ msg, section }); + + HandleWindowEvent(event); } diff --git a/src/WaveformViewer.hpp b/src/WaveformViewer.hpp index 9fad2cb..50c2931 100644 --- a/src/WaveformViewer.hpp +++ b/src/WaveformViewer.hpp @@ -36,17 +36,11 @@ class WaveformViewer : public wxPanel { public: - WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxStatusBar& statusbar, wxDataViewListCtrl& library, wxMediaCtrl& mediaCtrl, - wxTimer& timer, wxInfoBar& infoBar, const std::string& configFilepath, const std::string& databaseFilepath); + WaveformViewer(wxWindow* parentFrame, wxWindow* window, wxDataViewListCtrl& library, + wxMediaCtrl& mediaCtrl, wxTimer& timer, wxInfoBar& infoBar, + const std::string& configFilepath, const std::string& databaseFilepath); ~WaveformViewer(); - private: - // ------------------------------------------------------------------- - struct LoopPoints - { - double A, B; - }; - private: // ------------------------------------------------------------------- wxWindow* m_ParentFrame; @@ -56,7 +50,6 @@ class WaveformViewer : public wxPanel wxInfoBar& m_InfoBar; wxMediaCtrl& m_MediaCtrl; wxTimer& m_Timer; - wxStatusBar& m_StatusBar; const std::string& m_ConfigFilepath; const std::string& m_DatabaseFilepath; @@ -94,11 +87,12 @@ class WaveformViewer : public wxPanel void OnControlKeyUp(wxKeyEvent& event); void OnControlKeyDown(wxKeyEvent& event); - public: - LoopPoints GetLoopPoints(); + // ------------------------------------------------------------------- + // Send custom events + void SendLoopPoints(); + void SendStatusBarStatus(const wxString& msg, int section); public: - inline bool IsBitmapDirty() { return bBitmapDirty; } - inline void SetBitmapDirty(bool dirty) { bBitmapDirty = dirty; } - inline bool IsAreaSelected() { return bAreaSelected; } + // ------------------------------------------------------------------- + void ResetDC(); };