2019-01-24 16:43:03 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2018-12-11 18:07:40 +08:00
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Online.Multiplayer;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Multi.Lounge.Components
|
|
|
|
{
|
2018-12-25 10:58:28 +08:00
|
|
|
public class RoomsContainer : CompositeDrawable
|
2018-12-11 18:07:40 +08:00
|
|
|
{
|
2018-12-20 19:58:34 +08:00
|
|
|
public Action<Room> JoinRequested;
|
|
|
|
|
2019-01-07 17:50:27 +08:00
|
|
|
private readonly IBindableList<Room> rooms = new BindableList<Room>();
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
private readonly FillFlowContainer<DrawableRoom> roomFlow;
|
2018-12-25 10:59:08 +08:00
|
|
|
public IReadOnlyList<DrawableRoom> Rooms => roomFlow;
|
2018-12-11 18:07:40 +08:00
|
|
|
|
2019-02-08 13:57:51 +08:00
|
|
|
[Resolved]
|
|
|
|
private Bindable<Room> currentRoom { get; set; }
|
|
|
|
|
2018-12-11 18:07:40 +08:00
|
|
|
[Resolved]
|
2018-12-25 10:45:50 +08:00
|
|
|
private IRoomManager roomManager { get; set; }
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
public RoomsContainer()
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
InternalChild = roomFlow = new FillFlowContainer<DrawableRoom>
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(2),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2018-12-25 10:45:50 +08:00
|
|
|
rooms.BindTo(roomManager.Rooms);
|
2018-12-11 18:07:40 +08:00
|
|
|
|
|
|
|
rooms.ItemsAdded += addRooms;
|
|
|
|
rooms.ItemsRemoved += removeRooms;
|
|
|
|
|
2018-12-28 00:45:19 +08:00
|
|
|
roomManager.RoomsUpdated += updateSorting;
|
|
|
|
|
2018-12-11 18:07:40 +08:00
|
|
|
addRooms(rooms);
|
|
|
|
}
|
|
|
|
|
|
|
|
private FilterCriteria currentFilter;
|
|
|
|
|
|
|
|
public void Filter(FilterCriteria criteria)
|
|
|
|
{
|
2018-12-19 15:56:51 +08:00
|
|
|
roomFlow.Children.ForEach(r =>
|
|
|
|
{
|
|
|
|
if (criteria == null)
|
|
|
|
r.MatchingFilter = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool matchingFilter = true;
|
|
|
|
matchingFilter &= r.FilterTerms.Any(term => term.IndexOf(criteria.SearchString, StringComparison.InvariantCultureIgnoreCase) >= 0);
|
|
|
|
|
|
|
|
switch (criteria.SecondaryFilter)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case SecondaryFilter.Public:
|
|
|
|
r.MatchingFilter = r.Room.Availability.Value == RoomAvailability.Public;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.MatchingFilter = matchingFilter;
|
|
|
|
}
|
|
|
|
});
|
2018-12-19 17:02:05 +08:00
|
|
|
|
2018-12-11 18:07:40 +08:00
|
|
|
currentFilter = criteria;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addRooms(IEnumerable<Room> rooms)
|
|
|
|
{
|
|
|
|
foreach (var r in rooms)
|
|
|
|
roomFlow.Add(new DrawableRoom(r) { Action = () => selectRoom(r) });
|
|
|
|
|
|
|
|
Filter(currentFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void removeRooms(IEnumerable<Room> rooms)
|
|
|
|
{
|
|
|
|
foreach (var r in rooms)
|
|
|
|
{
|
|
|
|
var toRemove = roomFlow.Single(d => d.Room == r);
|
|
|
|
toRemove.Action = null;
|
|
|
|
|
|
|
|
roomFlow.Remove(toRemove);
|
2018-12-20 14:17:08 +08:00
|
|
|
|
2018-12-20 19:58:34 +08:00
|
|
|
selectRoom(null);
|
2018-12-11 18:07:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 00:45:19 +08:00
|
|
|
private void updateSorting()
|
|
|
|
{
|
|
|
|
foreach (var room in roomFlow)
|
2019-02-21 17:56:34 +08:00
|
|
|
roomFlow.SetLayoutPosition(room, room.Room.Position.Value);
|
2018-12-28 00:45:19 +08:00
|
|
|
}
|
|
|
|
|
2018-12-11 18:07:40 +08:00
|
|
|
private void selectRoom(Room room)
|
|
|
|
{
|
|
|
|
var drawable = roomFlow.FirstOrDefault(r => r.Room == room);
|
|
|
|
|
|
|
|
if (drawable != null && drawable.State == SelectionState.Selected)
|
2018-12-20 19:58:34 +08:00
|
|
|
JoinRequested?.Invoke(room);
|
2018-12-11 18:07:40 +08:00
|
|
|
else
|
|
|
|
roomFlow.Children.ForEach(r => r.State = r.Room == room ? SelectionState.Selected : SelectionState.NotSelected);
|
2018-12-20 19:58:34 +08:00
|
|
|
|
2019-02-05 18:00:01 +08:00
|
|
|
currentRoom.Value = room;
|
2018-12-11 18:07:40 +08:00
|
|
|
}
|
2018-12-28 00:45:19 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (roomManager != null)
|
|
|
|
roomManager.RoomsUpdated -= updateSorting;
|
|
|
|
}
|
2018-12-11 18:07:40 +08:00
|
|
|
}
|
|
|
|
}
|