implemented event dispatcher

This commit is contained in:
casperlamboo 2016-03-29 00:35:53 +02:00
parent 02e406f018
commit ef74eebf8a
5 changed files with 52 additions and 40 deletions

View File

@ -18,6 +18,7 @@ System.config({
map: { map: {
"babel": "npm:babel-core@5.8.21", "babel": "npm:babel-core@5.8.21",
"babel-runtime": "npm:babel-runtime@5.8.20", "babel-runtime": "npm:babel-runtime@5.8.20",
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"clipper-lib": "npm:clipper-lib@1.0.0", "clipper-lib": "npm:clipper-lib@1.0.0",
"core-js": "npm:core-js@0.9.18", "core-js": "npm:core-js@0.9.18",
"json": "github:systemjs/plugin-json@0.1.0", "json": "github:systemjs/plugin-json@0.1.0",

View File

@ -13,7 +13,7 @@ var slicer = new SLICER.Slicer();
//var slicer = new SLICER.SlicerWorker(); //var slicer = new SLICER.SlicerWorker();
slicer.setGeometry(geometry.clone()); slicer.setGeometry(geometry.clone());
slicer.onfinish = function (gCode) { slicer.addEventListener('finish', ({ gcode }) => {
document.getElementById('gcode').innerHTML = gCode.replace(/(?:\r\n|\r|\n)/g, '<br />'); document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '<br />');
}; });
slicer.slice(settings); slicer.slice(settings);

View File

@ -5,6 +5,7 @@
"lib": "src" "lib": "src"
}, },
"dependencies": { "dependencies": {
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"clipper-lib": "npm:clipper-lib@^1.0.0", "clipper-lib": "npm:clipper-lib@^1.0.0",
"nodeca/js-yaml": "github:nodeca/js-yaml@^3.3.1", "nodeca/js-yaml": "github:nodeca/js-yaml@^3.3.1",
"read-yaml": "npm:read-yaml@^1.0.0", "read-yaml": "npm:read-yaml@^1.0.0",

View File

@ -1,4 +1,5 @@
import THREE from 'three.js'; import THREE from 'three.js';
import EventDispatcher from 'casperlamboo/EventDispatcher';
import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js'; import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js';
import createLines from './sliceActions/createLines.js'; import createLines from './sliceActions/createLines.js';
import generateInfills from './sliceActions/generateInfills.js'; import generateInfills from './sliceActions/generateInfills.js';
@ -9,8 +10,10 @@ import optimizePaths from './sliceActions/optimizePaths.js';
import shapesToSlices from './sliceActions/shapesToSlices.js'; import shapesToSlices from './sliceActions/shapesToSlices.js';
import slicesToGCode from './sliceActions/slicesToGCode.js'; import slicesToGCode from './sliceActions/slicesToGCode.js';
export default class { export default class extends EventDispatcher {
constructor () { constructor () {
super();
this.progress = { this.progress = {
createdLines: false, createdLines: false,
calculatedLayerIntersections: false, calculatedLayerIntersections: false,
@ -98,34 +101,36 @@ export default class {
this.progress.generatedGCode = true; this.progress.generatedGCode = true;
this._updateProgress(settings); this._updateProgress(settings);
if (this.onfinish !== undefined) { this.dispatchEvent({
this.onfinish(gcode); type: 'finish',
} gcode
});
return gcode; return gcode;
} }
_updateProgress (settings) { _updateProgress (settings) {
if (this.onprogress !== undefined) { var supportEnabled = settings.config["supportEnabled"];
var supportEnabled = settings.config["supportEnabled"];
var progress = {}; var progress = {};
var procent = 0; var procent = 0;
var length = 0; var length = 0;
for (var i in this.progress) { for (var i in this.progress) {
if (!(!supportEnabled && i === "generatedSupport")) { if (!(!supportEnabled && i === "generatedSupport")) {
progress[i] = this.progress[i]; progress[i] = this.progress[i];
if (progress[i]) { if (progress[i]) {
procent += 1; procent += 1;
}
length += 1;
} }
length += 1;
} }
progress.procent = procent / length;
this.onprogress(progress);
} }
progress.procent = procent / length;
this.dispatchEvent({
type: 'progress',
progress
});
} }
} }

View File

@ -1,30 +1,35 @@
import THREE from 'three.js'; import THREE from 'three.js';
import Settings from './settings.js'; import Settings from './settings.js';
import EventDispatcher from 'casperlamboo/EventDispatcher';
export default class { export default class extends EventDispatcher {
constructor () { constructor () {
super();
this.worker = new Worker('./worker.js'); this.worker = new Worker('./worker.js');
this.worker.addEventListener('message', (event) => { this.worker.addEventListener('message', (event) => {
switch (event.data['cmd']) { switch (event.data['cmd']) {
case 'PROGRESS': case 'PROGRESS':
if (this.onprogress !== undefined) { var progress = event.data['progress'];
var progress = event.data['progress'];
this.onprogress(progress); this.dispatchEvent({
} type: 'progress',
progress
});
break; break;
case 'GCODE': case 'GCODE':
if (this.onfinish !== undefined) { var reader = new FileReader();
var reader = new FileReader(); reader.addEventListener("loadend", () => {
reader.addEventListener("loadend", () => { var gcode = reader.result;
var gcode = reader.result; this.dispatchEvent({
this.onfinish(gcode); type: 'finish',
gcode
}); });
reader.readAsBinaryString(event.data['gcode']); });
} reader.readAsBinaryString(event.data['gcode']);
break; break;
} }
}, false); }, false);
@ -68,11 +73,11 @@ export default class {
delete geometry.boundingSphere; delete geometry.boundingSphere;
this.worker.postMessage({ this.worker.postMessage({
'cmd': 'SET_MESH', 'cmd': 'SET_MESH',
'geometry': { 'geometry': {
'attributes': geometry.attributes, 'attributes': geometry.attributes,
'attributesKeys': geometry.attributesKeys 'attributesKeys': geometry.attributesKeys
}, },
'matrix': matrix.toArray() 'matrix': matrix.toArray()
}, buffers); }, buffers);
@ -81,7 +86,7 @@ export default class {
slice (settings) { slice (settings) {
this.worker.postMessage({ this.worker.postMessage({
'cmd': 'SLICE', 'cmd': 'SLICE',
'settings': settings.config 'settings': settings.config
}); });
@ -95,4 +100,4 @@ export default class {
return this; return this;
} }
} }