Özel uygulamalar
Özel uygulamaya başlangıç
Kayıt, tema, kenar boşlukları, bir NUI callback'i, bir bildirim ve bir kişi seçici içeren, üç dosyalık en küçük çalışan özel uygulama.
#Ne yapıyoruz
Üç dosyalı, my-app adlı bir resource. Uygulama:
- kendini telefona kaydeder ve her telefonda kurulu gelir,
- temayı ve güvenli alan kenar boşluklarını okur,
- kendi resource'undaki bir NUI callback'ini çağırır,
- bir bildirim gönderir,
- kişi seçiciyi açar.
Alan ve mesaj ayrıntıları Özel uygulamalara genel bakış ve Uygulama API'si sayfalarında.
#fxmanifest.lua
Sayfanın yüklediği her dosya files altında listelenmelidir.
luafx_version 'cerulean'
game 'gta5'
lua54 'yes'
client_script 'client.lua'
files { 'html/index.html', 'html/icon.png' }
#client.lua
Uygulama resource başladığında ve telefon her yüklendiğinde yeniden kaydedilir; böylece bir FX Phone yeniden başlatmasından sonra da kalır.
lualocal RESOURCE = GetCurrentResourceName()
local function register()
exports['fx-phone']:AddCustomApp({
identifier = 'hello',
name = 'Hello',
description = 'A minimal fx-phone app.',
defaultApp = true,
ui = RESOURCE .. '/html/index.html',
icon = RESOURCE .. '/html/icon.png',
})
end
AddEventHandler('fx-phone:client:phoneLoaded', register)
CreateThread(function()
if GetResourceState('fx-phone') == 'started' then register() end
end)
RegisterNUICallback('whoami', function(_, cb)
cb({ number = exports['fx-phone']:GetPhoneNumber(), hour = GetClockHours() })
end)
#html/index.html
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
:root { --bg: #f2f2f7; --card: #fff; --text: #000; --tint: #007aff; }
[data-theme='dark'] { --bg: #000; --card: #1c1c1e; --text: #fff; --tint: #0a84ff; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 17px/22px system-ui, sans-serif; }
body { box-sizing: border-box; padding: calc(var(--top) + 12px) 16px calc(var(--bottom) + 12px); }
button, input { display: block; width: 100%; box-sizing: border-box; margin-top: 10px; padding: 12px;
border: 0; border-radius: 12px; background: var(--card); color: var(--tint); font: inherit; text-align: left; }
input { color: var(--text); }
</style>
</head>
<body>
<h1>Hello</h1>
<input id="name" placeholder="Your name">
<button id="whoami">Ask my resource</button>
<button id="notify">Send a notification</button>
<button id="contact">Pick a contact</button>
<p id="out"></p>
<script>
const query = new URLSearchParams(location.search);
const appId = query.get('app');
const resource = query.get('resource');
const root = document.documentElement;
const $ = (id) => document.getElementById(id);
const show = (text) => { $('out').textContent = text; };
const send = (action, data) => parent.postMessage({ type: 'fx-phone', appId, action, data }, '*');
const theme = (value) => { root.dataset.theme = value === 'light' ? 'light' : 'dark'; };
const insets = ({ top, bottom }) => {
root.style.setProperty('--top', `${top}px`);
root.style.setProperty('--bottom', `${bottom}px`);
};
theme(query.get('theme'));
insets({ top: Number(query.get('insetTop')) || 59, bottom: Number(query.get('insetBottom')) || 34 });
window.addEventListener('message', ({ data: msg }) => {
if (!msg || typeof msg !== 'object' || msg.type !== 'fx-phone') return;
if (msg.action === 'ready') { theme(msg.theme); insets(msg.insets); }
if (msg.action === 'theme') theme(msg.theme);
if (msg.action === 'pickResult' && msg.id === 'contact') {
show(msg.data ? `${msg.data.firstname} ${msg.data.lastname ?? ''}: ${msg.data.number}` : 'Cancelled');
}
});
$('whoami').onclick = async () => {
const res = await fetch(`https://${resource}/whoami`, { method: 'POST', body: '{}' });
const data = await res.json();
show(`Number ${data.number}, game hour ${data.hour}`);
};
$('notify').onclick = () => send('notification', { title: 'Hello', description: `Hi ${$('name').value || 'there'}` });
$('contact').onclick = () => send('pick', { id: 'contact', type: 'contact' });
$('name').addEventListener('focus', () => send('setFocus', { focused: true }));
$('name').addEventListener('blur', () => send('setFocus', { focused: false }));
</script>
</body>
</html>
#Başlat
Resource'u server.cfg içinde telefondan sonra ekle:
cfgensure fx-phone
ensure my-app
Uygulama defaultApp = true sayesinde her telefonda kurulu gelir. Bu olmadan oyuncular uygulamayı App Store'dan kurar.
#Her parça ne yapar
- Sorgu dizesi. Telefon sayfayı sorgu dizesinde
app,resource,theme,insetTopveinsetBottomile yükler. Sayfa bunları, herhangi bir mesaj gelmeden önceki ilk çizimi için kullanır. - ready ve theme mesajları. Sayfa yüklendikten sonra telefon temayı ve kenar boşluklarını içeren
readymesajını gönderir. Sonraki bir tema değişikliğithemeolarak gönderilir. Sayfa yeniden yüklenmez. - Kendi NUI callback'in. iframe,
https://cfx-nui-<resource>/adresindeki senin sayfandır; bu yüzdenfetch('https://<resource>/whoami'), herhangi bir NUI sayfasında olduğu gibiclient.luaiçindekiRegisterNUICallbackişleyicisine ulaşır. - Telefona mesajlar.
send('notification', ...)vesend('pick', ...)üst pencereye mesaj gönderir. Seçicinin yanıtı aynıidilepickResultolarak geri gelir. - Metin girişi. Odaklanınca
focused: true, odak kayboluncafalseile gönderilensetFocus, yazarken karakterin hareket etmesini engeller. Her zaman ikisini de gönder.
#Sonraki adımlar
SendCustomAppMessageile Lua'dan sayfaya veri gönder. Bkz. Uygulama API'si.SetAppBadgeile simgeye bir sayı koy,DoPhoneConfirmationile bir onay göster ya daSetAppDataile karakter başına veri tut. Bkz. Diğer telefon özellikleri.- Zamanlayıcı ya da yoklama eklemeden önce Özel uygulama performansı sayfasını oku.