diff --git a/DynamicTranslation.js b/DynamicTranslation.js index b6de725..f206799 100644 --- a/DynamicTranslation.js +++ b/DynamicTranslation.js @@ -541,13 +541,32 @@ return langNames[currentLang] || currentLang.toUpperCase(); }; + var _Window_Options_processOk = Window_Options.prototype.processOk; Window_Options.prototype.processOk = function () { var index = this.index(); var symbol = this.commandSymbol(index); if (symbol === 'language') { this.changeLanguage(); } else { - Window_Command.prototype.processOk.call(this); + return _Window_Options_processOk.apply(this, arguments); + } + }; + + var _Window_Options_cursorRight = Window_Options.prototype.cursorRight; + Window_Options.prototype.cursorRight = function () { + if (this.commandSymbol(this.index()) === 'language') { + this.changeLanguage(); + } else { + return _Window_Options_cursorRight.apply(this, arguments); + } + }; + + var _Window_Options_cursorLeft = Window_Options.prototype.cursorLeft; + Window_Options.prototype.cursorLeft = function () { + if (this.commandSymbol(this.index()) === 'language') { + this.changeLanguage(); + } else { + return _Window_Options_cursorLeft.apply(this, arguments); } }; diff --git a/tests/options-cursor.test.js b/tests/options-cursor.test.js new file mode 100644 index 0000000..c45efed --- /dev/null +++ b/tests/options-cursor.test.js @@ -0,0 +1,60 @@ +describe('Window_Options cursor dispatch', () => { + let originals; + + beforeEach(() => { + jest.resetModules(); + require('./setup'); + global.$dataSystem = null; + originals = {}; + for (const [method, booleanValue, delta] of [ + ['cursorRight', true, 20], ['cursorLeft', false, -20] + ]) { + // RPG Maker MV's non-volume arrow handler writes a boolean option. + originals[method] = jest.fn(function (...args) { + const symbol = this.commandSymbol(this.index()); + ConfigManager[symbol] = symbol.endsWith('Volume') + ? Math.max(0, Math.min(100, ConfigManager[symbol] + delta)) + : booleanValue; + return args[0]; + }); + Window_Options.prototype[method] = originals[method]; + } + loadPlugin(); + }); + + test.each(['cursorRight', 'cursorLeft'])('%s preserves language strings and existing cycle order', method => { + const window = new Window_Options(); + $translationManager._availableLanguages = ['zh', 'en', 'ja']; + $translationManager._currentLanguage = 'en'; + $translationManager.setLanguage = jest.fn(language => { + $translationManager._currentLanguage = language; + }); + ConfigManager.language = 'en'; + window.redrawCurrentItem = jest.fn(); + + for (const expected of ['ja', 'zh', 'en']) { + window[method](false); + expect(ConfigManager.makeData().language).toBe(expected); + expect($translationManager.getCurrentLanguage()).toBe(expected); + } + expect(window.redrawCurrentItem).toHaveBeenCalledTimes(3); + expect(SoundManager.playCursor).toHaveBeenCalledTimes(3); + expect(originals[method]).not.toHaveBeenCalled(); + }); + + test.each(['cursorRight', 'cursorLeft'])('%s preserves other handlers, arguments and return', method => { + const window = new Window_Options(); + const marker = {}; + window.commandSymbol = jest.fn(() => 'alwaysDash'); + ConfigManager.alwaysDash = method === 'cursorLeft'; + expect(window[method](marker, 'extra')).toBe(marker); + expect(ConfigManager.alwaysDash).toBe(method === 'cursorRight'); + window.commandSymbol = jest.fn(() => 'bgmVolume'); + ConfigManager.bgmVolume = method === 'cursorRight' ? 90 : 10; + window[method](false); + expect(ConfigManager.bgmVolume).toBe(method === 'cursorRight' ? 100 : 0); + expect(originals[method].mock.contexts).toEqual([window, window]); + expect(originals[method]).toHaveBeenNthCalledWith(1, marker, 'extra'); + expect(originals[method]).toHaveBeenNthCalledWith(2, false); + }); +}); diff --git a/tests/options-process-ok.test.js b/tests/options-process-ok.test.js new file mode 100644 index 0000000..8d5f96e --- /dev/null +++ b/tests/options-process-ok.test.js @@ -0,0 +1,80 @@ +describe('Window_Options confirmation dispatch', () => { + let originalProcessOk; + let baseProcessOk; + + beforeEach(() => { + jest.resetModules(); + require('./setup'); + // Skip language-file loading. These tests only exercise option dispatch. + global.$dataSystem = null; + originalProcessOk = jest.fn(function (...args) { + const symbol = this.commandSymbol(this.index()); + const value = this.values[symbol]; + if (symbol.endsWith('Volume')) { + this.values[symbol] = value >= 100 ? 0 : Math.min(value + 20, 100); + } else { + this.values[symbol] = !value; + } + return args[0]; + }); + baseProcessOk = jest.fn(); + Window_Options.prototype.processOk = originalProcessOk; + Window_Command.prototype.processOk = baseProcessOk; + loadPlugin(); + }); + + function options(symbol, value) { + const window = new Window_Options(); + window.commandSymbol = jest.fn(() => symbol); + window.values = { [symbol]: value }; + return window; + } + + test('keeps the existing boolean option handler and receiver', () => { + const window = options('alwaysDash', false); + + window.processOk(); + expect(window.values.alwaysDash).toBe(true); + window.processOk(); + expect(window.values.alwaysDash).toBe(false); + expect(originalProcessOk).toHaveBeenCalledTimes(2); + expect(originalProcessOk.mock.contexts).toEqual([window, window]); + expect(baseProcessOk).not.toHaveBeenCalled(); + }); + + test('keeps volume adjustment and wraparound', () => { + const window = options('bgmVolume', 80); + + window.processOk(); + expect(window.values.bgmVolume).toBe(100); + window.processOk(); + expect(window.values.bgmVolume).toBe(0); + expect(originalProcessOk).toHaveBeenCalledTimes(2); + expect(baseProcessOk).not.toHaveBeenCalled(); + }); + + test('preserves arguments and return value for an earlier plugin handler', () => { + const window = options('customOption', false); + const marker = {}; + + expect(window.processOk(marker, 'extra')).toBe(marker); + expect(originalProcessOk).toHaveBeenCalledWith(marker, 'extra'); + expect(baseProcessOk).not.toHaveBeenCalled(); + }); + + test('still cycles language without invoking either inherited handler', () => { + const window = options('language', 'zh'); + $translationManager._availableLanguages = ['zh', 'en']; + $translationManager.setLanguage = jest.fn(); + window.redrawCurrentItem = jest.fn(); + + window.processOk(); + + expect($translationManager.setLanguage).toHaveBeenCalledWith('en'); + expect(ConfigManager.language).toBe('en'); + expect(window.redrawCurrentItem).toHaveBeenCalledTimes(1); + expect(SoundManager.playCursor).toHaveBeenCalledTimes(1); + expect(originalProcessOk).not.toHaveBeenCalled(); + expect(baseProcessOk).not.toHaveBeenCalled(); + }); +});