-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.js
140 lines (112 loc) · 3.25 KB
/
plugin.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
export default class MenuUiReplacer extends Plugin {
async prestart() {
const menus = await this.getMenus();
const playerMenus = window.customPlayerMenus = new Map;
let customMenuGfx;
if (menus.length) {
customMenuGfx = await this._createCustomMenu();
}
for (const menu of menus) {
const copy = JSON.parse(JSON.stringify(menu));
copy.menuGfx = customMenuGfx;
copy.gfx = new ig.Image(menu.gfx);
this.createIcon(copy);
playerMenus.set(copy.name, copy);
}
}
createIcon(config) {
const gfx = config.gfx;
const {offX, offY, sizeX, sizeY } = config.MapFloorButtonContainer;
const iconGfx = new ig.ImageGui(gfx, offX, offY, sizeX, sizeY);
config.icon = iconGfx;
iconGfx.hook.transitions = {
DEFAULT: {
state: {},
time: 0.2,
timeFunction: KEY_SPLINES.LINEAR
},
HIDDEN: {
state: {
alpha: 0
},
time: 0.2,
timeFunction: KEY_SPLINES.LINEAR
}
}
}
getMenus() {
return new Promise((resolve, reject) => {
$.ajax({
dataType: "json",
url: "data/menu.json",
success: (data) => {
resolve(data)
},
error: () => {
reject();
}
});
});
}
async getBaseMenuImg() {
const img = new Image;
await new Promise((resolve, reject) => {
img.onload = () => {
resolve();
};
img.onerror = () => {
reject();
};
img.src = "media/gui/menu.png";
});
return img;
}
async _createCustomMenu() {
const img = new ig.Image;
const baseImage = await this.getBaseMenuImg();
img.width = baseImage.width;
img.height = baseImage.height;
const settings = {
width: baseImage.width,
height: baseImage.height,
clearInstructions: [{
x: 280,
y: 424,
w: 16,
h: 11
},{
x: 280,
y: 472,
w: 126,
h: 35
}]
};
const modifiedImage = await this._createTemplateImage(baseImage, settings);
img.data = modifiedImage;
return img;
}
async _createTemplateImage(baseImage, settings) {
const canvas = document.createElement("canvas");
canvas.width = settings.width;
canvas.height = settings.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(baseImage, 0, 0);
for (const {x, y, w, h} of settings.clearInstructions) {
ctx.clearRect(x, y, w, h);
}
return await this.loadImage(canvas.toDataURL("image/png"));
}
async loadImage(src) {
let imgData = new Image;
await new Promise((resolve, reject) => {
imgData.onload = () => {
resolve();
};
imgData.onerror = () => {
reject();
};
imgData.src = src;
});
return imgData;
}
}