1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 15:12:57 +08:00

Use bindable logic for grouping name/description updates

This commit is contained in:
Dean Herbert 2019-06-14 20:30:09 +09:00
parent ef21a9e1d2
commit c9bd62e815
3 changed files with 66 additions and 41 deletions

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -62,39 +61,31 @@ namespace osu.Game.Tournament.Screens.Groupings
private void load() private void load()
{ {
foreach (var g in LadderInfo.Groupings) foreach (var g in LadderInfo.Groupings)
items.Add(new GroupingRow(g, updateGroupings)); items.Add(new GroupingRow(g));
}
protected override void LoadComplete()
{
base.LoadComplete();
Scheduler.AddDelayed(updateGroupings, 500, true);
} }
private void addNew() private void addNew()
{ {
items.Add(new GroupingRow(new TournamentGrouping var grouping = new TournamentGrouping
{ {
StartDate = StartDate =
{ {
Value = DateTimeOffset.UtcNow Value = DateTimeOffset.UtcNow
} }
}, updateGroupings)); };
updateGroupings(); items.Add(new GroupingRow(grouping));
} LadderInfo.Groupings.Add(grouping);
private void updateGroupings()
{
LadderInfo.Groupings.Clear();
LadderInfo.Groupings.AddRange(items.Children.Select(c => c.Grouping));
} }
public class GroupingRow : CompositeDrawable public class GroupingRow : CompositeDrawable
{ {
public readonly TournamentGrouping Grouping; public readonly TournamentGrouping Grouping;
public GroupingRow(TournamentGrouping grouping, Action onDelete) [Resolved]
private LadderInfo ladderInfo { get; set; }
public GroupingRow(TournamentGrouping grouping)
{ {
Margin = new MarginPadding(10); Margin = new MarginPadding(10);
@ -152,7 +143,7 @@ namespace osu.Game.Tournament.Screens.Groupings
Action = () => Action = () =>
{ {
Expire(); Expire();
onDelete(); ladderInfo.Groupings.Remove(Grouping);
}, },
} }
}; };

View File

@ -1,6 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using JetBrains.Annotations;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Graphics; using osu.Game.Graphics;
@ -11,8 +13,17 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
{ {
public class DrawableTournamentGrouping : CompositeDrawable public class DrawableTournamentGrouping : CompositeDrawable
{ {
[UsedImplicitly]
private readonly Bindable<string> name;
[UsedImplicitly]
private readonly Bindable<string> description;
public DrawableTournamentGrouping(TournamentGrouping grouping, bool losers = false) public DrawableTournamentGrouping(TournamentGrouping grouping, bool losers = false)
{ {
OsuSpriteText textName;
OsuSpriteText textDescription;
AutoSizeAxes = Axes.Both; AutoSizeAxes = Axes.Both;
InternalChild = new FillFlowContainer InternalChild = new FillFlowContainer
{ {
@ -20,16 +31,14 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Children = new Drawable[] Children = new Drawable[]
{ {
new OsuSpriteText textDescription = new OsuSpriteText
{ {
Text = grouping.Description.Value.ToUpper(),
Colour = Color4.Black, Colour = Color4.Black,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre Anchor = Anchor.TopCentre
}, },
new OsuSpriteText textName = new OsuSpriteText
{ {
Text = ((losers ? "Losers " : "") + grouping.Name).ToUpper(),
Font = OsuFont.GetFont(weight: FontWeight.Bold), Font = OsuFont.GetFont(weight: FontWeight.Bold),
Colour = Color4.Black, Colour = Color4.Black,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre,
@ -37,6 +46,12 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
}, },
} }
}; };
name = grouping.Name.GetBoundCopy();
name.BindValueChanged(n => textName.Text = ((losers ? "Losers " : "") + grouping.Name).ToUpper(), true);
description = grouping.Name.GetBoundCopy();
description.BindValueChanged(n => textDescription.Text = grouping.Description.Value.ToUpper(), true);
} }
} }
} }

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
@ -74,10 +75,7 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
}, },
}, },
textboxTeam2 = new OsuTextBox { RelativeSizeAxes = Axes.X, Height = 20 }, textboxTeam2 = new OsuTextBox { RelativeSizeAxes = Axes.X, Height = 20 },
groupingDropdown = new SettingsDropdown<TournamentGrouping> groupingDropdown = new SettingsGroupingDropdown(ladderInfo.Groupings),
{
Bindable = new Bindable<TournamentGrouping>(),
},
losersCheckbox = new PlayerCheckbox losersCheckbox = new PlayerCheckbox
{ {
LabelText = "Losers Bracket", LabelText = "Losers Bracket",
@ -89,24 +87,11 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
} }
}; };
IEnumerable<TournamentGrouping> groupingOptions = null;
void updateDropdownItems()
{
groupingOptions = ladderInfo.Groupings.Prepend(new TournamentGrouping());
groupingDropdown.Items = groupingOptions;
}
ladderInfo.Groupings.ItemsRemoved += _ => updateDropdownItems();
ladderInfo.Groupings.ItemsAdded += _ => updateDropdownItems();
updateDropdownItems();
editorInfo.Selected.ValueChanged += selection => editorInfo.Selected.ValueChanged += selection =>
{ {
textboxTeam1.Text = selection.NewValue?.Team1.Value?.Acronym; textboxTeam1.Text = selection.NewValue?.Team1.Value?.Acronym;
textboxTeam2.Text = selection.NewValue?.Team2.Value?.Acronym; textboxTeam2.Text = selection.NewValue?.Team2.Value?.Acronym;
groupingDropdown.Bindable.Value = selection.NewValue?.Grouping.Value ?? groupingOptions.First(); groupingDropdown.Bindable.Value = selection.NewValue?.Grouping.Value;
losersCheckbox.Current.Value = selection.NewValue?.Losers.Value ?? false; losersCheckbox.Current.Value = selection.NewValue?.Losers.Value ?? false;
dateTimeBox.Bindable.Value = selection.NewValue?.Date.Value ?? DateTimeOffset.UtcNow; dateTimeBox.Bindable.Value = selection.NewValue?.Date.Value ?? DateTimeOffset.UtcNow;
}; };
@ -164,5 +149,39 @@ namespace osu.Game.Tournament.Screens.Ladder.Components
protected override void OnHoverLost(HoverLostEvent e) protected override void OnHoverLost(HoverLostEvent e)
{ {
} }
private class SettingsGroupingDropdown : SettingsDropdown<TournamentGrouping>
{
public SettingsGroupingDropdown(BindableList<TournamentGrouping> groupings)
{
Bindable = new Bindable<TournamentGrouping>();
foreach (var g in groupings.Prepend(new TournamentGrouping()))
add(g);
groupings.ItemsRemoved += items => items.ForEach(i => Control.RemoveDropdownItem(i));
groupings.ItemsAdded += items => items.ForEach(add);
}
private readonly List<IUnbindable> refBindables = new List<IUnbindable>();
private T boundReference<T>(T obj)
where T : IBindable
{
obj = (T)obj.GetBoundCopy();
refBindables.Add(obj);
return obj;
}
private void add(TournamentGrouping grouping)
{
Control.AddDropdownItem(grouping);
boundReference(grouping.Name).BindValueChanged(_ =>
{
Control.RemoveDropdownItem(grouping);
Control.AddDropdownItem(grouping);
});
}
}
} }
} }