Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
V0odo0 committed Nov 8, 2024
1 parent bea5344 commit aa6e36d
Show file tree
Hide file tree
Showing 32 changed files with 1,544 additions and 51 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.4.0 (2024-11-08)

* New: Plural Forms
* Added KNOT_LOCALIZATION_DISABLE_EXTENSIONS define symbol. You can define it to get rid of project-wide extension methods
* Updated Samples/Demo

## 1.3.2 (2024-10-25)

* KnotTextKeyReference & KnotAssetReference ToString impl
Expand Down
32 changes: 29 additions & 3 deletions Editor/Scripts/UI/KnotKeysTabPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ protected KnotKeysTabPanel() : base("KnotKeysTabPanel")
TreeView = GetKeysTreeView();
TreeView.SearchFilter = SelectedSearchFilter;

TreeView.RemoveKeys += RemoveKeys;
TreeView.DuplicateKeys += DuplicateKeys;
TreeView.RequestRemoveKeys += RemoveKeys;
TreeView.RequestDuplicateKeys += DuplicateKeys;
TreeView.KeysSelected += SelectKeys;
if (TreeView is KnotTextKeysTreeView textKeysTreeView)
textKeysTreeView.RequestCreatePluralFormKey += CreatePluralFormKeys;

TreeViewContainer = Root.Q<IMGUIContainer>(nameof(TreeViewContainer));
TreeViewContainer.onGUIHandler = OnTreeViewGUI;
Expand Down Expand Up @@ -252,6 +254,30 @@ protected virtual void DuplicateKeys(params TKeyView[] keyViews)
TreeView.FrameKeys(true, newKeys.Values.ToArray());
}

protected virtual void CreatePluralFormKeys(KnotTextKeyView[] keyViews, KnotPluralForm pluralForm)
{
var newKeys = new HashSet<string>();
EditorUtils.RegisterCompleteObjects(nameof(CreatePluralFormKeys), () =>
{
//Duplicate keys
foreach (var keyView in keyViews.Where(view => view.SourceCollection != null && view.SourceCollection.ContainsKey(view.Key)))
{
var pluralKey = $"{keyView.Key}.{pluralForm.ToString()}";
if (keyView.SourceCollection.ContainsKey(pluralKey))
continue;

keyView.SourceCollection.Add(new KnotKeyData(pluralKey, keyView.KeyData));
newKeys.Add(pluralKey);

TreeView.SetExpanded(keyView.id, true);
}

}, keyViews.Select(v => v.SourceCollection).ToArray());

ReloadLayout();
TreeView.FrameKeys(true, newKeys.ToArray());
}

protected virtual void AddToKeyCollection(KnotKeyCollection keyCollection, params TKeyView[] keyViews)
{
if (!IsKeyCollectionsPersistent())
Expand Down Expand Up @@ -330,7 +356,7 @@ protected virtual void SelectKeys(TKeyView[] keyViews)

KeyViewEditor.Bind(keyViews);
}

