1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 20:47:30 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Lounge/LoungeSubScreen.cs

270 lines
8.7 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.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;
2021-07-14 17:24:30 +08:00
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-05-22 11:24:39 +08:00
using osu.Framework.Screens;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
using osu.Game.Screens.OnlinePlay.Match;
using osu.Game.Users;
using osuTK;
2021-07-14 17:24:30 +08:00
using osuTK.Graphics;
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";
protected override UserActivity InitialActivity => new UserActivity.SearchingForLobby();
protected Container<OsuButton> Buttons { get; } = new Container<OsuButton>
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
AutoSizeAxes = Axes.Both
};
private readonly IBindable<bool> initialRoomsReceived = new Bindable<bool>();
private readonly IBindable<bool> operationInProgress = new Bindable<bool>();
2020-12-20 22:36:56 +08:00
private FilterControl filter;
private LoadingLayer loadingLayer;
2018-05-22 11:07:04 +08:00
2019-02-08 14:20:11 +08:00
[Resolved]
private Bindable<Room> selectedRoom { get; set; }
2019-02-08 14:20:11 +08:00
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; }
[CanBeNull]
private IDisposable joiningRoomOperation { get; set; }
private RoomsContainer roomsContainer;
[BackgroundDependencyLoader]
private void load()
2018-05-22 11:07:04 +08:00
{
OsuScrollContainer scrollContainer;
Container filterContainer;
2021-07-14 17:24:30 +08:00
InternalChildren = new Drawable[]
2018-05-22 11:07:04 +08:00
{
2021-07-14 17:24:30 +08:00
new Box
2021-07-14 17:10:59 +08:00
{
2021-07-14 17:24:30 +08:00
RelativeSizeAxes = Axes.X,
Height = 100,
Colour = Color4.Black,
Alpha = 0.5f,
2021-07-14 17:10:59 +08:00
},
new Container
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
{
2021-07-14 17:24:30 +08:00
Top = 20,
Left = WaveOverlayContainer.WIDTH_PADDING,
Right = WaveOverlayContainer.WIDTH_PADDING,
2021-07-14 16:46:45 +08:00
},
2021-07-14 17:24:30 +08:00
Child = new GridContainer
2021-07-14 16:46:45 +08:00
{
2021-07-14 17:24:30 +08:00
RelativeSizeAxes = Axes.Both,
RowDimensions = new[]
2021-07-14 16:46:45 +08:00
{
2021-07-14 17:24:30 +08:00
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.Absolute, 20)
2021-07-14 16:46:45 +08:00
},
2021-07-14 17:24:30 +08:00
Content = new[]
2018-05-22 11:07:04 +08:00
{
2021-07-14 17:24:30 +08:00
new Drawable[]
2018-05-22 11:07:04 +08:00
{
filterContainer = new Container
{
RelativeSizeAxes = Axes.X,
Height = 70,
Depth = -1,
Children = new Drawable[]
{
filter = CreateFilterControl(),
Buttons.WithChild(CreateNewRoomButton().With(d =>
{
d.Size = new Vector2(150, 25);
d.Action = () => Open();
}))
}
}
2021-07-14 17:24:30 +08:00
},
null,
new Drawable[]
{
new Container
2021-07-14 16:46:45 +08:00
{
2021-07-14 17:24:30 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2021-07-14 16:46:45 +08:00
{
2021-07-14 17:24:30 +08:00
scrollContainer = new OsuScrollContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarOverlapsContent = false,
Child = roomsContainer = new RoomsContainer()
2021-07-14 17:24:30 +08:00
},
}
},
}
2021-07-13 15:00:42 +08:00
}
2021-07-14 17:24:30 +08:00
},
},
loadingLayer = new LoadingLayer(true),
filterContainer.CreateProxy()
2018-05-22 11:07:04 +08:00
};
// scroll selected room into view on selection.
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();
initialRoomsReceived.BindTo(RoomManager.InitialRoomsReceived);
initialRoomsReceived.BindValueChanged(_ => updateLoadingLayer());
2020-12-29 15:20:43 +08:00
if (ongoingOperationTracker != null)
{
operationInProgress.BindTo(ongoingOperationTracker.InProgress);
operationInProgress.BindValueChanged(_ => updateLoadingLayer(), true);
}
}
2018-10-02 11:02:47 +08:00
protected override void OnFocus(FocusEvent e)
2018-05-22 11:24:39 +08:00
{
2020-12-20 22:36:56 +08:00
filter.TakeFocus();
2018-05-22 11:24:39 +08:00
}
2019-01-25 18:32:37 +08:00
public override void OnEntering(IScreen last)
2018-05-22 11:24:39 +08:00
{
2019-01-25 18:32:37 +08:00
base.OnEntering(last);
onReturning();
}
public override void OnResuming(IScreen last)
{
base.OnResuming(last);
if (selectedRoom.Value?.RoomID.Value == null)
selectedRoom.Value = new Room();
music?.EnsurePlayingSomething();
onReturning();
}
2019-01-25 18:32:37 +08:00
public override bool OnExiting(IScreen next)
2018-05-22 11:24:39 +08:00
{
onLeaving();
2019-01-25 18:32:37 +08:00
return base.OnExiting(next);
}
2019-01-25 18:32:37 +08:00
public override void OnSuspending(IScreen next)
{
onLeaving();
2019-01-25 18:32:37 +08:00
base.OnSuspending(next);
}
private void onReturning()
{
filter.HoldFocus = true;
}
private void onLeaving()
{
2020-12-20 22:36:56 +08:00
filter.HoldFocus = false;
// ensure any password prompt is dismissed.
this.HidePopover();
2018-05-22 11:24:39 +08:00
}
public void Join(Room room, string password) => 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;
}, _ =>
{
2020-12-29 15:20:43 +08:00
joiningRoomOperation?.Dispose();
joiningRoomOperation = null;
});
});
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)
{
selectedRoom.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
protected abstract FilterControl CreateFilterControl();
2020-12-20 23:21:30 +08:00
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);
private void updateLoadingLayer()
{
if (operationInProgress.Value || !initialRoomsReceived.Value)
loadingLayer.Show();
else
loadingLayer.Hide();
}
2018-05-22 11:07:04 +08:00
}
}