1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 06:02:56 +08:00
osu-lazer/osu.Game/Overlays/DirectOverlay.cs

309 lines
11 KiB
C#
Raw Normal View History

2017-05-24 13:41:51 +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
using System.Collections.Generic;
using System.Linq;
2017-10-25 18:43:14 +08:00
using System.Threading.Tasks;
2017-05-24 13:41:51 +08:00
using OpenTK;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
2017-05-24 13:41:51 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
2017-07-26 12:22:46 +08:00
using osu.Game.Beatmaps;
2017-05-24 13:41:51 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
2017-05-24 13:41:51 +08:00
using osu.Game.Overlays.Direct;
2017-05-26 13:44:09 +08:00
using osu.Game.Overlays.SearchableList;
2017-07-26 12:22:46 +08:00
using osu.Game.Rulesets;
using OpenTK.Graphics;
2017-05-24 13:41:51 +08:00
namespace osu.Game.Overlays
2017-05-24 13:41:51 +08:00
{
public class DirectOverlay : SearchableListOverlay<DirectTab, DirectSortCriteria, RankStatus>
2017-05-24 13:41:51 +08:00
{
private const float panel_padding = 10f;
private APIAccess api;
private RulesetStore rulesets;
private BeatmapManager beatmaps;
2017-05-24 13:41:51 +08:00
private readonly FillFlowContainer resultCountsContainer;
private readonly OsuSpriteText resultCountsText;
private FillFlowContainer<DirectPanel> panels;
2017-09-18 04:39:34 +08:00
private DirectPanel playing;
2017-05-24 13:41:51 +08:00
protected override Color4 BackgroundColour => OsuColour.FromHex(@"485e74");
protected override Color4 TrianglesColourLight => OsuColour.FromHex(@"465b71");
protected override Color4 TrianglesColourDark => OsuColour.FromHex(@"3f5265");
2017-05-26 13:44:09 +08:00
protected override SearchableListHeader<DirectTab> CreateHeader() => new Header();
protected override SearchableListFilterControl<DirectSortCriteria, RankStatus> CreateFilterControl() => new FilterControl();
2017-05-24 13:41:51 +08:00
private IEnumerable<BeatmapSetInfo> beatmapSets;
public IEnumerable<BeatmapSetInfo> BeatmapSets
{
get { return beatmapSets; }
set
{
if (beatmapSets?.Equals(value) ?? false) return;
2017-05-24 13:41:51 +08:00
beatmapSets = value;
if (beatmapSets == null) return;
var artists = new List<string>();
var songs = new List<string>();
var tags = new List<string>();
foreach (var s in beatmapSets)
{
artists.Add(s.Metadata.Artist);
songs.Add(s.Metadata.Title);
tags.AddRange(s.Metadata.Tags.Split(' '));
}
ResultAmounts = new ResultCounts(distinctCount(artists), distinctCount(songs), distinctCount(tags));
2017-09-09 02:47:38 +08:00
2017-10-25 18:43:14 +08:00
recreatePanels(Filter.DisplayStyleControl.DisplayStyle.Value);
2017-05-24 13:41:51 +08:00
}
}
private ResultCounts resultAmounts;
public ResultCounts ResultAmounts
{
get { return resultAmounts; }
set
{
if (value == ResultAmounts) return;
resultAmounts = value;
updateResultCounts();
}
}
public DirectOverlay()
{
RelativeSizeAxes = Axes.Both;
// osu!direct colours are not part of the standard palette
FirstWaveColour = OsuColour.FromHex(@"19b0e2");
SecondWaveColour = OsuColour.FromHex(@"2280a2");
ThirdWaveColour = OsuColour.FromHex(@"005774");
FourthWaveColour = OsuColour.FromHex(@"003a4e");
ScrollFlow.Children = new Drawable[]
2017-05-24 13:41:51 +08:00
{
resultCountsContainer = new FillFlowContainer
2017-05-24 13:41:51 +08:00
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Margin = new MarginPadding { Top = 5 },
Children = new Drawable[]
2017-05-24 13:41:51 +08:00
{
new OsuSpriteText
2017-05-24 13:41:51 +08:00
{
Text = "Found ",
TextSize = 15,
2017-05-24 13:41:51 +08:00
},
resultCountsText = new OsuSpriteText
2017-05-24 13:41:51 +08:00
{
TextSize = 15,
Font = @"Exo2.0-Bold",
2017-05-24 13:41:51 +08:00
},
}
2017-05-24 13:41:51 +08:00
},
};
2017-06-08 17:47:21 +08:00
Filter.Search.Current.ValueChanged += text => { if (text != string.Empty) Header.Tabs.Current.Value = DirectTab.Search; };
((FilterControl)Filter).Ruleset.ValueChanged += ruleset => Scheduler.AddOnce(updateSearch);
2017-06-08 17:47:21 +08:00
Filter.DisplayStyleControl.DisplayStyle.ValueChanged += recreatePanels;
Filter.DisplayStyleControl.Dropdown.Current.ValueChanged += rankStatus => Scheduler.AddOnce(updateSearch);
2017-06-08 17:47:21 +08:00
2017-06-08 17:21:45 +08:00
Header.Tabs.Current.ValueChanged += tab =>
{
if (tab != DirectTab.Search)
{
currentQuery.Value = string.Empty;
2017-06-08 17:21:45 +08:00
Filter.Tabs.Current.Value = (DirectSortCriteria)Header.Tabs.Current.Value;
Scheduler.AddOnce(updateSearch);
2017-06-08 17:21:45 +08:00
}
};
2017-06-08 17:47:21 +08:00
currentQuery.ValueChanged += v =>
{
queryChangedDebounce?.Cancel();
if (string.IsNullOrEmpty(v))
Scheduler.AddOnce(updateSearch);
else
{
BeatmapSets = null;
ResultAmounts = null;
queryChangedDebounce = Scheduler.AddDelayed(updateSearch, 500);
}
};
2017-06-08 17:47:21 +08:00
currentQuery.BindTo(Filter.Search.Current);
2017-06-08 17:21:45 +08:00
Filter.Tabs.Current.ValueChanged += sortCriteria =>
{
if (Header.Tabs.Current.Value != DirectTab.Search && sortCriteria != (DirectSortCriteria)Header.Tabs.Current.Value)
Header.Tabs.Current.Value = DirectTab.Search;
Scheduler.AddOnce(updateSearch);
2017-06-08 17:21:45 +08:00
};
2017-05-24 13:41:51 +08:00
updateResultCounts();
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, APIAccess api, RulesetStore rulesets, BeatmapManager beatmaps)
2017-05-24 13:41:51 +08:00
{
this.api = api;
this.rulesets = rulesets;
this.beatmaps = beatmaps;
2017-05-24 13:41:51 +08:00
resultCountsContainer.Colour = colours.Yellow;
beatmaps.BeatmapSetAdded += setAdded;
}
private void setAdded(BeatmapSetInfo set)
{
// if a new map was imported, we should remove it from search results (download completed etc.)
panels?.FirstOrDefault(p => p.SetInfo.OnlineBeatmapSetID == set.OnlineBeatmapSetID)?.FadeOut(400).Expire();
2017-09-09 12:55:28 +08:00
BeatmapSets = BeatmapSets?.Where(b => b.OnlineBeatmapSetID != set.OnlineBeatmapSetID);
2017-05-24 13:41:51 +08:00
}
private void updateResultCounts()
{
2017-07-23 02:50:25 +08:00
resultCountsContainer.FadeTo(ResultAmounts == null ? 0f : 1f, 200, Easing.OutQuint);
2017-05-24 13:41:51 +08:00
if (ResultAmounts == null) return;
resultCountsText.Text = pluralize("Artist", ResultAmounts.Artists) + ", " +
pluralize("Song", ResultAmounts.Songs) + ", " +
pluralize("Tag", ResultAmounts.Tags);
}
private string pluralize(string prefix, int value)
{
return $@"{value} {prefix}" + (value == 1 ? string.Empty : @"s");
}
private void recreatePanels(PanelDisplayStyle displayStyle)
{
if (panels != null)
{
panels.FadeOut(200);
panels.Expire();
panels = null;
2017-09-18 04:39:34 +08:00
if (playing != null)
{
playing.PreviewPlaying.Value = false;
playing = null;
}
}
2017-05-24 13:41:51 +08:00
if (BeatmapSets == null) return;
var newPanels = new FillFlowContainer<DirectPanel>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(panel_padding),
Margin = new MarginPadding { Top = 10 },
ChildrenEnumerable = BeatmapSets.Select<BeatmapSetInfo, DirectPanel>(b =>
{
switch (displayStyle)
{
case PanelDisplayStyle.Grid:
return new DirectGridPanel(b);
default:
return new DirectListPanel(b);
}
})
};
LoadComponentAsync(newPanels, p =>
{
if (panels != null) ScrollFlow.Remove(panels);
ScrollFlow.Add(panels = newPanels);
foreach (DirectPanel panel in p.Children)
panel.PreviewPlaying.ValueChanged += newValue =>
{
if (newValue)
{
if (playing != null && playing != panel)
playing.PreviewPlaying.Value = false;
playing = panel;
}
};
});
2017-05-24 13:41:51 +08:00
}
private SearchBeatmapSetsRequest getSetsRequest;
private readonly Bindable<string> currentQuery = new Bindable<string>();
private ScheduledDelegate queryChangedDebounce;
private void updateSearch()
{
queryChangedDebounce?.Cancel();
if (!IsLoaded) return;
BeatmapSets = null;
2017-06-07 22:30:52 +08:00
ResultAmounts = null;
getSetsRequest?.Cancel();
if (api == null) return;
if (Header.Tabs.Current.Value == DirectTab.Search && (Filter.Search.Text == string.Empty || currentQuery == string.Empty)) return;
getSetsRequest = new SearchBeatmapSetsRequest(currentQuery.Value ?? string.Empty,
2017-06-07 22:00:05 +08:00
((FilterControl)Filter).Ruleset.Value,
Filter.DisplayStyleControl.Dropdown.Current.Value,
Filter.Tabs.Current.Value); //todo: sort direction (?)
2017-06-07 22:00:05 +08:00
getSetsRequest.Success += response =>
2017-06-07 22:30:52 +08:00
{
2017-10-25 18:43:14 +08:00
Task.Run(() =>
{
var onlineIds = response.Select(r => r.OnlineBeatmapSetID).ToList();
var presentOnlineIds = beatmaps.QueryBeatmapSets(s => onlineIds.Contains(s.OnlineBeatmapSetID)).Select(r => r.OnlineBeatmapSetID).ToList();
var sets = response.Select(r => r.ToBeatmapSet(rulesets)).Where(b => !presentOnlineIds.Contains(b.OnlineBeatmapSetID)).ToList();
2017-10-25 18:43:14 +08:00
// may not need scheduling; loads async internally.
Schedule(() => BeatmapSets = sets);
});
2017-06-07 22:30:52 +08:00
};
api.Queue(getSetsRequest);
}
2017-06-07 22:30:52 +08:00
private int distinctCount(List<string> list) => list.Distinct().ToArray().Length;
2017-05-24 13:41:51 +08:00
public class ResultCounts
{
public readonly int Artists;
public readonly int Songs;
public readonly int Tags;
public ResultCounts(int artists, int songs, int tags)
{
Artists = artists;
Songs = songs;
Tags = tags;
}
}
}
}