Skip to content

Commit

Permalink
Merge pull request #1581 from andy840119/should-reset-selected-items-…
Browse files Browse the repository at this point in the history
…in-the-mode-status

Should reset selected items in the mode status
  • Loading branch information
andy840119 authored Sep 11, 2022
2 parents 7593f53 + 9c0d30c commit a275923
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ protected void TriggerSelect()
SelectedItems.Add(item);
}

protected void TriggerUnselect()
{
SelectedItems.Remove(item);
}

protected abstract bool GetFieldValue(T item);

protected abstract void ApplyValue(T item, bool value);
Expand All @@ -73,13 +68,7 @@ protected void TriggerUnselect()
Selected = selected =>
{
if (selected)
{
TriggerSelect();
}
else
{
TriggerUnselect();
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ protected LabelledObjectFieldTextBox(T item)
{
bool highLight = SelectedItems.Contains(item);
Component.HighLight = highLight;

if (SelectedItems.Contains(item) && SelectedItems.Count == 1)
focus();
});

if (InternalChildren[1] is not FillFlowContainer fillFlowContainer)
Expand All @@ -70,11 +73,6 @@ protected void TriggerSelect()
SelectedItems.Add(item);
}

protected void TriggerUnselect()
{
SelectedItems.Remove(item);
}

protected abstract string GetFieldValue(T item);

protected abstract void ApplyValue(T item, string value);
Expand All @@ -89,24 +87,25 @@ protected void TriggerUnselect()
Selected = selected =>
{
if (selected)
{
TriggerSelect();
}
else
{
TriggerUnselect();
}
}
};

public void Focus()
private void focus()
{
Schedule(() =>
{
var focusedDrawable = GetContainingInputManager().FocusedDrawable;
if (focusedDrawable != null && IsFocused(focusedDrawable))
return;

GetContainingInputManager().ChangeFocus(Component);
});
}

protected virtual bool IsFocused(Drawable focusedDrawable)
=> focusedDrawable == Component;

