Skip to content

Commit

Permalink
Merge pull request #254 from ninjadev/paradigm-shift
Browse files Browse the repository at this point in the history
Shift paradigm from layers to nodes
  • Loading branch information
stianjensen committed Aug 20, 2016
2 parents 89f3cd9 + f35108d commit 782a977
Show file tree
Hide file tree
Showing 32 changed files with 816 additions and 461 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ nin is Ninjadev's internal demo tool. It is a tool for easing development of bro
This project has a node backend that keeps track of all files and compiles files as they are edited.
The frontend of this project is written in Angular and displays among other the layers that the demo consists of.


## nin is now node-based!

Nin just became node-based instead of layer-based.
Here is a list of gotchas to watch out for if you are used to layer-based nin:

- Changing res/graph.json does *not* trigger updates in a running nin instance.

## How it works
Create a new project with a structure like the one seen in the directory `example-project`.
It may be stored anywhere on your disk.
Expand Down
2 changes: 1 addition & 1 deletion nin/backend/generate/generate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs'),
path = require('path'),
utils = require('../utils'),
layers = require('../layers'),
graph = require('../graph'),
mkdirp = require('mkdirp');

var generate = function(projectRoot, type, name) {
Expand Down
62 changes: 62 additions & 0 deletions nin/backend/graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var fs = require('fs');
var utils = require('./utils');


function update(projectPath, nodeId, data, callback) {
read(projectPath, function (err, graph) {
if (err) {
if (callback) {
callback(err);
}
return;
}
var index = graph.findIndex(nodeInfo => nodeInfo.id == nodeId);
for (var key in data) {
graph[index][key] = data[key];
}
write(projectPath, graph, function (err) {
if (callback) {
callback(err);
}
});
});
}

var operationQueue = [];
var activeOperation = null;

function read(projectPath, callback) {
operationQueue.push([projectPath, callback]);
if (activeOperation === null) {
advanceQueue();
}
}

function write(projectPath, layers, callback) {
var data = JSON.stringify(layers, null, 2) + '\n';
fs.writeFile(projectPath + '/res/graph.json', data, function(err) {
advanceQueue();
callback(err);
});
}

function advanceQueue() {
activeOperation = operationQueue.shift() || null;
if (activeOperation) {
_read.apply(this, activeOperation);
}
}

function _read(projectPath, callback) {
fs.readFile(projectPath + '/res/graph.json', function(err, data) {
if (err) {
advanceQueue();
callback(err);
} else {
var graph = JSON.parse(data);
callback(err, graph);
}
});
}

module.exports = {update: update};
86 changes: 0 additions & 86 deletions nin/backend/layers.js

This file was deleted.

17 changes: 12 additions & 5 deletions nin/backend/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var serve = function(projectPath, shouldRunHeadlessly) {
path: path
};

if (filename == 'layers.json' ||
if (filename == 'graph.json' ||
filename == 'camerapaths.json') {
event.type = filenameWithoutExtension;
event.content = content;
Expand All @@ -72,10 +72,8 @@ var serve = function(projectPath, shouldRunHeadlessly) {
event.content = data.out;
event.shadername = p.basename(p.dirname(path));
} else {
event.type = 'layer';
event.type = 'node';
event.content = content;
event.layername = filenameWithoutExtension;
event.shaderDependencies = findShaderDependencies(content);
}

return event;
Expand All @@ -92,7 +90,16 @@ var serve = function(projectPath, shouldRunHeadlessly) {
var sockets = express();
var sockets_server = require('http').createServer(sockets);
var sock = socket(projectPath, function(conn) {
var sortedPaths = watcher.paths.sort();
var sortedPaths = watcher.paths.sort(function(a, b) {
var directoryPrecedence = {'lib': 0, 'src': 1, 'res': 2};
var directoryAScore = directoryPrecedence[a.slice(0, 3)];
var directoryBScore = directoryPrecedence[b.slice(0, 3)];
if(directoryAScore == directoryBScore) {
return a > b;
}
return directoryAScore > directoryBScore;

});
for (var i in sortedPaths) {
conn.send('add', eventFromPath({path: sortedPaths[i]}));
}
Expand Down
6 changes: 2 additions & 4 deletions nin/backend/socket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var chalk = require('chalk');
var generate = require('./generate/generate');
var layers = require('./layers');
var graph = require('./graph');
var sock = require('sockjs');


Expand Down Expand Up @@ -38,9 +38,7 @@ function socket(projectPath, onConnectionCallback) {
var event = JSON.parse(message);
switch (event.type) {
case 'set':
var data = {};
data[event.data.field] = event.data.value;
layers.update(projectPath, event.data.id, data, function (err) {
graph.update(projectPath, event.data.id, event.data.fields, function (err) {
if (err) {
console.log(err);
}
Expand Down
11 changes: 10 additions & 1 deletion nin/backend/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function watch(projectPath, cb) {
var watcher = chokidar.watch(
['src/',
'lib/',
'res/layers.json',
'res/graph.json',
'res/camerapaths.json'], {
ignored: [/[\/\\]\./, /\/shaders\//],
persistent: true,
Expand All @@ -29,12 +29,21 @@ function watch(projectPath, cb) {
* trigger intial callbacks for all files */
watcher.on('add', function(){ });

var graphAlreadyLoaded = false;

watcher.on('all', function (event, path) {
if (event === 'unlink') event = 'delete';
if (event == 'addDir') {
return;
}

if(path == 'res/graph.json') {
if(graphAlreadyLoaded) {
return;
}
graphAlreadyLoaded = true;
}

if (logFileChanges) {
console.log(chalk.yellow('Change in project detected: ') +
chalk.cyan(event) +
Expand Down
1 change: 1 addition & 0 deletions nin/dasBoot/00_NIN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window['NIN'] = window['NIN'] || {};
21 changes: 21 additions & 0 deletions nin/dasBoot/Input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function(NIN) {
'use strict';

class Input {

constructor(node) {
this.source = null;
this.node = node;
this.enabled = true;
}

getValue() {
if(!this.source) {
return null;
}
return this.source.getValue();
}
}

NIN.Input = Input;
})(this.NIN);
1 change: 1 addition & 0 deletions nin/dasBoot/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Loader.prototype.start = function(onprogress, oncomplete) {
'png': 'data:image/png;base64,',
'mp3': 'data:audio/mp3;base64,',
'mp4': 'data:video/mp4;base64,',
'webm': 'data:video/webm;base64,',
'svg': 'data:image/svg+xml;base64,',
}[item.filepath.slice(-3)];
console.log(that.id, item.filepath, prefix + (FILES[item.filepath] && FILES[item.filepath].slice(0, 10)));
Expand Down
22 changes: 22 additions & 0 deletions nin/dasBoot/Node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(function(NIN) {
'use strict';

class Node {
constructor(id, options) {
this.id = id;
this.inputs = options.inputs || {};
this.outputs = options.outputs || {};
}

resize() {
}

render() {
}

update() {
}
}

NIN.Node = Node;
})(this.NIN);

0 comments on commit 782a977

Please sign in to comment.