-
Notifications
You must be signed in to change notification settings - Fork 34
/
menu.js
81 lines (73 loc) · 2.3 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
function Menu(x, y) {
this.container = document.getElementById("menu");
this.visible = false;
this.offset = {
x: x,
y: y,
};
};
Menu.prototype = {
show: function(object, x, y, reuse, defaultAction) {
if (game.player.acting)
return false;
if (!object)
return false;
var actions = object.getActions();
if (defaultAction)
actions.Use = object.defaultAction;
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";
}
this.container.appendChild(this.createMenu(actions, object));
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) {
var item_a = document.createElement("a");
item_a.textContent = util.symbolToString(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);
var item = document.createElement("li");
item.appendChild(item_a)
return item;
},
createMenu: function(actions, object) {
var ul = document.createElement("ul");
for(var title in actions) {
var menuItem = this.createMenuItem(title, actions[title], object);
ul.appendChild(menuItem);
}
return ul;
}
};