Skip to content

Commit

Permalink
iOS: Added wave form scroll view autoscroll when changing the seconda…
Browse files Browse the repository at this point in the history
…ry position (i.e. changing the song position). More bug fixes for wave form controls.

Related to issue #405.
  • Loading branch information
ycastonguay committed Jul 8, 2013
1 parent 9fab84b commit a19ffa7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 30 deletions.
13 changes: 7 additions & 6 deletions MPfm/MPfm.iOS/Classes/Controllers/PlayerViewController.cs
Expand Up @@ -136,11 +136,12 @@ public override void ViewDidLoad()
float offset = 42;
viewPosition.Frame = new RectangleF(viewPosition.Frame.X, viewPosition.Frame.Y, viewPosition.Frame.Width, viewPosition.Frame.Height + offset);
scrollViewWaveForm.Frame = new RectangleF(scrollViewWaveForm.Frame.X, scrollViewWaveForm.Frame.Y + offset, scrollViewWaveForm.Frame.Width, scrollViewWaveForm.Frame.Height * 2);
scrollViewWaveForm.WaveFormView.Frame = new RectangleF(scrollViewWaveForm.WaveFormView.Frame.X, scrollViewWaveForm.WaveFormView.Frame.Y, scrollViewWaveForm.WaveFormView.Frame.Width, scrollViewWaveForm.WaveFormView.Frame.Height * 2);
scrollViewWaveForm.WaveFormView.Frame = new RectangleF(scrollViewWaveForm.WaveFormView.Frame.X, scrollViewWaveForm.WaveFormView.Frame.Y, scrollViewWaveForm.WaveFormView.Frame.Width, (scrollViewWaveForm.WaveFormView.Frame.Height * 2) + 22);
viewMain.Frame = new RectangleF(viewMain.Frame.X, viewPosition.Frame.Height + scrollViewWaveForm.Frame.Height, viewMain.Frame.Width, viewMain.Frame.Height);
_volumeView.Frame = new RectangleF(_volumeView.Frame.X, viewPosition.Frame.Height + scrollViewWaveForm.Frame.Height + viewMain.Frame.Height - 32, _volumeView.Frame.Width, _volumeView.Frame.Height);
lblSlideMessage.Alpha = 1;
lblScrubbingType.Alpha = 1;
scrollViewWaveForm.ShowSecondaryPosition(true);
});
};
sliderPosition.TouchesMovedEvent += (sender, e) => {
Expand All @@ -149,22 +150,22 @@ public override void ViewDidLoad()
PlayerPositionEntity entity = OnPlayerRequestPosition(sliderPosition.Value / 10000);
lblPosition.Text = entity.Position;
scrollViewWaveForm.WaveFormView.SecondaryPosition = entity.PositionBytes;
scrollViewWaveForm.SetSecondaryPosition(entity.PositionBytes);
};
sliderPosition.TouchesEndedEvent += (sender, e) => {
//Console.WriteLine("Position: Setting value to " + position.ToString());
UIView.Animate(0.2f, () => {
float offset = 42;
viewPosition.Frame = new RectangleF(viewPosition.Frame.X, viewPosition.Frame.Y, viewPosition.Frame.Width, viewPosition.Frame.Height - offset);
scrollViewWaveForm.Frame = new RectangleF(scrollViewWaveForm.Frame.X, scrollViewWaveForm.Frame.Y - offset, scrollViewWaveForm.Frame.Width, scrollViewWaveForm.Frame.Height / 2);
scrollViewWaveForm.WaveFormView.Frame = new RectangleF(scrollViewWaveForm.WaveFormView.Frame.X, scrollViewWaveForm.WaveFormView.Frame.Y, scrollViewWaveForm.WaveFormView.Frame.Width, scrollViewWaveForm.WaveFormView.Frame.Height / 2);
scrollViewWaveForm.WaveFormView.Frame = new RectangleF(scrollViewWaveForm.WaveFormView.Frame.X, scrollViewWaveForm.WaveFormView.Frame.Y, scrollViewWaveForm.WaveFormView.Frame.Width, (scrollViewWaveForm.WaveFormView.Frame.Height - 22) / 2);
viewMain.Frame = new RectangleF(viewMain.Frame.X, viewPosition.Frame.Height + scrollViewWaveForm.Frame.Height, viewMain.Frame.Width, viewMain.Frame.Height);
_volumeView.Frame = new RectangleF(_volumeView.Frame.X, viewPosition.Frame.Height + scrollViewWaveForm.Frame.Height + viewMain.Frame.Height - 32, _volumeView.Frame.Width, _volumeView.Frame.Height);
lblSlideMessage.Alpha = 0;
lblScrubbingType.Alpha = 0;
});
OnPlayerSetPosition(sliderPosition.Value / 100);
scrollViewWaveForm.WaveFormView.SecondaryPosition = 0;
scrollViewWaveForm.ShowSecondaryPosition(false);
_isPositionChanging = false;
};