protected override void BuildTabOptionsMenu(DropdownMenu menu)
{
base.BuildTabOptionsMenu(menu);
Expand Down
30 changes: 18 additions & 12 deletions Editor/Scripts/UI/KnotKeysTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public abstract class KnotKeysTreeView<TKeyView> : TreeView where TKeyView : Kno
{
public static string DragAndDropKeyName => $"{EditorUtils.CorePrefix}.{typeof(TKeyView).Name}";


public event Action<TKeyView[]> KeysSelected;
public event Action<TKeyView[]> RequestRemoveKeys;
public event Action<TKeyView[]> RequestDuplicateKeys;

static readonly GUIContent AddToKeyCollectionContent = new GUIContent("Add to Key Collection");
static readonly GUIContent RemoveFromKeyCollectionContent = new GUIContent("Remove from Key Collection");
static readonly GUIContent DuplicateKeyContent = new GUIContent("Duplicate");
static readonly GUIContent RemoveKeyContent = new GUIContent("Remove");


public event Action<TKeyView[]> KeysSelected;
public event Action<TKeyView[]> RemoveKeys;
public event Action<TKeyView[]> DuplicateKeys;


public IReadOnlyList<KnotTreeViewKeyItem> AllKeyItems => _allKeyItems;
Expand Down Expand Up @@ -171,16 +171,21 @@ protected sealed override void SetupDragAndDrop(SetupDragAndDropArgs args)
DragAndDrop.StartDrag(DragAndDropKeyName);
}

protected override void ContextClickedItem(int id)
protected virtual GenericMenu BuildGenericMenu()
{
GenericMenu menu = new GenericMenu();
var menu = new GenericMenu();

PrebuildGenericMenu(menu);
menu.AddItem(DuplicateKeyContent, false, RequestDuplicateSelected);
menu.AddItem(RemoveKeyContent, false, RequestRemoveSelected);

menu.ShowAsContext();
return menu;
}

protected virtual void PrebuildGenericMenu(GenericMenu menu) { }

protected override void ContextClickedItem(int id) => BuildGenericMenu().ShowAsContext();

void RequestRemoveSelected()
{
if (!SelectedKeyItems.Any())
Expand All @@ -189,9 +194,9 @@ void RequestRemoveSelected()
if (SelectedKeyItems.Any(item => item is KnotTreeViewKeyItemGroup))
{
var groupKeys = SelectedKeyItems.OfType<KnotTreeViewKeyItemGroup>().Select(g => g.Key);
RemoveKeys?.Invoke(AllKeyItems.Where(item => groupKeys.Any(key => item.Key.StartsWith($"{key}."))).OfType<TKeyView>().ToArray());
RequestRemoveKeys?.Invoke(AllKeyItems.Where(item => groupKeys.Any(key => item.Key.StartsWith($"{key}."))).OfType<TKeyView>().ToArray());
}
else RemoveKeys?.Invoke(SelectedKeyViews.ToArray());
else RequestRemoveKeys?.Invoke(SelectedKeyViews.ToArray());
}

void RequestDuplicateSelected()
Expand All @@ -202,11 +207,12 @@ void RequestDuplicateSelected()
if (SelectedKeyItems.Any(item => item is KnotTreeViewKeyItemGroup))
{
var groupKeys = SelectedKeyItems.OfType<KnotTreeViewKeyItemGroup>().Select(g => g.Key);
DuplicateKeys?.Invoke(AllKeyItems.Where(item => groupKeys.Any(key => item.Key.StartsWith(key))).OfType<TKeyView>().ToArray());
RequestDuplicateKeys?.Invoke(AllKeyItems.Where(item => groupKeys.Any(key => item.Key.StartsWith(key))).OfType<TKeyView>().ToArray());
}
else DuplicateKeys?.Invoke(SelectedKeyItems.OfType<TKeyView>().ToArray());
else RequestDuplicateKeys?.Invoke(SelectedKeyItems.OfType<TKeyView>().ToArray());
}


