Skip to content

Commit

Permalink
Simplify text flow localisation by leveraging localisation manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bdach committed Nov 1, 2021
1 parent cbc5a71 commit 31bebef
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 58 deletions.
6 changes: 0 additions & 6 deletions osu.Framework/Graphics/Containers/ITextPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public interface ITextPart
/// </summary>
IEnumerable<Drawable> Drawables { get; }

/// <summary>
/// Raised when the <see cref="ITextPart"/>'s content changed due to changing language, etc.
/// Signals to the parent <see cref="TextFlowContainer"/> that it needs to reload its contents in the following frame.
/// </summary>
event Action ContentChanged;

/// <summary>
/// Raised when <see cref="Drawables"/> is reconstructed (e.g. when the user language was changed).
/// Can be used by consumers to re-apply manual adjustments to the appearance of <see cref="Drawables"/>.
Expand Down
19 changes: 2 additions & 17 deletions osu.Framework/Graphics/Containers/TextChunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,6 @@ public class TextChunk<TSpriteText> : TextPart
private readonly Func<TSpriteText> creationFunc;
private readonly Action<TSpriteText>? creationParameters;

private ILocalisedBindableString? currentBacking;

private ILocalisedBindableString? current
{
get => currentBacking;
set
{
if (value == null)
return;

currentBacking = value;
currentBacking.BindValueChanged(_ => RaiseContentChanged());
}
}

public TextChunk(LocalisableString text, bool newLineIsParagraph, Func<TSpriteText> creationFunc, Action<TSpriteText>? creationParameters = null)
{
this.text = text;
Expand All @@ -48,7 +33,7 @@ public TextChunk(LocalisableString text, bool newLineIsParagraph, Func<TSpriteTe

protected override IEnumerable<Drawable> CreateDrawablesFor(TextFlowContainer textFlowContainer)
{
current ??= textFlowContainer.Localisation?.GetLocalisedBindableString(text);
string currentContent = textFlowContainer.Localisation?.GetLocalisedString(text) ?? text.ToString();

var drawables = new List<Drawable>();

Expand All @@ -61,7 +46,7 @@ protected override IEnumerable<Drawable> CreateDrawablesFor(TextFlowContainer te
drawables.AddRange(newLine.Drawables);
}

drawables.AddRange(CreateDrawablesFor(current?.Value ?? text.ToString(), textFlowContainer));
drawables.AddRange(CreateDrawablesFor(currentContent, textFlowContainer));
return drawables;
}

Expand Down
27 changes: 7 additions & 20 deletions osu.Framework/Graphics/Containers/TextFlowContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Localisation;
Expand Down Expand Up @@ -123,7 +124,7 @@ public LocalisableString Text
set
{
Clear();
clearParts();
parts.Clear();

AddText(value);
}
Expand All @@ -132,6 +133,8 @@ public LocalisableString Text
[Resolved]
internal LocalisationManager Localisation { get; private set; }

private readonly IBindable<LocalisationParameters> localisationParameters = new Bindable<LocalisationParameters>();

public TextFlowContainer(Action<SpriteText> defaultCreationParameters = null)
{
this.defaultCreationParameters = defaultCreationParameters;
Expand All @@ -140,7 +143,9 @@ public TextFlowContainer(Action<SpriteText> defaultCreationParameters = null)
protected override void LoadComplete()
{
base.LoadComplete();
recreateAllParts();

localisationParameters.BindTo(Localisation.CurrentParameters);
localisationParameters.BindValueChanged(_ => recreateAllParts(), true);
}

private void recreateAllParts()
Expand All @@ -157,8 +162,6 @@ private void recreateAllParts()
recreatePart(part);
}

private void scheduleRecreateAllParts() => Scheduler.AddOnce(recreateAllParts);

private void recreatePart(ITextPart part)
{
part.RecreateDrawablesFor(this);
Expand Down Expand Up @@ -320,7 +323,6 @@ public override void Clear(bool disposeChildren)
protected internal ITextPart AddPart(ITextPart part)
{
parts.Add(part);
part.ContentChanged += scheduleRecreateAllParts;

if (IsLoaded)
recreatePart(part);
Expand All @@ -337,27 +339,12 @@ public bool RemovePart(ITextPart partToRemove)
if (!parts.Remove(partToRemove))
return false;

partToRemove.ContentChanged -= scheduleRecreateAllParts;

if (IsLoaded)
recreateAllParts();

return true;
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
clearParts();
}

private void clearParts()
{
foreach (var part in parts)
part.ContentChanged -= scheduleRecreateAllParts;
parts.Clear();
}

private readonly Cached layout = new Cached();

private void computeLayout()
Expand Down
7 changes: 0 additions & 7 deletions osu.Framework/Graphics/Containers/TextPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public abstract class TextPart : ITextPart
{
public IEnumerable<Drawable> Drawables { get; }
public event Action<IEnumerable<Drawable>> DrawablePartsRecreated;
public event Action ContentChanged;

private readonly List<Drawable> drawables = new List<Drawable>();

Expand All @@ -36,11 +35,5 @@ public void RecreateDrawablesFor(TextFlowContainer textFlowContainer)
/// to be appended to the <paramref name="textFlowContainer"/>.
/// </summary>
protected abstract IEnumerable<Drawable> CreateDrawablesFor(TextFlowContainer textFlowContainer);

/// <summary>
/// Raises <see cref="ContentChanged"/>, signalling to the parent <see cref="TextFlowContainer"/>
/// that the contents of this part have changed.
/// </summary>
protected void RaiseContentChanged() => ContentChanged?.Invoke();
}
}
6 changes: 0 additions & 6 deletions osu.Framework/Graphics/Containers/TextPartManual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public event Action<IEnumerable<Drawable>> DrawablePartsRecreated
remove { }
}

public event Action ContentChanged
{
add { }
remove { }
}

public TextPartManual(IEnumerable<Drawable> drawables)
{
Drawables = drawables.ToImmutableArray();
Expand Down
6 changes: 4 additions & 2 deletions osu.Framework/Localisation/LocalisationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ namespace osu.Framework.Localisation
{
public partial class LocalisationManager
{
public IBindable<LocalisationParameters> CurrentParameters => currentParameters;

private readonly Bindable<LocalisationParameters> currentParameters = new Bindable<LocalisationParameters>(new LocalisationParameters(null, false));

private readonly List<LocaleMapping> locales = new List<LocaleMapping>();

private readonly Bindable<string> configLocale = new Bindable<string>();
private readonly Bindable<bool> configPreferUnicode = new BindableBool();

private readonly Bindable<LocalisationParameters> currentParameters = new Bindable<LocalisationParameters>(new LocalisationParameters(null, false));

public LocalisationManager(FrameworkConfigManager config)
{
config.BindWith(FrameworkSetting.Locale, configLocale);
Expand Down

0 comments on commit 31bebef

Please sign in to comment.