From d8836718740f6db0688f196b11436480c709f14e Mon Sep 17 00:00:00 2001 From: liabru Date: Fri, 25 Apr 2014 17:06:54 +0100 Subject: [PATCH] removed file functions from Matter.Gui and added file functions to Matter.Inspector --- src/tools/Gui.js | 69 +------------------------------------ src/tools/Inspector.js | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/tools/Gui.js b/src/tools/Gui.js index 0e25c7d..1233fc2 100644 --- a/src/tools/Gui.js +++ b/src/tools/Gui.js @@ -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 = ''; - 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 diff --git a/src/tools/Inspector.js b/src/tools/Inspector.js index 05f41f3..1263e98 100644 --- a/src/tools/Inspector.js +++ b/src/tools/Inspector.js @@ -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 = ''; + 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(); + }; + })(); \ No newline at end of file