1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 11:37:52 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Lounge/LoungeSubScreen.cs

410 lines
14 KiB
C#
Raw Normal View History

// 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-05-22 11:07:04 +08:00
2020-12-29 15:20:43 +08:00
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
2020-12-29 15:20:43 +08:00
using JetBrains.Annotations;
2019-02-08 14:20:11 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions;
2018-05-22 11:07:04 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Logging;
2018-05-22 11:24:39 +08:00
using osu.Framework.Screens;
using osu.Framework.Threading;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input;
using osu.Game.Online.API;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Rulesets;
using osu.Game.Screens.OnlinePlay.Components;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
using osu.Game.Screens.OnlinePlay.Match;
using osu.Game.Users;
using osuTK;
2018-05-22 11:07:04 +08:00
namespace osu.Game.Screens.OnlinePlay.Lounge
2018-05-22 11:07:04 +08:00
{
[Cached]
public abstract class LoungeSubScreen : OnlinePlaySubScreen
2018-05-22 11:07:04 +08:00
{
2019-01-25 18:32:37 +08:00
public override string Title => "Lounge";
2021-08-20 20:40:35 +08:00
protected override BackgroundScreen CreateBackground() => new LoungeBackgroundScreen
{
2021-08-23 13:29:15 +08:00
SelectedRoom = { BindTarget = SelectedRoom }
2021-08-20 20:40:35 +08:00
};
protected override UserActivity InitialActivity => new UserActivity.SearchingForLobby();
protected Container<OsuButton> Buttons { get; } = new Container<OsuButton>
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
AutoSizeAxes = Axes.Both
};
2021-08-16 12:09:04 +08:00
protected ListingPollingComponent ListingPollingComponent { get; private set; }
2021-08-23 13:29:15 +08:00
protected readonly Bindable<Room> SelectedRoom = new Bindable<Room>();
2020-08-11 11:40:58 +08:00
[Resolved]
private MusicController music { get; set; }
[Resolved(CanBeNull = true)]
2020-12-29 15:20:43 +08:00
private OngoingOperationTracker ongoingOperationTracker { get; set; }
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
2020-12-29 15:20:43 +08:00
[CanBeNull]
private IDisposable joiningRoomOperation { get; set; }
[CanBeNull]
private LeasedBindable<Room> selectionLease;
2021-08-17 08:36:43 +08:00
private readonly Bindable<FilterCriteria> filter = new Bindable<FilterCriteria>(new FilterCriteria());
private readonly IBindable<bool> operationInProgress = new Bindable<bool>();
private readonly IBindable<bool> isIdle = new BindableBool();
private PopoverContainer popoverContainer;
private LoadingLayer loadingLayer;
private RoomsContainer roomsContainer;
private SearchTextBox searchTextBox;
private Dropdown<RoomStatusFilter> statusDropdown;
[BackgroundDependencyLoader(true)]
private void load([CanBeNull] IdleTracker idleTracker)
2018-05-22 11:07:04 +08:00
{
const float controls_area_height = 25f;
if (idleTracker != null)
isIdle.BindTo(idleTracker.IsIdle);
OsuScrollContainer scrollContainer;
2021-07-14 17:24:30 +08:00
InternalChildren = new Drawable[]
2018-05-22 11:07:04 +08:00
{
2021-08-17 08:36:43 +08:00
ListingPollingComponent = CreatePollingComponent().With(c => c.Filter.BindTarget = filter),
popoverContainer = new PopoverContainer
2018-05-22 11:07:04 +08:00
{
Name = @"Rooms area",
2018-05-22 11:07:04 +08:00
RelativeSizeAxes = Axes.Both,
2021-07-14 17:24:30 +08:00
Padding = new MarginPadding
2018-05-22 11:07:04 +08:00
{
Horizontal = WaveOverlayContainer.WIDTH_PADDING,
Top = Header.HEIGHT + controls_area_height + 20,
2021-07-14 16:46:45 +08:00
},
Child = scrollContainer = new OsuScrollContainer
2021-07-14 16:46:45 +08:00
{
2021-07-14 17:24:30 +08:00
RelativeSizeAxes = Axes.Both,
ScrollbarOverlapsContent = false,
Child = roomsContainer = new RoomsContainer
2021-07-14 16:46:45 +08:00
{
Filter = { BindTarget = filter },
2021-08-23 13:29:15 +08:00
SelectedRoom = { BindTarget = SelectedRoom }
}
},
},
loadingLayer = new LoadingLayer(true),
new FillFlowContainer
{
Name = @"Header area flow",
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Horizontal = WaveOverlayContainer.WIDTH_PADDING },
Direction = FillDirection.Vertical,
Children = new Drawable[]
2021-07-14 16:46:45 +08:00
{
new Container
2018-05-22 11:07:04 +08:00
{
RelativeSizeAxes = Axes.X,
Height = Header.HEIGHT,
Child = searchTextBox = new BasicSearchTextBox
2018-05-22 11:07:04 +08:00
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
RelativeSizeAxes = Axes.X,
Width = 0.6f,
},
2021-07-14 16:46:45 +08:00
},
new Container
2018-05-22 11:07:04 +08:00
{
RelativeSizeAxes = Axes.X,
Height = controls_area_height,
Children = new Drawable[]
{
Buttons.WithChild(CreateNewRoomButton().With(d =>
{
d.Anchor = Anchor.BottomLeft;
d.Origin = Anchor.BottomLeft;
d.Size = new Vector2(150, 37.5f);
d.Action = () => Open();
})),
new FillFlowContainer
2021-07-14 16:46:45 +08:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(10),
ChildrenEnumerable = CreateFilterControls().Select(f => f.With(d =>
2021-07-14 16:46:45 +08:00
{
d.Anchor = Anchor.TopRight;
d.Origin = Anchor.TopRight;
}))
}
2021-07-14 17:24:30 +08:00
}
2021-07-13 15:00:42 +08:00
}
2021-07-14 17:24:30 +08:00
},
},
2018-05-22 11:07:04 +08:00
};
// scroll selected room into view on selection.
2021-08-23 13:29:15 +08:00
SelectedRoom.BindValueChanged(val =>
{
var drawable = roomsContainer.Rooms.FirstOrDefault(r => r.Room == val.NewValue);
if (drawable != null)
scrollContainer.ScrollIntoView(drawable);
});
2018-05-22 11:07:04 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
searchTextBox.Current.BindValueChanged(_ => updateFilterDebounced());
ruleset.BindValueChanged(_ => UpdateFilter());
isIdle.BindValueChanged(_ => updatePollingRate(this.IsCurrentScreen()), true);
2020-12-29 15:20:43 +08:00
if (ongoingOperationTracker != null)
{
operationInProgress.BindTo(ongoingOperationTracker.InProgress);
operationInProgress.BindValueChanged(_ => updateLoadingLayer());
}
ListingPollingComponent.InitialRoomsReceived.BindValueChanged(_ => updateLoadingLayer(), true);
updateFilter();
}
#region Filtering
2021-08-17 08:36:43 +08:00
public void UpdateFilter() => Scheduler.AddOnce(updateFilter);
private ScheduledDelegate scheduledFilterUpdate;
private void updateFilterDebounced()
{
scheduledFilterUpdate?.Cancel();
scheduledFilterUpdate = Scheduler.AddDelayed(UpdateFilter, 200);
}
private void updateFilter()
{
scheduledFilterUpdate?.Cancel();
filter.Value = CreateFilterCriteria();
}
protected virtual FilterCriteria CreateFilterCriteria() => new FilterCriteria
{
SearchString = searchTextBox.Current.Value,
Ruleset = ruleset.Value,
Status = statusDropdown.Current.Value
};
protected virtual IEnumerable<Drawable> CreateFilterControls()
2018-05-22 11:24:39 +08:00
{
statusDropdown = new SlimEnumDropdown<RoomStatusFilter>
{
RelativeSizeAxes = Axes.None,
Width = 160,
};
statusDropdown.Current.BindValueChanged(_ => UpdateFilter());
yield return statusDropdown;
2018-05-22 11:24:39 +08:00
}
#endregion
public override void OnEntering(ScreenTransitionEvent e)
2018-05-22 11:24:39 +08:00
{
base.OnEntering(e);
onReturning();
}
public override void OnResuming(ScreenTransitionEvent e)
{
base.OnResuming(e);
Debug.Assert(selectionLease != null);
selectionLease.Return();
selectionLease = null;
2021-08-23 13:29:15 +08:00
if (SelectedRoom.Value?.RoomID.Value == null)
SelectedRoom.Value = new Room();
music?.EnsurePlayingSomething();
onReturning();
}
public override bool OnExiting(ScreenExitEvent e)
2018-05-22 11:24:39 +08:00
{
onLeaving();
return base.OnExiting(e);
}
public override void OnSuspending(ScreenTransitionEvent e)
{
onLeaving();
base.OnSuspending(e);
}
protected override void OnFocus(FocusEvent e)
{
searchTextBox.TakeFocus();
}
private void onReturning()
{
updatePollingRate(true);
searchTextBox.HoldFocus = true;
}
private void onLeaving()
{
updatePollingRate(false);
searchTextBox.HoldFocus = false;
// ensure any password prompt is dismissed.
popoverContainer.HidePopover();
2018-05-22 11:24:39 +08:00
}
public virtual void Join(Room room, string password, Action<Room> onSuccess = null, Action<string> onFailure = null) => Schedule(() =>
{
if (joiningRoomOperation != null)
return;
2020-12-29 15:20:43 +08:00
joiningRoomOperation = ongoingOperationTracker?.BeginOperation();
RoomManager?.JoinRoom(room, password, r =>
{
2019-02-05 18:00:01 +08:00
Open(room);
2020-12-29 15:20:43 +08:00
joiningRoomOperation?.Dispose();
joiningRoomOperation = null;
onSuccess?.Invoke(room);
}, error =>
{
2020-12-29 15:20:43 +08:00
joiningRoomOperation?.Dispose();
joiningRoomOperation = null;
onFailure?.Invoke(error);
});
});
/// <summary>
/// Copies a room and opens it as a fresh (not-yet-created) one.
/// </summary>
/// <param name="room">The room to copy.</param>
public void OpenCopy(Room room)
{
Debug.Assert(room.RoomID.Value != null);
if (joiningRoomOperation != null)
return;
joiningRoomOperation = ongoingOperationTracker?.BeginOperation();
var req = new GetRoomRequest(room.RoomID.Value.Value);
req.Success += r =>
{
// ID must be unset as we use this as a marker for whether this is a client-side (not-yet-created) room or not.
r.RoomID.Value = null;
2022-03-15 10:30:57 +08:00
// Null out dates because end date is not supported client-side and the settings overlay will populate a duration.
r.EndDate.Value = null;
r.Duration.Value = null;
Open(r);
joiningRoomOperation?.Dispose();
joiningRoomOperation = null;
};
req.Failure += exception =>
{
Logger.Error(exception, "Couldn't create a copy of this room.");
joiningRoomOperation?.Dispose();
joiningRoomOperation = null;
};
api.Queue(req);
}
2018-12-26 19:21:30 +08:00
/// <summary>
/// Push a room as a new subscreen.
/// </summary>
/// <param name="room">An optional template to use when creating the room.</param>
public void Open(Room room = null) => Schedule(() =>
2018-12-04 14:26:06 +08:00
{
// Handles the case where a room is clicked 3 times in quick succession
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen())
2018-12-04 14:26:06 +08:00
return;
OpenNewRoom(room ?? CreateNewRoom());
});
protected virtual void OpenNewRoom(Room room)
{
2021-08-23 13:29:15 +08:00
selectionLease = SelectedRoom.BeginLease(false);
Debug.Assert(selectionLease != null);
selectionLease.Value = room;
2019-02-08 14:20:11 +08:00
2020-12-20 23:21:30 +08:00
this.Push(CreateRoomSubScreen(room));
2018-05-22 11:33:41 +08:00
}
2020-12-20 22:36:56 +08:00
private void updateLoadingLayer()
{
2021-08-16 12:09:04 +08:00
if (operationInProgress.Value || !ListingPollingComponent.InitialRoomsReceived.Value)
loadingLayer.Show();
else
loadingLayer.Hide();
}
private void updatePollingRate(bool isCurrentScreen)
{
if (!isCurrentScreen)
2021-08-16 12:09:04 +08:00
ListingPollingComponent.TimeBetweenPolls.Value = 0;
else
2021-08-16 12:09:04 +08:00
ListingPollingComponent.TimeBetweenPolls.Value = isIdle.Value ? 120000 : 15000;
2021-08-16 12:09:04 +08:00
Logger.Log($"Polling adjusted (listing: {ListingPollingComponent.TimeBetweenPolls.Value})");
}
protected abstract OsuButton CreateNewRoomButton();
/// <summary>
/// Creates a new room.
/// </summary>
/// <returns>The created <see cref="Room"/>.</returns>
protected abstract Room CreateNewRoom();
2020-12-20 23:21:30 +08:00
protected abstract RoomSubScreen CreateRoomSubScreen(Room room);
protected abstract ListingPollingComponent CreatePollingComponent();
2018-05-22 11:07:04 +08:00
}
}