/
_inventory.js
80 lines (80 loc) · 3.08 KB
/
_inventory.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
var EventHandler = (function () {
function EventHandler() {
this.connections = [];
}
EventHandler.prototype.connect = function (event, callback) {
event.connect(callback);
this.connections.push({
event: event,
callback: callback
});
};
EventHandler.prototype.disconnectAll = function () {
this.connections.forEach(function (con) {
con.event.disconnect(con.callback);
});
};
return EventHandler;
}());
var Inventory = (function () {
function Inventory() {
this.selectedIndex = 0;
this.imageSizes = {
inventory: { width: 546, height: 66 },
selector: { width: 72, height: 72 }
};
this.overlayIDs = {
inventory: Overlays.addOverlay("image", {
x: Window.innerWidth / 2 - this.imageSizes.inventory.width / 2,
y: this.imageSizes.inventory.height / 2,
width: this.imageSizes.inventory.width,
height: this.imageSizes.inventory.height,
imageURL: Script.resolvePath("assets/inventory.png")
}),
selector: Overlays.addOverlay("image", {
x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60,
y: this.imageSizes.selector.height / 2 - 6,
width: this.imageSizes.selector.width,
height: this.imageSizes.selector.height,
imageURL: Script.resolvePath("assets/selector.png")
})
};
this.events = new EventHandler();
this.events.connect(Window.geometryChanged, this.geometryChanged.bind(this));
this.events.connect(Overlays.mousePressOnOverlay, this.mousePressOnOverlay.bind(this));
}
Inventory.prototype.geometryChanged = function () {
console.log(JSON.stringify(this));
Overlays.editOverlay(this.overlayIDs.inventory, {
x: Window.innerWidth / 2 - this.imageSizes.inventory.width / 2
});
Overlays.editOverlay(this.overlayIDs.selector, {
x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60
});
};
Inventory.prototype.mousePressOnOverlay = function (overlayID, event) {
switch (overlayID) {
case this.overlayIDs.inventory:
console.log(JSON.stringify(event.pos2D));
break;
}
};
Inventory.prototype.changeSelectedIndex = function (index) {
this.selectedIndex = index;
Overlays.editOverlay(this.overlayIDs.selector, {
x: Window.innerWidth / 2 - this.imageSizes.selector.width / 2 + (this.selectedIndex - 4) * 60
});
};
Inventory.prototype.unload = function () {
var _this = this;
this.events.disconnectAll();
Object.keys(this.overlayIDs).forEach(function (key) {
Overlays.deleteOverlay(_this.overlayIDs[key]);
});
};
return Inventory;
}());
var inventory = new Inventory();
Script.scriptEnding.connect(function () {
inventory.unload();
});