From 6b21cfddd084c31e4435f0593aa5fb55caae22cc Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 9 Feb 2021 18:16:04 -0500 Subject: [PATCH] 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, () => {