From 7cd52515165f53940869381d0d778fe4257e3c26 Mon Sep 17 00:00:00 2001 From: xenakios Date: Thu, 22 Feb 2018 14:22:06 +0200 Subject: [PATCH] Added methods for the normalized time position to/from view x coordinate mappings --- Source/PluginEditor.cpp | 22 ++++++++++------------ Source/PluginEditor.h | 11 ++++++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index f28554d..8f592ae 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -505,8 +505,8 @@ void WaveformComponent::paint(Graphics & g) double sel_len = m_time_sel_end - m_time_sel_start; //if (sel_len > 0.0 && sel_len < 1.0) { - int xcorleft = (int)jmap(m_time_sel_start, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth()); - int xcorright = (int)jmap(m_time_sel_end, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth()); + int xcorleft = normalizedToViewX(m_time_sel_start); + int xcorright = normalizedToViewX(m_time_sel_end); g.fillRect(xcorleft, m_topmargin, xcorright - xcorleft, getHeight() - m_topmargin); } if (m_file_cached.first.getLength() > 0.0 && @@ -528,14 +528,12 @@ void WaveformComponent::paint(Graphics & g) g.setColour(Colours::white); if (CursorPosCallback) { - double pos = jmap(CursorPosCallback(), m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth()); - g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin); + g.fillRect(normalizedToViewX(CursorPosCallback()), m_topmargin, 1, getHeight() - m_topmargin); } if (m_rec_pos >= 0.0) { g.setColour(Colours::lightpink); - double pos = jmap(m_rec_pos, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth()); - g.fillRect((int)pos, m_topmargin, 1, getHeight() - m_topmargin); + g.fillRect(normalizedToViewX(m_rec_pos), m_topmargin, 1, getHeight() - m_topmargin); } g.setColour(Colours::aqua); g.drawText(GetFileCallback().getFileName(), 2, m_topmargin + 2, getWidth(), 20, Justification::topLeft); @@ -572,7 +570,7 @@ void WaveformComponent::mouseDown(const MouseEvent & e) { m_mousedown = true; m_lock_timesel_set = true; - double pos = jmap(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + double pos = viewXToNormalized(e.x); if (e.y < m_topmargin || e.mods.isCommandDown()) { if (SeekCallback) @@ -612,27 +610,27 @@ void WaveformComponent::mouseDrag(const MouseEvent & e) if (m_time_sel_drag_target == 0) { m_time_sel_start = m_drag_time_start; - m_time_sel_end = jmap(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + m_time_sel_end = viewXToNormalized(e.x); } double curlen = m_time_sel_end-m_time_sel_start; if (m_time_sel_drag_target == 1) { if (e.mods.isShiftDown()==false) - m_time_sel_start = jmap(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + m_time_sel_start = viewXToNormalized(e.x); else { - m_time_sel_start = jmap(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + m_time_sel_start = viewXToNormalized(e.x); m_time_sel_end = m_time_sel_start+curlen; } } if (m_time_sel_drag_target == 2) { if (e.mods.isShiftDown()==false) - m_time_sel_end = jmap(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + m_time_sel_end = viewXToNormalized(e.x); else { - m_time_sel_end = jmap(e.x, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + m_time_sel_end = viewXToNormalized(e.x); m_time_sel_start = m_time_sel_end-curlen; } } diff --git a/Source/PluginEditor.h b/Source/PluginEditor.h index 94c0642..718d36e 100644 --- a/Source/PluginEditor.h +++ b/Source/PluginEditor.h @@ -168,6 +168,15 @@ private: bool m_lock_timesel_set = false; bool m_using_audio_buffer = false; void updateCachedImage(); + double viewXToNormalized(double xcor) + { + return jmap(xcor, 0, getWidth(), m_view_range.getStart(), m_view_range.getEnd()); + } + template + inline T normalizedToViewX(double norm) + { + return static_cast(jmap(norm, m_view_range.getStart(), m_view_range.getEnd(), 0, getWidth())); + } }; class SpectralChainEditor : public Component @@ -218,6 +227,6 @@ private: void showSettingsMenu(); String m_last_err; zoom_scrollbar m_zs; - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PaulstretchpluginAudioProcessorEditor) + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PaulstretchpluginAudioProcessorEditor) };