2017-03-03 12:17:24 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
2017-03-03 19:08:32 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-03-09 14:52:40 +08:00
|
|
|
|
using System.Diagnostics;
|
2017-03-03 19:08:32 +08:00
|
|
|
|
using System.Linq;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
2017-03-03 19:08:32 +08:00
|
|
|
|
using osu.Framework.Graphics.Colour;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
|
|
|
|
using osu.Framework.Threading;
|
2017-03-03 19:42:22 +08:00
|
|
|
|
using osu.Game.Screens.Tournament.Teams;
|
2017-03-03 19:08:32 +08:00
|
|
|
|
using OpenTK;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Tournament
|
|
|
|
|
{
|
|
|
|
|
public class ScrollingTeamContainer : Container
|
|
|
|
|
{
|
2017-02-27 21:50:34 +08:00
|
|
|
|
public event Action OnScrollStarted;
|
2017-03-03 13:51:19 +08:00
|
|
|
|
public event Action<Team> OnSelected;
|
2017-02-27 21:50:34 +08:00
|
|
|
|
|
|
|
|
|
private readonly List<Team> availableTeams = new List<Team>();
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
|
|
|
|
private Container tracker;
|
|
|
|
|
|
2017-03-03 19:08:32 +08:00
|
|
|
|
private float speed;
|
|
|
|
|
private int expiredCount;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
|
|
|
|
private float offset;
|
|
|
|
|
private float timeOffset;
|
|
|
|
|
private float leftPos => offset + timeOffset + expiredCount * ScrollingTeam.WIDTH;
|
|
|
|
|
|
|
|
|
|
private double lastTime;
|
|
|
|
|
|
2017-03-03 12:01:57 +08:00
|
|
|
|
private ScheduledDelegate delayedStateChangeDelegate;
|
2017-02-27 21:50:34 +08:00
|
|
|
|
|
|
|
|
|
public ScrollingTeamContainer()
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-03-03 19:08:32 +08:00
|
|
|
|
tracker = new Container
|
2017-02-27 21:50:34 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
|
|
|
|
|
Masking = true,
|
|
|
|
|
CornerRadius = 10f,
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
2017-03-03 19:08:32 +08:00
|
|
|
|
new Box
|
2017-02-27 21:50:34 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.BottomCentre,
|
|
|
|
|
Size = new Vector2(2, 55),
|
|
|
|
|
|
|
|
|
|
ColourInfo = ColourInfo.GradientVertical(Color4.Transparent, Color4.White)
|
|
|
|
|
},
|
2017-03-03 19:08:32 +08:00
|
|
|
|
new Box
|
2017-02-27 21:50:34 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Size = new Vector2(2, 55),
|
|
|
|
|
|
|
|
|
|
ColourInfo = ColourInfo.GradientVertical(Color4.White, Color4.Transparent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
private ScrollState _scrollState;
|
|
|
|
|
private ScrollState scrollState
|
|
|
|
|
{
|
|
|
|
|
get { return _scrollState; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-03-03 16:59:16 +08:00
|
|
|
|
if (_scrollState == value)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
_scrollState = value;
|
|
|
|
|
|
2017-03-03 12:01:57 +08:00
|
|
|
|
delayedStateChangeDelegate?.Cancel();
|
2017-03-02 11:26:42 +08:00
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case ScrollState.Scrolling:
|
|
|
|
|
resetSelected();
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
OnScrollStarted?.Invoke();
|
|
|
|
|
|
|
|
|
|
speedTo(1000f, 200);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
tracker.FadeOut(100);
|
|
|
|
|
break;
|
|
|
|
|
case ScrollState.Stopping:
|
|
|
|
|
speedTo(0f, 2000);
|
|
|
|
|
tracker.FadeIn(200);
|
|
|
|
|
|
2017-03-03 12:01:57 +08:00
|
|
|
|
delayedStateChangeDelegate = Delay(2300).Schedule(() => scrollState = ScrollState.Stopped);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
break;
|
|
|
|
|
case ScrollState.Stopped:
|
|
|
|
|
// Find closest to center
|
2017-03-09 14:52:40 +08:00
|
|
|
|
if (!Children.Any())
|
|
|
|
|
break;
|
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
Drawable closest = null;
|
2017-03-09 14:52:40 +08:00
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
foreach (var c in Children)
|
|
|
|
|
{
|
|
|
|
|
if (!(c is ScrollingTeam))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (closest == null)
|
|
|
|
|
{
|
|
|
|
|
closest = c;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-06 17:49:23 +08:00
|
|
|
|
float o = Math.Abs(c.Position.X + c.DrawWidth / 2f - DrawWidth / 2f);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
float lastOffset = Math.Abs(closest.Position.X + closest.DrawWidth / 2f - DrawWidth / 2f);
|
|
|
|
|
|
2017-03-06 17:49:23 +08:00
|
|
|
|
if (o < lastOffset)
|
2017-02-27 14:02:38 +08:00
|
|
|
|
closest = c;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 14:52:40 +08:00
|
|
|
|
Trace.Assert(closest != null, "closest != null");
|
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
offset += DrawWidth / 2f - (closest.Position.X + closest.DrawWidth / 2f);
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
ScrollingTeam st = closest as ScrollingTeam;
|
|
|
|
|
|
|
|
|
|
availableTeams.RemoveAll(at => at == st.Team);
|
|
|
|
|
|
|
|
|
|
st.Selected = true;
|
2017-03-03 13:51:19 +08:00
|
|
|
|
OnSelected?.Invoke(st.Team);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-03-03 12:01:57 +08:00
|
|
|
|
delayedStateChangeDelegate = Delay(10000).Schedule(() => scrollState = ScrollState.Idle);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
break;
|
|
|
|
|
case ScrollState.Idle:
|
|
|
|
|
resetSelected();
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
OnScrollStarted?.Invoke();
|
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
speedTo(40f, 200);
|
|
|
|
|
tracker.FadeOut(100);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
public void AddTeam(Team team)
|
2017-02-27 14:02:38 +08:00
|
|
|
|
{
|
2017-02-27 21:50:34 +08:00
|
|
|
|
if (availableTeams.Contains(team))
|
|
|
|
|
return;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
availableTeams.Add(team);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
RemoveAll(c => c is ScrollingTeam);
|
|
|
|
|
scrollState = ScrollState.Idle;
|
|
|
|
|
}
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-03-03 14:59:33 +08:00
|
|
|
|
public void AddTeams(IEnumerable<Team> teams)
|
|
|
|
|
{
|
|
|
|
|
if (teams == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (Team t in teams)
|
|
|
|
|
AddTeam(t);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
public void ClearTeams()
|
|
|
|
|
{
|
|
|
|
|
availableTeams.Clear();
|
|
|
|
|
RemoveAll(c => c is ScrollingTeam);
|
|
|
|
|
scrollState = ScrollState.Idle;
|
|
|
|
|
}
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
public void RemoveTeam(Team team)
|
|
|
|
|
{
|
2017-03-02 11:02:50 +08:00
|
|
|
|
availableTeams.Remove(team);
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
foreach (var c in Children)
|
|
|
|
|
{
|
|
|
|
|
ScrollingTeam st = c as ScrollingTeam;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
if (st == null)
|
|
|
|
|
continue;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
if (st.Team == team)
|
|
|
|
|
{
|
|
|
|
|
st.FadeOut(200);
|
|
|
|
|
st.Expire();
|
2017-02-27 14:02:38 +08:00
|
|
|
|
}
|
2017-02-27 21:50:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
public void StartScrolling()
|
|
|
|
|
{
|
2017-03-02 11:02:50 +08:00
|
|
|
|
if (availableTeams.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
scrollState = ScrollState.Scrolling;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopScrolling()
|
|
|
|
|
{
|
2017-03-02 11:02:50 +08:00
|
|
|
|
if (availableTeams.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-03-03 19:07:57 +08:00
|
|
|
|
switch (scrollState)
|
|
|
|
|
{
|
|
|
|
|
case ScrollState.Stopped:
|
|
|
|
|
case ScrollState.Idle:
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-03 17:45:04 +08:00
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
scrollState = ScrollState.Stopping;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2017-02-27 14:02:38 +08:00
|
|
|
|
scrollState = ScrollState.Idle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
timeOffset -= (float)(Time.Current - lastTime) / 1000 * speed;
|
|
|
|
|
lastTime = Time.Current;
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
if (availableTeams.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// Fill more than required to account for transformation + scrolling speed
|
|
|
|
|
while (Children.Count(c => c is ScrollingTeam) < DrawWidth * 2 / ScrollingTeam.WIDTH)
|
|
|
|
|
addFlags();
|
|
|
|
|
}
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
|
|
|
|
float pos = leftPos;
|
|
|
|
|
|
|
|
|
|
foreach (var c in Children)
|
|
|
|
|
{
|
|
|
|
|
if (!(c is ScrollingTeam))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (c.Position.X + c.DrawWidth < 0)
|
|
|
|
|
{
|
|
|
|
|
c.ClearTransforms();
|
|
|
|
|
c.Expire();
|
|
|
|
|
expiredCount++;
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-02-27 21:50:34 +08:00
|
|
|
|
{
|
2017-02-27 14:02:38 +08:00
|
|
|
|
c.MoveToX(pos, 100);
|
2017-02-27 21:50:34 +08:00
|
|
|
|
c.FadeTo(1.0f - Math.Abs(pos - DrawWidth / 2f) / (DrawWidth / 2.5f), 100);
|
|
|
|
|
}
|
2017-02-27 14:02:38 +08:00
|
|
|
|
|
|
|
|
|
pos += ScrollingTeam.WIDTH;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addFlags()
|
|
|
|
|
{
|
2017-03-09 13:24:16 +08:00
|
|
|
|
foreach (Team t in availableTeams)
|
2017-02-27 14:02:38 +08:00
|
|
|
|
{
|
2017-03-09 13:24:16 +08:00
|
|
|
|
Add(new ScrollingTeam(t)
|
2017-02-27 14:02:38 +08:00
|
|
|
|
{
|
|
|
|
|
X = leftPos + DrawWidth
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resetSelected()
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in Children)
|
|
|
|
|
{
|
|
|
|
|
ScrollingTeam st = c as ScrollingTeam;
|
|
|
|
|
if (st == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (st.Selected)
|
|
|
|
|
{
|
2017-02-27 21:50:34 +08:00
|
|
|
|
st.Selected = false;
|
|
|
|
|
RemoveTeam(st.Team);
|
2017-02-27 14:02:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void speedTo(float value, double duration = 0, EasingTypes easing = EasingTypes.None)
|
|
|
|
|
{
|
|
|
|
|
DelayReset();
|
|
|
|
|
|
|
|
|
|
UpdateTransformsOfType(typeof(TransformScrollSpeed));
|
|
|
|
|
TransformFloatTo(speed, value, duration, easing, new TransformScrollSpeed());
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 09:59:19 +08:00
|
|
|
|
private enum ScrollState
|
2017-02-27 14:02:38 +08:00
|
|
|
|
{
|
2017-03-03 17:45:04 +08:00
|
|
|
|
None,
|
2017-02-27 14:02:38 +08:00
|
|
|
|
Idle,
|
|
|
|
|
Stopping,
|
|
|
|
|
Stopped,
|
|
|
|
|
Scrolling
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class TransformScrollSpeed : TransformFloat
|
|
|
|
|
{
|
|
|
|
|
public override void Apply(Drawable d)
|
|
|
|
|
{
|
|
|
|
|
base.Apply(d);
|
2017-03-09 14:52:40 +08:00
|
|
|
|
((ScrollingTeamContainer)d).speed = CurrentValue;
|
2017-02-27 14:02:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ScrollingTeam : Container
|
|
|
|
|
{
|
|
|
|
|
public const float WIDTH = 58;
|
|
|
|
|
public const float HEIGHT = 41;
|
|
|
|
|
|
|
|
|
|
public Team Team;
|
|
|
|
|
|
|
|
|
|
private Sprite flagSprite;
|
|
|
|
|
private Box outline;
|
|
|
|
|
|
|
|
|
|
private bool selected;
|
|
|
|
|
public bool Selected
|
|
|
|
|
{
|
|
|
|
|
get { return selected; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
selected = value;
|
|
|
|
|
|
|
|
|
|
if (selected)
|
|
|
|
|
outline.FadeIn(100);
|
|
|
|
|
else
|
|
|
|
|
outline.FadeOut(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ScrollingTeam(Team team)
|
|
|
|
|
{
|
|
|
|
|
Team = team;
|
|
|
|
|
|
|
|
|
|
Anchor = Anchor.CentreLeft;
|
|
|
|
|
Origin = Anchor.CentreLeft;
|
|
|
|
|
|
|
|
|
|
Size = new Vector2(WIDTH, HEIGHT);
|
|
|
|
|
Masking = true;
|
|
|
|
|
CornerRadius = 8f;
|
|
|
|
|
|
2017-02-27 21:50:34 +08:00
|
|
|
|
Alpha = 0;
|
|
|
|
|
|
2017-02-27 14:02:38 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2017-03-03 19:08:32 +08:00
|
|
|
|
outline = new Box
|
2017-02-27 14:02:38 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Alpha = 0
|
|
|
|
|
},
|
2017-03-03 19:08:32 +08:00
|
|
|
|
flagSprite = new Sprite
|
2017-02-27 14:02:38 +08:00
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
|
|
|
|
|
Size = new Vector2(WIDTH, HEIGHT) - new Vector2(8)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(TextureStore textures)
|
|
|
|
|
{
|
|
|
|
|
flagSprite.Texture = textures.Get($@"Flags/{Team.FlagName}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|