From 6b21cfddd084c31e4435f0593aa5fb55caae22cc Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 9 Feb 2021 18:16:04 -0500 Subject: [PATCH 1/5] cleanup(scripts/cl_photo): Cleaned up code --- resources/client/cl_photo.ts | 98 +++++++++++++++++------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/resources/client/cl_photo.ts b/resources/client/cl_photo.ts index 6bc00e4..e65bfe7 100644 --- a/resources/client/cl_photo.ts +++ b/resources/client/cl_photo.ts @@ -4,7 +4,10 @@ const SCREENSHOT_BASIC_TOKEN = GetConvar('SCREENSHOT_BASIC_TOKEN', 'none'); const exp = (global as any).exports; +let inCameraMode = false; + function closePhoneTemp() { + SetNuiFocus(false, false); SendNuiMessage( //Hides phone JSON.stringify({ @@ -16,6 +19,7 @@ function closePhoneTemp() { } function openPhoneTemp() { + SetNuiFocus(true, true); SendNuiMessage( //Opens phone JSON.stringify({ @@ -26,74 +30,66 @@ function openPhoneTemp() { ); } -let takingPhoto = false; - -function CellFrontCamActivate(activate: any) { +function CellFrontCamActivate(activate: boolean) { return Citizen.invokeNative('0x2491A93618B7D838', activate); } -RegisterNuiCallbackType(events.CAMERA_TAKE_PHOTO); -on(`__cfx_nui:${events.CAMERA_TAKE_PHOTO}`, async () => { - CreateMobilePhone(1); - CellCamActivate(true, true); +const displayHelperText = () => { + BeginTextCommandDisplayHelp('TWOSTRINGS'); + AddTextComponentString('Exit Camera Mode: ~INPUT_CELLPHONE_CANCEL~'); + AddTextComponentString('Toggle Front/Back: ~INPUT_PHONE~'); + EndTextCommandDisplayHelp(0, true, false, -1); +}; +RegisterNuiCallbackType(events.CAMERA_TAKE_PHOTO); +on(`__cfx_nui:${events.CAMERA_TAKE_PHOTO}`, async (data: any, cb: Function) => { + cb(); + // Create Phone Prop + CreateMobilePhone(1); + // Active Camera Change + CellCamActivate(true, true); + // Hide phone from rendering temporary closePhoneTemp(); + SetNuiFocus(false, false); - takingPhoto = true; + inCameraMode = true; - while (takingPhoto) { + while (inCameraMode) { await Delay(0); let frontCam = false; - + displayHelperText(); if (IsControlJustPressed(1, 27)) { - if (!frontCam) { - frontCam = true; - CellFrontCamActivate(true); - } else { - frontCam = false; - CellFrontCamActivate(false); - } + frontCam = !frontCam; + CellFrontCamActivate(frontCam); } else if (IsControlJustPressed(1, 176)) { - takePhoto(); - await Delay(200); - console.log('phone closing'); - DestroyMobilePhone(); - CellCamActivate(false, false); - - openPhoneTemp(); - SetNuiFocus(true, true); - - takingPhoto = false; + await handleTakePicture(); } else if (IsControlJustPressed(1, 177)) { - DestroyMobilePhone(); - CellCamActivate(false, false); - - openPhoneTemp(); - SetNuiFocus(true, true); - - takingPhoto = false; + handleCameraExit(); break; } } + ClearHelp(true); }); -//setTick(async () => { -// while (takingPhoto) { -// await Delay(0); -// -// if (IsControlReleased(1, 177)) { -// DestroyMobilePhone(); -// CellCamActivate(false, false); -// takingPhoto = false; -// SetNuiFocus(true, true); -// break; -// } -// } -//}) +const handleTakePicture = async () => { + takePhoto(); + await Delay(200); + DestroyMobilePhone(); + CellCamActivate(false, false); + openPhoneTemp(); + inCameraMode = false; +}; + +const handleCameraExit = () => { + DestroyMobilePhone(); + CellCamActivate(false, false); + openPhoneTemp(); + inCameraMode = false; +}; onNet(events.CAMERA_SEND_PHOTOS, (photos: string[]) => { - console.log(photos); + // console.log(photos); SendNuiMessage( JSON.stringify({ app: 'CAMERA', @@ -104,7 +100,6 @@ onNet(events.CAMERA_SEND_PHOTOS, (photos: string[]) => { }); function takePhoto() { - const [width, height] = GetActiveScreenResolution(); // Return and log error if screenshot basic token not found if (SCREENSHOT_BASIC_TOKEN === 'none') { return console.error( @@ -129,13 +124,14 @@ function takePhoto() { onNet(events.CAMERA_UPLOAD_PHOTO_SUCCESS, () => { emitNet(events.CAMERA_FETCH_PHOTOS); -}) +}); // delete photo RegisterNuiCallbackType(events.CAMERA_DELETE_PHOTO); -on(`__cfx_nui:${events.CAMERA_DELETE_PHOTO}`, (data: any) => { +on(`__cfx_nui:${events.CAMERA_DELETE_PHOTO}`, (data: any, cb: Function) => { emitNet(events.CAMERA_DELETE_PHOTO, data); + cb(); }); onNet(events.CAMERA_DELETE_PHOTO_SUCCESS, () => { From 47e1f2950ef5a2af1445c71e566479723e225323 Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 9 Feb 2021 18:43:54 -0500 Subject: [PATCH 2/5] fix(scripts/nui-callbacks): Fixed NUI callback stalls --- resources/client/cl_bank.ts | 6 +++--- resources/client/cl_call.ts | 13 ++++++++----- resources/client/cl_contacts.ts | 17 +++++++++-------- resources/client/cl_main.ts | 21 ++++++++++++++------- resources/client/cl_messages.ts | 16 +++++++++++----- 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/resources/client/cl_bank.ts b/resources/client/cl_bank.ts index 2657cc7..3bf7d6f 100644 --- a/resources/client/cl_bank.ts +++ b/resources/client/cl_bank.ts @@ -21,9 +21,9 @@ onNet(events.BANK_SEND_CREDENTIALS, (credentials: any) => { }); RegisterNuiCallbackType(events.BANK_ADD_TRANSFER); -on(`__cfx_nui:${events.BANK_ADD_TRANSFER}`, (data: any) => { - const transfer = data; - emitNet(events.BANK_ADD_TRANSFER, transfer); +on(`__cfx_nui:${events.BANK_ADD_TRANSFER}`, (data: any, cb: Function) => { + emitNet(events.BANK_ADD_TRANSFER, data); + cb(); }); onNet(events.BANK_ADD_TRANSFER_SUCCESS, () => { diff --git a/resources/client/cl_call.ts b/resources/client/cl_call.ts index b066904..2e58fa7 100644 --- a/resources/client/cl_call.ts +++ b/resources/client/cl_call.ts @@ -4,10 +4,10 @@ import { ICall } from '../../phone/src/common/typings/call'; const exp = (global as any).exports; RegisterNuiCallbackType(events.PHONE_INITIALIZE_CALL); -on(`__cfx_nui:${events.PHONE_INITIALIZE_CALL}`, (data: any) => { +on(`__cfx_nui:${events.PHONE_INITIALIZE_CALL}`, (data: any, cb: Function) => { const start = Date.now(); - emitNet(events.PHONE_INITIALIZE_CALL, data.number, start); + cb(); }); onNet( @@ -31,8 +31,9 @@ onNet( ); RegisterNuiCallbackType(events.PHONE_ACCEPT_CALL); -on(`__cfx_nui:${events.PHONE_ACCEPT_CALL}`, (data: any) => { +on(`__cfx_nui:${events.PHONE_ACCEPT_CALL}`, (data: any, cb: Function) => { emitNet(events.PHONE_ACCEPT_CALL, data.transmitterNumber); + cb(); }); onNet( @@ -56,9 +57,10 @@ onNet( ); RegisterNuiCallbackType(events.PHONE_CALL_REJECTED); -on(`__cfx_nui:${events.PHONE_CALL_REJECTED}`, (data: any) => { +on(`__cfx_nui:${events.PHONE_CALL_REJECTED}`, (data: any, cb: Function) => { const end = Date.now(); emitNet(events.PHONE_CALL_REJECTED, data.transmitterNumber, end); + cb(); }); onNet(events.PHONE_CALL_WAS_REJECTED, () => { @@ -79,9 +81,10 @@ onNet(events.PHONE_CALL_WAS_REJECTED, () => { }); RegisterNuiCallbackType(events.PHONE_END_CALL); -on(`__cfx_nui:${events.PHONE_END_CALL}`, (data: any) => { +on(`__cfx_nui:${events.PHONE_END_CALL}`, (data: any, cb: Function) => { const end = Date.now(); emitNet(events.PHONE_END_CALL, data.transmitterNumber, end); + cb(); }); onNet(events.PHONE_CALL_WAS_ENDED, () => { diff --git a/resources/client/cl_contacts.ts b/resources/client/cl_contacts.ts index 5177bbd..3f1cbe0 100644 --- a/resources/client/cl_contacts.ts +++ b/resources/client/cl_contacts.ts @@ -11,11 +11,12 @@ onNet(events.CONTACTS_SEND_CONTACTS, (contacts: any) => { }); RegisterNuiCallbackType(events.CONTACTS_ADD_CONTACT); -on(`__cfx_nui:${events.CONTACTS_ADD_CONTACT}`, (data: any) => { +on(`__cfx_nui:${events.CONTACTS_ADD_CONTACT}`, (data: any, cb: Function) => { const display = data.name; const number = data.number; const avatar = data.avatar; emitNet(events.CONTACTS_ADD_CONTACT, number, display, avatar); + cb(); }); onNet(events.CONTACTS_ADD_CONTACT_SUCCESS, () => { @@ -23,20 +24,20 @@ onNet(events.CONTACTS_ADD_CONTACT_SUCCESS, () => { }); RegisterNuiCallbackType(events.CONTACTS_UPDATE_CONTACT); -on(`__cfx_nui:${events.CONTACTS_UPDATE_CONTACT}`, (data: any) => { - const contact = data; - emitNet(events.CONTACTS_UPDATE_CONTACT, contact); +on(`__cfx_nui:${events.CONTACTS_UPDATE_CONTACT}`, (data: any, cb: Function) => { + emitNet(events.CONTACTS_UPDATE_CONTACT, data); + cb(); }); onNet(events.CONTACTS_UPDATE_CONTACT_SUCCESS, () => { emitNet(events.CONTACTS_GET_CONTACTS); - console.log("did you update the contact??????????????????"); + console.log('did you update the contact??????????????????'); }); RegisterNuiCallbackType(events.CONTACTS_DELETE_CONTACT); -on(`__cfx_nui:${events.CONTACTS_DELETE_CONTACT}`, (data: any) => { - const contact = data; - emitNet(events.CONTACTS_DELETE_CONTACT, contact); +on(`__cfx_nui:${events.CONTACTS_DELETE_CONTACT}`, (data: any, cb: Function) => { + emitNet(events.CONTACTS_DELETE_CONTACT, data); + cb(); }); onNet(events.CONTACTS_DELETE_CONTACT_SUCCESS, () => { diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index c99da94..ec9fd7c 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -326,35 +326,41 @@ async function Phone() { // contacts app RegisterNuiCallbackType(events.OPEN_APP_CONTACTS); -on(`__cfx_nui:${events.OPEN_APP_CONTACTS}`, () => { +on(`__cfx_nui:${events.OPEN_APP_CONTACTS}`, (data: any, cb: Function) => { emitNet(events.CONTACTS_GET_CONTACTS); + cb(); }); RegisterNuiCallbackType(events.OPEN_APP_LISTINGS); -on(`__cfx_nui:${events.OPEN_APP_LISTINGS}`, () => { +on(`__cfx_nui:${events.OPEN_APP_LISTINGS}`, (data: any, cb: Function) => { emitNet(events.SELLOUT_FETCH_LISTING); + cb(); }); RegisterNuiCallbackType(events.OPEN_APP_NOTES); -on(`__cfx_nui:${events.OPEN_APP_NOTES}`, () => { +on(`__cfx_nui:${events.OPEN_APP_NOTES}`, (data: any, cb: Function) => { emitNet(events.NOTE_FETCH_ALL_NOTES); + cb(); }); RegisterNuiCallbackType(events.OPEN_APP_BANK); -on(`__cfx_nui:${events.OPEN_APP_BANK}`, () => { +on(`__cfx_nui:${events.OPEN_APP_BANK}`, (data: any, cb: Function) => { emitNet(events.BANK_FETCH_TRANSACTIONS); emitNet(events.BANK_GET_CREDENTIALS); + cb(); }); RegisterNuiCallbackType(events.OPEN_APP_CAMERA); -on(`__cfx_nui:${events.OPEN_APP_CAMERA}`, () => { +on(`__cfx_nui:${events.OPEN_APP_CAMERA}`, (data: any, cb: Function) => { emitNet(events.CAMERA_FETCH_PHOTOS); + cb(); }); RegisterNuiCallbackType(events.OPEN_APP_DAILER); -on(`__cfx_nui:${events.OPEN_APP_DAILER}`, () => { +on(`__cfx_nui:${events.OPEN_APP_DAILER}`, (data: any, cb: Function) => { emitNet(events.CONTACTS_GET_CONTACTS); emitNet(events.PHONE_CALL_FETCH_CALLS); + cb(); }); RegisterCommand( @@ -367,8 +373,9 @@ RegisterCommand( ); RegisterNuiCallbackType('phone:close'); -on(`__cfx_nui:phone:close`, () => { +on(`__cfx_nui:phone:close`, (data: any, cb: Function) => { Phone(); + cb(); }); // Called for when the phone is closed via the UI. AddEventHandler('onResourceStop', function (resource: string) { diff --git a/resources/client/cl_messages.ts b/resources/client/cl_messages.ts index 9fbb212..68b9cd2 100644 --- a/resources/client/cl_messages.ts +++ b/resources/client/cl_messages.ts @@ -34,8 +34,9 @@ emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS); RegisterNuiCallbackType(events.MESSAGES_CREATE_MESSAGE_GROUP); on( `__cfx_nui:${events.MESSAGES_CREATE_MESSAGE_GROUP}`, - ({ phoneNumbers, label }: any): void => { + ({ phoneNumbers, label }: any, cb: Function): void => { emitNet(events.MESSAGES_CREATE_MESSAGE_GROUP, phoneNumbers, label); + cb(); } ); @@ -52,9 +53,13 @@ onNet(events.MESSAGES_CREATE_MESSAGE_GROUP_FAILED, (result: any): void => { * Messages fetch messages */ RegisterNuiCallbackType(events.MESSAGES_FETCH_MESSAGES); -on(`__cfx_nui:${events.MESSAGES_FETCH_MESSAGES}`, ({ groupId }: any): void => { - emitNet(events.MESSAGES_FETCH_MESSAGES, groupId); -}); +on( + `__cfx_nui:${events.MESSAGES_FETCH_MESSAGES}`, + ({ groupId }: any, cb: Function): void => { + emitNet(events.MESSAGES_FETCH_MESSAGES, groupId); + cb(); + } +); onNet(events.MESSAGES_FETCH_MESSAGES_SUCCESS, (messages: Message[]): void => { sendMessageEvent(events.MESSAGES_FETCH_MESSAGES_SUCCESS, messages); @@ -68,8 +73,9 @@ onNet(events.MESSAGES_FETCH_MESSAGES_FAILED, (): void => { * Messages send message */ RegisterNuiCallbackType(events.MESSAGES_SEND_MESSAGE); -on(`__cfx_nui:${events.MESSAGES_SEND_MESSAGE}`, (data: any) => { +on(`__cfx_nui:${events.MESSAGES_SEND_MESSAGE}`, (data: any, cb: Function) => { emitNet(events.MESSAGES_SEND_MESSAGE, data.groupId, data.message); + cb(); }); onNet(events.MESSAGES_SEND_MESSAGE_SUCCESS, (groupId: string) => { From 368a8c57f851daff2be9e0334a063e498727026f Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 9 Feb 2021 18:45:48 -0500 Subject: [PATCH 3/5] optimize(scripts/anims): Optimized and organized open/close animations --- resources/client/cl_main.ts | 179 +++++++++++++++++------------------- 1 file changed, 83 insertions(+), 96 deletions(-) diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index ec9fd7c..bfd9189 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -29,9 +29,9 @@ const newPhoneProp = async () => { deletePhone(); //deletes the already existing prop before creating another. if (!propCreated) { RequestModel(phoneModel); + while (!HasModelLoaded(phoneModel)) { await Delay(1); - console.log('MODEL HASNT LOADED'); } const playerPed = PlayerPedId(); @@ -78,7 +78,6 @@ function deletePhone() { DeleteEntity(prop); prop = 0; propCreated = false; - console.log('prop destroyed'); } } @@ -90,108 +89,96 @@ async function loadAnimDict(dict: any) { } } -async function phoneOpenAnim() { - //Phone Open Animation +const handleOpenVehicleAnim = async (playerPed: number): Promise => { + const dict = 'anim@cellphone@in_car@ps'; + + ClearPedTasks(playerPed); + await loadAnimDict(dict); + TaskPlayAnim( + playerPed, + dict, + 'cellphone_text_in', + 8.0, + -1, + -1, + 50, + 0, + false, + false, + false + ); + await Delay(300); //Gives time for animation starts before creating the phone + await newPhoneProp(); //Creates the phone and attaches it. +}; + +const handleOpenNormalAnim = async (playerPed: number): Promise => { + //While not in a vehicle it will use this dict. + const dict = 'cellphone@'; + + ClearPedTasks(playerPed); + await loadAnimDict(dict); + TaskPlayAnim( + playerPed, + dict, + 'cellphone_text_in', + 8.0, + -1, + -1, + 50, + 0, + false, + false, + false + ); + await Delay(300); //Gives time for animation starts before creating the phone + await newPhoneProp(); //Creates the phone and attaches it. +}; + +const handleCloseVehicleAnim = async (playerPed: number): Promise => { + //true refers to at get in. + const DICT = 'anim@cellphone@in_car@ps'; + const ANIM = 'cellphone_text_out'; + + StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); //Stop the pull out animation + deletePhone(); //Deletes the prop early incase they get out of the vehicle. + await Delay(250); //lets it get to a certain point + await loadAnimDict(DICT); //loads the new animation + TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket + await Delay(200); //waits until the phone is in the pocket + StopAnimTask(playerPed, DICT, ANIM, 1.0); //clears the animation +}; + +const handleCloseNormalAnim = async (playerPed: number): Promise => { + const DICT = 'cellphone@'; + const ANIM = 'cellphone_text_out'; + StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); //Stop the pull out animation + await Delay(100); //lets it get to a certain point + await loadAnimDict(DICT); //loads the new animation + TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket + await Delay(200); //waits until the phone is in the pocket + StopAnimTask(playerPed, DICT, ANIM, 1.0); //clears the animation + deletePhone(); //Deletes the prop. +}; + +// Determines the phoneOpen animation to play +async function phoneOpenAnim(): Promise { + const playerPed = PlayerPedId(); console.log('phoneOpenAnim'); //Left for testing purposes. - const flag = 50; //https://runtime.fivem.net/doc/natives/?_0xEA47FE3719165B94 deletePhone(); //Deleting before creating a new phone where itll be deleted again. - if (IsPedInAnyVehicle(GetPlayerPed(-1), true)) { - //-- true refers to at get in. - const dict = 'anim@cellphone@in_car@ps'; - - ClearPedTasks(GetPlayerPed(-1)); - await loadAnimDict(dict); - TaskPlayAnim( - GetPlayerPed(-1), - dict, - 'cellphone_text_in', - 8.0, - -1, - -1, - flag, - 0, - false, - false, - false - ); - await Delay(300); //Gives time for animation starts before creating the phone - await newPhoneProp(); //Creates the phone and attaches it. - } else { - //While not in a vehicle it will use this dict. - const dict = 'cellphone@'; - - ClearPedTasks(GetPlayerPed(-1)); - await loadAnimDict(dict); - TaskPlayAnim( - GetPlayerPed(-1), - dict, - 'cellphone_text_in', - 8.0, - -1, - -1, - flag, - 0, - false, - false, - false - ); - await Delay(300); //Gives time for animation starts before creating the phone - await newPhoneProp(); //Creates the phone and attaches it. + if (IsPedInAnyVehicle(playerPed, true)) { + return await handleOpenVehicleAnim(playerPed); } + return await handleOpenNormalAnim(playerPed); } +// Determines the phoneClose animation to play async function phoneCloseAnim() { - //Phone Close Animation + const playerPed = PlayerPedId(); console.log('phoneCloseAnim'); //Left for testing purposes. - const flag = 50; //https://runtime.fivem.net/doc/natives/?_0xEA47FE3719165B94 - const anim = 'cellphone_text_out'; - if (IsPedInAnyVehicle(GetPlayerPed(-1), true)) { - //true refers to at get in. - const dict = 'anim@cellphone@in_car@ps'; - - StopAnimTask(GetPlayerPed(-1), dict, 'cellphone_text_in', 1.0); //Stop the pull out animation - deletePhone(); //Deletes the prop early incase they get out of the vehicle. - await Delay(250); //lets it get to a certain point - loadAnimDict(dict); //loads the new animation - TaskPlayAnim( - GetPlayerPed(-1), - dict, - anim, - 8.0, - -1, - -1, - flag, - 0, - false, - false, - false - ); //puts phone into pocket - await Delay(200); //waits until the phone is in the pocket - StopAnimTask(GetPlayerPed(-1), dict, anim, 1.0); //clears the animation - } else { - //While not in a vehicle it will use this dict. - const dict = 'cellphone@'; - - StopAnimTask(GetPlayerPed(-1), dict, 'cellphone_text_in', 1.0); //Stop the pull out animation - await Delay(100); //lets it get to a certain point - loadAnimDict(dict); //loads the new animation - TaskPlayAnim( - GetPlayerPed(-1), - dict, - anim, - 8.0, - -1, - -1, - flag, - 0, - false, - false, - false - ); //puts phone into pocket - await Delay(200); //waits until the phone is in the pocket - StopAnimTask(GetPlayerPed(-1), dict, anim, 1.0); //clears the animation - deletePhone(); //Deletes the prop. + if (IsPedInAnyVehicle(playerPed, true)) { + return await handleCloseVehicleAnim(playerPed); } + await handleCloseNormalAnim(playerPed); } async function carryingPhone(cb: any) { From f0a2d8c77edb49e114f7789dda70ec4d165636ff Mon Sep 17 00:00:00 2001 From: Taso Date: Wed, 10 Feb 2021 14:25:00 -0500 Subject: [PATCH 4/5] refactor(phone/visibility): Improved organization of visibility flow --- resources/client/cl_main.ts | 319 ++++++++++++++++-------------------- 1 file changed, 139 insertions(+), 180 deletions(-) diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index bfd9189..112b1b4 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -1,12 +1,22 @@ -import { ESX } from '../client/client'; import config from '../utils/config'; import { Delay } from '../utils/fivem'; import events from '../utils/events'; +let prop = 0; +let isPhoneOpen = false; +let propCreated = false; +let phoneModel = 'prop_amb_phone'; // Refered to in newphoneProp function. Requires custom phone being streamed. + +/* * * * * * * * * * * * * + * + * Register Command and Keybinding + * + * * * * * * * * * * * * */ + RegisterCommand( 'phone:close', - (source: any, args: string[], raw: any) => { - phoneCloseAnim(); + async (source: any, args: string[], raw: any) => { + await phoneCloseAnim(); SetNuiFocus(false, false); SendNuiMessage( JSON.stringify({ @@ -19,14 +29,33 @@ RegisterCommand( false ); -let prop = 0; -let isPhoneOpen = false; -let propCreated = false; -let phoneModel = 'prop_amb_phone'; // Refered to in newphoneProp function. Requires custom phone being streamed. +RegisterCommand( + '+phone', + async () => { + //-- Toggles Phone + await Phone(); + }, + false +); + +RegisterCommand( + 'phone:open', + async () => { + await Phone(); + }, + false +); + +RegisterKeyMapping('+phone', 'Open Phone', 'keyboard', 'f1'); + +/* * * * * * * * * * * * * + * + * Prop Deletion/Creation handling + * + * * * * * * * * * * * * */ const newPhoneProp = async () => { - //Function for creating the phone prop - deletePhone(); //deletes the already existing prop before creating another. + removePhoneProp(); //deletes the already existing prop before creating another. if (!propCreated) { RequestModel(phoneModel); @@ -71,7 +100,7 @@ const newPhoneProp = async () => { } }; -function deletePhone() { +function removePhoneProp() { //-- Triggered in newphoneProp function. Only way to destory the prop correctly. if (prop != 0) { //Citizen.invokeNative(0xAE3CBE5BF394C9C9 , Citizen.pointerValueIntInitialized(prop)) @@ -89,6 +118,12 @@ async function loadAnimDict(dict: any) { } } +/* * * * * * * * * * * * * + * + * Animations + * + * * * * * * * * * * * * */ + const handleOpenVehicleAnim = async (playerPed: number): Promise => { const dict = 'anim@cellphone@in_car@ps'; @@ -140,7 +175,7 @@ const handleCloseVehicleAnim = async (playerPed: number): Promise => { const ANIM = 'cellphone_text_out'; StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); //Stop the pull out animation - deletePhone(); //Deletes the prop early incase they get out of the vehicle. + removePhoneProp(); //Deletes the prop early incase they get out of the vehicle. await Delay(250); //lets it get to a certain point await loadAnimDict(DICT); //loads the new animation TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket @@ -157,44 +192,89 @@ const handleCloseNormalAnim = async (playerPed: number): Promise => { TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket await Delay(200); //waits until the phone is in the pocket StopAnimTask(playerPed, DICT, ANIM, 1.0); //clears the animation - deletePhone(); //Deletes the prop. + removePhoneProp(); //Deletes the prop. }; -// Determines the phoneOpen animation to play async function phoneOpenAnim(): Promise { const playerPed = PlayerPedId(); - console.log('phoneOpenAnim'); //Left for testing purposes. - deletePhone(); //Deleting before creating a new phone where itll be deleted again. + removePhoneProp(); //Deleting before creating a new phone where itll be deleted again. if (IsPedInAnyVehicle(playerPed, true)) { return await handleOpenVehicleAnim(playerPed); } return await handleOpenNormalAnim(playerPed); } -// Determines the phoneClose animation to play async function phoneCloseAnim() { const playerPed = PlayerPedId(); - console.log('phoneCloseAnim'); //Left for testing purposes. if (IsPedInAnyVehicle(playerPed, true)) { return await handleCloseVehicleAnim(playerPed); } await handleCloseNormalAnim(playerPed); } -async function carryingPhone(cb: any) { - if (ESX === null) return cb(0); - ESX.TriggerServerCallback( - 'phone:getItemAmount', - (qtty: number) => { - cb(qtty > 0); - }, - 'phone' - ); -} +// setTick(() => { +// if (IsControlJustPressed(1, config.KeyTogglePhone)) { +// Phone(); +// } +// }); -function noPhone() { - if (ESX === null) return; - ESX.ShowNotification('Oi Mate, No El Telephono', false, false, 1); +/* * * * * * * * * * * * * + * + * Phone Visibility Handling + * + * * * * * * * * * * * * */ + +const showPhone = async (): Promise => { + isPhoneOpen = true; + await phoneOpenAnim(); // Animation starts before the phone is open + emitNet('phone:getCredentials'); + SetCursorLocation(0.9, 0.922); //Experimental + SendNuiMessage( + JSON.stringify({ + app: 'PHONE', + method: 'setVisibility', + data: true, + }) + ); + sendPhoneConfig(); + SetNuiFocus(true, true); +}; + +const hidePhone = async (): Promise => { + isPhoneOpen = false; + SendNuiMessage( + //Hides phone + JSON.stringify({ + app: 'PHONE', + method: 'setVisibility', + data: false, + }) + ); + SetNuiFocus(false, false); +}; + +const togglePhoneVisible = async (): Promise => { + if (isPhoneOpen) { + await hidePhone(); + } else { + await showPhone(); + } +}; + +/* * * * * * * * * * * * * + * + * Misc. Helper Functions + * + * * * * * * * * * * * * */ + +async function Phone(): Promise { + if (config.PhoneAsItem) { + // TODO: Do promise callback here + // const hasPhoneItem = await emitNetPromise('phone:hasPhoneItem') + // if (!hasPhoneItem) return + return await togglePhoneVisible(); + } + await togglePhoneVisible(); } function sendPhoneConfig() { @@ -207,110 +287,39 @@ function sendPhoneConfig() { ); } -setTick(() => { - if (IsControlJustPressed(1, config.KeyTogglePhone)) { - Phone(); +AddEventHandler('onResourceStop', function (resource: string) { + if (resource === GetCurrentResourceName()) { + SendNuiMessage( + JSON.stringify({ + app: 'PHONE', + method: 'setVisibility', + data: false, + }) + ); + SetNuiFocus(false, false); + removePhoneProp(); //Deletes the phone incase it was attached. + ClearPedTasks(PlayerPedId()); //Leave here until launch as it'll fix any stuck animations. } }); -// setTick(() => { -// if (isPhoneOpen) { -// DisableControlAction(0, 1, true) -// DisableControlAction(0, 2, true) -// DisableControlAction(0, 24, true) -// DisableControlAction(0, 257, true) -// DisableControlAction(0, 25, true) -// DisableControlAction(0, 263, true) -// } else { -// EnableControlAction(0, 1, true) -// EnableControlAction(0, 2, true) -// EnableControlAction(0, 24, true) -// EnableControlAction(0, 257, true) -// EnableControlAction(0, 25, true) -// EnableControlAction(0, 263, true) -// } -// }) - -async function Phone() { - if (config.PhoneAsItem) { - console.log('CONFIG ON'); - carryingPhone(async (carryingPhone: any) => { - if (carryingPhone) { - if (!isPhoneOpen) { - isPhoneOpen = true; - await phoneOpenAnim(); // Animation starts before the phone is open - emitNet('phone:getCredentials'); - console.log('Sellout fetched from client side'); // Gets the credentials. Will eventually most likely only get the phone number and name, idk. - SetCursorLocation(0.9, 0.922); //Experimental - let res = GetActiveScreenResolution(); - SendNuiMessage( - JSON.stringify({ - app: 'PHONE', - method: 'setVisibility', - data: true, - }) - ); - sendPhoneConfig(); - SetNuiFocus(true, true); - //SetNuiFocusKeepInput(true) - } else { - isPhoneOpen = false; - SendNuiMessage( - //Hides phone - JSON.stringify({ - app: 'PHONE', - method: 'setVisibility', - data: false, - }) - ); - SetNuiFocus(false, false); - //SetNuiFocusKeepInput(false) - - await phoneCloseAnim(); // Animation starts after the UI is closed. - } - } else { - noPhone(); - } - }); - } else if (!config.PhoneAsItem) { - console.log('CONFIG OFF'); - if (!isPhoneOpen) { - isPhoneOpen = true; - await phoneOpenAnim(); // Animation starts before the phone is open - emitNet('phone:getCredentials'); - console.log('Sellout fetched from client side'); - SetCursorLocation(0.936, 0.922); //Experimental - let res = GetActiveScreenResolution(); - SendNuiMessage( - JSON.stringify({ - app: 'PHONE', - method: 'setVisibility', - data: true, - }) - ); - sendPhoneConfig(); - SetNuiFocus(true, true); - //SetNuiFocusKeepInput(true) - } else { - isPhoneOpen = false; - SendNuiMessage( - //Hides phone - JSON.stringify({ - app: 'PHONE', - method: 'setVisibility', - data: false, - }) - ); - SetNuiFocus(false, false); - //SetNuiFocusKeepInput(false) - - await phoneCloseAnim(); // Animation starts after the UI is closed. - } - } -} +onNet('phone:sendCredentials', (number: string) => { + SendNuiMessage( + JSON.stringify({ + app: 'SIMCARD', + method: 'setNumber', + data: number, + }) + ); +}); // DO NOT CHANGE THIS EITHER, PLEASE - CHIP +/* * * * * * * * * * * * * + * + * NUI Service Callback Registration + * + * * * * * * * * * * * * */ + // contacts app RegisterNuiCallbackType(events.OPEN_APP_CONTACTS); on(`__cfx_nui:${events.OPEN_APP_CONTACTS}`, (data: any, cb: Function) => { @@ -350,52 +359,12 @@ on(`__cfx_nui:${events.OPEN_APP_DAILER}`, (data: any, cb: Function) => { cb(); }); -RegisterCommand( - 'phone', - () => { - //-- Toggles Phone - Phone(); - }, - false -); - RegisterNuiCallbackType('phone:close'); -on(`__cfx_nui:phone:close`, (data: any, cb: Function) => { - Phone(); +on(`__cfx_nui:phone:close`, async (data: any, cb: Function) => { + await hidePhone(); cb(); }); // Called for when the phone is closed via the UI. -AddEventHandler('onResourceStop', function (resource: string) { - if (resource === GetCurrentResourceName()) { - SendNuiMessage( - JSON.stringify({ - app: 'PHONE', - method: 'setVisibility', - data: false, - }) - ); - SetNuiFocus(false, false); - deletePhone(); //Deletes the phone incase it was attached. - ClearPedTasks(GetPlayerPed(-1)); //Leave here until launch as it'll fix any stuck animations. - } -}); - -// -//Phone Destory When Wet -// -function countPhone(cb: any) { - if (ESX === null) return cb(0); - ESX.TriggerServerCallback( - 'phone:getItemAmount', - (qtty: number) => { - cb(qtty > 0); - }, - 'phone' - ); -} - -let destroyedPhone = false; - // setTick(async () => { // while (config.SwimDestroy) { // await Delay(config.RunRate * 1000); @@ -415,13 +384,3 @@ let destroyedPhone = false; // } // } // }); - -onNet('phone:sendCredentials', (number: string) => { - SendNuiMessage( - JSON.stringify({ - app: 'SIMCARD', - method: 'setNumber', - data: number, - }) - ); -}); From 587185535f4f3bc938f85914d46db64efd45bea9 Mon Sep 17 00:00:00 2001 From: Taso Date: Wed, 10 Feb 2021 17:18:15 -0500 Subject: [PATCH 5/5] fix(phone/anim): Added missing phoneClose anim to handler --- resources/client/cl_main.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index 1723da1..37b9dd2 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -250,6 +250,7 @@ const hidePhone = async (): Promise => { data: false, }) ); + await phoneCloseAnim(); SetNuiFocus(false, false); }; @@ -363,7 +364,7 @@ on(`__cfx_nui:${events.OPEN_APP_DAILER}`, (data: any, cb: Function) => { RegisterNuiCallbackType('phone:close'); on(`__cfx_nui:phone:close`, async (data: any, cb: Function) => { - await hidePhone(); + await Phone(); cb(); }); // Called for when the phone is closed via the UI.