forked from vgrizly/rogalik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.js
107 lines (96 loc) · 3.13 KB
/
menu.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
function Menu(x, y) {
this.container = document.getElementById("menu");
this.visible = false;
this.length = 0;
this.offset = {
x: x,
y: y,
};
};
Menu.prototype = {
activate: function(index) {
//TODO: use internal array;
var item = document.getElementById("menu-item-" + index);
if (item)
item.click();
},
show: function(object, x, y, reuse, defaultAction) {
if (game.player.acting)
return false;
if (!object)
return false;
var actions = object.getActions();
if (!actions)
return false;
x = x || game.controller.iface.x;
y = y || game.controller.iface.y;
this.container.style.display = "inline-block";
this.container.innerHTML = '';
if (!reuse) {
this.container.style.left = x + game.offset.x + "px";
this.container.style.top = y + game.offset.y + "px";
}
if (!Array.isArray(actions))
actions = [actions];
if (defaultAction)
actions[0]["Use"] = object.defaultAction;
this.length = 0;
actions.forEach(function(actions) {
if (Object.keys(actions).length > 0)
this.container.appendChild(this.createMenu(actions, object));
}.bind(this));
this.visible = true;
return true;
},
mouseover: function(e) {
var menuItem = e.target;
var item = menuItem.item;
if (!item)
return;
game.controller.world.menuHovered = item;
},
hide: function() {
if(!this.visible)
return;
this.container.style.display = "none";
this.visible = false;
game.controller.world.menuHovered = null;
},
createMenuItem: function(title, action, object, index) {
if (title == "Destroy")
index = 0;
var item_a = document.createElement("a");
item_a.textContent = index + ". " + TS(title);
item_a.className = "action";
if (action instanceof Function) {
var callback = action.bind(object);
} else {
item_a.item = action.item;
item_a.addEventListener("mousemove", this.mouseover);
callback = action.callback.bind(action.item);
}
item_a.onclick = function() {
this.hide();
callback();
}.bind(this);
item_a.id = "menu-item-" + index;
var item = document.createElement("li");
item.appendChild(item_a);
return item;
},
createMenu: function(actions, object) {
var menu = document.createElement("ul");
var sorted = Object.keys(actions).sort(function(a, b) {
if (a == "Destroy")
return +1;
else
return TS(a) > TS(b);
});
sorted.forEach(function(title) {
//TODO: fixme controller.drawItemsMenu hack and remove trim
var menuItem = this.createMenuItem(title.trim(), actions[title], object, ++this.length);
menu.appendChild(menuItem);
}.bind(this));
return menu;
}
};