Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify editor "ternary button" structure #31462

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Catch/Edit/CatchHitObjectComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void load()

protected override Drawable CreateHitObjectInspector() => new CatchHitObjectInspector(DistanceSnapProvider);

protected override IEnumerable<TernaryButton> CreateTernaryButtons()
protected override IEnumerable<DrawableTernaryButton> CreateTernaryButtons()
=> base.CreateTernaryButtons()
.Concat(DistanceSnapProvider.CreateTernaryButtons());

Expand Down
9 changes: 7 additions & 2 deletions osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ protected override DrawableRuleset<OsuHitObject> CreateDrawableRuleset(Ruleset r

protected override Drawable CreateHitObjectInspector() => new OsuHitObjectInspector();

protected override IEnumerable<TernaryButton> CreateTernaryButtons()
protected override IEnumerable<DrawableTernaryButton> CreateTernaryButtons()
=> base.CreateTernaryButtons()
.Append(new TernaryButton(rectangularGridSnapToggle, "Grid Snap", () => new SpriteIcon { Icon = OsuIcon.EditorGridSnap }))
.Append(new DrawableTernaryButton
{
Current = rectangularGridSnapToggle,
Description = "Grid Snap",
CreateIcon = () => new SpriteIcon { Icon = OsuIcon.EditorGridSnap },
})
.Concat(DistanceSnapProvider.CreateTernaryButtons());

private BindableList<HitObject> selectedHitObjects;
Expand Down
9 changes: 7 additions & 2 deletions osu.Game/Rulesets/Edit/ComposerDistanceSnapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,14 @@ protected override void Update()
}
}

public IEnumerable<TernaryButton> CreateTernaryButtons() => new[]
public IEnumerable<DrawableTernaryButton> CreateTernaryButtons() => new[]
{
new TernaryButton(DistanceSnapToggle, "Distance Snap", () => new SpriteIcon { Icon = OsuIcon.EditorDistanceSnap })
new DrawableTernaryButton
{
Current = DistanceSnapToggle,
Description = "Distance Snap",
CreateIcon = () => new SpriteIcon { Icon = OsuIcon.EditorDistanceSnap },
}
};

public void HandleToggleViaKey(KeyboardEvent key)
Expand Down
14 changes: 4 additions & 10 deletions osu.Game/Rulesets/Edit/HitObjectComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,9 @@ private void load(OsuConfigManager config, [CanBeNull] Editor editor)
};
}

TernaryStates = CreateTernaryButtons().ToArray();
togglesCollection.AddRange(TernaryStates.Select(b => new DrawableTernaryButton(b)));
togglesCollection.AddRange(CreateTernaryButtons().ToArray());

sampleBankTogglesCollection.AddRange(BlueprintContainer.SampleBankTernaryStates.Zip(BlueprintContainer.SampleAdditionBankTernaryStates).Select(b => new SampleBankTernaryButton(b.First, b.Second)));
sampleBankTogglesCollection.AddRange(BlueprintContainer.SampleBankTernaryStates);

SetSelectTool();

Expand Down Expand Up @@ -368,15 +367,10 @@ protected override void Update()
/// </remarks>
protected abstract IReadOnlyList<CompositionTool> CompositionTools { get; }

/// <summary>
/// A collection of states which will be displayed to the user in the toolbox.
/// </summary>
public TernaryButton[] TernaryStates { get; private set; }

/// <summary>
/// Create all ternary states required to be displayed to the user.
/// </summary>
protected virtual IEnumerable<TernaryButton> CreateTernaryButtons() => BlueprintContainer.MainTernaryStates;
protected virtual IEnumerable<DrawableTernaryButton> CreateTernaryButtons() => BlueprintContainer.MainTernaryStates;

/// <summary>
/// Construct a relevant blueprint container. This will manage hitobject selection/placement input handling and display logic.
Expand Down Expand Up @@ -437,7 +431,7 @@ protected override bool OnKeyDown(KeyDownEvent e)
{
if (togglesCollection.ElementAtOrDefault(rightIndex) is DrawableTernaryButton button)
{
button.Button.Toggle();
button.Toggle();
return true;
}
}
Expand Down
7 changes: 6 additions & 1 deletion osu.Game/Rulesets/Edit/ScrollingHitObjectComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ private void load(OsuConfigManager config)
Spacing = new Vector2(0, 5),
Children = new[]
{
new DrawableTernaryButton(new TernaryButton(showSpeedChanges, "Show speed changes", () => new SpriteIcon { Icon = FontAwesome.Solid.TachometerAlt }))
new DrawableTernaryButton
{
Current = showSpeedChanges,
Description = "Show speed changes",
CreateIcon = () => new SpriteIcon { Icon = FontAwesome.Solid.TachometerAlt },
}
}
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
Expand All @@ -16,23 +19,38 @@

