Sound preview misc fixes (#3655)

* start sound preview playback at scrubber position instead of 0, only increment time if playing

* Fix SceneRect calculation so that there isnt 4 pixels of extra scroll when zoomed all the way out, only show scroll bar if there's anything to scroll

* clamp scrubber position so it cant be visually dragged outside normal bounds

* use correct offset when setting scrubber position during playback

* update VisibleRect before redrawing waveform, waveform was previously drawing in wrong position (view from last frame)

* clamp on scrubber should be using SceneRect not ContentRect

* no need to update VisibleRect twice

* use correct offset for scrubber in DoLayout too

* update scrubber so it stays nice and stable while playing+zoomed in or scrubbing, use correct offset when manipulating scrubber

* offsetting by 4 everywhere is dumb just set HandlePosition on scrubber

* fix drift on scrubber while zoomed in

---------

Co-authored-by: boxrocket <splatterbiker@gmail.com>
This commit is contained in:
sboxbot
2025-12-22 13:55:12 +00:00
committed by GitHub
parent 77df75674b
commit dbdfbff73a
2 changed files with 20 additions and 13 deletions

View File

@@ -14,6 +14,7 @@ public partial class SoundPlayer
Cursor = CursorShape.SizeH;
Movable = true;
Selectable = true;
HandlePosition = new( 0.5f, 0f );
}
protected override void OnPaint()
@@ -41,6 +42,7 @@ public partial class SoundPlayer
protected override void OnMouseReleased( GraphicsMouseEvent e )
{
base.OnMouseReleased( e );
TimelineView.MoveScrubber( Position.x );
TimelineView.Scrubbing = false;
}
@@ -49,7 +51,7 @@ public partial class SoundPlayer
base.OnMoved();
TimelineView.Scrubbing = true;
TimelineView.MoveScrubber( Position.x, false );
TimelineView.MoveScrubber( Position.x + 0.5f, false );
}
}

View File

@@ -129,7 +129,7 @@ public partial class SoundPlayer : Widget
{
Timeline = parent;
SceneRect = new( 0, Size );
HorizontalScrollbar = ScrollbarMode.On;
HorizontalScrollbar = ScrollbarMode.Auto;
VerticalScrollbar = ScrollbarMode.Off;
Scale = 1;
Time = 0;
@@ -152,6 +152,7 @@ public partial class SoundPlayer : Widget
var size = Size;
size.x = MathF.Max( Size.x, PositionFromTime( Duration ) );
size.x -= Width - ContentRect.Width;
SceneRect = new( 0, size );
TimeAxis.Size = new Vector2( size.x, Theme.RowHeight );
Scrubber.Size = new Vector2( 9, size.y );
@@ -162,7 +163,7 @@ public partial class SoundPlayer : Widget
WaveForm.SceneRect = r;
WaveForm.Analyse();
Scrubber.Position = Scrubber.Position.WithX( PositionFromTime( Time ) - 3 ).SnapToGrid( 1.0f );
Scrubber.Position = Scrubber.Position.WithX( PositionFromTime( Time ) ).SnapToGrid( 1.0f );
}
protected override void OnResize()
@@ -191,8 +192,6 @@ public partial class SoundPlayer : Widget
SoundHandle = null;
}
VisibleRect = Rect.FromPoints( ToScene( LocalRect.TopLeft ), ToScene( LocalRect.BottomRight ) );
if ( Timeline.Playing && !Scrubbing )
{
var time = Time % Duration;
@@ -219,13 +218,14 @@ public partial class SoundPlayer : Widget
if ( Timeline.Playing && !SoundHandle.IsValid() )
{
SoundHandle = EditorUtility.PlaySound( Sound, Time );
SoundHandle.Time = 0;
SoundHandle.Time = Time;
SoundHandle.Occlusion = false;
SoundHandle.DistanceAttenuation = false;
}
Scrubber.Position = Scrubber.Position.WithX( PositionFromTime( Time ) - 3 ).SnapToGrid( 1.0f );
Time += RealTime.SmoothDelta;
Scrubber.Position = Scrubber.Position.WithX( PositionFromTime( Time ) ).SnapToGrid( 1.0f );
if ( Timeline.Playing )
Time += RealTime.SmoothDelta;
}
if ( SoundHandle.IsValid() )
@@ -233,13 +233,16 @@ public partial class SoundPlayer : Widget
SoundHandle.Paused = Scrubbing;
}
TimeAxis.Update();
WaveForm.Update();
if ( Scrubbing || Timeline.Playing )
{
Translate( 1 );
CenterOn( Scrubber.Position );
}
VisibleRect = Rect.FromPoints( ToScene( LocalRect.TopLeft ), ToScene( LocalRect.BottomRight ) );
TimeAxis.Update();
WaveForm.Update();
}
public float PositionFromTime( float time )
@@ -261,8 +264,8 @@ public partial class SoundPlayer : Widget
public void MoveScrubber( float position, bool centreOn = true )
{
Scrubber.Position = Vector2.Right * (position - 4).SnapToGrid( 1.0f );
Time = TimeFromPosition( Scrubber.Position.x + 4 );
Scrubber.Position = Vector2.Right * position.Clamp( 0, SceneRect.Width + 4 ).SnapToGrid( 1.0f );
Time = TimeFromPosition( Scrubber.Position.x );
if ( SoundHandle.IsValid() )
{
@@ -285,6 +288,8 @@ public partial class SoundPlayer : Widget
DoLayout();
VisibleRect = Rect.FromPoints( ToScene( LocalRect.TopLeft ), ToScene( LocalRect.BottomRight ) );
TimeAxis.Update();
WaveForm.Update();