1
0
mirror of https://github.com/ppy/osu.git synced 2026-05-16 01:22:52 +08:00
Files
osu-lazer/osu.Game/Graphics/UserInterface/ShearedToggleButton.cs
T
Krzysztof Gutkowski b76a7e1bb6 Refactor ShearedButton to allow easier relative sizing (#36802)
This commit rearranges the contents of `ShearedButtons` to be more
independent of each other in regards to sizing. Thanks to that, the
custom logic related to enabling autosizing is no longer necessary.
Witdh and height are no longer set via the constructor, and can be
freely configured using the initializer syntax.

Additionally, this allows the button to use relative sizing without
having to resort to any hackery with `Size` (this will come in handy for
me when implementing the new footer on multiplayer screens).

Given that most of the `ShearedButton`s currently in use set their width
explicitly, I did not set `AutoSizeAxes = Axes.X` like it would be by
default previously. Instead it is set on the only two such buttons (show
converts/selected mods on ssv2). I suppose it might be a good idea to
have it set that by default if no `Width` is specified, as right now
it'll just not show anything.

Also I've set the margin on the text field by default in all cases
instead of only when autosizing like how it was previously, since
otherwise it would be a pain to set that on each button instance when
needed. I've checked all affected components and could not find any text
overflowing issues that this could cause.

---------

Co-authored-by: Dean Herbert <pe@ppy.sh>
2026-03-06 02:09:05 +09:00

68 lines
2.2 KiB
C#

// 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.
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
namespace osu.Game.Graphics.UserInterface
{
public partial class ShearedToggleButton : ShearedButton
{
private Sample? sampleOff;
private Sample? sampleOn;
/// <summary>
/// Sheared toggle buttons by default play two samples when toggled: a click and a toggle (on/off).
/// Sometimes this might be too much. Setting this to <c>false</c> will silence the toggle sound.
/// </summary>
protected virtual bool PlayToggleSamples => true;
/// <summary>
/// Whether this button is currently toggled to an active state.
/// </summary>
public BindableBool Active { get; } = new BindableBool();
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleOn = audio.Samples.Get(@"UI/check-on");
sampleOff = audio.Samples.Get(@"UI/check-off");
}
protected override HoverSounds CreateHoverSounds(HoverSampleSet sampleSet) => new HoverSounds(sampleSet);
protected override void LoadComplete()
{
Active.BindDisabledChanged(disabled => Action = disabled ? null : Active.Toggle, true);
Active.BindValueChanged(_ =>
{
UpdateActiveState();
playSample();
});
UpdateActiveState();
base.LoadComplete();
}
protected virtual void UpdateActiveState()
{
DarkerColour = Active.Value ? ColourProvider.Highlight1 : ColourProvider.Background3;
LighterColour = Active.Value ? ColourProvider.Colour0 : ColourProvider.Background1;
TextColour = Active.Value ? ColourProvider.Background6 : ColourProvider.Content1;
}
private void playSample()
{
if (PlayToggleSamples)
{
if (Active.Value)
sampleOn?.Play();
else
sampleOff?.Play();
}
}
}
}