From f32cb79973fd3d553020a701430331690802a54e Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 3 Sep 2023 16:04:56 +0800 Subject: [PATCH] Create edit mode switch button sub-section for able to switch to different edit mode with description. --- .../Settings/EditModeSwitchSubsection.cs | 160 ++++++++++++++++++ .../TimeTags/TimeTagCreateConfigSubsection.cs | 125 +------------- 2 files changed, 164 insertions(+), 121 deletions(-) create mode 100644 osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/EditModeSwitchSubsection.cs diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/EditModeSwitchSubsection.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/EditModeSwitchSubsection.cs new file mode 100644 index 000000000..9c94a798d --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/EditModeSwitchSubsection.cs @@ -0,0 +1,160 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Collections.Generic; +using System.Linq; +using osu.Framework.Allocation; +using osu.Framework.Bindables; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Framework.Graphics.UserInterface; +using osu.Framework.Localisation; +using osu.Game.Graphics; +using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings.Components.Markdown; +using osu.Game.Rulesets.Karaoke.Screens.Edit.Components.Markdown; +using osuTK; +using osuTK.Graphics; + +namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings; + +public abstract partial class EditModeSwitchSubsection : FillFlowContainer, IHasCurrentValue + where TEditMode : struct, Enum +{ + private const int button_vertical_margin = 20; + private const int horizontal_padding = 20; + private const int corner_radius = 15; + + private readonly BindableWithCurrent current = new(); + + public Bindable Current + { + get => current.Current; + set => current.Current = value; + } + + private readonly Box background; + + protected EditModeSwitchSubsection() + { + RelativeSizeAxes = Axes.X; + AutoSizeAxes = Axes.Y; + Spacing = new Vector2(10); + + LyricEditorDescriptionTextFlowContainer lyricEditorDescription; + + Children = new Drawable[] + { + new Container + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Masking = true, + CornerRadius = corner_radius, + Children = new Drawable[] + { + background = new Box + { + RelativeSizeAxes = Axes.Both, + }, + new EditModeSwitcher(this) + { + Current = current, + }, + }, + }, + lyricEditorDescription = new LyricEditorDescriptionTextFlowContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + Padding = new MarginPadding { Horizontal = horizontal_padding }, + }, + }; + + current.BindValueChanged(e => + { + // update description text. + lyricEditorDescription.Description = GetDescription(e.NewValue); + }, true); + } + + [BackgroundDependencyLoader] + private void load(LyricEditorColourProvider colourProvider, ILyricEditorState state) + { + background.Colour = colourProvider.Background4(state.Mode); + } + + protected abstract LocalisableString GetButtonTitle(TEditMode mode); + + protected abstract Color4 GetButtonColour(OsuColour colours, TEditMode mode, bool active); + + protected abstract DescriptionFormat GetDescription(TEditMode mode); + + private partial class EditModeSwitcher : CompositeDrawable, IHasCurrentValue + { + private readonly BindableWithCurrent current = new(); + + public Bindable Current + { + get => current.Current; + set => current.Current = value; + } + + private readonly EditModeSwitchSubsection parent; + private readonly EditModeButton[] buttons; + + public EditModeSwitcher(EditModeSwitchSubsection parent) + { + this.parent = parent; + + RelativeSizeAxes = Axes.X; + AutoSizeAxes = Axes.Y; + + InternalChild = new GridContainer + { + RelativeSizeAxes = Axes.X, + AutoSizeAxes = Axes.Y, + RowDimensions = new[] + { + new Dimension(GridSizeMode.AutoSize), + }, + Content = new[] + { + buttons = Enum.GetValues().Select(x => new EditModeButton(x) + { + Text = parent.GetButtonTitle(x), + Margin = new MarginPadding { Vertical = button_vertical_margin }, + Padding = new MarginPadding { Horizontal = horizontal_padding }, + Action = () => current.Value = x, + }).ToArray(), + }, + }; + } + + [BackgroundDependencyLoader] + private void load(OsuColour colours) + { + current.BindValueChanged(e => + { + // update button style. + foreach (var button in buttons) + { + bool highLight = EqualityComparer.Default.Equals(button.Mode, e.NewValue); + button.Alpha = highLight ? 0.8f : 0.4f; + button.BackgroundColour = parent.GetButtonColour(colours, button.Mode, highLight); + } + }, true); + } + + private partial class EditModeButton : EditorSectionButton + { + public TEditMode Mode { get; } + + public EditModeButton(TEditMode mode) + { + Mode = mode; + } + } + } +} diff --git a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/TimeTags/TimeTagCreateConfigSubsection.cs b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/TimeTags/TimeTagCreateConfigSubsection.cs index 954f48cfe..f0f62442c 100644 --- a/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/TimeTags/TimeTagCreateConfigSubsection.cs +++ b/osu.Game.Rulesets.Karaoke/Screens/Edit/Beatmaps/Lyrics/Settings/TimeTags/TimeTagCreateConfigSubsection.cs @@ -3,124 +3,17 @@ using System; using System.Collections.Generic; -using System.Linq; -using osu.Framework.Allocation; -using osu.Framework.Bindables; -using osu.Framework.Graphics; -using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Shapes; -using osu.Framework.Graphics.UserInterface; using osu.Framework.Localisation; using osu.Game.Graphics; -using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings.Components.Markdown; using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States.Modes; using osu.Game.Rulesets.Karaoke.Screens.Edit.Components.Markdown; -using osuTK; using osuTK.Graphics; namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings.TimeTags; -public partial class TimeTagCreateConfigSubsection : FillFlowContainer, IHasCurrentValue +public partial class TimeTagCreateConfigSubsection : EditModeSwitchSubsection { - private const int button_vertical_margin = 20; - private const int horizontal_padding = 20; - private const int corner_radius = 15; - - private readonly EditModeButton[] buttons; - private readonly LyricEditorDescriptionTextFlowContainer lyricEditorDescription; - - private readonly BindableWithCurrent current = new(); - - public Bindable Current - { - get => current.Current; - set => current.Current = value; - } - - [Resolved] - private OsuColour colours { get; set; } = null!; - - private readonly Box background; - - public TimeTagCreateConfigSubsection() - { - RelativeSizeAxes = Axes.X; - AutoSizeAxes = Axes.Y; - Spacing = new Vector2(10); - - Children = new Drawable[] - { - new Container - { - RelativeSizeAxes = Axes.X, - AutoSizeAxes = Axes.Y, - Masking = true, - CornerRadius = corner_radius, - Children = new Drawable[] - { - background = new Box - { - RelativeSizeAxes = Axes.Both, - }, - new GridContainer - { - RelativeSizeAxes = Axes.X, - AutoSizeAxes = Axes.Y, - RowDimensions = new[] - { - new Dimension(GridSizeMode.AutoSize), - }, - Content = new[] - { - buttons = Enum.GetValues().Select(x => new EditModeButton(x) - { - Text = getButtonTitle(x), - Margin = new MarginPadding { Vertical = button_vertical_margin }, - Padding = new MarginPadding { Horizontal = horizontal_padding }, - Action = () => current.Value = x, - }).ToArray(), - }, - }, - }, - }, - lyricEditorDescription = new LyricEditorDescriptionTextFlowContainer - { - RelativeSizeAxes = Axes.X, - AutoSizeAxes = Axes.Y, - Padding = new MarginPadding { Horizontal = horizontal_padding }, - }, - }; - - current.BindValueChanged(e => - { - Schedule(() => - { - updateEditMode(e.NewValue); - }); - }, true); - } - - [BackgroundDependencyLoader] - private void load(LyricEditorColourProvider colourProvider, ILyricEditorState state) - { - background.Colour = colourProvider.Background4(state.Mode); - } - - private void updateEditMode(CreateTimeTagEditMode mode) - { - // update button style. - foreach (var button in buttons) - { - bool highLight = EqualityComparer.Default.Equals(button.Mode, mode); - button.Alpha = highLight ? 0.8f : 0.4f; - button.BackgroundColour = getButtonColour(button.Mode, highLight); - } - - // update description text. - lyricEditorDescription.Description = getDescription(mode); - } - - private LocalisableString getButtonTitle(CreateTimeTagEditMode mode) + protected override LocalisableString GetButtonTitle(CreateTimeTagEditMode mode) => mode switch { CreateTimeTagEditMode.Create => "Create", @@ -128,7 +21,7 @@ private LocalisableString getButtonTitle(CreateTimeTagEditMode mode) _ => throw new InvalidOperationException(nameof(mode)), }; - private Color4 getButtonColour(CreateTimeTagEditMode mode, bool active) + protected override Color4 GetButtonColour(OsuColour colours, CreateTimeTagEditMode mode, bool active) => mode switch { CreateTimeTagEditMode.Create => active ? colours.Green : colours.GreenDarker, @@ -136,7 +29,7 @@ private Color4 getButtonColour(CreateTimeTagEditMode mode, bool active) _ => throw new InvalidOperationException(nameof(mode)), }; - private DescriptionFormat getDescription(CreateTimeTagEditMode mode) => + protected override DescriptionFormat GetDescription(CreateTimeTagEditMode mode) => mode switch { CreateTimeTagEditMode.Create => new DescriptionFormat @@ -200,14 +93,4 @@ private DescriptionFormat getDescription(CreateTimeTagEditMode mode) => }, _ => throw new InvalidOperationException(nameof(mode)), }; - - private partial class EditModeButton : EditorSectionButton - { - public CreateTimeTagEditMode Mode { get; } - - public EditModeButton(CreateTimeTagEditMode mode) - { - Mode = mode; - } - } }