Skip to content

Commit cb8a527

Browse files
MidZMHyuchia
authored andcommitted
Configuration Fixes
New: - Added a "minTextSpeed" global variable to keep track of the minimum variable speed for later use. - Added the ability to allow semi-instant skipping when the dev has chosen to keep "InstantText" off. (Setting the "min" value on the "text-speed" slider below 0 enables it) - Added a comment to the "stopTyping" function to internally understand what the main differences are when "InstantText" is active and not. Changes: - Changed how text speed was being applied, to keep consistency when reloading the page. Fixes: - Text speed slider bar was occasionally incorrect when refreshing the page. - Fixed an issue where "selector" wasn't being passed to the "init" functions in actions and components.
1 parent 12d315a commit cb8a527

4 files changed

Lines changed: 44 additions & 9 deletions

File tree

src/actions/Dialog.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ export class Dialog extends Action {
8989

9090
static override async bind(selector: string): Promise<void> {
9191
// Add listener for the text speed setting (TypeWriter reads from preference directly)
92+
const clamp = (num: number, min: number, max: number): number => Math.min(Math.max(num, min), max);
9293
$_(`${selector} [data-action="set-text-speed"]`).on('change mouseover', function (this: HTMLInputElement) {
93-
const textbox = Dialog.engine.element().find('[data-component="text-box"] [data-component="type-writer"]').get(0) as TypeWriter | undefined;
94+
const textbox = Dialog.engine.element().find('[data-component="text-box"] [data-component="type-writer"]').get(0) as TypeWriter | undefined;
9495
const maxTextSpeed = Dialog.engine.setting('maxTextSpeed') as number;
95-
const value = maxTextSpeed + parseInt(this.value);
96+
const minPlaySpeed = Dialog.engine.setting('minTextSpeed') as number;
97+
const value = clamp(parseInt(this.value), minPlaySpeed, maxTextSpeed);
9698

9799
Dialog.engine.preference('TextSpeed', value);
98100
textbox?.setState({ config: { typeSpeed: value } });
@@ -118,6 +120,7 @@ export class Dialog extends Action {
118120
}
119121

120122
this.engine.setting('maxTextSpeed', parseInt(($_(`${selector} [data-action="set-text-speed"]`).attribute('max') || '0')));
123+
this.engine.setting('minTextSpeed', parseInt(($_(`${selector} [data-action="set-text-speed"]`).attribute('min') || '0')));
121124
}
122125

123126
static override async reset({ keepNVL = false, saveNVL = false }: { keepNVL?: boolean; saveNVL?: boolean } = {}): Promise<void> {

src/components/settings-screen/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ class SettingsScreen extends ScreenComponent<Properties, ScreenState> {
163163
this.element().find('[data-platform="electron"]').remove();
164164
}
165165

166-
this.element().find('[data-action="set-text-speed"]').value(
167-
(this.engine.setting('maxTextSpeed') as number) - (this.engine.preference('TextSpeed') as number)
168-
);
166+
this.element().find('[data-action="set-text-speed"]').value((this.engine.preference('TextSpeed') as number));
169167
});
170168

171169
// Disable audio settings in iOS since they are not supported

src/components/type-writer/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ class TypeWriter extends Component<TypeWriterProps, TypeWriterState> {
746746
container.innerHTML = (this.constructor as typeof TypeWriter).stripActionMarkers(str);
747747
} else {
748748
// Rush through animation at maximum speed
749-
this.speed = 0;
749+
const minSpeed = this.engine.setting('minTextSpeed') as number;
750+
this.speed = minSpeed > 0 ? 0 : minSpeed;
750751
this.ignorePause = true;
751752

752753
if (this.loops) {
@@ -1132,6 +1133,15 @@ class TypeWriter extends Component<TypeWriterProps, TypeWriterState> {
11321133
this._lastFrameTime = timestamp;
11331134
this._accumulatedTime += deltaTime;
11341135

1136+
// Some developers may want the text to appear instantly,
1137+
// while disabling "InstantText".
1138+
if (this._targetWaitTime < 0) {
1139+
while (this.elements && this.nodeCounter < this.elements.length) {
1140+
this._revealNextCharacter(true);
1141+
}
1142+
return;
1143+
}
1144+
11351145
// Check if we've waited long enough
11361146
if (this._accumulatedTime >= this._targetWaitTime) {
11371147
this._revealNextCharacter();
@@ -1144,7 +1154,7 @@ class TypeWriter extends Component<TypeWriterProps, TypeWriterState> {
11441154
/**
11451155
* Reveal the next character and schedule the next one.
11461156
*/
1147-
private _revealNextCharacter(): void {
1157+
private _revealNextCharacter(instant: boolean = false): void {
11481158
if (this.nextPause) {
11491159
this.nextPause = null;
11501160

@@ -1172,6 +1182,24 @@ class TypeWriter extends Component<TypeWriterProps, TypeWriterState> {
11721182
}
11731183
this.nodeCounter += 1;
11741184

1185+
// If the developer wants instant text while keeping "InstantText" off,
1186+
// this will handle executing all remaining actions semi-instantly.
1187+
if (instant && this.elements && this.nodeCounter < this.elements.length) {
1188+
if (this.actions[this.nodeCounter]) {
1189+
this.executeAction(this.actions[this.nodeCounter]!);
1190+
1191+
this.actionsPlayed++;
1192+
return;
1193+
}
1194+
1195+
if (this.actionsPlayed) {
1196+
this.nodeCounter -= this.actionsPlayed;
1197+
this.actionsPlayed = 0;
1198+
}
1199+
1200+
return;
1201+
}
1202+
11751203
if (this.elements && this.nodeCounter < this.elements.length) {
11761204
// Continue typing - start new timing cycle
11771205
this.typewrite();

src/monogatari.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,12 @@ class Monogatari {
29922992
* @returns {void}
29932993
*/
29942994
static stopTyping (component: TypeWriterComponent): void {
2995+
// Main differences between instant text & speed text:
2996+
// Instant Text:
2997+
// -- Appear instantly & removes all non-node formatting.
2998+
// Speed Text:
2999+
// -- Appear gradually at the fastest speed while keeping all non-node formatting.
3000+
// -- Setting min-speed to -1 or lower results in the benefits of speed text and instant text.
29953001
const instant = this.setting('InstantText') as boolean;
29963002

29973003
// TypeWriter.finish() handles setting finished_typing and triggering events
@@ -3907,11 +3913,11 @@ class Monogatari {
39073913
const init: Promise<void>[] = [];
39083914

39093915
for (const component of this.components ()) {
3910-
init.push (component.init ());
3916+
init.push (component.init (selector));
39113917
}
39123918

39133919
for (const action of this.actions ()) {
3914-
init.push (action.init ());
3920+
init.push (action.init (selector));
39153921
}
39163922

39173923
if (this.setting ('AutoSave') != 0 && typeof this.setting ('AutoSave') === 'number') {

0 commit comments

Comments
 (0)