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: {
"babel": "npm:babel-core@5.8.21",
"babel-runtime": "npm:babel-runtime@5.8.20",
"casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"clipper-lib": "npm:clipper-lib@1.0.0",
"core-js": "npm:core-js@0.9.18",
"json": "github:systemjs/plugin-json@0.1.0",

View File

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

View File

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

View File

@ -1,4 +1,5 @@
import THREE from 'three.js';
import EventDispatcher from 'casperlamboo/EventDispatcher';
import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js';
import createLines from './sliceActions/createLines.js';
import generateInfills from './sliceActions/generateInfills.js';
@ -9,8 +10,10 @@ import optimizePaths from './sliceActions/optimizePaths.js';
import shapesToSlices from './sliceActions/shapesToSlices.js';
import slicesToGCode from './sliceActions/slicesToGCode.js';
export default class {
export default class extends EventDispatcher {
constructor () {
super();
this.progress = {
createdLines: false,
calculatedLayerIntersections: false,
@ -98,34 +101,36 @@ export default class {
this.progress.generatedGCode = true;
this._updateProgress(settings);
if (this.onfinish !== undefined) {
this.onfinish(gcode);
}
this.dispatchEvent({
type: 'finish',
gcode
});
return gcode;
}
_updateProgress (settings) {
if (this.onprogress !== undefined) {
var supportEnabled = settings.config["supportEnabled"];
var supportEnabled = settings.config["supportEnabled"];
var progress = {};
var progress = {};
var procent = 0;
var length = 0;
for (var i in this.progress) {
if (!(!supportEnabled && i === "generatedSupport")) {
progress[i] = this.progress[i];
if (progress[i]) {
procent += 1;
}
length += 1;
var procent = 0;
var length = 0;
for (var i in this.progress) {
if (!(!supportEnabled && i === "generatedSupport")) {
progress[i] = this.progress[i];
if (progress[i]) {
procent += 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 Settings from './settings.js';
import EventDispatcher from 'casperlamboo/EventDispatcher';
export default class {
export default class extends EventDispatcher {
constructor () {
super();
this.worker = new Worker('./worker.js');
this.worker.addEventListener('message', (event) => {
switch (event.data['cmd']) {
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;
case 'GCODE':
if (this.onfinish !== undefined) {
var reader = new FileReader();
reader.addEventListener("loadend", () => {
var gcode = reader.result;
this.onfinish(gcode);
var reader = new FileReader();
reader.addEventListener("loadend", () => {
var gcode = reader.result;
this.dispatchEvent({
type: 'finish',
gcode
});
reader.readAsBinaryString(event.data['gcode']);
}
});
reader.readAsBinaryString(event.data['gcode']);
break;
}
}, false);