Browse Source

--Added copy and paste options for prefab transforms and component settings.

weinand 9 years ago
parent
commit
6f8c90fd58

+ 67 - 0
Script/AtomicEditor/ui/frames/inspector/SelectionInspector.ts

@@ -93,7 +93,27 @@ class ComponentSection extends SelectionSection {
 
         };
 
+        var copyButton = new Atomic.UIButton();
+        copyButton.text = "Copy Settings";
+
+        copyButton.onClick = () => {
+
+            inspector.onComponentCopy(editType);
+            return true;
+
+        };
+
+        var pasteButton = new Atomic.UIButton();
+        pasteButton.text = "Paste Settings";
+
+        pasteButton.onClick = () => {
+            inspector.onComponentPaste(editType);
+            return true;
+        };
+
         this.attrLayout.addChild(deleteButton);
+        this.attrLayout.addChild(copyButton);
+        this.attrLayout.addChild(pasteButton);
 
     }
 
@@ -147,6 +167,8 @@ class JSComponentSection extends ComponentSection {
 
 class SelectionInspector extends ScriptWidget {
 
+    component: Atomic.Component;
+
     constructor(sceneEditor: Editor.SceneEditor3D) {
 
         super();
@@ -579,6 +601,51 @@ class SelectionInspector extends ScriptWidget {
 
     }
 
+    onComponentCopy(editType: SerializableEditType) {
+
+        var copy: Atomic.Component[] = [];
+
+        for (var i in editType.objects) {
+
+            var c = <Atomic.Component>editType.objects[i];
+            copy.push(c);
+
+        }
+
+        for (var i in copy) {
+
+            var c = copy[i];
+
+            this.component = c;
+
+            this.sceneEditor.scene.sendEvent("SceneEditComponentCopy", { component: this.component });
+            this.refresh();
+
+        }
+    }
+
+    onComponentPaste(editType: SerializableEditType) {
+        var paste: Atomic.Component[] = [];
+
+        for (var i in editType.objects) {
+
+            var c = <Atomic.Component>editType.objects[i];
+            paste.push(c);
+
+        }
+
+        for (var i in paste) {
+
+            var c = paste[i];
+
+            this.component = c;
+
+            this.sceneEditor.scene.sendEvent("SceneEditComponentPaste", { component: this.component });
+            this.refresh();
+        }
+
+    }
+
     handleSelectionCreateComponent(ev) {
 
         var valid = true;

+ 20 - 0
Script/AtomicEditor/ui/frames/inspector/SelectionPrefabWidget.ts

@@ -79,6 +79,24 @@ class SelectionPrefabWidget extends Atomic.UILayout {
             return true;
         };
 
+        var copyButton = new Atomic.UIButton();
+        copyButton.text = "Copy";
+        copyButton.fontDescription = fd;
+
+        copyButton.onClick = () => {
+            this.node.scene.sendEvent("SceneEditPrefabCopy", {node : this.node });
+            return true;
+        };
+
+        var pasteButton = new Atomic.UIButton();
+        pasteButton.text = "Paste";
+        pasteButton.fontDescription = fd;
+
+        pasteButton.onClick = () => {
+            this.node.scene.sendEvent("SceneEditPrefabPaste", {node : this.node });
+            return true;
+        };
+
         var noticeName = new Atomic.UITextField();
         noticeName.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
         noticeName.skinBg = "InspectorTextAttrName";
@@ -98,6 +116,8 @@ class SelectionPrefabWidget extends Atomic.UILayout {
         widgetLayout.addChild(saveButton);
         widgetLayout.addChild(undoButton);
         widgetLayout.addChild(breakButton);
+        widgetLayout.addChild(copyButton);
+        widgetLayout.addChild(pasteButton);
 
         this.addChild(this.widgetLayout);
         this.addChild(this.noticeLayout);

+ 20 - 0
Source/AtomicEditor/Editors/SceneEditor3D/SceneEditor3DEvents.h

@@ -128,12 +128,32 @@ EVENT(E_SCENEEDITPREFABREVERT, SceneEditPrefabRevert)
     PARAM(P_NODE, Node);                    // Node pointer
 }
 
+EVENT(E_SCENEEDITPREFABCOPY, SceneEditPrefabCopy)
+{
+    PARAM(P_NODE, Node);                    // Node pointer
+}
+
+EVENT(E_SCENEEDITPREFABPASTE, SceneEditPrefabPaste)
+{
+    PARAM(P_NODE, Node);                    // Node pointer
+}
 
 EVENT(E_SCENEEDITPREFABBREAK, SceneEditPrefabBreak)
 {
     PARAM(P_NODE, Node);                    // Node pointer
 }
 
+EVENT(E_SCENEEDITCOMPONENTCOPY, SceneEditComponentCopy)
+{
+    PARAM(P_COMPONENT, Component);          // Component pointer
+}
+
+EVENT(E_SCENEEDITCOMPONENTPASTE, SceneEditComponentPaste)
+{
+    PARAM(P_COMPONENT, Component);          // Component pointer
+    PARAM(P_END, End);                       // bool
+}
+
 EVENT(E_SCENEEDITADDREMOVENODES, SceneEditAddRemoveNodes)
 {
     PARAM(P_END, End);       // bool

+ 93 - 1
Source/AtomicEditor/Editors/SceneEditor3D/SceneSelection.cpp

@@ -42,7 +42,7 @@
 namespace AtomicEditor
 {
 
-SceneSelection::SceneSelection(Context* context, SceneEditor3D *sceneEditor) : Object(context)
+SceneSelection::SceneSelection(Context* context, SceneEditor3D *sceneEditor) : Object(context), hasCopied_(false)
 {
     sceneEditor3D_ = sceneEditor;
     scene_ = sceneEditor3D_->GetScene();
@@ -53,6 +53,10 @@ SceneSelection::SceneSelection(Context* context, SceneEditor3D *sceneEditor) : O
     SubscribeToEvent(scene_, E_SCENEEDITPREFABSAVE, HANDLER(SceneSelection, HandleSceneEditPrefabSave));
     SubscribeToEvent(scene_, E_SCENEEDITPREFABREVERT, HANDLER(SceneSelection, HandleSceneEditPrefabRevert));
     SubscribeToEvent(scene_, E_SCENEEDITPREFABBREAK, HANDLER(SceneSelection, HandleSceneEditPrefabBreak));
+    SubscribeToEvent(scene_, E_SCENEEDITPREFABCOPY, HANDLER(SceneSelection, HandleSceneEditPrefabCopy));
+    SubscribeToEvent(scene_, E_SCENEEDITPREFABPASTE, HANDLER(SceneSelection, HandleSceneEditPrefabPaste));
+    SubscribeToEvent(scene_, E_SCENEEDITCOMPONENTCOPY, HANDLER(SceneSelection, HandleSceneEditComponentCopy));
+    SubscribeToEvent(scene_, E_SCENEEDITCOMPONENTPASTE, HANDLER(SceneSelection, HandleSceneEditComponentPaste));
 }
 
 SceneSelection::~SceneSelection()
@@ -394,5 +398,93 @@ void SceneSelection::HandleSceneEditPrefabBreak(StringHash eventType, VariantMap
 
 }
 
+void SceneSelection::HandleSceneEditPrefabCopy(StringHash eventType, VariantMap & eventData)
+{
+    Node* node = static_cast<Node*> (eventData[SceneEditPrefabCopy::P_NODE].GetPtr());
+
+    PrefabComponent* prefab = node->GetComponent<PrefabComponent>();
+    if (!prefab)
+    {
+        LOGERRORF("Prefab Copy: Unable to get prefab component for node: %s", node->GetName().CString());
+        return;
+    }
+
+    nodePosition_ = node->GetAttribute("Position").GetVector3();
+    nodeRotation_ = node->GetAttribute("Rotation").GetQuaternion();
+    nodeScale_ = node->GetAttribute("Scale").GetVector3();
+}
+
+void SceneSelection::HandleSceneEditPrefabPaste(StringHash eventType, VariantMap & eventData)
+{
+    Node* node = static_cast<Node*> (eventData[SceneEditPrefabPaste::P_NODE].GetPtr());
+
+    PrefabComponent* prefab = node->GetComponent<PrefabComponent>();
+    if (!prefab)
+    {
+        LOGERRORF("Prefab Paste: Unable to get prefab component for node: %s", node->GetName().CString());
+        return;
+    }
+
+    node->SetPosition(nodePosition_);
+    node->SetRotation(nodeRotation_);
+    node->SetScale(nodeScale_);
+}
+
+void SceneSelection::HandleSceneEditComponentCopy(StringHash eventType, VariantMap & eventData)
+{
+    Component* component = static_cast<Component*> (eventData[SceneEditComponentCopy::P_COMPONENT].GetPtr());
+    copiedComponent_ = component;
+
+    if (!component)
+    {
+        LOGERRORF("Component Copy: Unable to copy component from node: %s", component->GetAttribute("type").ToString());
+        return;
+    }
+
+    componentAttributeNames_.Clear();
+    componentAttributeValues_.Clear();
+
+    for (int i = 0; i < component->GetAttributes()->Size(); i++)
+    {
+        String attributeName = component->GetAttributes()->At(i).name_;
+        Variant attributeValue = component->GetAttribute(attributeName);
+        componentAttributeNames_.Push(attributeName);
+        componentAttributeValues_.Push(attributeValue);
+    }
+
+    hasCopied_ = true;
+
+}
+
+void SceneSelection::HandleSceneEditComponentPaste(StringHash eventType, VariantMap & eventData)
+{
+    Component* component = static_cast<Component*> (eventData[SceneEditComponentCopy::P_COMPONENT].GetPtr());
+
+    if (!component)
+    {
+        LOGERRORF("Component Paste: Unable to paste component to node: %s", component->GetAttribute("type").ToString());
+        return;
+    }
+
+    if (!hasCopied_)
+    {
+        LOGERROR("Component Paste: Unable to paste, no information has been copied.");
+        return;
+    }
+
+    if (copiedComponent_->GetType() != component->GetType())
+    {
+        LOGERROR("Component Paste: Unable to paste, component type differ.");
+        return;
+    }
+
+    for (int i = 0; i < componentAttributeNames_.Size(); i++)
+    {
+        String attrName = componentAttributeNames_[i];
+        Variant attrValue = componentAttributeValues_[i];
+        component->SetAttribute(attrName, attrValue);
+    }
+}
+
 
 }

+ 15 - 0
Source/AtomicEditor/Editors/SceneEditor3D/SceneSelection.h

@@ -75,6 +75,10 @@ private:
     void HandleSceneEditPrefabSave(StringHash eventType, VariantMap& eventData);
     void HandleSceneEditPrefabRevert(StringHash eventType, VariantMap& eventData);
     void HandleSceneEditPrefabBreak(StringHash eventType, VariantMap& eventData);
+    void HandleSceneEditPrefabCopy(StringHash eventType, VariantMap& eventData);
+    void HandleSceneEditPrefabPaste(StringHash eventType, VariantMap& eventData);
+    void HandleSceneEditComponentCopy(StringHash eventType, VariantMap& eventData);
+    void HandleSceneEditComponentPaste(StringHash eventType, VariantMap& eventData);
 
     WeakPtr<SceneEditor3D> sceneEditor3D_;
     WeakPtr<Scene> scene_;
@@ -82,6 +86,17 @@ private:
     Vector<SharedPtr<Node>> clipBoardNodes_;
     Vector<SharedPtr<Node>> nodes_;
 
+    Component* copiedComponent_;
+
+    Vector3 nodePosition_;
+    Quaternion nodeRotation_;
+    Vector3 nodeScale_;
+
+    Vector<String> componentAttributeNames_;
+    Vector<Variant> componentAttributeValues_;
+
+    bool hasCopied_;
+
 };
 
 }