-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontextMenu.js
144 lines (112 loc) · 3.75 KB
/
contextMenu.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
141
142
143
144
//contextMenu.js
import {selection} from './axonViewer.js';
let posX = 0;
let posY = 0;
let dragging = false;
function main(){
//------ open, drag, close ------
document.querySelector('#c').addEventListener("contextmenu", showContextMenu);
document.querySelector('#closeMenu').addEventListener("mouseup", closeMenu);
document.querySelector('#contextMenu').addEventListener("mousedown", startDrag);
window.addEventListener("mousemove", doDrag);
window.addEventListener("mouseup", endDrag);
//------ open, drag, close ------
document.querySelector('#X').addEventListener('input', changeX);
document.querySelector('#Y').addEventListener('input', changeY);
document.querySelector('#Z').addEventListener('input', changeZ);
document.querySelector('#maxExplode').addEventListener('input', changeMaxExplode);
document.querySelector('#modelColor').addEventListener('input', changeColor);
}
function setMenuState(obj){
console.log(obj.userData.explode);
document.querySelector('#X').value = obj.userData.explode.vec.x.toFixed(2);
document.querySelector('#Y').value = obj.userData.explode.vec.y.toFixed(2);
document.querySelector('#Z').value = obj.userData.explode.vec.z.toFixed(2);
document.querySelector('#maxExplode').value = obj.userData.explode.max;
let col = obj.userData.attributes.drawColor;
document.querySelector('#modelColor').value = rgbToHex(col.r, col.g, col.b);
}
function changeX(e){
console.log("hi");
let val = parseFloat(e.target.value);
selection.map(obj => obj.userData.explode.vec.setX(val).normalize());
}
function changeY(e){
let val = parseFloat(e.target.value);
selection.map(obj => obj.userData.explode.vec.setY(val).normalize());
}
function changeZ(e){
let val = parseFloat(e.target.value);
selection.map(obj => obj.userData.explode.vec.setZ(val).normalize());
}
function changeMaxExplode(e){
let val = parseInt(e.target.value);
selection.map(obj => obj.userData.explode.max = val);
}
function changeColor(e){
let val = e.target.value;
selection.map(a => a.traverse((b) => {
if(b.material) {
b.material.color.set(val);
}
if(b.userData.attributes){
b.userData.attributes.drawColor = hexToRgb(val);
}
return b;
})
);
}
//------ open, drag, close ------
function showContextMenu(e) {
e.preventDefault();
e.stopPropagation();
posX = e.clientX;
posY = e.clientY;
console.log(selection);
if(selection.length){
setMenuState(selection[0]);
}
document.querySelector("#contextMenu").style.visibility = "visible";
document.querySelector("#contextMenu").style.top = posY + "px";
document.querySelector("#contextMenu").style.left = posX + "px";
}
function startDrag(e) {
if(e.target.id != "contextMenu") return;
dragging = true;
}
function doDrag(e) {
if(!dragging) return;
e.stopPropagation();
e.preventDefault();
posX = e.clientX;
posY = e.clientY;
document.querySelector("#contextMenu").style.top = posY + "px";
document.querySelector("#contextMenu").style.left = posX + "px";
}
function endDrag(e) {
if(dragging) e.preventDefault();
dragging = false;
}
function closeMenu(e){
e.preventDefault();
document.querySelector("#contextMenu").style.visibility = "hidden";
dragging = false;
}
//------ open, drag, close ------
// https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
main();