Modularization

This commit is contained in:
Chinmay Pandhare
2017-06-27 00:26:03 +05:30
parent 4ec9f4640a
commit d22a95beaf
8 changed files with 208 additions and 161 deletions

View File

@@ -8,15 +8,6 @@ ImageSequencer = function ImageSequencer(options) {
// if (options.inBrowser) options.ui = options.ui || require('./UserInterface');
options.sequencerCounter = 0;
function CImage(src) {
datauri = (options.inBrowser)?(src):require('urify')(src);
image = {
src: datauri,
format: datauri.split(':')[1].split(';')[0].split('/')[1]
}
return image;
}
function objTypeOf(object){
return Object.prototype.toString.call(object).split(" ")[1].slice(0,-1)
}
@@ -31,7 +22,13 @@ ImageSequencer = function ImageSequencer(options) {
function copy(a) {
if (!typeof(a) == "object") return a;
if (objTypeOf(a) == "Array") return a.slice();
if (objTypeOf(a) == "Object") return JSON.parse(JSON.stringify(a));
if (objTypeOf(a) == "Object") {
var b = {};
for (v in a) {
b[v] = copy(a[v]);
}
return b;
}
return a;
}
@@ -50,34 +47,6 @@ ImageSequencer = function ImageSequencer(options) {
// if (options.imageSelect || options.inBrowser) addStep('image-select');
// else if (options.imageUrl) loadImage(imageUrl);
function addStep(image, name, o_) {
log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
o = {};
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o_.name || name;
o.selector = o_.selector || 'ismod-' + name;
o.container = o_.container || options.selector;
o.image = image;
var module = modules[name].call(this.images,o);
images[image].steps.push(module);
function defaultSetupModule() {
if (options.ui && options.ui!="none") module.options.ui = options.ui({
selector: o.selector,
title: module.options.title,
id: o.id
});
}
if (module.hasOwnProperty('setup')) module.setup(); // add a default UI, unless the module has one specified
else defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
// tell the UI that a step has been added.
return true;
}
function addSteps(){
args = [];
json_q = {};
@@ -85,8 +54,7 @@ ImageSequencer = function ImageSequencer(options) {
json_q = formatInput.call(this,args,"+");
for (i in json_q)
for (j in json_q[i])
addStep.call(this,i,json_q[i][j].name,json_q[i][j].o);
require("./AddStep")(this,i,json_q[i][j].name,json_q[i][j].o);
}
function removeStep(image,index) {
@@ -96,7 +64,6 @@ ImageSequencer = function ImageSequencer(options) {
images[image].steps.splice(index,1);
}
//tell the UI a step has been removed
}
function removeSteps(image,index) {
@@ -114,34 +81,6 @@ ImageSequencer = function ImageSequencer(options) {
// this.run(run); // This is creating problems
}
function insertStep(image, index, name, o) {
log('\x1b[36m%s\x1b[0m','inserting step \"' + name + '\" to \"' + image + '\" at \"'+index+'\".');
o = o || {};
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o.name || name;
o.selector = o.selector || 'ismod-' + name;
o.container = o.container || options.selector;
o.image = image;
var module = modules[name](o);
images[image].steps.splice(index, 0, module);
function defaultSetupModule() {
if (options.ui && options.ui!="none") module.options.ui = options.ui({
selector: o.selector,
title: module.options.title,
id: o.id
});
}
if (module.hasOwnProperty('setup')) module.setup(); // add a default UI, unless the module has one specified
else defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
// tell the UI that a step has been inserted.
return true;
}
function insertSteps(image, index, name, o) {
run = {};
this_ = this;
@@ -154,7 +93,7 @@ ImageSequencer = function ImageSequencer(options) {
var details = json_q[img];
details = details.sort(function(a,b){return b.index-a.index});
for (i in details)
insertStep(img,details[i].index,details[i].name,details[i].o);
require("./InsertStep")(this,img,details[i].index,details[i].name,details[i].o);
// run[img] = details[details.length-1].index;
}
// this.run(run); // This is Creating issues
@@ -165,92 +104,25 @@ ImageSequencer = function ImageSequencer(options) {
this_ = this;
args = [];
for (var arg in arguments) args.push(copy(arguments[arg]));
callback = function() {};
for (var arg in args)
if(objTypeOf(args[arg]) == "Function")
var callback = args.splice(arg,1)[0];
function drawStep(drawarray,pos) {
if(pos==drawarray.length) if(objTypeOf(callback)=='Function') callback();
if(pos>=drawarray.length) return true;
image = drawarray[pos].image;
i = drawarray[pos].i;
input = images[image].steps[i-1].output;
images[image].steps[i].draw(copy(input),function(){
drawStep(drawarray,++pos);
});
}
function drawSteps(json_q) {
drawarray = [];
for (image in json_q) {
no_steps = images[image].steps.length;
init = json_q[image];
for(i = 0; i < no_steps-init; i++) {
drawarray.push({image: image,i: init+i});
}
}
drawStep(drawarray,0);
}
function filter(json_q){
for (image in json_q) {
if (json_q[image]==0 && this_.images[image].steps.length==1)
delete json_q[image];
else json_q[image]++;
}
for (image in json_q) {
prevstep = images[image].steps[json_q[image]-1];
while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") {
prevstep = images[image].steps[(--json_q[image]) - 1];
}
}
return json_q;
}
callback = args.splice(arg,1)[0];
json_q = formatInput.call(this,args,"r");
json_q = filter(json_q);
drawSteps(json_q);
}
function loadImage(name, src, callback) {
image = {
src: src,
steps: [{
options: {
id: options.sequencerCounter++,
name: "load-image",
title: "Load Image"
},
draw: function() {
if(arguments.length==1){
this.outputData = CImage(arguments[0]);
return true;
}
return false;
},
output: CImage(src)
}]
};
images[name] = image;
if (callback) callback();
require('./Run')(this, json_q, callback);
}
function loadImages() {
if (arguments.length == 1) {
for (image in arguments[0])
loadImage(image,arguments[0][image]);
}
else if (arguments.length == 2) {
if (objTypeOf(arguments[1]) == "Function") {
for (image in arguments[0]) {
loadImage(image,arguments[0][image])
}
arguments[1]();
}
else {
loadImage(arguments[0],arguments[1])
}
}
else if (arguments.length == 3) {
loadImage(arguments[0],arguments[1],arguments[2])
}
args = [];
for (arg in arguments) args.push(copy(arguments[arg]));
json_q = formatInput.call(this,args,"l");
for (i in json_q.images)
require('./LoadImage')(this,i,json_q.images[i])
json_q.callback();
}
return {
@@ -262,9 +134,11 @@ ImageSequencer = function ImageSequencer(options) {
run: run,
modules: modules,
images: images,
ui: options.ui
ui: options.ui,
log: log,
objTypeOf: objTypeOf,
copy: copy
}
}
module.exports = ImageSequencer;