diff --git a/README.md b/README.md index d81b598..ee65302 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ LZ-String, Copyright 2013 pieroxy under MIT license https://github.com/pieroxy/l ## Changelog +### 0.4.11 + +* Copy and Paste + ### 0.4.10 * Draw optimizations, the drawing loop has been considerably optimized. diff --git a/js/elements/ICElements.js b/js/elements/ICElements.js index 61748fe..f698024 100644 --- a/js/elements/ICElements.js +++ b/js/elements/ICElements.js @@ -324,7 +324,7 @@ class ICElement extends Element { getOutput(Output = 0) { if (super.getOutput() === 0) return false; if (this.Outputs.length >= Output) { - return this.Outputs[Output].fromElement.getOutput(this.Outputs[Output].Input); + return this?.Outputs[Output]?.fromElement?.getOutput(this?.Outputs[Output]?.Input); } return false; } diff --git a/js/globalfunctions.js b/js/globalfunctions.js index 67f3984..54e1a1e 100644 --- a/js/globalfunctions.js +++ b/js/globalfunctions.js @@ -153,6 +153,16 @@ function KeybindToString(binding) { return kbs; } +function GetCopyObject() { + if (logicEngine?.ActiveContainer?.Selected?.length > 0) { + let copyObject = {}; + copyObject.Elements = logicEngine.ActiveContainer.Selected; + return copyObject; + } else { + return false; + } +} + function BuildTopMenu() { // Make sure settings are set correct if (logicEngine.Settings.HideConnections) { @@ -335,6 +345,87 @@ function download(filename, savestate) { document.body.removeChild(element); } +function getPastedElementDesignator(map,element) { + for (let a = 0; a < map?.length; a++) { + if (map[a].Old == element) return map[a].New; + } + return false; +} + +function loadActiveContainer(Elements) { + + let elementConnections = new Array(); + let icelementConnections = new Array(); + let designatorMap = new Array(); + + for (let a = 0; a < Elements.length; a++) { + if (Elements[a].IsIC) { + let classRef = getElementInfo(Elements[a].Name).Class; + if (!classRef) { + // We need to add this IC to the toolbox + let newIC = createIC(JSON.stringify(Elements[a].ICBlueprint),Elements[a].Name,Elements[a].Description); + //console.log("NewIC:" + newIC); + if (!newIC) return false; + } + } + let classRef = getElementInfo(Elements[a].Name).Class; + + let newElement = new classRef(logicEngine.ActiveContainer,Elements[a],logicEngine,getElementInfo(Elements[a].Name).Args[0]); + + logicEngine.ActiveContainer.AddElement(newElement); + designatorMap.push({Old: Elements[a].Designator, New: newElement.Designator}); + + if (Elements[a].Outputs) { + if (Elements[a].Outputs.length > 0) { + for (let b=0; b < Elements[a].Outputs.length; b++) { + elementConnections.push({ + FromElement: newElement, + Input: Elements[a].Outputs[b].Input, + Output: Elements[a].Outputs[b].Output, + ToElement: Elements[a].Outputs[b].Element + }); + } + } + } + if (Elements[a].ICOutputs) { + if (Elements[a].ICOutputs.length > 0) { + for (let b=0; b < Elements[a].ICOutputs.length; b++) { + icelementConnections.push({ + Element: newElement, + FromContainer: newElement.Container, + FromElement: Elements[a].ICOutputs[b].fromElement, + Input: Elements[a].ICOutputs[b].Input, + ToElement: Elements[a].ICOutputs[b].toElement + }); + } + } + } + } + + for (let a = 0; a < elementConnections.length; a++) { + let toElement = logicEngine.ActiveContainer.HasElement(getPastedElementDesignator(designatorMap,elementConnections[a].ToElement)); + if (toElement) { + if (elementConnections[a].FromContainer) { + let fromElement = elementConnections[a].FromContainer.HasElement(elementConnections[a].fromElement); + let newConnection = new ContainerConnection(elementConnections[a].FromContainer,logicEngine.ActiveContainer,fromElement,toElement,elementConnections[a].Input); + elementConnections[a].Element.Outputs.push(newConnection); + + } else { + let newConnection = new ElementConnection(logicEngine.ActiveContainer, toElement, elementConnections[a].Input, elementConnections[a].Output); + if (newConnection) { + elementConnections[a].FromElement.addConnection(logicEngine.ActiveContainer,toElement,elementConnections[a].Input, elementConnections[a].Output) + } else { + console.log("We dont have a new connection!!!"); + console.log(toElement); + console.log(elementConnections[a]); + } + + } + } + } + +} + function loadContainer(Elements) { let newContainer = new elementContainer(); let elementConnections = new Array(); diff --git a/js/logicengine.js b/js/logicengine.js index 7a5e206..142ce8f 100644 --- a/js/logicengine.js +++ b/js/logicengine.js @@ -147,9 +147,9 @@ class LogicEngineSettings { Name: "Toggle FPS", Description: "Show / Hide FPS counter", Category: "View"}, - CreateIC: {Key: "c", // lowercase + CreateIC: {Key: "i", // lowercase Alt: false, - Shift: false, + Shift: true, Ctrl: true, Meta: false, Name: "Create IC", diff --git a/js/main.js b/js/main.js index 9575b2d..282c4ab 100644 --- a/js/main.js +++ b/js/main.js @@ -2,7 +2,7 @@ MatCat BrowserLogic Simulator */ -let Version = "0.4.10"; +let Version = "0.4.11"; let spanVersion = document.getElementById("version"); spanVersion.innerText = Version; @@ -18,6 +18,37 @@ window.addEventListener('resize', function(evt) { logicEngine.Resize(evt); }, false); +document.addEventListener('copy', function(evt) { + evt.preventDefault(); + let copyObject = GetCopyObject(); + if (!copyObject) return; + + evt.clipboardData.setData('logicparts/json',JSON.stringify(copyObject)); + evt.clipboardData.setData('text',JSON.stringify(copyObject)); +}); + +document.addEventListener('cut', function(evt) { + evt.preventDefault(); + let copyObject = GetCopyObject(); + if (!copyObject) return; + + evt.clipboardData.setData('logicparts/json',JSON.stringify(copyObject)); + evt.clipboardData.setData('text',JSON.stringify(copyObject)); + + logicEngine.Key_Press({ctrlKey: false, key: "Delete"}); + disableSelectedRCMs(true); +}); + + +document.addEventListener('paste', function(evt) { + evt.preventDefault(); + if (evt.clipboardData.getData('logicparts/json')) { + //console.log(evt.clipboardData.getData('logicparts/json')); + loadActiveContainer(JSON.parse(evt.clipboardData.getData('logicparts/json')).Elements); + } +}); + + window.addEventListener('keydown', function(evt) { logicEngine.Key_Press(evt); }, false);