File size: 5,572 Bytes
d548706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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);
}

let number = 0
window.input_div
window.user_input
function userInput(_text) {
  if (window.input_div == null){
	  const div = document.createElement("div");
	  window.input_div = div
	  div.setAttribute(
		"style",
		"width: 100%;display: flex;flex-direction: column;position: fixed;bottom: 0%;left: 50%;transform: translate(-50%, 0%); background:block;box-shadow:0 0px 4px 4px #00000033;border-radius:5px;overflow:hidden;z-index:99;opacity:0"
	  );
	  const inputContianer = document.createElement("div");
	  inputContianer.setAttribute("style", "display:flex;width:100%;box-sizing:border-box;padding:10px 15px;");
	  const input = document.createElement("input");
	  window.user_input = input
	  input.type = "text";
	  input.setAttribute(
		"style",
		"display:block;width:100%;border:1px solid #007aff;border-radius:2px;font-size:16px;height:200px;line-height:28px;padding:0 5px;"
	  );
	  // 设置输入框的默认值为传入的参数 _text
	  input.value = _text || "";  // 如果 _text 是 undefined 或者 null,默认设置为空
	  
	  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;";
	  // buttonContianer.appendChild(confirmButton);
	  div.appendChild(buttonContianer);
	  document.querySelector("body").appendChild(div);
	  // 输入
	  input.addEventListener("input", ()=>{
		const value = input.value;
		window.godotUserInput(value); //将输入传递给Godot
	  });
	  // 获取焦点
	  input.addEventListener("focus", ()=>{
		number += 1
		div.style.left = "-999999px";
	  });
	  // 失去焦点
	  input.addEventListener("blur", () => {
		div.remove();
		window.input_div = null;
	  });
	  if (number >0){
		input.focus();
		input.click();
	  }
  }
}

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);