-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
RS_WXPos.js
276 lines (225 loc) · 8.45 KB
/
RS_WXPos.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//================================================================
// RS_WXPos.js
// ---------------------------------------------------------------
// The MIT License
// Copyright (c) 2020 biud436
// ---------------------------------------------------------------
// Free for commercial and non commercial use.
//================================================================
/*:
* @plugindesc <RS_WXPos>
* @author biud436
*
* @help
*
*/
var Imported = Imported || {};
Imported.RS_WXPos = true;
var RS = RS || {};
RS.WXPos = RS.WXPos || {};
(function($) {
"use strict";
var parameters = $plugins.filter(function (i) {
return i.description.contains('<RS_WXPos>');
});
parameters = (parameters.length > 0) && parameters[0].parameters;
let Config = {
"Scene_Menu": {
"Window_Gold": {
x: 300,
y: 0,
},
},
"Scene_Title": {
"Window_TitleCommand": {
x: 200,
y: 200,
}
}
};
const utils = {
configFile : "myWindowConfig.json",
async writeConfigFile() {
if(!Utils.isNwjs()) return;
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const writeFile = promisify(fs.writeFile);
let rootFolder = path.dirname(process.mainModule.filename).replace(/\\/g, "/");
let contents = JSON.stringify(Config, null, " ");
await writeFile(path.join(rootFolder, "data", this.configFile), contents, "utf8")
.then(val => {
console.log(`myWindowConfig.json file is written completely!`);
}).catch(err => {
throw new Error(err);
})
},
async readConfigFile() {
if(!Utils.isNwjs()) return;
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const readFile = promisify(fs.readFile);
let rootFolder = path.dirname(process.mainModule.filename).replace(/\\/g, "/");
let targetFile = path.join(rootFolder, "data", this.configFile);
if(fs.existsSync(targetFile)) {
await readFile(targetFile, "utf8")
.then(contents => {
Config = JSON.parse(contents);
console.log(`myWindowConfig.json file is loaded successfully`);
}).catch(err => {
throw new Error(err);
})
}
},
async internalDownloadFile(url, filename, callback) {
const options = {
port: 443,
headers: {},
};
let req = https.get(url, options, res => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
const headers = res.headers;
if('content-disposition' in headers) {
if(/(?:filename=)(.*)/i.exec( headers['content-disposition'])) {
filename = RegExp.$1;
}
}
let out = fs.createWriteStream(filename);
let len = 1048576;
let isValid = false;
if("content-length" in res.headers) {
len = parseInt(res.headers["content-length"], 10);
isValid = true;
}
let cur = 0;
let total = len / 1048576;
if(res.statusCode === 200) {
res.pipe(out);
res.on('data', c => {
cur += c.length;
if(isValid) {
console.log(`${filename} => ${(100.0 * cur / len).toFixed(0)}% (${(cur / total).toFixed(2)} MB)`);
}
});
res.on('end', async () => {
await callback(null, res);
});
}
});
req.on('error', async err => {
await callback(err, null);
});
},
download(url, filename) {
if(!Utils.isNwjs()) return;
const {promisify} = require('util');
const downloadFile = promisify(this.internalDownloadFile);
downloadFile(url, filename).then(res => {
console.log(`Completed download the file called ${filename}`);
}).catch(err => {
throw new Error(err);
});
}
};
/**
* interactive는 버그가 있다.
* GameCanvas의 zIndex가 다른 모든 HtmlElement보다 위에 있어야 동작한다.
* 그렇게 하지 않으면, mousemove하고 pointermove만 동작하게 된다.
*/
Scene_Base.prototype.prepareInteraction = function() {
let maxIndex = 0;
this._tempCanvasIndex = parseInt(Graphics._canvas.style.zIndex);
this.on("removed", () => {
Graphics._canvas.style.zIndex = this._tempCanvasIndex;
});
for(const el of document.body.children) {
let targetIndex = parseInt(el.style.zIndex);
if(targetIndex > maxIndex) {
maxIndex = targetIndex;
}
}
Graphics._canvas.style.zIndex = maxIndex + 1;
this._isReadyToInteraction = true;
}
Scene_Base.prototype.handleTouchInsideWindow = function(ecurrentSceneName, e) {
e.interactive = true;
e.hitArea = new PIXI.Rectangle(0, 0, e.width || 100, e.height || 100);
const Events = [
"mousedown",
"mousemove",
// "mouseout",
// "mouseover",
"mouseup",
"mouseupoutside",
"pointercancel",
"pointerdown",
"pointermove",
"pointerout",
"pointerover",
"pointertap",
"pointerup",
"pointerupoutside"
];
Events.forEach(evName => {
e.on(evName,
/**
* @param {Event} event
*/
(event) => {
console.log(evName);
const data = event.data;
const mouseX = data.global.x;
const mouseY = data.global.y;
});
})
e.on("mouseover", (ev) => {
e.opacity = 228;
})
e.on("mouseout", (ev) => {
e.opacity = 40;
})
};
/**
* 윈도우 내 특정 프로퍼티를 재정의합니다.
* @param {String} currentSceneName
* @param {Window} e
*/
Scene_Base.prototype.resetWindow = function(currentSceneName, e) {
if(e instanceof Window) {
let name = e.constructor.name.toString();
let config = Config[currentSceneName];
const keys = Object.keys(config);
if(keys.contains(name)) {
e.x = config[name].x;
e.y = config[name].y;
this.handleTouchInsideWindow(currentSceneName, e);
// 회전된 상태는 이 로직으로 감지 불가능
if(e.visible && e.opacity > 0) {
if(e.x < 0 && e.x > Graphics.boxWidth) {
e.visible = false;
}
if(e.visible && e.y < 0 && e.y > Graphics.boxHeight) {
e.visible = false;
}
}
e._isWindow = false;
}
}
};
/**
* 씬이 생성될 때, 모든 윈도우의 위치를 재설정합니다.
*/
Scene_Base.prototype.initWithWindowPosition = function() {
let currentSceneName = this.constructor.name.toString();
this.children.forEach(e => this.resetWindow(currentSceneName, e));
if(this._windowLayer) this._windowLayer.children.forEach(e => this.resetWindow(currentSceneName, e));
};
var alias_Scene_Menu_create = Scene_Menu.prototype.create;
Scene_Menu.prototype.create = function() {
alias_Scene_Menu_create.call(this);
this.prepareInteraction();
this.initWithWindowPosition();
};
})(RS.WXPos);