Fix TTS module initialization and dependency issues. Update module IDs for consistency, improve circular dependency detection, and fix UI Controller event handling.
This commit is contained in:
@@ -1,106 +1,58 @@
|
||||
import { BaseModule } from './base-module.js';
|
||||
import { moduleRegistry } from './module-registry.js';
|
||||
import { ModuleEvent } from './base-module.js';
|
||||
|
||||
class UIInputHandler extends BaseModule {
|
||||
constructor() {
|
||||
super('ui-input-handler');
|
||||
super('ui-input-handler', 'UI Input Handler');
|
||||
|
||||
// Explicitly declare ui-display-handler as a dependency
|
||||
this.dependencies = ['ui-display-handler'];
|
||||
|
||||
// Reference to display handler
|
||||
this.displayHandler = null;
|
||||
|
||||
// Input elements
|
||||
this.inputArea = null;
|
||||
this.playerInput = null;
|
||||
this.cursor = null;
|
||||
this.commandHistoryElement = null; // Changed: renamed to avoid conflict
|
||||
this.commandHistoryElement = null;
|
||||
|
||||
// Input state
|
||||
this.inputEnabled = true;
|
||||
this.historyIndex = -1;
|
||||
this.commandHistory = []; // Now this is clearly the array of previous commands
|
||||
this.commandHistory = [];
|
||||
this.inputBuffer = '';
|
||||
|
||||
// Add this method to properly dispatch custom events
|
||||
this._dispatchModuleEvent = (name, detail) => {
|
||||
document.dispatchEvent(new CustomEvent(name, {
|
||||
detail: { moduleId: this.id, ...detail },
|
||||
bubbles: true
|
||||
}));
|
||||
};
|
||||
|
||||
// Bind method contexts
|
||||
this.setupInputElements = this.setupInputElements.bind(this);
|
||||
this.handlePlayerInput = this.handlePlayerInput.bind(this);
|
||||
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
|
||||
this.positionCursor = this.positionCursor.bind(this);
|
||||
this.handleKeyboardInput = this.handleKeyboardInput.bind(this);
|
||||
// Bind methods using the parent class bindMethods utility
|
||||
this.bindMethods([
|
||||
'setupInputElements',
|
||||
'handlePlayerInput',
|
||||
'handleInputKeyDown',
|
||||
'positionCursor',
|
||||
'handleKeyboardInput',
|
||||
'submitCommand',
|
||||
'addToHistory',
|
||||
'resetCursorPosition'
|
||||
]);
|
||||
|
||||
console.log('UIInputHandler: Constructor initialized');
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for dependencies before initializing
|
||||
* This ensures displayHandler is ready before we try to use it
|
||||
*/
|
||||
async waitForDependencies() {
|
||||
try {
|
||||
// Explicitly wait for the display handler to be ready
|
||||
console.log('UIInputHandler: Waiting for display handler to be ready');
|
||||
|
||||
// Get reference to the display handler
|
||||
this.displayHandler = moduleRegistry.getModule('ui-display-handler');
|
||||
|
||||
if (!this.displayHandler) {
|
||||
console.error('UIInputHandler: Display handler dependency not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wait for display handler to reach FINISHED state
|
||||
const displayHandlerReady = await moduleRegistry.waitForModule('ui-display-handler');
|
||||
if (!displayHandlerReady) {
|
||||
console.error('UIInputHandler: Display handler not ready after waiting');
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log('UIInputHandler: Display handler is ready');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('UIInputHandler: Error waiting for dependencies:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize input handler
|
||||
*/
|
||||
async initialize() {
|
||||
this.reportProgress(0, 'Initializing UI Input Handler');
|
||||
|
||||
try {
|
||||
// Double-check display handler reference
|
||||
this.reportProgress(0, 'Initializing UI Input Handler');
|
||||
|
||||
// Get display handler reference through the parent's getModule method
|
||||
this.displayHandler = this.getModule('ui-display-handler');
|
||||
if (!this.displayHandler) {
|
||||
this.displayHandler = moduleRegistry.getModule('ui-display-handler');
|
||||
|
||||
if (!this.displayHandler) {
|
||||
console.error('UIInputHandler: Display handler still not available');
|
||||
return false;
|
||||
}
|
||||
console.error('UIInputHandler: Display handler module not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
this.reportProgress(30, 'Setting up keyboard listeners');
|
||||
|
||||
// Set up keyboard event listeners
|
||||
document.addEventListener('keydown', (event) => {
|
||||
this.handleKeyboardInput(event);
|
||||
});
|
||||
// Use the parent's addEventListener for automatic cleanup
|
||||
this.addEventListener(document, 'keydown', this.handleKeyboardInput);
|
||||
|
||||
this.reportProgress(60, 'Setting up input elements');
|
||||
|
||||
// Set up input elements
|
||||
this.setupInputElements();
|
||||
|
||||
this.reportProgress(100, 'UI Input Handler ready');
|
||||
@@ -156,9 +108,9 @@ class UIInputHandler extends BaseModule {
|
||||
commandHistory = document.createElement('div');
|
||||
commandHistory.id = 'command_history';
|
||||
choicesContainer.appendChild(commandHistory);
|
||||
this.commandHistoryElement = commandHistory; // Changed: store in renamed property
|
||||
this.commandHistoryElement = commandHistory;
|
||||
} else {
|
||||
this.commandHistoryElement = commandHistory; // Changed: store in renamed property
|
||||
this.commandHistoryElement = commandHistory;
|
||||
}
|
||||
|
||||
// Create input container if needed
|
||||
@@ -246,8 +198,8 @@ class UIInputHandler extends BaseModule {
|
||||
this.positionCursor(this.playerInput, this.cursor);
|
||||
}
|
||||
|
||||
// Dispatch event using the properly defined method
|
||||
this._dispatchModuleEvent('ui:input:change', {
|
||||
// Use the parent class dispatchEvent method instead of custom _dispatchModuleEvent
|
||||
this.dispatchEvent('ui:input:change', {
|
||||
text: this.playerInput.value
|
||||
});
|
||||
}
|
||||
@@ -280,11 +232,9 @@ class UIInputHandler extends BaseModule {
|
||||
const command = this.playerInput.value.trim();
|
||||
console.log(`UIInputHandler: Submitting command: "${command}"`);
|
||||
|
||||
// Add command to history
|
||||
this.addToHistory(command);
|
||||
|
||||
// Dispatch command event
|
||||
this._dispatchModuleEvent('ui:command', {
|
||||
this.dispatchEvent('ui:command', {
|
||||
type: 'input',
|
||||
text: command
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user