Skip to content

Commit bc40c51

Browse files
Merge pull request #820 from xiaoxustudio/feat/getUserInput-rules
feat: add rules argument for getUserInput
2 parents 00b0cb6 + 32463ff commit bc40c51

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

packages/webgal/src/Core/gameScripts/getUserInput/index.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { getStringArgByKey } from '@/Core/util/getSentenceArg';
1313
import { nextSentence } from '@/Core/controller/gamePlay/nextSentence';
1414
import { setStageVar } from '@/store/stageReducer';
1515
import { getCurrentFontFamily } from '@/hooks/useFontFamily';
16+
import { logger } from '@/Core/util/logger';
17+
import { tryToRegex } from '@/Core/util/global';
18+
import { showGlogalDialog } from '@/UI/GlobalDialog/GlobalDialog';
1619

1720
/**
1821
* 显示选择枝
@@ -26,6 +29,10 @@ export const getUserInput = (sentence: ISentence): IPerform => {
2629
let buttonText = getStringArgByKey(sentence, 'buttonText') ?? '';
2730
buttonText = buttonText === '' ? 'OK' : buttonText;
2831
const defaultValue = getStringArgByKey(sentence, 'defaultValue');
32+
const rule = getStringArgByKey(sentence, 'rule');
33+
const ruleFlag = getStringArgByKey(sentence, 'ruleFlag');
34+
const ruleText = getStringArgByKey(sentence, 'ruleText');
35+
const ruleButtonText = getStringArgByKey(sentence, 'ruleButtonText') ?? 'OK';
2936

3037
const font = getCurrentFontFamily();
3138

@@ -39,6 +46,20 @@ export const getUserInput = (sentence: ISentence): IPerform => {
3946
onMouseEnter={playSeEnter}
4047
onClick={() => {
4148
const userInput: HTMLInputElement = document.getElementById('user-input') as HTMLInputElement;
49+
if (rule) {
50+
const reg = tryToRegex(rule, ruleFlag);
51+
if (reg && !reg.test(userInput.value)) {
52+
if (ruleText)
53+
showGlogalDialog({
54+
title: ruleText.replaceAll(/\$0/g, userInput.value),
55+
leftText: ruleButtonText,
56+
});
57+
return;
58+
}
59+
if (!reg) {
60+
logger.warn(`getUserInput: rule ${rule} is not a valid regex`);
61+
}
62+
}
4263
if (userInput) {
4364
webgalStore.dispatch(
4465
setStageVar({
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function tryToRegex(str: string, flag: string | null): RegExp | false {
2+
try {
3+
return new RegExp(str, flag || '');
4+
} catch (e) {
5+
return false;
6+
}
7+
}

packages/webgal/src/UI/GlobalDialog/GlobalDialog.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ export default function GlobalDialog() {
1313
interface IShowGlobalDialogProps {
1414
title: string;
1515
leftText: string;
16-
rightText: string;
17-
leftFunc: Function;
18-
rightFunc: Function;
16+
rightText?: string;
17+
leftFunc?: Function;
18+
rightFunc?: Function;
1919
}
2020

2121
export function showGlogalDialog(props: IShowGlobalDialogProps) {
2222
const { playSeClick, playSeEnter } = useSEByWebgalStore();
2323
webgalStore.dispatch(setVisibility({ component: 'showGlobalDialog', visibility: true }));
2424
const handleLeft = () => {
2525
playSeClick();
26-
props.leftFunc();
26+
props.leftFunc?.();
2727
hideGlobalDialog();
2828
};
2929
const handleRight = () => {
3030
playSeClick();
31-
props.rightFunc();
31+
props.rightFunc?.();
3232
hideGlobalDialog();
3333
};
3434
const renderElement = (
@@ -37,12 +37,16 @@ export function showGlogalDialog(props: IShowGlobalDialogProps) {
3737
<div className={styles.glabalDialog_container_inner}>
3838
<div className={styles.title}>{props.title}</div>
3939
<div className={styles.button_list}>
40-
<div className={styles.button} onClick={handleLeft} onMouseEnter={playSeEnter}>
41-
{props.leftText}
42-
</div>
43-
<div className={styles.button} onClick={handleRight} onMouseEnter={playSeEnter}>
44-
{props.rightText}
45-
</div>
40+
{props.leftText && (
41+
<div className={styles.button} onClick={handleLeft} onMouseEnter={playSeEnter}>
42+
{props.leftText}
43+
</div>
44+
)}
45+
{props.rightText && (
46+
<div className={styles.button} onClick={handleRight} onMouseEnter={playSeEnter}>
47+
{props.rightText}
48+
</div>
49+
)}
4650
</div>
4751
</div>
4852
</div>

0 commit comments

Comments
 (0)