Skip to content

Commit

Permalink
Improve localization support and settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed Jan 6, 2025
1 parent 02d9731 commit 8af415f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Murder.Editor/CustomFields/LocalizedStringField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override (bool modified, object? result) ProcessInput(EditorMember member
bool modified = false;
LocalizedString? localizedString = (LocalizedString?)fieldValue;

LocalizationAsset localization = LocalizationServices.GetCurrentLocalization();
LocalizationAsset localization = Game.Data.GetDefaultLocalization();

if (localizedString is null || localizedString.Value.Id == Guid.Empty)
{
Expand Down
1 change: 1 addition & 0 deletions src/Murder.Editor/Data/Graphics/FontImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public static bool GenerateFontJsonAndPng(int fontIndex, string fontPath, int fo
bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
}

// EditorTextureServices.SaveAsPng(stream, Path.Join(sourcePackedPath, $"{name}.png"));
EditorTextureServices.ConvertPngStreamToQuoiGz(stream, imageSourcePackedPath);

// ProcessFinished:
Expand Down
6 changes: 3 additions & 3 deletions src/Murder.Editor/Services/EditorLocalizationServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class EditorLocalizationServices
{
public static LocalizedString? SearchLocalizedString()
{
LocalizationAsset localization = LocalizationServices.GetCurrentLocalization();
LocalizationAsset localization = Game.Data.GetDefaultLocalization();

SearchBoxSettings<Guid> settings = new(initialText: "Create localized string")
{
Expand Down Expand Up @@ -68,7 +68,7 @@ internal static class EditorLocalizationServices
{
LocalizedString result = new(Guid.NewGuid());

LocalizationAsset asset = Game.Data.Localization;
LocalizationAsset asset = Game.Data.GetDefaultLocalization();
asset.AddResource(result.Id);

EditorServices.SaveAssetWhenSelectedAssetIsSaved(asset.Guid);
Expand All @@ -79,7 +79,7 @@ internal static class EditorLocalizationServices

private static void AddExistingResource(Guid g)
{
LocalizationAsset asset = Game.Data.Localization;
LocalizationAsset asset = Game.Data.GetDefaultLocalization();
asset.AddResource(g);

EditorServices.SaveAssetWhenSelectedAssetIsSaved(asset.Guid);
Expand Down
13 changes: 12 additions & 1 deletion src/Murder.Editor/Services/EditorTextureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ public static void SaveAsQoiGz(this Texture2D texture, string path)
}

/// <summary>
/// Save a <paramref name="texture"/> as a QOI image.
/// Save a <paramref name="stream"/> as a QOI image.
/// </summary>
public static void ConvertPngStreamToQuoiGz(MemoryStream stream, string path)
{
using Texture2D texture = Texture2D.FromStream(Architect.GraphicsDevice, stream);

SaveAsQoiGz(texture, path);
}

public static void SaveAsPng(MemoryStream stream, string path)
{
using Texture2D texture = Texture2D.FromStream(Architect.GraphicsDevice, stream);

{
using FileStream fileStream = File.Open(path, FileMode.OpenOrCreate);
texture.SaveAsPng(fileStream, texture.Width, texture.Height);
fileStream.Close();
}
}
}
6 changes: 4 additions & 2 deletions src/Murder/Assets/Localization/Languages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class Languages
{
private static LanguageIdData[]? _all = null;

public static LanguageIdData[] All => _all ??= [ English, Portuguese ];
public static LanguageIdData[] All => _all ??= [ English, Portuguese, Japanese];

public static LanguageIdData Get(LanguageId id) =>
All[(int)id];
Expand All @@ -22,11 +22,13 @@ public static LanguageIdData Next(LanguageId id) =>

public static readonly LanguageIdData English = new(LanguageId.English, "en-US");
public static readonly LanguageIdData Portuguese = new(LanguageId.Portuguese, "pt-BR");
public static readonly LanguageIdData Japanese = new(LanguageId.Japanese, "ja");
}

public enum LanguageId
{
English = 0,
Portuguese = 1
Portuguese = 1,
Japanese = 2
}
}
7 changes: 6 additions & 1 deletion src/Murder/Assets/Localization/LocalizedString.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Murder.Assets;
using Murder.Services;

namespace Murder.Assets;

public readonly struct LocalizedString
{
Expand All @@ -15,4 +17,7 @@ public LocalizedString() { }
public LocalizedString(Guid id) => Id = id;

public LocalizedString(string overrideText) => OverrideText = overrideText;

public static implicit operator string(LocalizedString localizedString) =>
LocalizationServices.GetLocalizedString(localizedString);
}
5 changes: 5 additions & 0 deletions src/Murder/Data/GameDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,11 @@ public ImmutableDictionary<Guid, GameAsset> FilterOutAssets(params Type[] types)

public PixelFont GetFont(int index)
{
if (_game is not null)
{
index = _game.GetLocalizedFont(index);
}

if (_fonts.TryGetValue(index, out PixelFont? font))
{
return font;
Expand Down
5 changes: 5 additions & 0 deletions src/Murder/IMurderGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public void OnExit() { }
/// </summary>
public GamePreferences CreateGamePreferences() => new();

/// <summary>
/// Allow the game to override a font based on localization settings.
/// </summary>
public int GetLocalizedFont(int index) => index;

/// <summary>
/// Creates a custom render context for the game.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Murder/Services/RenderServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
using Murder.Utilities;
using System.Collections.Immutable;
using System.Numerics;
using static System.Formats.Asn1.AsnWriter;
using static System.Net.Mime.MediaTypeNames;
using Matrix = Microsoft.Xna.Framework.Matrix;
using Vector3 = Microsoft.Xna.Framework.Vector3;

Expand Down

0 comments on commit 8af415f

Please sign in to comment.