mirror of
https://github.com/iDisaster/GTAConnected.git
synced 2026-03-08 09:25:23 +00:00
Fix chat UI position, font initialization, and input handling
- Move chat Y position from 200 to 50 to display correctly at top-left - Fix font initialization to use lucasFont.createDefaultFont() instead of raw lucasFont - Add fallback font (Tahoma) if Arial fails to load - Use bindKey for T key binding for more reliable chat toggle - Define key code constants with fallbacks for SDLK_* values - Filter control characters from text input - Add test message on resource load to verify chat is working - Improve native chat disabling with proper function checks
This commit is contained in:
@@ -70,7 +70,7 @@ let notificationDuration = 5000; // 5 seconds
|
|||||||
// Chat dimensions
|
// Chat dimensions
|
||||||
const chat = {
|
const chat = {
|
||||||
x: 25,
|
x: 25,
|
||||||
y: 200,
|
y: 50,
|
||||||
width: 550,
|
width: 550,
|
||||||
messageHeight: 22,
|
messageHeight: 22,
|
||||||
maxVisibleMessages: 12,
|
maxVisibleMessages: 12,
|
||||||
@@ -85,10 +85,18 @@ const chat = {
|
|||||||
addEventHandler("OnResourceStart", function(event, resource) {
|
addEventHandler("OnResourceStart", function(event, resource) {
|
||||||
if (resource == thisResource) {
|
if (resource == thisResource) {
|
||||||
try {
|
try {
|
||||||
chatFont = lucasFont;
|
// Create font using lucasFont.createDefaultFont() like the mod menu does
|
||||||
console.log("[Chat] Custom chat UI initialized");
|
chatFont = lucasFont.createDefaultFont(16.0, "Arial", "Regular");
|
||||||
|
console.log("[Chat] Custom chat UI initialized with font");
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log("[Chat] Font load error: " + e);
|
console.log("[Chat] Font load error: " + e);
|
||||||
|
try {
|
||||||
|
// Fallback to Tahoma
|
||||||
|
chatFont = lucasFont.createDefaultFont(16.0, "Tahoma");
|
||||||
|
console.log("[Chat] Using fallback font Tahoma");
|
||||||
|
} catch(e2) {
|
||||||
|
console.log("[Chat] Fallback font also failed: " + e2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -460,47 +468,84 @@ function padZero(num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// INPUT HANDLING
|
// KEY CODE CONSTANTS (in case they're not predefined)
|
||||||
|
// SDL key codes use ASCII values for letters (lowercase)
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
addEventHandler("OnKeyUp", function(event, key, scancode, mods) {
|
const KEY_T = typeof SDLK_t !== 'undefined' ? SDLK_t : 116; // 't' = ASCII 116
|
||||||
// T key to open chat
|
const KEY_RETURN = typeof SDLK_RETURN !== 'undefined' ? SDLK_RETURN : 13;
|
||||||
if (key === SDLK_t && !chatInputActive) {
|
const KEY_ESCAPE = typeof SDLK_ESCAPE !== 'undefined' ? SDLK_ESCAPE : 27;
|
||||||
chatInputActive = true;
|
const KEY_BACKSPACE = typeof SDLK_BACKSPACE !== 'undefined' ? SDLK_BACKSPACE : 8;
|
||||||
chatInputText = "";
|
const KEY_PAGEUP = typeof SDLK_PAGEUP !== 'undefined' ? SDLK_PAGEUP : 1073741899;
|
||||||
gui.showCursor(true, true);
|
const KEY_PAGEDOWN = typeof SDLK_PAGEDOWN !== 'undefined' ? SDLK_PAGEDOWN : 1073741902;
|
||||||
event.preventDefault();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// INPUT HANDLING - Using bindKey for more reliable input capture
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
// Bind T key to open chat
|
||||||
|
bindKey(KEY_T, KEYSTATE_UP, function(event) {
|
||||||
|
if (!chatInputActive) {
|
||||||
|
openChatInput();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Function to open chat input
|
||||||
|
function openChatInput() {
|
||||||
|
chatInputActive = true;
|
||||||
|
chatInputText = "";
|
||||||
|
chatFadeAlpha = 1.0;
|
||||||
|
lastMessageTime = Date.now();
|
||||||
|
try {
|
||||||
|
gui.showCursor(true);
|
||||||
|
} catch(e) {
|
||||||
|
console.log("[Chat] Could not show cursor: " + e);
|
||||||
|
}
|
||||||
|
console.log("[Chat] Chat input opened");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to close chat input
|
||||||
|
function closeChatInput() {
|
||||||
|
chatInputActive = false;
|
||||||
|
chatInputText = "";
|
||||||
|
try {
|
||||||
|
gui.showCursor(false);
|
||||||
|
} catch(e) {
|
||||||
|
console.log("[Chat] Could not hide cursor: " + e);
|
||||||
|
}
|
||||||
|
console.log("[Chat] Chat input closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to send chat message
|
||||||
|
function sendChatMessage() {
|
||||||
|
if (chatInputText.length > 0) {
|
||||||
|
triggerNetworkEvent("chatSendMessage", chatInputText);
|
||||||
|
console.log("[Chat] Sent message: " + chatInputText);
|
||||||
|
}
|
||||||
|
closeChatInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
addEventHandler("OnKeyUp", function(event, key, scancode, mods) {
|
||||||
// Escape to close chat
|
// Escape to close chat
|
||||||
if (key === SDLK_ESCAPE && chatInputActive) {
|
if ((key === KEY_ESCAPE || key === SDLK_ESCAPE) && chatInputActive) {
|
||||||
chatInputActive = false;
|
closeChatInput();
|
||||||
chatInputText = "";
|
|
||||||
gui.showCursor(false, false);
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enter to send message
|
// Enter to send message
|
||||||
if (key === SDLK_RETURN && chatInputActive) {
|
if ((key === KEY_RETURN || key === SDLK_RETURN) && chatInputActive) {
|
||||||
if (chatInputText.length > 0) {
|
sendChatMessage();
|
||||||
// Send message to server
|
|
||||||
triggerNetworkEvent("chatSendMessage", chatInputText);
|
|
||||||
}
|
|
||||||
chatInputActive = false;
|
|
||||||
chatInputText = "";
|
|
||||||
gui.showCursor(false, false);
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Page up/down for scrolling
|
// Page up/down for scrolling
|
||||||
if (key === SDLK_PAGEUP && chatMessages.length > chat.maxVisibleMessages) {
|
if ((key === KEY_PAGEUP || key === SDLK_PAGEUP) && chatMessages.length > chat.maxVisibleMessages) {
|
||||||
chatScrollOffset = Math.min(chatScrollOffset + 3, chatMessages.length - chat.maxVisibleMessages);
|
chatScrollOffset = Math.min(chatScrollOffset + 3, chatMessages.length - chat.maxVisibleMessages);
|
||||||
chatFadeAlpha = 1.0;
|
chatFadeAlpha = 1.0;
|
||||||
lastMessageTime = Date.now();
|
lastMessageTime = Date.now();
|
||||||
}
|
}
|
||||||
if (key === SDLK_PAGEDOWN) {
|
if (key === KEY_PAGEDOWN || key === SDLK_PAGEDOWN) {
|
||||||
chatScrollOffset = Math.max(0, chatScrollOffset - 3);
|
chatScrollOffset = Math.max(0, chatScrollOffset - 3);
|
||||||
chatFadeAlpha = 1.0;
|
chatFadeAlpha = 1.0;
|
||||||
lastMessageTime = Date.now();
|
lastMessageTime = Date.now();
|
||||||
@@ -510,7 +555,10 @@ addEventHandler("OnKeyUp", function(event, key, scancode, mods) {
|
|||||||
// Handle text input
|
// Handle text input
|
||||||
addEventHandler("OnCharacter", function(event, character) {
|
addEventHandler("OnCharacter", function(event, character) {
|
||||||
if (chatInputActive) {
|
if (chatInputActive) {
|
||||||
chatInputText += character;
|
// Filter out control characters
|
||||||
|
if (character.charCodeAt(0) >= 32) {
|
||||||
|
chatInputText += character;
|
||||||
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -519,10 +567,13 @@ addEventHandler("OnKeyDown", function(event, key, scancode, mods) {
|
|||||||
if (!chatInputActive) return;
|
if (!chatInputActive) return;
|
||||||
|
|
||||||
// Backspace
|
// Backspace
|
||||||
if (key === SDLK_BACKSPACE && chatInputText.length > 0) {
|
if ((key === KEY_BACKSPACE || key === SDLK_BACKSPACE) && chatInputText.length > 0) {
|
||||||
chatInputText = chatInputText.slice(0, -1);
|
chatInputText = chatInputText.slice(0, -1);
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Block other keys from reaching the game while typing
|
||||||
|
event.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -550,29 +601,34 @@ addEventHandler("OnMouseWheel", function(event, x, y) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// PROCESS - Animation updates
|
// PROCESS - Keep native chat disabled
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
addEventHandler("OnProcess", function(event) {
|
addEventHandler("OnProcess", function(event) {
|
||||||
// Block game input while typing
|
// Keep native chat disabled while our custom chat is active
|
||||||
if (chatInputActive) {
|
try {
|
||||||
// Disable native chat
|
if (typeof setChatWindowEnabled === 'function') {
|
||||||
try {
|
setChatWindowEnabled(false);
|
||||||
chatInputEnabled = false;
|
}
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// DISABLE DEFAULT CHAT
|
// DISABLE DEFAULT CHAT ON RESOURCE START
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
addEventHandler("OnResourceReady", function(event, resource) {
|
addEventHandler("OnResourceReady", function(event, resource) {
|
||||||
if (resource == thisResource) {
|
if (resource == thisResource) {
|
||||||
try {
|
try {
|
||||||
// Disable default chat window
|
// Disable default chat window
|
||||||
setChatWindowEnabled(false);
|
if (typeof setChatWindowEnabled === 'function') {
|
||||||
|
setChatWindowEnabled(false);
|
||||||
|
}
|
||||||
|
console.log("[Chat] Native chat disabled");
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log("[Chat] Could not disable default chat: " + e);
|
console.log("[Chat] Could not disable default chat: " + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add a test message to verify the chat is working
|
||||||
|
addChatMessage("Custom chat loaded! Press T to type.", "system");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user