-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft-menu.js
115 lines (83 loc) · 3.04 KB
/
draft-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
108
109
110
111
112
113
114
115
class DirectionalMenu {
constructor() {
this.buttons = [];
this.focus = null;
}
closestButtonTo(position) {
let minDistance = Infinity;
let closest = null;
for(let i = 0; i < this.buttons.length; ++i) {
let button = this.buttons[i];
let distance = 0;
let minDim = Math.min(position.length, button.length);
for(let dim = 0; dim < minDim; ++dim) {
distance += Math.pow(button[dim] - position[dim], 2);
}
distance = Math.sqrt(distance);
if(distance < minDistance) {
closest = button;
minDistance = distance;
}
}
return closest;
}
setPosition(position) {
this.focus = this.closestButtonTo(position);
return this;
}
move(vector, approx = Math.PI / 4) {
const x = vector[0], y = vector[1];
let center = this.focus;
let angle = Math.atan2(y, x);
let closeAngles = [];
for(let i = 0; i < this.buttons.length; ++i) {
let button = this.buttons[i];
if(button != center) {
let a = Math.atan2(button[1] - center[1], button[0] - center[0]);
if(Math.abs(a - angle) % (2*Math.PI) <= approx) {
closeAngles.push(button);
}
}
}
if(closeAngles.length > 0) {
closeAngles.sort(function(a, b) {
let distA = 0;
let distB = 0;
const minDimA = Math.min(center.length, a.length);
const minDimB = Math.min(center.length, b.length);
for(let dim = 0; dim < minDimA; ++dim) {
distA += Math.pow(a[dim] - center[dim], 2);
}
for(let dim = 0; dim < minDimA; ++dim) {
distB += Math.pow(b[dim] - center[dim], 2);
}
distA = Math.sqrt(distA);
distB = Math.sqrt(distB);
return distA - distB;
});
this.focus = closeAngles[0];
}
return this;
}
addButton(position) {
let button = position;
this.buttons.push(button);
if(this.focus == null) {
this.focus = button;
}
return this;
}
}
let directionalMenu = new DirectionalMenu();
directionalMenu.addButton([0, 0]);
directionalMenu.addButton([16, 0]);
directionalMenu.addButton([-16, 0]);
directionalMenu.addButton([-16*Math.sqrt(2)/2, -16*Math.sqrt(2)/2]);
directionalMenu.addButton([-16*Math.sqrt(2)/2, +16*Math.sqrt(2)/2]);
class MenuButton extends Array {
constructor() {
super();
this.text = "";
this.color = "black";
}
}