1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 02:22:59 +08:00

Refactor + Stars Counter (initial)

Moved a few things to allow using common transforms
for a star counter. This implementation is basic and
hacky, but good enough as proof of concept.
This commit is contained in:
Adonais Romero González 2016-10-08 19:11:01 -05:00
parent a3b4a34a1a
commit 5ebb2fc289
8 changed files with 210 additions and 45 deletions

View File

@ -87,6 +87,14 @@ namespace osu.Desktop.Tests
};
Add(pc);
StarCounter tc = new StarCounter
{
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Position = new Vector2(20, 160),
};
Add(tc);
AddButton(@"Reset all", delegate
{
uc.Count = 0;
@ -119,6 +127,11 @@ namespace osu.Desktop.Tests
pc.Denominator++;
});
AddButton(@"Alter stars", delegate
{
tc.Count = RNG.NextSingle() * tc.MaxStars;
});
AddButton(@"Stop counters", delegate
{
uc.StopRolling();
@ -126,6 +139,7 @@ namespace osu.Desktop.Tests
cc.StopRolling();
ac.StopRolling();
pc.StopRolling();
tc.StopRolling();
});
}
}

View File

@ -16,7 +16,7 @@ namespace osu.Game.Graphics.UserInterface
/// <summary>
/// Used as an accuracy counter. Represented visually as a percentage, internally as a fraction.
/// </summary>
public class AccuracyCounter : RollingCounter<float>
public class AccuracyCounter : NumericRollingCounter<float>
{
protected override Type transformType => typeof(TransformAccuracy);

View File

@ -19,7 +19,7 @@ namespace osu.Game.Graphics.UserInterface
{
public Color4 OriginalColour;
public Color4 TintColour = Color4.OrangeRed;
public int TintDuration = 500;
public int TintDuration = 250;
public float ScaleFactor = 2;
public EasingTypes TintEasing = EasingTypes.None;
public bool CanAnimateWhenBackwards = false;

View File

@ -0,0 +1,64 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// Skeleton for a numeric counter with a simple roll-up animation.
/// </summary>
/// <typeparam name="T">Type of the actual counter.</typeparam>
public abstract class NumericRollingCounter<T> : RollingCounter<T>
{
protected SpriteText countSpriteText;
protected float textSize = 20.0f;
public float TextSize
{
get { return textSize; }
set
{
textSize = value;
updateTextSize();
}
}
public override void Load()
{
base.Load();
Children = new Drawable[]
{
countSpriteText = new SpriteText
{
Text = formatCount(Count),
TextSize = this.TextSize,
Anchor = this.Anchor,
Origin = this.Origin,
},
};
}
protected override void transformVisibleCount(T currentValue, T newValue)
{
if (countSpriteText != null)
{
countSpriteText.Text = formatCount(newValue);
}
}
protected virtual void updateTextSize()
{
if (countSpriteText != null)
countSpriteText.TextSize = TextSize;
}
}
}

View File

@ -1,9 +1,5 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using System;
using System.Collections.Generic;
@ -15,8 +11,12 @@ using System.Threading.Tasks;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// Skeleton for a counter with a simple roll-up animation.
/// Skeleton for a counter which value rolls-up in a lapse of time.
/// </summary>
/// <remarks>
/// This class only abstracts the basics to roll-up a value in a lapse of time by using Transforms.
/// In order to show a value, you must implement a way to display it, i.e., as a numeric counter or a bar.
/// </remarks>
/// <typeparam name="T">Type of the actual counter.</typeparam>
public abstract class RollingCounter<T> : Container
{
@ -28,20 +28,8 @@ namespace osu.Game.Graphics.UserInterface
/// </remarks>
protected virtual Type transformType => typeof(Transform<T>);
protected SpriteText countSpriteText;
protected ulong RollingTotalDuration = 0;
protected float textSize = 20.0f;
public float TextSize
{
get { return textSize; }
set
{
textSize = value;
updateTextSize();
}
}
/// <summary>
/// If true, each time the Count is updated, it will roll over from the current visible value.
/// Else, it will roll up from the current count value.
@ -121,16 +109,6 @@ namespace osu.Game.Graphics.UserInterface
if (Count == null)
ResetCount();
VisibleCount = Count;
Children = new Drawable[]
{
countSpriteText = new SpriteText
{
Text = formatCount(Count),
TextSize = this.TextSize,
Anchor = this.Anchor,
Origin = this.Origin,
},
};
}
/// <summary>
@ -250,18 +228,6 @@ namespace osu.Game.Graphics.UserInterface
/// </summary>
/// <param name="currentValue">Visible count value before modification.</param>
/// <param name="newValue">Expected visible count value after modification-</param>
protected virtual void transformVisibleCount(T currentValue, T newValue)
{
if (countSpriteText != null)
{
countSpriteText.Text = formatCount(newValue);
}
}
protected virtual void updateTextSize()
{
if (countSpriteText != null)
countSpriteText.TextSize = TextSize;
}
protected abstract void transformVisibleCount(T currentValue, T newValue);
}
}

