Skip to content

Commit 5b0af32

Browse files
fix: English text split and layout problem #587
1 parent 0ef2268 commit 5b0af32

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

packages/webgal/public/game/scene/demo_en.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Welcome to WebGAL! -e002_Welcome_to_WebGAL.mp3;
1111
WebGAL is a completely new web visual engine never seen before. -e003_WebGAL_is_a_completely_new_web.mp3;
1212
changeFigure:stand2.png -right -next;
1313
It is an engine developed using web technology, so it performs well on web pages. -e004_It_is_an_engine_developedusing_web.mp3;
14-
Thanks to this feature, once published on your website's platform,|players can simply click a link to play your game on your website anytime, anywhere! -e005_Thanks_to_this_ feature_once.mp3;
14+
Thanks to this feature, once published on your website's platform, players can simply click a link to play your game on your website anytime, anywhere! -e005_Thanks_to_this_ feature_once.mp3;
1515
setAnimation:move-front-and-back -target=fig-left -next;
1616
Very attractive, don't you think? -e006_Very attractive.mp3;
1717
changeFigure:none -right -next;

packages/webgal/src/Core/gameScripts/say.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const say = (sentence: ISentence): IPerform => {
2424
let dialogKey = Math.random().toString(); // 生成一个随机的key
2525
let dialogToShow = sentence.content; // 获取对话内容
2626
if (dialogToShow) {
27-
dialogToShow = String(dialogToShow).replace(/ /g, '\u00a0'); // 替换空格
27+
dialogToShow = String(dialogToShow).replace(/ {2,}/g, (match) => '\u00a0'.repeat(match.length)); // 替换连续两个或更多空格
2828
}
2929
const isConcat = getSentenceArgByKey(sentence, 'concat'); // 是否是继承语句
3030
const isNotend = getSentenceArgByKey(sentence, 'notend') as boolean; // 是否有 notend 参数

packages/webgal/src/Stage/TextBox/TextBox.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ function isCJK(character: string) {
101101
return !!character.match(/[\u4e00-\u9fa5]|[\u0800-\u4e00]|[\uac00-\ud7ff]/);
102102
}
103103

104-
export function compileSentence(sentence: string, lineLimit: number, ignoreLineLimit?: boolean): EnhancedNode[][] {
104+
// eslint-disable-next-line max-params
105+
export function compileSentence(
106+
sentence: string,
107+
lineLimit: number,
108+
ignoreLineLimit?: boolean,
109+
replace_space_with_nbsp = true,
110+
): EnhancedNode[][] {
105111
// 先拆行
106112
const lines = sentence.split(/(?<!\\)\|/).map((val: string) => useEscape(val));
107113
// 对每一行进行注音处理
@@ -111,7 +117,7 @@ export function compileSentence(sentence: string, lineLimit: number, ignoreLineL
111117
line.forEach((node, index) => {
112118
match(node.type)
113119
.with(SegmentType.String, () => {
114-
const chars = splitChars(node.value as string);
120+
const chars = splitChars(node.value as string, replace_space_with_nbsp);
115121
// eslint-disable-next-line max-nested-callbacks
116122
ln.push(...chars.map((c) => ({ reactNode: c })));
117123
})
@@ -135,8 +141,9 @@ export function compileSentence(sentence: string, lineLimit: number, ignoreLineL
135141

136142
/**
137143
* @param sentence
144+
* @param replace_space_with_nbsp
138145
*/
139-
export function splitChars(sentence: string) {
146+
export function splitChars(sentence: string, replace_space_with_nbsp = true) {
140147
if (!sentence) return [''];
141148
const words: string[] = [];
142149
let word = '';
@@ -157,13 +164,15 @@ export function splitChars(sentence: string) {
157164
// cjkFlag = false;
158165
// continue;
159166
// }
160-
if (character === ' ') {
167+
if (character === ' ' || character === '\u00a0') {
161168
// Space
162169
if (word) {
163170
words.push(word);
164171
word = '';
165172
}
166-
words.push('\u00a0');
173+
if (replace_space_with_nbsp) {
174+
words.push('\u00a0');
175+
} else words.push(character);
167176
cjkFlag = false;
168177
} else if (isCJK(character) && !isPunctuation(character)) {
169178
if (!cjkFlag && word) {

packages/webgal/src/UI/Backlog/Backlog.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { setVisibility } from '@/store/GUIReducer';
77
import { logger } from '@/Core/util/logger';
88
import { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
99
import useTrans from '@/hooks/useTrans';
10-
import { compileSentence, EnhancedNode, splitChars } from '@/Stage/TextBox/TextBox';
10+
import { compileSentence, EnhancedNode } from '@/Stage/TextBox/TextBox';
1111
import useSoundEffect from '@/hooks/useSoundEffect';
1212
import { WebGAL } from '@/Core/WebGAL';
1313

@@ -27,7 +27,7 @@ export const Backlog = () => {
2727
// logger.info('backlogList render');
2828
for (let i = 0; i < WebGAL.backlogManager.getBacklog().length; i++) {
2929
const backlogItem = WebGAL.backlogManager.getBacklog()[i];
30-
const showTextArray = compileSentence(backlogItem.currentStageState.showText, 3, true);
30+
const showTextArray = compileSentence(backlogItem.currentStageState.showText, 3, true, false);
3131
const showTextArray2 = showTextArray.map((line) => {
3232
return line.map((c) => {
3333
return c.reactNode;
@@ -48,13 +48,13 @@ export const Backlog = () => {
4848
);
4949
});
5050
const showNameArray = compileSentence(backlogItem.currentStageState.showName, 3, true);
51-
const showNameArray2 = showNameArray.map((line)=>{
51+
const showNameArray2 = showNameArray.map((line) => {
5252
return line.map((c) => {
53-
return c.reactNode;
53+
return c.reactNode;
5454
});
5555
});
5656
const showNameArrayReduced = mergeStringsAndKeepObjects(showNameArray2);
57-
const nameElementList = showNameArrayReduced.map((line,index)=>{
57+
const nameElementList = showNameArrayReduced.map((line, index) => {
5858
return (
5959
<div key={`backlog-line-${index}`}>
6060
{line.map((e, index) => {

0 commit comments

Comments
 (0)