Skip to content

Commit

Permalink
Custom Fonts for SGB (#46)
Browse files Browse the repository at this point in the history
This PR adds the SGBFontManager, which is a super simple system to manage overriding the font that SGB uses.
  • Loading branch information
Iseeicy authored Sep 6, 2023
1 parent 8a1bc1e commit a88c1f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
29 changes: 29 additions & 0 deletions Runtime/Scripts/SGBFontManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BobboNet.SGB.IMod
{
public static class SGBFontManager
{
//
// Properties
//

/// <summary>
/// The current font that SGB should use. By default this is whatever the first font with the name "font.ttf"
/// is found in the resource folder.
/// </summary>
public static Font CurrentFont { get; set; }

//
// Init
//

static SGBFontManager()
{
// Load the default font
CurrentFont = Resources.Load<Font>("font");
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/SGBFontManager.cs.meta

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

17 changes: 12 additions & 5 deletions Runtime/src/fakekmy/SpriteBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace SharpKmyGfx
{
public class SpriteBatch
{
public static int DEFAULT_SCREEN_X = 960;
public static int DEFAULT_SCREEN_Y = 540;
public static int DEFAULT_SCREEN_X = 960;
public static int DEFAULT_SCREEN_Y = 540;
public static float offsetX;
public static float offsetY;

Expand All @@ -32,7 +32,11 @@ internal enum CallType
}
private static List<DrawCall> drawCalls = new List<DrawCall>();
private static Texture2D rectTex = null;
#if IMOD
public static UnityEngine.Font defaultFont { get => BobboNet.SGB.IMod.SGBFontManager.CurrentFont; }
#else
internal static UnityEngine.Font defaultFont;
#endif
private static Material mtl;
private static bool sIsFont_textureRebuilt = false;

Expand Down Expand Up @@ -62,7 +66,8 @@ public SpriteBatch()
}
// End of custom overrides

if (rectTex == null) {
if (rectTex == null)
{
rectTex = new Texture2D(1, 1);
rectTex.SetPixel(0, 0, new UnityEngine.Color(1, 1, 1, 1));
rectTex.Apply();
Expand All @@ -78,7 +83,9 @@ public SpriteBatch()

static SpriteBatch()
{
#if !IMOD
defaultFont = Resources.Load<UnityEngine.Font>("font");
#endif
UnityEngine.Font.textureRebuilt += (UnityEngine.Font obj) =>
{
sIsFont_textureRebuilt = true;
Expand Down Expand Up @@ -163,7 +170,7 @@ internal void drawText(Font font, byte[] bytes, int x, int y, float r, float g,
//
call.style.font = defaultFont;
call.style.fontSize = (int)(font.fontSize * scl * scale);
if(italic)
if (italic)
call.style.fontStyle = FontStyle.Italic;
GUIStyleState styleState = new GUIStyleState();
styleState.textColor = new UnityEngine.Color(r, g, b, a);
Expand Down Expand Up @@ -472,7 +479,7 @@ private static Rect expandDestRect(Rect dest)
float destY = dest.y * scale + offsetY; // 2Dオブジェクトのy座標
float destW = dest.width * scale; // 2Dオブジェクトの横幅
float destH = dest.height * scale; // 2Dオブジェクトの縦幅

var result = new Rect((int)destX, (int)destY, 0, 0);
result.xMax = (int)(destX + destW);
result.yMax = (int)(destY + destH);
Expand Down

0 comments on commit a88c1f0

Please sign in to comment.