View File

@ -0,0 +1,119 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Transformations;
using osu.Framework.MathUtils;
using osu.Framework.Timing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// Shows a float count as stars. Used as star difficulty display.
/// </summary>
public class StarCounter : RollingCounter<float>
{
protected override Type transformType => typeof(TransformStar);
protected float MinStarSize = 0.001f;
protected FlowContainer starContainer;
protected List<TextAwesome> stars = new List<TextAwesome>();
public int MaxStars = 10;
public StarCounter() : base()
{
RollingDuration = 5000;
}
public override void ResetCount()
{
Count = 0;
StopRolling();
}
public override void Load()
{
base.Load();
Children = new Drawable[]
{
starContainer = new FlowContainer
{
Direction = FlowDirection.HorizontalOnly,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
}
};
for (int i = 0; i < MaxStars; i++)
{
TextAwesome star = new TextAwesome
{
Icon = FontAwesome.star,
Origin = Anchor.Centre,
TextSize = 20,
};
stars.Add(star);
starContainer.Add(star);
}
// HACK: To mantain container height constant
starContainer.Add(new TextAwesome
{
Icon = FontAwesome.star,
Origin = Anchor.Centre,
TextSize = 20,
Alpha = 0.002f,
});
ResetCount();
}
protected override void transformVisibleCount(float currentValue, float newValue)
{
for (int i = 0; i < MaxStars; i++)
{
if (newValue < i)
stars[i].ScaleTo(MinStarSize);
else if (newValue > (i + 1))
stars[i].ScaleTo(1f);
else
stars[i].ScaleTo(Interpolation.ValueAt(newValue, MinStarSize, 1f, i, i + 1, EasingTypes.None));
}
}
protected class TransformStar : Transform<float>
{
public override float CurrentValue
{
get
{
double time = Time;
if (time < StartTime) return StartValue;
if (time >= EndTime) return EndValue;
return Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
}
}
public override void Apply(Drawable d)
{
base.Apply(d);
(d as StarCounter).VisibleCount = CurrentValue;
}
public TransformStar(IClock clock)
: base(clock)
{
}
}
}
}

View File

@ -16,7 +16,7 @@ namespace osu.Game.Graphics.UserInterface
/// <summary>
/// A simple rolling counter that accepts unsigned long values.
/// </summary>
public class ULongCounter : RollingCounter<ulong>
public class ULongCounter : NumericRollingCounter<ulong>
{
protected override Type transformType => typeof(TransformULongCounter);

View File

@ -102,15 +102,17 @@
<Compile Include="Graphics\Processing\RatioAdjust.cs" />
<Compile Include="Graphics\TextAwesome.cs" />
<Compile Include="Graphics\UserInterface\AlternativeComboCounter.cs" />
<Compile Include="Graphics\UserInterface\RollingCounter.cs" />
<Compile Include="Graphics\UserInterface\KeyCounter.cs" />
<Compile Include="Graphics\UserInterface\KeyCounterKeyboard.cs" />
<Compile Include="Graphics\UserInterface\KeyCounterCollection.cs" />
<Compile Include="Graphics\UserInterface\KeyCounterMouse.cs" />
<Compile Include="Graphics\UserInterface\AccuracyCounter.cs" />
<Compile Include="Graphics\UserInterface\RollingCounter.cs" />
<Compile Include="Graphics\UserInterface\NumericRollingCounter.cs" />
<Compile Include="Graphics\UserInterface\ScoreCounter.cs" />
<Compile Include="Graphics\UserInterface\CatchComboCounter.cs" />
<Compile Include="Graphics\UserInterface\StandardComboCounter.cs" />
<Compile Include="Graphics\UserInterface\StarCounter.cs" />
<Compile Include="Graphics\UserInterface\ULongCounter.cs" />
<Compile Include="Online\API\APIAccess.cs" />
<Compile Include="Online\API\APIRequest.cs" />