Expand Down Expand Up @@ -223,7 +224,7 @@ public override void ViewDidLayoutSubviews()
var screenSize = UIKitHelper.GetDeviceSize();
if (UserInterfaceIdiomIsPhone)
{
_volumeView.Frame = new RectangleF(8, screenSize.Height - 44 - 52, screenSize.Width - 16, 46);
//_volumeView.Frame = new RectangleF(8, screenSize.Height - 44 - 52, screenSize.Width - 16, 46);
}
else
{
Expand Down Expand Up @@ -408,7 +409,7 @@ public void RefreshPlayerPosition(PlayerPositionEntity entity)
sliderPosition.SetPosition(entity.PositionPercentage * 100);
}
scrollViewWaveForm.WaveFormView.Position = entity.PositionBytes;
scrollViewWaveForm.SetPosition(entity.PositionBytes);
});
}

Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.iOS/Classes/Controls/MPfmWaveFormScaleView.cs
Expand Up @@ -89,7 +89,7 @@ public MPfmWaveFormScaleView(RectangleF frame)

private void Initialize()
{
this.BackgroundColor = UIColor.Blue;
BackgroundColor = GlobalTheme.BackgroundColor;
}

public override void Draw(RectangleF rect)
Expand Down
109 changes: 98 additions & 11 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmWaveFormScrollView.cs
Expand Up @@ -47,7 +47,7 @@ public class MPfmWaveFormScrollView : UIScrollView
private UIView _viewCenterLine;
private float _scaleHeight = 22f;

private WaveFormScrollViewMode _scrollViewMode = WaveFormScrollViewMode.SelectPosition;
private WaveFormScrollViewMode _scrollViewMode = WaveFormScrollViewMode.Standard;
public WaveFormScrollViewMode ScrollViewMode
{
get
Expand All @@ -59,13 +59,9 @@ public WaveFormScrollViewMode ScrollViewMode
_scrollViewMode = value;

if (_scrollViewMode == WaveFormScrollViewMode.Standard)
{
_viewCenterLine.Alpha = 0;
}
else if (_scrollViewMode == WaveFormScrollViewMode.SelectPosition)
{
_viewCenterLine.Alpha = 1;
}
}
}

Expand Down Expand Up @@ -144,7 +140,7 @@ private void Initialize()

_viewCenterLine = new UIView(new RectangleF(Bounds.Width / 2, 0, 1, Bounds.Height));
_viewCenterLine.BackgroundColor = GlobalTheme.LightColor;
_viewCenterLine.Alpha = 1;
_viewCenterLine.Alpha = 0;
AddSubview(_viewCenterLine);

ContentSize = WaveFormView.Bounds.Size;
Expand Down Expand Up @@ -192,31 +188,28 @@ public override void LayoutSubviews()
{
base.LayoutSubviews();

_lblZoom.Frame = new RectangleF(ContentOffset.X + ((Bounds.Width - 54) / 2), (Bounds.Height - 20) / 2, 54, 20);
_lblZoom.Frame = new RectangleF(ContentOffset.X + ((Bounds.Width - 70) / 2), (Bounds.Height - 20) / 2, 54, 20);
_viewCenterLine.Frame = new RectangleF(ContentOffset.X + (Bounds.Width / 2), 0, 1, Bounds.Height);
}