namespace osu.Game.Screens.Edit.Components.TernaryButtons
{
public partial class DrawableTernaryButton : OsuButton, IHasTooltip
public partial class DrawableTernaryButton : OsuButton, IHasTooltip, IHasCurrentValue<TernaryState>
{
public Bindable<TernaryState> Current
{
get => current.Current;
set => current.Current = value;
}

private readonly BindableWithCurrent<TernaryState> current = new BindableWithCurrent<TernaryState>();

public required LocalisableString Description
{
get => Text;
set => Text = value;
}

public LocalisableString TooltipText { get; set; }

/// <summary>
/// A function which creates a drawable icon to represent this item. If null, a sane default should be used.
/// </summary>
public Func<Drawable>? CreateIcon { get; init; }

private Color4 defaultBackgroundColour;
private Color4 defaultIconColour;
private Color4 selectedBackgroundColour;
private Color4 selectedIconColour;

protected Drawable Icon { get; private set; } = null!;

public readonly TernaryButton Button;

public DrawableTernaryButton(TernaryButton button)
public DrawableTernaryButton()
{
Button = button;

Text = button.Description;

RelativeSizeAxes = Axes.X;
}

Expand All @@ -45,7 +63,7 @@ private void load(OverlayColourProvider colourProvider)
defaultIconColour = defaultBackgroundColour.Darken(0.5f);
selectedIconColour = selectedBackgroundColour.Lighten(0.5f);

Add(Icon = (Button.CreateIcon?.Invoke() ?? new Circle()).With(b =>
Add(Icon = (CreateIcon?.Invoke() ?? new Circle()).With(b =>
{
b.Blending = BlendingParameters.Additive;
b.Anchor = Anchor.CentreLeft;
Expand All @@ -59,26 +77,40 @@ protected override void LoadComplete()
{
base.LoadComplete();

Button.Bindable.BindValueChanged(_ => updateSelectionState(), true);
Button.Enabled.BindTo(Enabled);
current.BindValueChanged(_ => updateSelectionState(), true);

Action = onAction;
}

private void onAction()
{
if (!Button.Enabled.Value)
if (!Enabled.Value)
return;

Button.Toggle();
Toggle();
}

public void Toggle()
{
switch (Current.Value)
{
case TernaryState.False:
case TernaryState.Indeterminate:
Current.Value = TernaryState.True;
break;

case TernaryState.True:
Current.Value = TernaryState.False;
break;
}
}

private void updateSelectionState()
{
if (!IsLoaded)
return;

switch (Button.Bindable.Value)
switch (Current.Value)
{
case TernaryState.Indeterminate:
Icon.Colour = selectedIconColour.Darken(0.5f);
Expand All @@ -104,7 +136,5 @@ private void updateSelectionState()
Anchor = Anchor.CentreLeft,
X = 40f
};

public LocalisableString TooltipText => Button.Tooltip;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using Humanizer;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Edit;

namespace osu.Game.Screens.Edit.Components.TernaryButtons
{
public partial class SampleBankTernaryButton : CompositeDrawable
{
public readonly TernaryButton NormalButton;
public readonly TernaryButton AdditionsButton;
public string BankName { get; }
public Func<Drawable>? CreateIcon { get; init; }

public SampleBankTernaryButton(TernaryButton normalButton, TernaryButton additionsButton)
public readonly BindableWithCurrent<TernaryState> NormalState = new BindableWithCurrent<TernaryState>();
public readonly BindableWithCurrent<TernaryState> AdditionsState = new BindableWithCurrent<TernaryState>();

public DrawableTernaryButton NormalButton { get; private set; } = null!;
public DrawableTernaryButton AdditionsButton { get; private set; } = null!;

public SampleBankTernaryButton(string bankName)
{
NormalButton = normalButton;
AdditionsButton = additionsButton;
BankName = bankName;
}

[BackgroundDependencyLoader]
Expand All @@ -36,7 +45,12 @@ private void load()
AutoSizeAxes = Axes.Y,
Width = 0.5f,
Padding = new MarginPadding { Right = 1 },
Child = new InlineDrawableTernaryButton(NormalButton),
Child = NormalButton = new InlineDrawableTernaryButton
{
Current = NormalState,
Description = BankName.Titleize(),
CreateIcon = CreateIcon,
},
},
new Container
{
Expand All @@ -46,18 +60,18 @@ private void load()
AutoSizeAxes = Axes.Y,
Width = 0.5f,
Padding = new MarginPadding { Left = 1 },
Child = new InlineDrawableTernaryButton(AdditionsButton),
Child = AdditionsButton = new InlineDrawableTernaryButton
{
Current = AdditionsState,
Description = BankName.Titleize(),
CreateIcon = CreateIcon,
},
},
};
}

private partial class InlineDrawableTernaryButton : DrawableTernaryButton
{
public InlineDrawableTernaryButton(TernaryButton button)
: base(button)
{
}

[BackgroundDependencyLoader]
private void load()
{
Expand Down
48 changes: 0 additions & 48 deletions osu.Game/Screens/Edit/Components/TernaryButtons/TernaryButton.cs

This file was deleted.

Loading
Loading