mirror of
https://github.com/cookiengineer/audacity
synced 2025-06-30 15:18:42 +02:00
Move code from LabelTrack into LabelGlyphHandle
This commit is contained in:
parent
822183febc
commit
e0c4a94cfd
@ -1525,142 +1525,6 @@ auto LabelStruct::RegionRelation(
|
||||
}
|
||||
}
|
||||
|
||||
/// If the index is for a real label, adjust its left or right boundary.
|
||||
/// @iLabel - index of label, -1 for none.
|
||||
/// @iEdge - which edge is requested to move, -1 for left +1 for right.
|
||||
/// @bAllowSwapping - if we can switch which edge is being dragged.
|
||||
/// fNewTime - the NEW time for this edge of the label.
|
||||
void LabelGlyphHandle::MayAdjustLabel
|
||||
( LabelTrackHit &hit, int iLabel, int iEdge, bool bAllowSwapping, double fNewTime)
|
||||
{
|
||||
if( iLabel < 0 )
|
||||
return;
|
||||
|
||||
const auto pTrack = mpLT;
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
auto labelStruct = mLabels[ iLabel ];
|
||||
|
||||
// Adjust the requested edge.
|
||||
bool flipped = labelStruct.AdjustEdge( iEdge, fNewTime );
|
||||
// If the edges did not swap, then we are done.
|
||||
if( ! flipped ) {
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
return;
|
||||
}
|
||||
|
||||
// If swapping's not allowed we must also move the edge
|
||||
// we didn't move. Then we're done.
|
||||
if( !bAllowSwapping )
|
||||
{
|
||||
labelStruct.AdjustEdge( -iEdge, fNewTime );
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
return;
|
||||
}
|
||||
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
|
||||
// Swap our record of what we are dragging.
|
||||
std::swap( hit.mMouseOverLabelLeft, hit.mMouseOverLabelRight );
|
||||
}
|
||||
|
||||
// If the index is for a real label, adjust its left and right boundary.
|
||||
void LabelGlyphHandle::MayMoveLabel( int iLabel, int iEdge, double fNewTime)
|
||||
{
|
||||
if( iLabel < 0 )
|
||||
return;
|
||||
|
||||
const auto pTrack = mpLT;
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
auto labelStruct = mLabels[ iLabel ];
|
||||
labelStruct.MoveLabel( iEdge, fNewTime );
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
}
|
||||
|
||||
// Constrain function, as in processing/arduino.
|
||||
// returned value will be between min and max (inclusive).
|
||||
static int Constrain( int value, int min, int max )
|
||||
{
|
||||
wxASSERT( min <= max );
|
||||
int result=value;
|
||||
if( result < min )
|
||||
result=min;
|
||||
if( result > max )
|
||||
result=max;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LabelGlyphHandle::HandleGlyphDragRelease
|
||||
(LabelTrackHit &hit, const wxMouseEvent & evt,
|
||||
wxRect & r, const ZoomInfo &zoomInfo,
|
||||
SelectedRegion *newSel)
|
||||
{
|
||||
const auto pTrack = mpLT;
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
if(evt.LeftUp())
|
||||
{
|
||||
bool lupd = false, rupd = false;
|
||||
if( hit.mMouseOverLabelLeft >= 0 ) {
|
||||
auto labelStruct = mLabels[ hit.mMouseOverLabelLeft ];
|
||||
lupd = labelStruct.updated;
|
||||
labelStruct.updated = false;
|
||||
pTrack->SetLabel( hit.mMouseOverLabelLeft, labelStruct );
|
||||
}
|
||||
if( hit.mMouseOverLabelRight >= 0 ) {
|
||||
auto labelStruct = mLabels[ hit.mMouseOverLabelRight ];
|
||||
rupd = labelStruct.updated;
|
||||
labelStruct.updated = false;
|
||||
pTrack->SetLabel( hit.mMouseOverLabelRight, labelStruct );
|
||||
}
|
||||
|
||||
hit.mIsAdjustingLabel = false;
|
||||
hit.mMouseOverLabelLeft = -1;
|
||||
hit.mMouseOverLabelRight = -1;
|
||||
return lupd || rupd;
|
||||
}
|
||||
|
||||
if(evt.Dragging())
|
||||
{
|
||||
//If we are currently adjusting a label,
|
||||
//just reset its value and redraw.
|
||||
// LL: Constrain to inside track rectangle for now. Should be changed
|
||||
// to allow scrolling while dragging labels
|
||||
int x = Constrain( evt.m_x + mxMouseDisplacement - r.x, 0, r.width);
|
||||
|
||||
// If exactly one edge is selected we allow swapping
|
||||
bool bAllowSwapping =
|
||||
( hit.mMouseOverLabelLeft >=0 ) !=
|
||||
( hit.mMouseOverLabelRight >= 0);
|
||||
// If we're on the 'dot' and nowe're moving,
|
||||
// Though shift-down inverts that.
|
||||
// and if both edges the same, then we're always moving the label.
|
||||
bool bLabelMoving = hit.mbIsMoving;
|
||||
bLabelMoving ^= evt.ShiftDown();
|
||||
bLabelMoving |= ( hit.mMouseOverLabelLeft == hit.mMouseOverLabelRight );
|
||||
double fNewX = zoomInfo.PositionToTime(x, 0);
|
||||
if( bLabelMoving )
|
||||
{
|
||||
MayMoveLabel( hit.mMouseOverLabelLeft, -1, fNewX );
|
||||
MayMoveLabel( hit.mMouseOverLabelRight, +1, fNewX );
|
||||
}
|
||||
else
|
||||
{
|
||||
MayAdjustLabel( hit, hit.mMouseOverLabelLeft, -1, bAllowSwapping, fNewX );
|
||||
MayAdjustLabel( hit, hit.mMouseOverLabelRight, +1, bAllowSwapping, fNewX );
|
||||
}
|
||||
|
||||
if( pTrack->HasSelection() )
|
||||
{
|
||||
auto selIndex = LabelTrackView::Get( *pTrack ).GetSelectedIndex();
|
||||
//Set the selection region to be equal to
|
||||
//the NEW size of the label.
|
||||
*newSel = mLabels[ selIndex ].selectedRegion;
|
||||
}
|
||||
pTrack->SortLabels();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void LabelTextHandle::HandleTextDragRelease(const wxMouseEvent & evt)
|
||||
{
|
||||
auto pTrack = mpLT.lock();
|
||||
@ -1713,64 +1577,6 @@ void LabelTextHandle::HandleTextDragRelease(const wxMouseEvent & evt)
|
||||
return;
|
||||
}
|
||||
|
||||
void LabelGlyphHandle::HandleGlyphClick
|
||||
(LabelTrackHit &hit, const wxMouseEvent & evt,
|
||||
const wxRect & r, const ZoomInfo &zoomInfo,
|
||||
SelectedRegion *WXUNUSED(newSel))
|
||||
{
|
||||
if (evt.ButtonDown())
|
||||
{
|
||||
//OverGlyph sets mMouseOverLabel to be the chosen label.
|
||||
const auto pTrack = mpLT;
|
||||
LabelTrackView::OverGlyph(*pTrack, hit, evt.m_x, evt.m_y);
|
||||
hit.mIsAdjustingLabel = evt.Button(wxMOUSE_BTN_LEFT) &&
|
||||
( hit.mEdge & 3 ) != 0;
|
||||
|
||||
if (hit.mIsAdjustingLabel)
|
||||
{
|
||||
float t = 0.0;
|
||||
// We move if we hit the centre, we adjust one edge if we hit a chevron.
|
||||
// This is if we are moving just one edge.
|
||||
hit.mbIsMoving = (hit.mEdge & 4)!=0;
|
||||
// When we start dragging the label(s) we don't want them to jump.
|
||||
// so we calculate the displacement of the mouse from the drag center
|
||||
// and use that in subsequent dragging calculations. The mouse stays
|
||||
// at the same relative displacement throughout dragging.
|
||||
|
||||
// However, if two label's edges are being dragged
|
||||
// then the displacement is relative to the initial average
|
||||
// position of them, and in that case there can be a jump of at most
|
||||
// a few pixels to bring the two label boundaries to exactly the same
|
||||
// position when we start dragging.
|
||||
|
||||
// Dragging of three label edges at the same time is not supported (yet).
|
||||
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
if( ( hit.mMouseOverLabelRight >= 0 ) &&
|
||||
( hit.mMouseOverLabelLeft >= 0 )
|
||||
)
|
||||
{
|
||||
t = (mLabels[ hit.mMouseOverLabelRight ].getT1() +
|
||||
mLabels[ hit.mMouseOverLabelLeft ].getT0()) / 2.0f;
|
||||
// If we're moving two edges, then it's a move (label size preserved)
|
||||
// if both edges are the same label, and it's an adjust (label sizes change)
|
||||
// if we're on a boundary between two different labels.
|
||||
hit.mbIsMoving =
|
||||
( hit.mMouseOverLabelLeft == hit.mMouseOverLabelRight );
|
||||
}
|
||||
else if( hit.mMouseOverLabelRight >=0)
|
||||
{
|
||||
t = mLabels[ hit.mMouseOverLabelRight ].getT1();
|
||||
}
|
||||
else if( hit.mMouseOverLabelLeft >=0)
|
||||
{
|
||||
t = mLabels[ hit.mMouseOverLabelLeft ].getT0();
|
||||
}
|
||||
mxMouseDisplacement = zoomInfo.TimeToPosition(t, r.x) - evt.m_x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LabelTextHandle::HandleTextClick(const wxMouseEvent & evt,
|
||||
const wxRect & r, const ZoomInfo &zoomInfo,
|
||||
SelectedRegion *newSel)
|
||||
|
@ -121,6 +121,64 @@ LabelGlyphHandle::~LabelGlyphHandle()
|
||||
{
|
||||
}
|
||||
|
||||
void LabelGlyphHandle::HandleGlyphClick
|
||||
(LabelTrackHit &hit, const wxMouseEvent & evt,
|
||||
const wxRect & r, const ZoomInfo &zoomInfo,
|
||||
SelectedRegion *WXUNUSED(newSel))
|
||||
{
|
||||
if (evt.ButtonDown())
|
||||
{
|
||||
//OverGlyph sets mMouseOverLabel to be the chosen label.
|
||||
const auto pTrack = mpLT;
|
||||
LabelTrackView::OverGlyph(*pTrack, hit, evt.m_x, evt.m_y);
|
||||
hit.mIsAdjustingLabel = evt.Button(wxMOUSE_BTN_LEFT) &&
|
||||
( hit.mEdge & 3 ) != 0;
|
||||
|
||||
if (hit.mIsAdjustingLabel)
|
||||
{
|
||||
float t = 0.0;
|
||||
// We move if we hit the centre, we adjust one edge if we hit a chevron.
|
||||
// This is if we are moving just one edge.
|
||||
hit.mbIsMoving = (hit.mEdge & 4)!=0;
|
||||
// When we start dragging the label(s) we don't want them to jump.
|
||||
// so we calculate the displacement of the mouse from the drag center
|
||||
// and use that in subsequent dragging calculations. The mouse stays
|
||||
// at the same relative displacement throughout dragging.
|
||||
|
||||
// However, if two label's edges are being dragged
|
||||
// then the displacement is relative to the initial average
|
||||
// position of them, and in that case there can be a jump of at most
|
||||
// a few pixels to bring the two label boundaries to exactly the same
|
||||
// position when we start dragging.
|
||||
|
||||
// Dragging of three label edges at the same time is not supported (yet).
|
||||
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
if( ( hit.mMouseOverLabelRight >= 0 ) &&
|
||||
( hit.mMouseOverLabelLeft >= 0 )
|
||||
)
|
||||
{
|
||||
t = (mLabels[ hit.mMouseOverLabelRight ].getT1() +
|
||||
mLabels[ hit.mMouseOverLabelLeft ].getT0()) / 2.0f;
|
||||
// If we're moving two edges, then it's a move (label size preserved)
|
||||
// if both edges are the same label, and it's an adjust (label sizes change)
|
||||
// if we're on a boundary between two different labels.
|
||||
hit.mbIsMoving =
|
||||
( hit.mMouseOverLabelLeft == hit.mMouseOverLabelRight );
|
||||
}
|
||||
else if( hit.mMouseOverLabelRight >=0)
|
||||
{
|
||||
t = mLabels[ hit.mMouseOverLabelRight ].getT1();
|
||||
}
|
||||
else if( hit.mMouseOverLabelLeft >=0)
|
||||
{
|
||||
t = mLabels[ hit.mMouseOverLabelLeft ].getT0();
|
||||
}
|
||||
mxMouseDisplacement = zoomInfo.TimeToPosition(t, r.x) - evt.m_x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UIHandle::Result LabelGlyphHandle::Click
|
||||
(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
|
||||
{
|
||||
@ -152,6 +210,142 @@ UIHandle::Result LabelGlyphHandle::Click
|
||||
return result;
|
||||
}
|
||||
|
||||
/// If the index is for a real label, adjust its left or right boundary.
|
||||
/// @iLabel - index of label, -1 for none.
|
||||
/// @iEdge - which edge is requested to move, -1 for left +1 for right.
|
||||
/// @bAllowSwapping - if we can switch which edge is being dragged.
|
||||
/// fNewTime - the NEW time for this edge of the label.
|
||||
void LabelGlyphHandle::MayAdjustLabel
|
||||
( LabelTrackHit &hit, int iLabel, int iEdge, bool bAllowSwapping, double fNewTime)
|
||||
{
|
||||
if( iLabel < 0 )
|
||||
return;
|
||||
|
||||
const auto pTrack = mpLT;
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
auto labelStruct = mLabels[ iLabel ];
|
||||
|
||||
// Adjust the requested edge.
|
||||
bool flipped = labelStruct.AdjustEdge( iEdge, fNewTime );
|
||||
// If the edges did not swap, then we are done.
|
||||
if( ! flipped ) {
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
return;
|
||||
}
|
||||
|
||||
// If swapping's not allowed we must also move the edge
|
||||
// we didn't move. Then we're done.
|
||||
if( !bAllowSwapping )
|
||||
{
|
||||
labelStruct.AdjustEdge( -iEdge, fNewTime );
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
return;
|
||||
}
|
||||
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
|
||||
// Swap our record of what we are dragging.
|
||||
std::swap( hit.mMouseOverLabelLeft, hit.mMouseOverLabelRight );
|
||||
}
|
||||
|
||||
// If the index is for a real label, adjust its left and right boundary.
|
||||
void LabelGlyphHandle::MayMoveLabel( int iLabel, int iEdge, double fNewTime)
|
||||
{
|
||||
if( iLabel < 0 )
|
||||
return;
|
||||
|
||||
const auto pTrack = mpLT;
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
auto labelStruct = mLabels[ iLabel ];
|
||||
labelStruct.MoveLabel( iEdge, fNewTime );
|
||||
pTrack->SetLabel( iLabel, labelStruct );
|
||||
}
|
||||
|
||||
// Constrain function, as in processing/arduino.
|
||||
// returned value will be between min and max (inclusive).
|
||||
static int Constrain( int value, int min, int max )
|
||||
{
|
||||
wxASSERT( min <= max );
|
||||
int result=value;
|
||||
if( result < min )
|
||||
result=min;
|
||||
if( result > max )
|
||||
result=max;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool LabelGlyphHandle::HandleGlyphDragRelease
|
||||
(LabelTrackHit &hit, const wxMouseEvent & evt,
|
||||
wxRect & r, const ZoomInfo &zoomInfo,
|
||||
SelectedRegion *newSel)
|
||||
{
|
||||
const auto pTrack = mpLT;
|
||||
const auto &mLabels = pTrack->GetLabels();
|
||||
if(evt.LeftUp())
|
||||
{
|
||||
bool lupd = false, rupd = false;
|
||||
if( hit.mMouseOverLabelLeft >= 0 ) {
|
||||
auto labelStruct = mLabels[ hit.mMouseOverLabelLeft ];
|
||||
lupd = labelStruct.updated;
|
||||
labelStruct.updated = false;
|
||||
pTrack->SetLabel( hit.mMouseOverLabelLeft, labelStruct );
|
||||
}
|
||||
if( hit.mMouseOverLabelRight >= 0 ) {
|
||||
auto labelStruct = mLabels[ hit.mMouseOverLabelRight ];
|
||||
rupd = labelStruct.updated;
|
||||
labelStruct.updated = false;
|
||||
pTrack->SetLabel( hit.mMouseOverLabelRight, labelStruct );
|
||||
}
|
||||
|
||||
hit.mIsAdjustingLabel = false;
|
||||
hit.mMouseOverLabelLeft = -1;
|
||||
hit.mMouseOverLabelRight = -1;
|
||||
return lupd || rupd;
|
||||
}
|
||||
|
||||
if(evt.Dragging())
|
||||
{
|
||||
//If we are currently adjusting a label,
|
||||
//just reset its value and redraw.
|
||||
// LL: Constrain to inside track rectangle for now. Should be changed
|
||||
// to allow scrolling while dragging labels
|
||||
int x = Constrain( evt.m_x + mxMouseDisplacement - r.x, 0, r.width);
|
||||
|
||||
// If exactly one edge is selected we allow swapping
|
||||
bool bAllowSwapping =
|
||||
( hit.mMouseOverLabelLeft >=0 ) !=
|
||||
( hit.mMouseOverLabelRight >= 0);
|
||||
// If we're on the 'dot' and nowe're moving,
|
||||
// Though shift-down inverts that.
|
||||
// and if both edges the same, then we're always moving the label.
|
||||
bool bLabelMoving = hit.mbIsMoving;
|
||||
bLabelMoving ^= evt.ShiftDown();
|
||||
bLabelMoving |= ( hit.mMouseOverLabelLeft == hit.mMouseOverLabelRight );
|
||||
double fNewX = zoomInfo.PositionToTime(x, 0);
|
||||
if( bLabelMoving )
|
||||
{
|
||||
MayMoveLabel( hit.mMouseOverLabelLeft, -1, fNewX );
|
||||
MayMoveLabel( hit.mMouseOverLabelRight, +1, fNewX );
|
||||
}
|
||||
else
|
||||
{
|
||||
MayAdjustLabel( hit, hit.mMouseOverLabelLeft, -1, bAllowSwapping, fNewX );
|
||||
MayAdjustLabel( hit, hit.mMouseOverLabelRight, +1, bAllowSwapping, fNewX );
|
||||
}
|
||||
|
||||
if( pTrack->HasSelection() )
|
||||
{
|
||||
auto selIndex = LabelTrackView::Get( *pTrack ).GetSelectedIndex();
|
||||
//Set the selection region to be equal to
|
||||
//the NEW size of the label.
|
||||
*newSel = mLabels[ selIndex ].selectedRegion;
|
||||
}
|
||||
pTrack->SortLabels();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
UIHandle::Result LabelGlyphHandle::Drag
|
||||
(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user