private void UpdateZoomScale(float offsetRatio)
{
var originalZoomScale = ZoomScale;
_zoomScale *= ZoomScale;
_zoomScale = (_zoomScale < MinimumZoomScale) ? MinimumZoomScale : _zoomScale;
_zoomScale = (_zoomScale > MaximumZoomScale) ? MaximumZoomScale : _zoomScale;
ZoomScale = 1.0f;
_lblZoom.Text = (_zoomScale * 100).ToString("0.0") + "%";
_lblZoom.Text = (_zoomScale * 100).ToString("0") + "%";
//Console.WriteLine("MPfmWaveFormScrollView - DidZoom ZoomScale: " + originalZoomScale.ToString() + " _zoomScale: " + _zoomScale.ToString());

if(ScrollViewMode == WaveFormScrollViewMode.Standard)
{
//WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
WaveFormScaleView.Frame = new RectangleF(WaveFormScaleView.Frame.X, WaveFormScaleView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, _scaleHeight);
ContentSize = new SizeF(WaveFormView.Frame.Width, Bounds.Height);
ContentOffset = new PointF(WaveFormView.Frame.Width * offsetRatio, 0);
}
else if(ScrollViewMode == WaveFormScrollViewMode.SelectPosition)
{
//WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
WaveFormScaleView.Frame = new RectangleF(WaveFormScaleView.Frame.X, WaveFormScaleView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, _scaleHeight);
ContentSize = new SizeF(WaveFormView.Frame.Width + Bounds.Width, Bounds.Height);
Expand Down Expand Up @@ -257,6 +250,100 @@ public void SetMarkers(IEnumerable<Marker> markers)
WaveFormView.SetMarkers(markers);
}

public void SetPosition(long position)
{
WaveFormView.Position = position;

// // Process autoscroll
// if (_zoomScale > 1)
// {
// float positionPercentage = (float)position / (float)WaveFormView.Length;
// float cursorX = (positionPercentage * WaveFormView.Bounds.Width);
// float scrollStartX = ContentOffset.X;
// float scrollCenterX = ContentOffset.X + Bounds.Width / 2;
// float scrollEndX = Bounds.Width + ContentOffset.X;
// Console.WriteLine("WaveFormScrollView - AutoScroll - positionPct: {0} cursorX: {1} contentOffset.X: {2} waveFormView.Width: {3} scrollStartX: {4} scrollCenterX: {5} scrollEndX: {6}", positionPercentage, cursorX, ContentOffset.X, WaveFormView.Bounds.Width, scrollStartX, scrollCenterX, scrollEndX);
//// if (cursorX > scrollEndX)
//// Console.WriteLine("WaveFormScrollView - Cursor is offscreen to the RIGHT!");
//// else if (cursorX < scrollStartX)
//// Console.WriteLine("WaveFormScrollView - Cursor is offscreen to the LEFT!");
//
// if (cursorX != scrollCenterX)
// {
// Console.WriteLine("WaveFormScrollView - Cursor isn't centered!");
//
// if (cursorX < scrollCenterX)
// {
// Console.WriteLine("WaveFormScrollView - Cursor isn't centered - The cursor is left of center X!");
// if (ContentOffset.X > (Bounds.Width / 2f))
// {
// //float newContentOffsetX = scrollCenterX - (Bounds.Width / 2f);
// float newContentOffsetX = cursorX - (Bounds.Width / 2f);
// Console.WriteLine("WaveFormScrollView - Cursor isn't centered - There is space on the left; AUTOCENTER! currentContentOffsetX: {0} newContentOffsetX: {1}", ContentOffset.X, newContentOffsetX);
// ContentOffset = new PointF(newContentOffsetX, 0);
// }
// }
// else if(cursorX > scrollCenterX)
// {
// Console.WriteLine("WaveFormScrollView - Cursor isn't centered - The cursor is right of center X!");
// if (ContentOffset.X < WaveFormView.Bounds.Width - Bounds.Width)
// {
// float newContentOffsetX = cursorX - (Bounds.Width / 2f);
// Console.WriteLine("WaveFormScrollView - Cursor isn't centered - There is space on the right; AUTOCENTER! currentContentOffsetX: {0} newContentOffsetX: {1}", ContentOffset.X, newContentOffsetX);
// ContentOffset = new PointF(newContentOffsetX, 0);
// }
// }
// }
// }
}

public void ShowSecondaryPosition(bool show)
{
WaveFormView.ShowSecondaryPosition = show;
}

public void SetSecondaryPosition(long position)
{
WaveFormView.SecondaryPosition = position;

// Process autoscroll
if (_zoomScale > 1)
{
float positionPercentage = (float)position / (float)WaveFormView.Length;
float cursorX = (positionPercentage * WaveFormView.Bounds.Width);
float scrollStartX = ContentOffset.X;
float scrollCenterX = ContentOffset.X + Bounds.Width / 2;
float scrollEndX = Bounds.Width + ContentOffset.X;
//Console.WriteLine("WaveFormScrollView - AutoScroll - positionPct: {0} cursorX: {1} contentOffset.X: {2} waveFormView.Width: {3} scrollStartX: {4} scrollCenterX: {5} scrollEndX: {6}", positionPercentage, cursorX, ContentOffset.X, WaveFormView.Bounds.Width, scrollStartX, scrollCenterX, scrollEndX);

if (cursorX != scrollCenterX)
{
if (cursorX < scrollCenterX)
{
//Console.WriteLine("WaveFormScrollView - Cursor isn't centered - The cursor is left of center X!");
if (ContentOffset.X >= 0)
{
float newContentOffsetX = cursorX - (Bounds.Width / 2f);
newContentOffsetX = newContentOffsetX < 0 ? 0 : newContentOffsetX;
//Console.WriteLine("WaveFormScrollView - Cursor isn't centered - There is space on the left; AUTOCENTER! currentContentOffsetX: {0} newContentOffsetX: {1}", ContentOffset.X, newContentOffsetX);
ContentOffset = new PointF(newContentOffsetX, 0);
}
}
else if(cursorX > scrollCenterX)
{
//Console.WriteLine("WaveFormScrollView - Cursor isn't centered - The cursor is right of center X!");
if (ContentOffset.X < WaveFormView.Bounds.Width - Bounds.Width)
{
float newContentOffsetX = cursorX - (Bounds.Width / 2f);
newContentOffsetX = newContentOffsetX > WaveFormView.Bounds.Width - Bounds.Width ? WaveFormView.Bounds.Width - Bounds.Width : newContentOffsetX;
//Console.WriteLine("WaveFormScrollView - Cursor isn't centered - There is space on the right; AUTOCENTER! currentContentOffsetX: {0} newContentOffsetX: {1}", ContentOffset.X, newContentOffsetX);
ContentOffset = new PointF(newContentOffsetX, 0);
}
}
}
}
}

public override void Draw(RectangleF rect)
{
var context = UIGraphics.GetCurrentContext();
Expand Down
24 changes: 12 additions & 12 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmWaveFormView.cs
Expand Up @@ -115,29 +115,29 @@ public long SecondaryPosition
}
}

