-
Notifications
You must be signed in to change notification settings - Fork 8
Spin() function added #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,9 @@ public partial class RotaryWheel : UserControl, INotifyPropertyChanged | |
| public Color BackgroundColor | ||
| { | ||
| get { return _backgroundColor; } | ||
| set { SetField(ref _backgroundColor, value); } | ||
| set { SetField(ref _backgroundColor, value); | ||
| Draw(); | ||
| } | ||
| } | ||
|
|
||
| private Color _foregroundColor = Colors.White; | ||
|
|
@@ -143,6 +145,52 @@ public RotaryWheel() | |
| }; | ||
| } | ||
|
|
||
| private void SpinTo(int itemIndex, int durationInSec = -1) | ||
| { | ||
| Random r = new Random(DateTime.Now.Millisecond); | ||
| var angleFromYAxis = 360 - Angle; | ||
| SelectedItem = _pieSlices | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you are getting the currently selected pie slice? Why not use the |
||
| .SingleOrDefault(p => p.StartAngle <= angleFromYAxis && (p.StartAngle + p.Angle) > angleFromYAxis); | ||
|
|
||
| int count = _pieSlices.Count; | ||
| int currIndex = _pieSlices.IndexOf(SelectedItem); | ||
| int fullSpin = itemIndex / count; | ||
| itemIndex = itemIndex % count; | ||
| int steps = currIndex-itemIndex; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider not modifying the
|
||
| if (steps < 0) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, can you add |
||
| steps = count + steps; | ||
|
|
||
| var startAngle = SelectedItem.StartAngle + SelectedItem.Angle / 2; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to rotate the pieslice at middle :) |
||
| var finalAngle = startAngle + fullSpin*360 + steps*360/count; | ||
|
|
||
| doubleAnimation.From = startAngle; | ||
| doubleAnimation.To = finalAngle; | ||
| if(durationInSec>0) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, can you add |
||
| doubleAnimation.Duration = new Windows.UI.Xaml.Duration(new TimeSpan(0, 0, durationInSec)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||
| else | ||
| doubleAnimation.Duration = new Windows.UI.Xaml.Duration(new TimeSpan(0, 0, r.Next(3, 6))); | ||
| storyBoard.Begin(); | ||
| storyBoard.Completed += StoryBoard_Completed; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please set the
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for |
||
| Angle = ((int)finalAngle) % 360; | ||
|
|
||
| } | ||
| /// <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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the |
||
| { | ||
| Random r = new Random(DateTime.Now.Millisecond); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's necessary to pass in a seed value, the default value should be sufficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if someone wants to spin more :)
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seed defines how the random numbers will be generated. As you are using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! yes, you're right. |
||
| int steps = r.Next(_pieSlices.Count, _pieSlices.Count* maxSpins); | ||
| SpinTo(steps,durationInSec); | ||
| } | ||
| 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(); | ||
|
|
@@ -210,4 +258,4 @@ private void SetField<T>(ref T field, T value, [CallerMemberName] string propert | |
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 fromINotifyPropertyChanged...There was a problem hiding this comment.
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