forked from aframevr/aframe-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolbar.js
More file actions
154 lines (142 loc) · 4.09 KB
/
Copy pathToolbar.js
File metadata and controls
154 lines (142 loc) · 4.09 KB
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
145
146
147
148
149
150
151
152
153
154
import React from 'react';
import {
faPlus,
faPause,
faPlay,
faFloppyDisk,
faQuestion
} from '@fortawesome/free-solid-svg-icons';
import { AwesomeIcon } from '../AwesomeIcon';
import Events from '../../lib/Events';
import { saveBlob } from '../../lib/utils';
import GLTFIcon from '../../../assets/gltf.svg';
function filterHelpers(scene, visible) {
scene.traverse((o) => {
if (o.userData.source === 'INSPECTOR') {
o.visible = visible;
}
});
}
function getSceneName(scene) {
return scene.id || slugify(window.location.host + window.location.pathname);
}
/**
* Slugify the string removing non-word chars and spaces
* @param {string} text String to slugify
* @return {string} Slugified string
*/
function slugify(text) {
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w-]+/g, '-') // Replace all non-word chars with -
.replace(/--+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
/**
* Tools and actions.
*/
export default class Toolbar extends React.Component {
constructor(props) {
super(props);
this.state = {
isPlaying: false
};
}
exportSceneToGLTF() {
const sceneName = getSceneName(AFRAME.scenes[0]);
const scene = AFRAME.scenes[0].object3D;
filterHelpers(scene, false);
AFRAME.INSPECTOR.exporters.gltf.parse(
scene,
function (buffer) {
filterHelpers(scene, true);
const blob = new Blob([buffer], { type: 'application/octet-stream' });
saveBlob(blob, sceneName + '.glb');
},
function (error) {
console.error(error);
},
{ binary: true }
);
}
addEntity() {
Events.emit('entitycreate', { element: 'a-entity', components: {} });
}
/**
* Try to write changes with aframe-inspector-watcher.
*/
writeChanges = () => {
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:51234/save');
xhr.onerror = () => {
alert(
'aframe-watcher not running. This feature requires a companion service running locally. npm install aframe-watcher to save changes back to file. Read more at https://github.com/supermedium/aframe-watcher'
);
};
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(AFRAME.INSPECTOR.history.updates));
};
toggleScenePlaying = () => {
if (this.state.isPlaying) {
AFRAME.scenes[0].pause();
this.setState({ isPlaying: false });
AFRAME.scenes[0].isPlaying = true;
document.getElementById('aframeInspectorMouseCursor').play();
return;
}
AFRAME.scenes[0].isPlaying = false;
AFRAME.scenes[0].play();
this.setState({ isPlaying: true });
};
openHelpModal = () => {
Events.emit('openhelpmodal');
};
render() {
const watcherTitle = 'Write changes with aframe-watcher.';
return (
<div id="toolbar">
<div className="toolbarActions">
<a
className="button"
title="Add a new entity"
onClick={this.addEntity}
>
<AwesomeIcon icon={faPlus} />
</a>
<a
id="playPauseScene"
className="button"
title={this.state.isPlaying ? 'Pause scene' : 'Resume scene'}
onClick={this.toggleScenePlaying}
>
{this.state.isPlaying ? (
<AwesomeIcon icon={faPause} />
) : (
<AwesomeIcon icon={faPlay} />
)}
</a>
<a
className="gltfIcon button"
title="Export to GLTF"
onClick={this.exportSceneToGLTF}
>
<GLTFIcon />
</a>
<a
className="button"
title={watcherTitle}
onClick={this.writeChanges}
>
<AwesomeIcon icon={faFloppyDisk} />
</a>
<a className="button" title="Help" onClick={this.openHelpModal}>
<AwesomeIcon icon={faQuestion} />
</a>
</div>
</div>
);
}
}