private long _length;
public long Length
private bool _showSecondaryPosition = false;
public bool ShowSecondaryPosition
{
get
{
return _length;
return _showSecondaryPosition;
}
set
{
_length = value;
_showSecondaryPosition = value;
}
}

private float _scrollX;
public float ScrollX
private long _length;
public long Length
{
get
{
return _scrollX;
return _length;
}
set
{
_scrollX = value;
_length = value;
}
}

Expand Down Expand Up @@ -296,13 +296,13 @@ private void DrawWaveFormBitmap(CGContext context)

// Calculate position
float positionPercentage = (float)Position / (float)Length;
_cursorX = (positionPercentage * Bounds.Width) - ScrollX;
_cursorX = positionPercentage * Bounds.Width;

// Draw markers
for(int a = 0; a < _markers.Count; a++)
{
float xPct = (float)_markers[a].PositionBytes / (float)Length;
float x = (xPct * Bounds.Width) - ScrollX;
float x = xPct * Bounds.Width;

// Draw cursor line
context.SetStrokeColor(new CGColor(1, 0, 0, 1));
Expand All @@ -324,10 +324,10 @@ private void DrawWaveFormBitmap(CGContext context)
context.StrokeLineSegments(new PointF[2] { new PointF(_cursorX, 0), new PointF(_cursorX, heightAvailable) });

// Check if a secondary cursor must be drawn (i.e. when changing position)
if(_secondaryPosition > 0)
if(_showSecondaryPosition)
{
float secondaryPositionPercentage = (float)SecondaryPosition / (float)Length;
_secondaryCursorX = (secondaryPositionPercentage * Bounds.Width) - ScrollX;
_secondaryCursorX = secondaryPositionPercentage * Bounds.Width;

// Draw cursor line
context.SetStrokeColor(new CGColor(1, 1, 1, 1));
Expand Down

0 comments on commit a19ffa7

Please sign in to comment.