From 1116687d88553289263885c89f3cd9c8d124fb9c Mon Sep 17 00:00:00 2001 From: Christopher Date: Fri, 4 Dec 2020 23:09:59 +0100 Subject: [PATCH] feat(photo/client): initial commit for client-side functionality --- resources/client/cl_photo.ts | 143 +++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 resources/client/cl_photo.ts diff --git a/resources/client/cl_photo.ts b/resources/client/cl_photo.ts new file mode 100644 index 0000000..7a02ca5 --- /dev/null +++ b/resources/client/cl_photo.ts @@ -0,0 +1,143 @@ +import events from "../utils/events"; +import { Delay } from "../utils/fivem"; +import { tokenData } from '../utils/token'; + +const exp = (global as any).exports + +function closePhoneTemp() { + SendNuiMessage( + //Hides phone + JSON.stringify({ + app: "PHONE", + method: "setVisibility", + data: false, + }) + ); +} + +function openPhoneTemp() { + SendNuiMessage( + //Opens phone + JSON.stringify({ + app: "PHONE", + method: "setVisibility", + data: true, + }) + ); +} + + +let takingPhoto = false + +function CellFrontCamActivate(activate: any) { + return Citizen.invokeNative('0x2491A93618B7D838', activate) +} + +RegisterNuiCallbackType(events.CAMERA_TAKE_PHOTO); +on(`__cfx_nui:${events.CAMERA_TAKE_PHOTO}`, async () => { + CreateMobilePhone(1); + CellCamActivate(true, true); + + closePhoneTemp() + SetNuiFocus(false, false); + + takingPhoto = true; + + while (takingPhoto) { + await Delay(0); + let frontCam = false; + + if (IsControlJustPressed(1, 27)) { + if (!frontCam) { + frontCam = true; + CellFrontCamActivate(true); + } else { + frontCam = false; + CellFrontCamActivate(false); + } + + } else if (IsControlJustPressed(1, 176)) { + takePhoto() + await Delay(200) + console.log("phone closing") + DestroyMobilePhone(); + CellCamActivate(false, false); + + openPhoneTemp() + SetNuiFocus(true, true); + + takingPhoto = false; + + } else if (IsControlJustPressed(1, 177)) { + DestroyMobilePhone(); + CellCamActivate(false, false); + + openPhoneTemp() + SetNuiFocus(true, true); + + takingPhoto = false; + break; + } + } +}); + +//setTick(async () => { +// while (takingPhoto) { +// await Delay(0); +// +// if (IsControlReleased(1, 177)) { +// DestroyMobilePhone(); +// CellCamActivate(false, false); +// takingPhoto = false; +// SetNuiFocus(true, true); +// break; +// } +// } +//}) + + + +onNet(events.CAMERA_SEND_PHOTOS, (photos: string[]) => { + console.log(photos) + SendNuiMessage( + JSON.stringify({ + app: "CAMERA", + method: "setPhotos", + data: photos + }) + ) +}) + + +function takePhoto() { + const [width, height] = GetActiveScreenResolution(); + console.log("photo taken") + exp["screenshot-basic"].requestScreenshotUpload( + "https://api.imgur.com/3/image", + "imgur", + { + headers: { + 'authorization': `Client-ID ${ tokenData.token }`, + 'content-type': 'multipart/form-data' + } + }, + (data: string) => { + console.log("herrrrroooooooo") + const imageLink = JSON.parse(data).data.link; + emitNet(events.CAMERA_UPLOAD_PHOTO, imageLink) + } + ); +} + + +// delete photo + +RegisterNuiCallbackType(events.CAMERA_DELETE_PHOTO); +on(`__cfx_nui:${events.CAMERA_DELETE_PHOTO}`, (data: any) => { + emitNet(events.CAMERA_DELETE_PHOTO, data) +}) + +onNet(events.CAMERA_DELETE_PHOTO_SUCCESS, () => { + emitNet(events.CAMERA_FETCH_PHOTOS) +}) +