Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions RotaryWheelControl/RotaryWheel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public partial class RotaryWheel : UserControl, INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;


private Color _backgroundColor = Colors.Black;
public Color BackgroundColor
{
get { return _backgroundColor; }
set { SetField(ref _backgroundColor, value); }
set { SetField(ref _backgroundColor, value);
Draw();
Copy link
Copy Markdown
Owner

@jpoon jpoon Jul 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to add the Draw()? Setting a new color should automatically be handled as this class inherits from INotifyPropertyChanged...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i've experienced that without Draw() function call, BackgroundColor doesnot changes instantly

}
}

private Color _foregroundColor = Colors.White;
Expand Down Expand Up @@ -80,7 +83,8 @@ public bool HideLabels
}
}
}



public string SelectedItemValue
{
get { return _selectedItem?.Label; }
Expand Down Expand Up @@ -142,7 +146,58 @@ public RotaryWheel()
}
};
}
/// <summary>
/// Spins the wheel randomly.
/// </summary>
/// <param name="maxSpins">Maximum no. of spins or revolutions.</param>
/// <param name="durationInSec">Spin duration in Second. [-1 denotes random duration]</param>
public void Spin(int maxSpins=5, int durationInSec = -1)
{
Random r = new Random();
int steps = r.Next(_pieSlices.Count, _pieSlices.Count* maxSpins);
SpinTo(steps,durationInSec);
}
private void SpinTo(int itemIndex, int durationInSec = -1)
{
Random r = new Random();
var angleFromYAxis = 360 - Angle;
SelectedItem = _pieSlices
.SingleOrDefault(p => p.StartAngle <= angleFromYAxis && (p.StartAngle + p.Angle) > angleFromYAxis);

int count = _pieSlices.Count;
int currIndex = _pieSlices.IndexOf(SelectedItem);
int fullSpin = itemIndex / count;
int steps = currIndex- (itemIndex % count);
if (steps < 0)
{
steps = count + steps;
}

var startAngle = SelectedItem.StartAngle + SelectedItem.Angle / 2;
var finalAngle = startAngle + fullSpin*360 + steps*360/count;

doubleAnimation.From = startAngle;
doubleAnimation.To = finalAngle;
if(durationInSec>0)
{
doubleAnimation.Duration = new Windows.UI.Xaml.Duration(new TimeSpan(0, 0, durationInSec));
}
else
{
doubleAnimation.Duration = new Windows.UI.Xaml.Duration(new TimeSpan(0, 0, r.Next(3, 6)));
}
storyBoard.Begin();
storyBoard.Completed += StoryBoard_Completed;
Angle = ((int)finalAngle) % 360;

}

private void StoryBoard_Completed(object sender, object e)
{
var angleFromYAxis = 360 - Angle;
SelectedItem = _pieSlices
.SingleOrDefault(p => p.StartAngle <= angleFromYAxis && (p.StartAngle + p.Angle) > angleFromYAxis);
}
private void Draw()
{
_pieSlices.Clear();
Expand Down Expand Up @@ -210,4 +265,4 @@ private void SetField<T>(ref T field, T value, [CallerMemberName] string propert
}
}
}
}
}