Skip to content

Commit

Permalink
Improved UI, supported multi-font
Browse files Browse the repository at this point in the history
  • Loading branch information
XcantloadX committed Nov 16, 2020
1 parent ecdc9a0 commit 979ddbf
Show file tree
Hide file tree
Showing 10 changed files with 772 additions and 392 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Unitale/CYF 字体制作器

## 使用说明 How to use
### 制作字体
1.点击 `Add` 从 txt 文件添加里字符,你可以在 `CharacterTemplate` 里找到常用汉字
2.选择字体名称(`Font Name`),通常 `uidialog` 为旁白字体,`monster` 为怪物对话框字体,其他自行探索(有时间补充)
1.点击 `Add` 从 txt 文件添加里字符,你可以在 `CharacterTemplate` 文件夹里找到常用汉字
2.选择字体名称(`Font Name`),通常 `uidialog` 为旁白字体,`monster` 为怪物对话框字体,其他的有时间再补充
3.行间距(`Line Spacing`)一般不需要改
4.点击 `Change Font` 选择一个字体,建议的字体如下
```
Expand All @@ -26,5 +26,7 @@ Zpix 四号 宽 1000 高 1400 行间距 30

### 安装字体
复制到`[MOD名称]\Sprites\UI\Fonts\`下覆盖即可
通常来说不建议复制到`Default`目录下,会导致所有 Mod 加载速度变慢(因为默认加载中文字体)
至于在对话中如何使用自定义名称的字体,参考 Unitale/CYF 的文档
通常来说不建议复制到`Default`目录下,会导致所有 Mod 加载速度变慢(如果你的字体有很多字的话)
至于在对话中如何使用自定义名称的字体,参考 Unitale/CYF 的文档

**注意:强烈建议每次更改字体之后重启游戏再测试(特别是 Unitale)!**
Binary file modified UnitaleFontMaker.suo
Binary file not shown.
23 changes: 22 additions & 1 deletion UnitaleFontMaker/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class Character
{
private char _char;
private RectangleF rect;

public static char[] ENGLISH_CHARS = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'r', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '/', '*', '-', '+', ',', '.', '?', '!', '\'', '"', ':', ';', '/', '\\', '<', '>', '(', ')', '[', ']', '(', ')', '-', '_', '=', '~', '@', '#', '$', '%', '^', '&', '*', '(', ')', ' ' }; //别忘了空格字符!

public char Char
{
get { return _char; }
Expand Down Expand Up @@ -53,6 +54,26 @@ public Character(char character, float x, float y, float width, float height)
{

}

/// <summary>
/// 检查传入的字符是否是因为字符
/// </summary>
/// <param name="c">要检查的字符</param>
/// <returns>结果</returns>
public static bool isEnglishChar(char c)
{
return Array.IndexOf(ENGLISH_CHARS, c) >= 0;
}

/// <summary>
/// 检查传入的字符是否是因为字符
/// </summary>
/// <param name="c">要检查的字符</param>
/// <returns>结果</returns>
public static bool isEnglishChar(Character c)
{
return Array.IndexOf(ENGLISH_CHARS, c.Char) >= 0;
}

}
}
69 changes: 52 additions & 17 deletions UnitaleFontMaker/FontPainter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ public class FontPainter
private string str;
private char[] characters;
private StringFormat format;

public Font font;


/// <summary>
/// 默认字体
/// </summary>
public Font Font { get; set; }
/// <summary>
/// 如果此项不为空,则绘制英文字符时使用此字体
/// </summary>
public Font EnglishFont { get; set; }
/// <summary>
/// 默认字体的 Y 坐标 Offset TODO 1
/// </summary>
public float fontYOffset = 0;

public bool drawDebugBorders = false;

/// <summary>
/// 字体的颜色
/// </summary>
Expand Down Expand Up @@ -67,7 +80,7 @@ public Size Size

public FontPainter(Font font, int width, int height)
{
this.font = font;
this.Font = font;
brush = new SolidBrush(Color.White);
backBrush = new SolidBrush(Color.FromArgb(0, 0, 0, 0));

Expand Down Expand Up @@ -101,7 +114,13 @@ public void Paint()
int width = (int)chars[i].Width;
int height = (int)chars[i].Height;

gImage.DrawString(chars[i].Char.ToString(), font, brush, x, y, format);
Font font = this.Font;
if (!Character.isEnglishChar(chars[i])) //中文字体设置了 offset 的情况
y += (int)fontYOffset;

gImage.DrawString(chars[i].Char.ToString(), GetFontByChar(chars[i].Char), brush, x, y, format);
if (drawDebugBorders)
gImage.DrawRectangle(Pens.Red, x, y, width, height);
}

}
Expand Down Expand Up @@ -129,25 +148,29 @@ public Character[] GetCharacters()
char[] chars = Characters;
Character[] characters = new Character[chars.Length];

float x = 0;
float x = 5;
float y = 0;

for (int i = 0; i < chars.Length; i++)
{
//转换前(左上原点)
float width = GetCharWidth(font, chars[i]);
float height = GetCharHeight(font, chars[i]);

Character c = new Character(chars[i], x, y, width, height);
float width = GetCharWidth(Font, chars[i]);
float height = GetCharHeight(Font, chars[i]);

Character c = null;
if(!Character.isEnglishChar(chars[i])) //非英文字体的 Offset
c = new Character(chars[i], x, y, width, height + fontYOffset);
else
c = new Character(chars[i], x, y, width, height);
characters[i] = c;


x += width + 3;
x += width + 10;

if(x + GetCharWidth(font, chars[i]) + 3 > this.width)
if(x + GetCharWidth(Font, chars[i]) + 10 > this.width)
{
x = 0;
y += height;
x = 5;
y += height + 10;
}
}

Expand All @@ -165,13 +188,25 @@ public Character[] PositionConvert(Character[] chars)

private float GetCharWidth(Font font, char c)
{
return gImage.MeasureString(c.ToString(), font, width, format).Width;
return gImage.MeasureString(c.ToString(), GetFontByChar(c), width, format).Width;
}

private float GetCharHeight(Font font, char c)
{
return gImage.MeasureString(c.ToString(), font, width, format).Height;
return gImage.MeasureString(c.ToString(), GetFontByChar(c), width, format).Height;
}


/// <summary>
/// 判断是使用英文字体还是默认字体
/// </summary>
/// <param name="c">待判断字符</param>
/// <returns>应该使用的字体</returns>
private Font GetFontByChar(char c)
{
if (EnglishFont != null && Character.isEnglishChar(c))
return EnglishFont;
else
return Font;
}
}
}
97 changes: 97 additions & 0 deletions UnitaleFontMaker/FormInput.Designer.cs

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

49 changes: 49 additions & 0 deletions UnitaleFontMaker/FormInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UnitaleFontMaker
{
public partial class FormInput : Form
{
public bool done;
public string text;

public FormInput(string title, string description)
{
InitializeComponent();

Text = title;
label1.Text = description;
}

private void btnOK_Click(object sender, EventArgs e)
{
done = true;
text = textBox1.Text;
}

private void btnCancel_Click(object sender, EventArgs e)
{
done = true;
text = null;
}


}

public class InputDialog
{
public static string Show(string title, string description)
{
FormInput form = new FormInput(title, description);
while (!form.done) { };
return form.text;
}
}
}
Loading

0 comments on commit 979ddbf

Please sign in to comment.