0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-26 13:49:01 -05:00

removed file functions from Matter.Gui and added file functions to Matter.Inspector

This commit is contained in:
liabru 2014-04-25 17:06:54 +01:00
parent fa06a05a04
commit d883671874
2 changed files with 79 additions and 68 deletions

View file

@ -93,9 +93,7 @@ var Gui = {};
addBody: function() { _addBody(gui); },
clear: function() { _clear(gui); },
save: function() { _save(gui); },
saveFile: function() { _saveFile(gui); },
load: function() { _load(gui); },
loadFile: function() { _loadFile(gui); }
load: function() { _load(gui); }
};
var metrics = datGui.addFolder('Metrics');
@ -130,8 +128,6 @@ var Gui = {};
var worldGui = datGui.addFolder('World');
worldGui.add(funcs, 'load');
worldGui.add(funcs, 'save');
worldGui.add(funcs, 'loadFile');
worldGui.add(funcs, 'saveFile');
worldGui.add(funcs, 'clear');
worldGui.open();
@ -241,34 +237,6 @@ var Gui = {};
Events.trigger(gui, 'save');
};
var _saveFile = function(gui) {
var engine = gui.engine;
if (gui.serializer) {
var json = gui.serializer.stringify(engine.world, function(key, value) {
// skip non-required values
if (key === 'path')
return undefined;
// limit precision of floats
if (!/^#/.exec(key) && typeof value === 'number') {
return parseFloat(value.toFixed(2));
}
return value;
});
var blob = new Blob([json], { type: 'application/json' }),
anchor = document.createElement('a');
anchor.download = "world.json";
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.dataset.downloadurl = ['application/json', anchor.download, anchor.href].join(':');
anchor.click();
}
Events.trigger(gui, 'save');
};
var _load = function(gui) {
var engine = gui.engine,
loadedWorld;
@ -284,41 +252,6 @@ var Gui = {};
Events.trigger(gui, 'load');
};
var _loadFile = function(gui) {
var engine = gui.engine;
var element = document.createElement('div');
element.innerHTML = '<input type="file">';
var fileInput = element.firstChild;
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
if (file.name.match(/\.(txt|json)$/)) {
var reader = new FileReader();
reader.onload = function(e) {
var loadedWorld;
if (gui.serializer)
loadedWorld = gui.serializer.parse(reader.result);
if (loadedWorld) {
Engine.merge(engine, { world: loadedWorld });
}
Events.trigger(gui, 'load');
}
reader.readAsText(file);
} else {
alert("File not supported, JSON text files only");
}
});
fileInput.click();
};
/*
*
* Events Documentation

View file

@ -36,6 +36,13 @@ var Inspector = {};
selected: []
};
if (Resurrect) {
inspector.serializer = new Resurrect({ prefix: '$' });
inspector.serializer.parse = inspector.serializer.resurrect;
} else {
inspector.serializer = JSON;
}
_initControls(inspector);
_initEvents(inspector);
@ -111,6 +118,14 @@ var Inspector = {};
}
});
controls.exportButton.click(function() {
_exportFile(inspector);
});
controls.importButton.click(function() {
_importFile(inspector);
});
Events.on(engine, 'tick', function() {
if (engine.world.isModified) {
var data = _generateCompositeTreeNode(engine.world);
@ -429,4 +444,67 @@ var Inspector = {};
return node;
};
var _exportFile = function(inspector) {
var engine = inspector.engine;
if (inspector.serializer) {
var json = inspector.serializer.stringify(engine.world, function(key, value) {
// skip non-required values
if (key === 'path')
return undefined;
// limit precision of floats
if (!/^#/.exec(key) && typeof value === 'number') {
return parseFloat(value.toFixed(2));
}
return value;
});
var blob = new Blob([json], { type: 'application/json' }),
anchor = document.createElement('a');
anchor.download = "world.json";
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.dataset.downloadurl = ['application/json', anchor.download, anchor.href].join(':');
anchor.click();
}
Events.trigger(inspector, 'save');
};
var _importFile = function(inspector) {
var engine = inspector.engine;
var element = document.createElement('div');
element.innerHTML = '<input type="file">';
var fileInput = element.firstChild;
fileInput.addEventListener('change', function(e) {
var file = fileInput.files[0];
if (file.name.match(/\.(txt|json)$/)) {
var reader = new FileReader();
reader.onload = function(e) {
var loadedWorld;
if (inspector.serializer)
loadedWorld = inspector.serializer.parse(reader.result);
if (loadedWorld) {
Engine.merge(engine, { world: loadedWorld });
}
Events.trigger(inspector, 'load');
}
reader.readAsText(file);
} else {
alert("File not supported, JSON text files only");
}
});
fileInput.click();
};
})();