h5prod / house /gameapp.js
Death-alter's picture
Upload 14 files
62d8dcc verified
function confirmSelectFile(accept = "all") {
const div = document.createElement("div");
div.innerHTML = `<div style="margin-bottom:10px;padding:8px;">Do you allow application to access phone storage?</div>`;
div.setAttribute(
"style",
"width: 220px;display: flex;flex-direction: column;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%); background:white;box-shadow:0 0px 4px 4px #00000033;border-radius:5px;overflow:hidden;z-index:99;"
);
const input = document.createElement("input");
input.type = "file";
input.id = "btn";
if (accept !== "all") {
if (accept !== "video" && accept !== "audio") throw new Error("不支持文件类型");
input.accept = `${accept}/*`;
}
// input.multiple = true;
input.onchange = async function (event) {
div.remove();
uni.postMessage({
data: {
action: "show-loading", //显示加载框
},
});
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const fileContent = e.target.result; // 读取到的文件内容(Base64 编码)
if (fileContent) {
WebViewMessage.send(
"request-file-system",
JSON.stringify({
success: true,
file: fileContent,
})
);
} else {
WebViewMessage.send(
"request-file-system",
JSON.stringify({
success: false,
file: null,
})
);
}
uni.postMessage({
data: {
action: "hide-loading", //显示加载框
},
});
};
reader.readAsDataURL(file); // 读取文件内容为 Data URL
}
};
const buttonContianer = document.createElement("div");
buttonContianer.setAttribute("style", "display:flex;width:100%;");
const buttonStyle =
"display: block;background:white;width:50%;padding: 5px;border: 0;height: 40px;border-top:1px solid #ccc;";
const confirmButton = document.createElement("button");
confirmButton.setAttribute("style", buttonStyle + "color: #007aff;");
confirmButton.innerText = "Confirm";
confirmButton.onclick = () => {
input.click();
};
const cancelButton = document.createElement("button");
cancelButton.setAttribute("style", buttonStyle + "color: red;border-right:1px solid #ccc;");
cancelButton.innerText = "Cancel";
cancelButton.onclick = () => {
div.remove();
};
buttonContianer.appendChild(cancelButton);
buttonContianer.appendChild(confirmButton);
div.appendChild(buttonContianer);
document.querySelector("body").appendChild(div);
}
function userInput() {
const div = document.createElement("div");
div.setAttribute(
"style",
"width: 240px;display: flex;flex-direction: column;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%); background:white;box-shadow:0 0px 4px 4px #00000033;border-radius:5px;overflow:hidden;z-index:99;"
);
const inputContianer = document.createElement("div");
inputContianer.setAttribute("style", "display:flex;width:100%;box-sizing:border-box;padding:10px 15px;");
const input = document.createElement("input");
input.type = "text";
input.setAttribute(
"style",
"display:block;width:100%;height:20px;border:1px solid #007aff;border-radius:2px;font-size:16px;height:28px;line-height:28px;padding:0 5px;"
);
inputContianer.appendChild(input);
div.append(inputContianer);
const buttonContianer = document.createElement("div");
buttonContianer.setAttribute("style", "display:flex;width:100%;");
const buttonStyle =
"display: block;background:white;width:50%;padding: 5px;border: 0;height: 40px;border-top:1px solid #ccc;";
const confirmButton = document.createElement("button");
confirmButton.setAttribute("style", buttonStyle + "color: #007aff;");
confirmButton.innerText = "Confirm";
confirmButton.onclick = () => {
const value = input.value;
if (typeof window.godotUserInput === "function") {
window.godotUserInput(value); // 修正了传参的语法
}
div.remove();
};
const cancelButton = document.createElement("button");
cancelButton.setAttribute("style", buttonStyle + "color: red;border-right:1px solid #ccc;");
cancelButton.innerText = "Cancel";
cancelButton.onclick = () => {
div.remove();
};
buttonContianer.appendChild(cancelButton);
buttonContianer.appendChild(confirmButton);
div.appendChild(buttonContianer);
document.querySelector("body").appendChild(div);
}
window.WebViewMessage = {
actionList: {},
on(action, callback) {
if (!this.actionList[action]) this.actionList[action] = [];
this.actionList[action].push(callback);
},
clear(action) {
this.actionList[action] = [];
},
send(action, data) {
if (!this.actionList[action] instanceof Array) return;
for (let func of this.actionList[action]) {
func(data);
}
},
};
function adjustCanvas() {
const canvas = document.getElementById("canvas");
const deviceWidth = window.innerWidth;
const deviceHeight = window.innerHeight;
const aspect = deviceWidth / deviceHeight;
canvas.style = `width:${deviceWidth}px; height:${deviceHeight}px;`;
if (aspect >= 9 / 16) {
canvas.width = 1080;
canvas.height = 1080 / aspect;
} else {
canvas.width = 1920 * aspect;
canvas.height = 1920;
}
}
window.addEventListener("load", adjustCanvas);
window.addEventListener("resize", adjustCanvas);