refactor(phone/visibility): Improved organization of visibility flow

This commit is contained in:
Taso committed 2021-02-10 14:25:00 -05:00
1 parent 368a8c57f8
commit f0a2d8c77e
1 file changed
+135 -176
+135 -176
View File
@@ -1,12 +1,22 @@
import { ESX } from '../client/client';
import config from '../utils/config'; import config from '../utils/config';
import { Delay } from '../utils/fivem'; import { Delay } from '../utils/fivem';
import events from '../utils/events'; 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( RegisterCommand(
'phone:close', 'phone:close',
(source: any, args: string[], raw: any) => { async (source: any, args: string[], raw: any) => {
phoneCloseAnim(); await phoneCloseAnim();
SetNuiFocus(false, false); SetNuiFocus(false, false);
SendNuiMessage( SendNuiMessage(
JSON.stringify({ JSON.stringify({
@@ -19,14 +29,33 @@ RegisterCommand(
false false
); );
let prop = 0; RegisterCommand(
let isPhoneOpen = false; '+phone',
let propCreated = false; async () => {
let phoneModel = 'prop_amb_phone'; // Refered to in newphoneProp function. Requires custom phone being streamed. //-- 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 () => { const newPhoneProp = async () => {
//Function for creating the phone prop removePhoneProp(); //deletes the already existing prop before creating another.
deletePhone(); //deletes the already existing prop before creating another.
if (!propCreated) { if (!propCreated) {
RequestModel(phoneModel); RequestModel(phoneModel);
@@ -71,7 +100,7 @@ const newPhoneProp = async () => {
} }
}; };
function deletePhone() { function removePhoneProp() {
//-- Triggered in newphoneProp function. Only way to destory the prop correctly. //-- Triggered in newphoneProp function. Only way to destory the prop correctly.
if (prop != 0) { if (prop != 0) {
//Citizen.invokeNative(0xAE3CBE5BF394C9C9 , Citizen.pointerValueIntInitialized(prop)) //Citizen.invokeNative(0xAE3CBE5BF394C9C9 , Citizen.pointerValueIntInitialized(prop))
@@ -89,6 +118,12 @@ async function loadAnimDict(dict: any) {
} }
} }
/* * * * * * * * * * * * *
*
* Animations
*
* * * * * * * * * * * * */
const handleOpenVehicleAnim = async (playerPed: number): Promise<void> => { const handleOpenVehicleAnim = async (playerPed: number): Promise<void> => {
const dict = 'anim@cellphone@in_car@ps'; const dict = 'anim@cellphone@in_car@ps';
@@ -140,7 +175,7 @@ const handleCloseVehicleAnim = async (playerPed: number): Promise<void> => {
const ANIM = 'cellphone_text_out'; const ANIM = 'cellphone_text_out';
StopAnimTask(playerPed, DICT, 'cellphone_text_in', 1.0); //Stop the pull out animation 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 Delay(250); //lets it get to a certain point
await loadAnimDict(DICT); //loads the new animation await loadAnimDict(DICT); //loads the new animation
TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket 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<void> => {
TaskPlayAnim(playerPed, DICT, ANIM, 8.0, -1, -1, 50, 0, false, false, false); //puts phone into pocket 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 await Delay(200); //waits until the phone is in the pocket
StopAnimTask(playerPed, DICT, ANIM, 1.0); //clears the animation 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<void> { async function phoneOpenAnim(): Promise<void> {
const playerPed = PlayerPedId(); const playerPed = PlayerPedId();
console.log('phoneOpenAnim'); //Left for testing purposes. removePhoneProp(); //Deleting before creating a new phone where itll be deleted again.
deletePhone(); //Deleting before creating a new phone where itll be deleted again.
if (IsPedInAnyVehicle(playerPed, true)) { if (IsPedInAnyVehicle(playerPed, true)) {
return await handleOpenVehicleAnim(playerPed); return await handleOpenVehicleAnim(playerPed);
} }
return await handleOpenNormalAnim(playerPed); return await handleOpenNormalAnim(playerPed);
} }
// Determines the phoneClose animation to play
async function phoneCloseAnim() { async function phoneCloseAnim() {
const playerPed = PlayerPedId(); const playerPed = PlayerPedId();
console.log('phoneCloseAnim'); //Left for testing purposes.
if (IsPedInAnyVehicle(playerPed, true)) { if (IsPedInAnyVehicle(playerPed, true)) {
return await handleCloseVehicleAnim(playerPed); return await handleCloseVehicleAnim(playerPed);
} }
await handleCloseNormalAnim(playerPed); await handleCloseNormalAnim(playerPed);
} }
async function carryingPhone(cb: any) { // setTick(() => {
if (ESX === null) return cb(0); // if (IsControlJustPressed(1, config.KeyTogglePhone)) {
ESX.TriggerServerCallback( // Phone();
'phone:getItemAmount', // }
(qtty: number) => { // });
cb(qtty > 0);
},
'phone'
);
}
function noPhone() { /* * * * * * * * * * * * *
if (ESX === null) return; *
ESX.ShowNotification('Oi Mate, No El Telephono', false, false, 1); * Phone Visibility Handling
*
* * * * * * * * * * * * */
const showPhone = async (): Promise<void> => {
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<void> => {
isPhoneOpen = false;
SendNuiMessage(
//Hides phone
JSON.stringify({
app: 'PHONE',
method: 'setVisibility',
data: false,
})
);
SetNuiFocus(false, false);
};
const togglePhoneVisible = async (): Promise<void> => {
if (isPhoneOpen) {
await hidePhone();
} else {
await showPhone();
}
};
/* * * * * * * * * * * * *
*
* Misc. Helper Functions
*
* * * * * * * * * * * * */
async function Phone(): Promise<void> {
if (config.PhoneAsItem) {
// TODO: Do promise callback here
// const hasPhoneItem = await emitNetPromise('phone:hasPhoneItem')
// if (!hasPhoneItem) return
return await togglePhoneVisible();
}
await togglePhoneVisible();
} }
function sendPhoneConfig() { function sendPhoneConfig() {
@@ -207,110 +287,39 @@ function sendPhoneConfig() {
); );
} }
setTick(() => { AddEventHandler('onResourceStop', function (resource: string) {
if (IsControlJustPressed(1, config.KeyTogglePhone)) { if (resource === GetCurrentResourceName()) {
Phone(); 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(() => { onNet('phone:sendCredentials', (number: string) => {
// 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( SendNuiMessage(
JSON.stringify({ JSON.stringify({
app: 'PHONE', app: 'SIMCARD',
method: 'setVisibility', method: 'setNumber',
data: true, data: number,
}) })
); );
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.
}
}
}
// DO NOT CHANGE THIS EITHER, PLEASE - CHIP // DO NOT CHANGE THIS EITHER, PLEASE - CHIP
/* * * * * * * * * * * * *
*
* NUI Service Callback Registration
*
* * * * * * * * * * * * */
// contacts app // contacts app
RegisterNuiCallbackType(events.OPEN_APP_CONTACTS); RegisterNuiCallbackType(events.OPEN_APP_CONTACTS);
on(`__cfx_nui:${events.OPEN_APP_CONTACTS}`, (data: any, cb: Function) => { 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(); cb();
}); });
RegisterCommand(
'phone',
() => {
//-- Toggles Phone
Phone();
},
false
);
RegisterNuiCallbackType('phone:close'); RegisterNuiCallbackType('phone:close');
on(`__cfx_nui:phone:close`, (data: any, cb: Function) => { on(`__cfx_nui:phone:close`, async (data: any, cb: Function) => {
Phone(); await hidePhone();
cb(); cb();
}); // Called for when the phone is closed via the UI. }); // 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 () => { // setTick(async () => {
// while (config.SwimDestroy) { // while (config.SwimDestroy) {
// await Delay(config.RunRate * 1000); // 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,
})
);
});