protected class ObjectFieldTextBox : OsuTextBox
{
[Resolved]
Expand All @@ -124,6 +123,9 @@ protected override void OnFocusLost(FocusLostEvent e)
{
base.OnFocusLost(e);

// should not change the border size because still need to highlight the textarea without focus.
BorderThickness = 3f;

// note: should trigger commit event first in the base class.
Selected?.Invoke(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ void reCreateEditComponents()
}
}

protected override void UpdateDisabledState(bool disabled)
{
if (disabled)
return;

// should auto-focus to the first note property if change the lyric.
var firstTextTagTextBox = Children.OfType<LabelledObjectFieldTextBox<Note>>().FirstOrDefault();
firstTextTagTextBox?.Focus();
}

[BackgroundDependencyLoader]
private void load(IEditNoteModeState editNoteModeState)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ protected LabelledTextTagTextBox(Lyric lyric, T textTag)
// trigger selected if hover on delete button.
TriggerSelect();
}
else
{
// do not clear current selected if typing.
if (Component.HasFocus)
return;

TriggerUnselect();
}
}
}
});
Expand All @@ -95,13 +87,7 @@ protected LabelledTextTagTextBox(Lyric lyric, T textTag)
Selected = selected =>
{
if (selected)
{
TriggerSelect();
}
else
{
TriggerUnselect();
}
},
Action = (indexType, action) =>
{
Expand Down Expand Up @@ -153,6 +139,9 @@ protected sealed override string GetFieldValue(T item)

protected abstract void RemoveTextTag(T item);

protected override bool IsFocused(Drawable focusedDrawable)
=> base.IsFocused(focusedDrawable) || focusedDrawable == indexShiftingPart;

public new CompositeDrawable TabbableContentContainer
{
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ protected override void OnLyricChanged(Lyric lyric)
TextTags.BindTo(GetBindableTextTags(lyric));
}

protected override void UpdateDisabledState(bool disabled)
{
if (disabled)
return;

// should auto-focus to the first time-tag if change the lyric.
var firstTextTagTextBox = Children.OfType<LabelledTextTagTextBox<TTextTag>>().FirstOrDefault();
firstTextTagTextBox?.Focus();
}

private void addCreateButton()
{
var fillFlowContainer = Content as FillFlowContainer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.Extends.Notes;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Rulesets.Objects;
using osu.Game.Screens.Edit;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.States.Modes
{
public class EditNoteModeState : Component, IEditNoteModeState
public class EditNoteModeState : ModeStateWithBlueprintContainer<Note>, IEditNoteModeState
{
private readonly Bindable<NoteEditMode> bindableEditMode = new();
private readonly BindableList<HitObject> selectedHitObjects = new();

[Resolved, AllowNull]
private EditorBeatmap editorBeatmap { get; set; }

public IBindable<NoteEditMode> BindableEditMode => bindableEditMode;

public void ChangeEditMode(NoteEditMode mode)
=> bindableEditMode.Value = mode;

public BindableList<Note> SelectedItems { get; } = new();

public Bindable<NoteEditModeSpecialAction> BindableSpecialAction { get; } = new();

public Bindable<NoteEditPropertyMode> NoteEditPropertyMode { get; } = new();

[BackgroundDependencyLoader]
private void load(EditorBeatmap editorBeatmap)
private void load()
{
BindablesUtils.Sync(SelectedItems, selectedHitObjects);
selectedHitObjects.BindTo(editorBeatmap.SelectedHitObjects);
}

protected override bool IsWriteLyricPropertyLocked(Lyric lyric)
=> HitObjectWritableUtils.IsCreateOrRemoveNoteLocked(lyric);

protected override bool SelectFirstProperty(Lyric lyric)
=> BindableEditMode.Value == NoteEditMode.Edit;

protected override IEnumerable<Note> SelectableProperties(Lyric lyric)
=> EditorBeatmapUtils.GetNotesByLyric(editorBeatmap, lyric);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.States.Modes
{
public class EditRomajiModeState : Component, IEditRomajiModeState
public class EditRomajiModeState : ModeStateWithBlueprintContainer<RomajiTag>, IEditRomajiModeState
{
private readonly Bindable<TextTagEditMode> bindableEditMode = new();

Expand All @@ -16,6 +17,13 @@ public class EditRomajiModeState : Component, IEditRomajiModeState
public void ChangeEditMode(TextTagEditMode mode)
=> bindableEditMode.Value = mode;

public BindableList<RomajiTag> SelectedItems { get; } = new();
protected override bool IsWriteLyricPropertyLocked(Lyric lyric)
=> HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.RomajiTags));

protected override bool SelectFirstProperty(Lyric lyric)
=> BindableEditMode.Value == TextTagEditMode.Edit;

protected override IEnumerable<RomajiTag> SelectableProperties(Lyric lyric)
=> lyric.RomajiTags;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.States.Modes
{
public class EditRubyModeState : Component, IEditRubyModeState
public class EditRubyModeState : ModeStateWithBlueprintContainer<RubyTag>, IEditRubyModeState
{
private readonly Bindable<TextTagEditMode> bindableEditMode = new();

Expand All @@ -16,6 +17,13 @@ public class EditRubyModeState : Component, IEditRubyModeState
public void ChangeEditMode(TextTagEditMode mode)
=> bindableEditMode.Value = mode;

public BindableList<RubyTag> SelectedItems { get; } = new();
protected override bool IsWriteLyricPropertyLocked(Lyric lyric)
=> HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.RubyTags));

protected override bool SelectFirstProperty(Lyric lyric)
=> BindableEditMode.Value == TextTagEditMode.Edit;

protected override IEnumerable<RubyTag> SelectableProperties(Lyric lyric)
=> lyric.RubyTags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) andy840119 <[email protected]>. 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.Game.Rulesets.Karaoke.Edit.Lyrics.CaretPosition;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.States.Modes
{
public abstract class ModeStateWithBlueprintContainer<TObject> : Component, IHasBlueprintSelection<TObject> where TObject : class
{
private readonly IBindable<LyricEditorMode> bindableMode = new Bindable<LyricEditorMode>();
private readonly IBindable<ICaretPosition?> bindableCaretPosition = new Bindable<ICaretPosition?>();
private readonly IBindable<int> bindableLyricPropertyWritableVersion = new Bindable<int>();

public BindableList<TObject> SelectedItems { get; } = new();

protected ModeStateWithBlueprintContainer()
{
bindableMode.BindValueChanged(e =>
{
TriggerDisableStateChanged();
});

bindableCaretPosition.BindValueChanged(e =>
{
bindableLyricPropertyWritableVersion.UnbindBindings();

var lyric = e.NewValue?.Lyric;

if (lyric == null)
return;

bindableLyricPropertyWritableVersion.BindTo(lyric.LyricPropertyWritableVersion);
TriggerDisableStateChanged();
});

bindableLyricPropertyWritableVersion.BindValueChanged(_ =>
{
TriggerDisableStateChanged();
});
}

protected virtual void TriggerDisableStateChanged()
{
var caret = bindableCaretPosition.Value;
if (caret == null)
return;

var lyric = caret.Lyric;
var generateType = caret.GenerateType;

SelectedItems.Clear();
bool locked = IsWriteLyricPropertyLocked(lyric);
if (locked)
return;

switch (generateType)
{
case CaretGenerateType.Action:
if (SelectFirstProperty(lyric))
SelectedItems.Add(SelectableProperties(lyric).FirstOrDefault());
break;

case CaretGenerateType.TargetLyric:
break;

default:
throw new ArgumentOutOfRangeException();
}
}

protected abstract bool IsWriteLyricPropertyLocked(Lyric lyric);

protected abstract bool SelectFirstProperty(Lyric lyric);

protected abstract IEnumerable<TObject> SelectableProperties(Lyric lyric);

[BackgroundDependencyLoader]
private void load(ILyricEditorState state, ILyricCaretState lyricCaretState)
{
bindableMode.BindTo(state.BindableMode);
bindableCaretPosition.BindTo(lyricCaretState.BindableCaretPosition);
}
}
}
Loading

0 comments on commit a275923

Please sign in to comment.