protected override void KeyEvent()
{
if (Event.current.type == EventType.KeyDown)
Expand Down
25 changes: 24 additions & 1 deletion Editor/Scripts/UI/KnotTextKeysTreeView.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using System.Linq;
using System;
using System.Linq;
using Knot.Core.Editor;
using Knot.Localization.Data;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace Knot.Localization.Editor
{
public sealed class KnotTextKeysTreeView : KnotKeysTreeView<KnotTextKeyView>
{
public event Action<KnotTextKeyView[], KnotPluralForm> RequestCreatePluralFormKey;

public static IKnotKeyViewLabel<KnotTextKeyView>[] Labels
{
get
Expand All @@ -23,5 +30,21 @@ public KnotTextKeysTreeView() : base(EditorUtils.UserSettings.TextKeysTreeViewSt
{

}


protected override void PrebuildGenericMenu(GenericMenu menu)
{
foreach (KnotPluralForm pluralForm in Enum.GetValues(typeof(KnotPluralForm)))
menu.AddItem(new GUIContent($"Add Plural Form/{pluralForm.ToString()}"),
false, () => RequestCreatePluralFormKeySelected(pluralForm));
}

void RequestCreatePluralFormKeySelected(KnotPluralForm pluralForm)
{
if (!SelectedKeyItems.Any())
return;

RequestCreatePluralFormKey?.Invoke(SelectedKeyViews.ToArray(), pluralForm);
}
}
}
2 changes: 1 addition & 1 deletion Editor/Scripts/UI/Windows/KnotDatabaseEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void UpdateActiveDatabase()
{
if (EditorUtils.ActiveDatabase != null)
return;

EditorUtils.UpdateDatabaseAssets();
ReloadLayout();
Repaint();
Expand Down
1 change: 1 addition & 0 deletions Runtime/Scripts/Controllers/KnotTextController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class KnotTextController : KnotController<KnotTextData, IKnotText, string

[SerializeField] private string _fallbackValue = KeyFormatArg;


protected override IKnotText CreateValueFromItemData(KnotTextData itemData, params IKnotMetadata[] metadata)
{
return new KnotText(itemData.RawText, metadata);
Expand Down
15 changes: 15 additions & 0 deletions Runtime/Scripts/Data/KnotPluralForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Knot.Localization.Data
{
[Serializable]
public enum KnotPluralForm
{
Zero,
One,
Two,
Few,
Many,
Other
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Data/KnotPluralForm.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Scripts/Key References/KnotTextKeyReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ protected override void UnRegisterValueUpdatedCallback(string key, Action<string
KnotLocalization.UnRegisterTextUpdatedCallback(key, valueUpdated);
}

public virtual string GetValue(KnotPluralForm pluralForm)
{
if (_formatters == null || _formatters.Count == 0)
return KnotLocalization.GetText(Key, pluralForm);

return KnotText.Format(KnotLocalization.GetText(Key, pluralForm), Formatters);
}

public override string ToString() => Application.isPlaying? Value : Key;
}
}
15 changes: 10 additions & 5 deletions Runtime/Scripts/KnotLocalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,22 @@ internal static void Log(object message, LogType type, Object context = null)
public static string GetText(string key) => Manager.GetTextValue(key)?.Value;

/// <summary>
/// Returns current language <see cref="IKnotText"/> assigned to <paramref name="key"/> from <see cref="Manager"/>
/// A shortcut for <see cref="IKnotManager.GetTextValue"/>. Returns current language localized text assigned to <paramref name="key"/> from <see cref="Manager"/> with <see cref="KnotPluralForm"/>
/// </summary>
public static IKnotText GetTextFormat(string key) => Manager.GetTextValue(key);
public static string GetText(string key, KnotPluralForm pluralForm) => Manager.GetTextValue(key, pluralForm)?.Value;

#if !KNOT_LOCALIZATION_DISABLE_EXTENSIONS
/// <summary>
/// Returns current language localized text assigned to <paramref name="key"/> from <see cref="Manager"/>
/// Extensions method that returns current language localized text assigned to <paramref name="key"/> from <see cref="Manager"/>
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string Localize(this string key) => Manager.GetTextValue(key)?.Value;

/// <summary>
/// Extensions method that returns current language localized text assigned to <paramref name="key"/> from <see cref="Manager"/> for <see cref="KnotPluralForm"/>
/// </summary>
public static string Localize(this string key, KnotPluralForm pluralForm) => Manager.GetTextValue(key, pluralForm)?.Value;
#endif

/// <summary>
/// A shortcut for <see cref="IKnotManager.GetAssetValue"/>. Returns asset assigned to <paramref name="key"/> from <see cref="Manager"/>
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Scripts/Managers/IKnotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public interface IKnotManager : IDisposable
/// </summary>
IKnotText GetTextValue(string key);

/// <summary>
/// Returns <see cref="IKnotText"/> with given <paramref name="key"/> from <see cref="TextController"/> with <see cref="KnotPluralForm"/>
/// </summary>
IKnotText GetTextValue(string key, KnotPluralForm pluralForm);

/// <summary>
/// Returns <see cref="IKnotAsset"/> with given <paramref name="key"/> from <see cref="AssetController"/>
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions Runtime/Scripts/Managers/KnotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ public virtual void LoadLanguage(KnotLanguageData languageData)

public virtual IKnotText GetTextValue(string key) => TextController.TryGetValue(key, out var value) ? value : new KnotText(TextController.GetFallbackValue(key));

public virtual IKnotText GetTextValue(string key, KnotPluralForm pluralForm)
{
var pluralFormKey = $"{key}.{pluralForm.ToString()}";
if (TextController.ContainsKey(pluralFormKey))
{
if (TextController.TryGetValue(pluralFormKey, out var value))
return value;

if (pluralForm != KnotPluralForm.One && TextController.TryGetValue($"{key}.{KnotPluralForm.Many}", out var manyValue))
return manyValue;
}

return GetTextValue(key);
}

public virtual IKnotAsset GetAssetValue(string key) => AssetController.TryGetValue(key, out var value) ? value : new KnotAsset(AssetController.GetFallbackValue(key));

public virtual void Dispose()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit aa6e36d

Please sign in to comment.