1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 23:42:55 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Lounge/Components/DrawableRoom.cs

390 lines
13 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-04-13 17:19:50 +08:00
2018-05-11 09:48:07 +08:00
using System;
2018-05-22 11:33:41 +08:00
using System.Collections.Generic;
2018-05-11 09:48:07 +08:00
using osu.Framework;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
2018-05-11 09:48:07 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Input.Bindings;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Components;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.OnlinePlay.Lounge.Components
2018-04-13 17:19:50 +08:00
{
public class DrawableRoom : OsuClickableContainer, IStateful<SelectionState>, IFilterable, IHasContextMenu, IHasPopover, IKeyBindingHandler<GlobalAction>
2018-04-13 17:19:50 +08:00
{
2018-05-22 11:07:04 +08:00
public const float SELECTION_BORDER_WIDTH = 4;
2018-05-11 09:48:07 +08:00
private const float corner_radius = 5;
2018-05-28 12:02:15 +08:00
private const float transition_duration = 60;
2018-04-13 17:19:50 +08:00
private const float content_padding = 10;
2018-12-19 14:20:23 +08:00
private const float height = 110;
2018-04-13 17:19:50 +08:00
private const float side_strip_width = 5;
private const float cover_width = 145;
2018-12-04 14:26:06 +08:00
public event Action<SelectionState> StateChanged;
2018-12-22 13:01:06 +08:00
private readonly Box selectionBox;
2018-12-13 17:38:03 +08:00
[Resolved(canBeNull: true)]
private OnlinePlayScreen parentScreen { get; set; }
2018-12-06 11:21:30 +08:00
[Resolved]
private BeatmapManager beatmaps { get; set; }
2021-07-12 13:35:08 +08:00
[Resolved(canBeNull: true)]
private Bindable<Room> selectedRoom { get; set; }
[Resolved(canBeNull: true)]
private LoungeSubScreen lounge { get; set; }
2018-04-13 17:19:50 +08:00
public readonly Room Room;
2018-05-11 09:48:07 +08:00
private SelectionState state;
2019-02-28 12:31:40 +08:00
2018-05-11 09:48:07 +08:00
public SelectionState State
{
get => state;
2018-05-11 09:48:07 +08:00
set
{
if (value == state) return;
2019-02-28 12:31:40 +08:00
2018-05-11 09:48:07 +08:00
state = value;
if (state == SelectionState.Selected)
selectionBox.FadeIn(transition_duration);
else
selectionBox.FadeOut(transition_duration);
StateChanged?.Invoke(State);
}
}
2018-05-22 11:33:41 +08:00
public IEnumerable<string> FilterTerms => new[] { Room.Name.Value };
private bool matchingFilter;
2019-02-28 12:31:40 +08:00
2018-05-22 11:33:41 +08:00
public bool MatchingFilter
{
get => matchingFilter;
2018-05-22 11:33:41 +08:00
set
{
matchingFilter = value;
if (!IsLoaded)
return;
if (matchingFilter)
this.FadeIn(200);
else
Hide();
2018-05-22 11:33:41 +08:00
}
}
2019-03-28 23:29:07 +08:00
public bool FilteringActive { get; set; }
private PasswordProtectedIcon passwordIcon;
private readonly Bindable<bool> hasPassword = new Bindable<bool>();
2018-04-13 17:19:50 +08:00
public DrawableRoom(Room room)
{
Room = room;
RelativeSizeAxes = Axes.X;
2018-05-22 11:07:04 +08:00
Height = height + SELECTION_BORDER_WIDTH * 2;
CornerRadius = corner_radius + SELECTION_BORDER_WIDTH / 2;
2018-04-13 17:19:50 +08:00
Masking = true;
2018-05-11 09:48:07 +08:00
// create selectionBox here so State can be set before being loaded
selectionBox = new Box
{
2018-05-11 09:48:07 +08:00
RelativeSizeAxes = Axes.Both,
Alpha = 0f,
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
float stripWidth = side_strip_width * (Room.Category.Value == RoomCategory.Spotlight ? 2 : 1);
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new StatusColouredContainer(transition_duration)
2019-02-06 12:52:48 +08:00
{
RelativeSizeAxes = Axes.Both,
Child = selectionBox
},
2018-04-13 17:19:50 +08:00
new Container
{
2018-05-11 09:48:07 +08:00
RelativeSizeAxes = Axes.Both,
2018-05-22 11:07:04 +08:00
Padding = new MarginPadding(SELECTION_BORDER_WIDTH),
Child = new Container
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = corner_radius,
EdgeEffect = new EdgeEffectParameters
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
2018-04-13 17:19:50 +08:00
},
2018-05-11 09:48:07 +08:00
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
new Box
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex(@"212121"),
2018-05-11 09:48:07 +08:00
},
new StatusColouredContainer(transition_duration)
2018-05-11 09:48:07 +08:00
{
RelativeSizeAxes = Axes.Y,
Width = stripWidth,
2019-02-06 12:52:48 +08:00
Child = new Box { RelativeSizeAxes = Axes.Both }
2018-05-11 09:48:07 +08:00
},
2018-12-06 11:21:30 +08:00
new Container
2018-05-11 09:48:07 +08:00
{
RelativeSizeAxes = Axes.Y,
2018-12-06 11:21:30 +08:00
Width = cover_width,
2018-05-11 09:48:07 +08:00
Masking = true,
Margin = new MarginPadding { Left = stripWidth },
Child = new OnlinePlayBackgroundSprite(BeatmapSetCoverType.List) { RelativeSizeAxes = Axes.Both }
2018-04-13 17:19:50 +08:00
},
2018-05-11 09:48:07 +08:00
new Container
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
Vertical = content_padding,
Left = stripWidth + cover_width + content_padding,
2018-05-11 09:48:07 +08:00
Right = content_padding,
2018-04-13 17:19:50 +08:00
},
2018-05-11 09:48:07 +08:00
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
new FillFlowContainer
2018-04-13 17:19:50 +08:00
{
2018-05-11 09:48:07 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(5f),
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
new RoomName { Font = OsuFont.GetFont(size: 18) },
2019-02-05 18:00:01 +08:00
new ParticipantInfo(),
2018-04-13 17:19:50 +08:00
},
2018-05-11 09:48:07 +08:00
},
new FillFlowContainer
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 5),
2018-05-11 09:48:07 +08:00
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2019-02-05 18:00:01 +08:00
new RoomStatusInfo(),
new BeatmapTitle { TextSize = 14 },
2018-04-13 17:19:50 +08:00
},
},
2019-02-05 18:00:01 +08:00
new ModeTypeInfo
2018-05-11 09:48:07 +08:00
{
Anchor = Anchor.BottomRight,
Origin = Anchor.BottomRight,
},
2018-04-13 17:19:50 +08:00
},
},
passwordIcon = new PasswordProtectedIcon { Alpha = 0 }
2018-04-13 17:19:50 +08:00
},
},
},
};
}
2018-05-22 11:07:04 +08:00
2019-02-05 18:00:01 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
return new CachedModelDependencyContainer<Room>(base.CreateChildDependencies(parent))
{
Model = { Value = Room }
};
2019-02-05 18:00:01 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
if (matchingFilter)
this.FadeInFromZero(transition_duration);
else
Alpha = 0;
hasPassword.BindTo(Room.HasPassword);
hasPassword.BindValueChanged(v => passwordIcon.Alpha = v.NewValue ? 1 : 0, true);
}
2019-02-06 12:52:48 +08:00
public Popover GetPopover() => new PasswordEntryPopover(Room) { JoinRequested = lounge.Join };
public MenuItem[] ContextMenuItems => new MenuItem[]
{
new OsuMenuItem("Create copy", MenuItemType.Standard, () =>
{
2021-07-19 21:23:31 +08:00
parentScreen?.OpenNewRoom(Room.DeepClone());
})
};
public bool OnPressed(GlobalAction action)
{
if (selectedRoom.Value != Room)
return false;
switch (action)
{
case GlobalAction.Select:
Click();
return true;
}
return false;
}
public void OnReleased(GlobalAction action)
{
}
protected override bool ShouldBeConsideredForInput(Drawable child) => state == SelectionState.Selected;
protected override bool OnClick(ClickEvent e)
{
if (Room != selectedRoom.Value)
{
selectedRoom.Value = Room;
return true;
}
if (Room.HasPassword.Value)
{
this.ShowPopover();
return true;
}
lounge?.Join(Room, null);
return base.OnClick(e);
}
2019-02-06 12:52:48 +08:00
private class RoomName : OsuSpriteText
{
2020-12-25 12:38:11 +08:00
[Resolved(typeof(Room), nameof(Online.Rooms.Room.Name))]
2019-02-06 12:52:48 +08:00
private Bindable<string> name { get; set; }
[BackgroundDependencyLoader]
private void load()
{
Current = name;
}
}
public class PasswordProtectedIcon : CompositeDrawable
{
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Anchor = Anchor.TopRight;
Origin = Anchor.TopRight;
Size = new Vector2(32);
InternalChildren = new Drawable[]
{
new Box
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopCentre,
Colour = colours.Gray5,
Rotation = 45,
RelativeSizeAxes = Axes.Both,
Width = 2,
},
new SpriteIcon
{
Icon = FontAwesome.Solid.Lock,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding(6),
Size = new Vector2(14),
}
};
}
}
public class PasswordEntryPopover : OsuPopover
{
2021-07-12 17:29:50 +08:00
private readonly Room room;
public Action<Room, string> JoinRequested;
public PasswordEntryPopover(Room room)
2021-07-12 17:29:50 +08:00
{
this.room = room;
}
private OsuPasswordTextBox passwordTextbox;
2021-07-12 17:29:50 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2021-07-12 17:29:50 +08:00
Child = new FillFlowContainer
{
2021-07-12 17:29:50 +08:00
Margin = new MarginPadding(10),
Spacing = new Vector2(5),
AutoSizeAxes = Axes.Both,
2021-07-12 17:29:50 +08:00
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
2021-07-12 17:29:50 +08:00
passwordTextbox = new OsuPasswordTextBox
{
2021-07-12 17:29:50 +08:00
Width = 200,
},
2021-07-12 17:29:50 +08:00
new TriangleButton
{
2021-07-12 17:29:50 +08:00
Width = 80,
Text = "Join Room",
Action = () => JoinRequested?.Invoke(room, passwordTextbox.Text)
2021-07-12 17:29:50 +08:00
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Schedule(() => GetContainingInputManager().ChangeFocus(passwordTextbox));
passwordTextbox.OnCommit += (_, __) => JoinRequested?.Invoke(room, passwordTextbox.Text);
}
}
2018-04-13 17:19:50 +08:00
}
}