From a5580a47d246c1272b10adba68070f6e13da5e41 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Tue, 16 Jul 2019 21:08:38 -0400
Subject: [PATCH] Replace/restore buttons.

---
 ObjEditor.java              |  159 +++++++++++++++++++++++--------
 icons/restore.png           |    0 
 CameraPane.java             |    3 
 Globals.java                |    2 
 cRadio.java                 |    4 
 GroupEditor.java            |  100 +++++++++++++------
 icons/arrow-down-01-512.png |    0 
 icons/replace.png           |    0 
 Object3D.java               |    6 +
 cToggleButton.java          |    7 +
 10 files changed, 200 insertions(+), 81 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index 80b657b..6286dc1 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -15708,6 +15708,7 @@
                     info.bounds.y += (height - desired) / 2;
                 }
             }
+            
             info.g = gr;
             info.camera = renderCamera;
             /* 
@@ -15717,6 +15718,8 @@
              */
             if (!isRenderer)
             {
+                Grafreed.Assert(object != null);
+                Grafreed.Assert(object.selection != null);
                 if (object.selection.Size() > 0)
                 {
                     int hitSomething = object.selection.get(0).hitSomething;
diff --git a/Globals.java b/Globals.java
index 2601880..3d6158a 100644
--- a/Globals.java
+++ b/Globals.java
@@ -10,7 +10,7 @@
         public static boolean COMPUTESHADOWWHENLIVE = true;
         public static boolean RENDERSHADOW = true;
         
-        public static boolean SAVEONMAKE = true; // problems when auto-save (works with manual save)
+        public static boolean REPLACEONMAKE = true; // problems when auto-save (works with manual save)
         
         public static boolean MOUSEDRAGGED = false;
         public static boolean TIMERRUNNING = false;
diff --git a/GroupEditor.java b/GroupEditor.java
index ec26bdb..7a750fb 100644
--- a/GroupEditor.java
+++ b/GroupEditor.java
@@ -84,10 +84,10 @@
 
     void CloneSelection(boolean supports)
     {
-        if (Globals.SAVEONMAKE)
+        if (Globals.REPLACEONMAKE)
             Save();
-        boolean keep = Globals.SAVEONMAKE;
-        Globals.SAVEONMAKE = false;
+        boolean keep = Globals.REPLACEONMAKE;
+        Globals.REPLACEONMAKE = false;
         //   Object3D keep = GrafreeD.clipboard;
            //Object3D obj;
            for (int i=0; i<group.selection.size(); i++)// Enumeration e = group.selection.elements(); e.hasMoreElements();)
@@ -98,7 +98,7 @@
                
                makeSomething(clone, i==group.selection.size()-1);
            }
-        Globals.SAVEONMAKE = keep;
+        Globals.REPLACEONMAKE = keep;
     }
 
     void CloneClipboard(boolean supports)
@@ -544,6 +544,7 @@
 		buildToolsMenu(menu);
 	}
 	
+        
 	void SetupUI2(ObjEditor oe)
 	{
             // June 2019
@@ -615,29 +616,42 @@
                 restoreCameraButton.setToolTipText("Restore viewpoint");
 		restoreCameraButton.addActionListener(this);
                 
+		copyOptionsPanel.add(saveButton = GetButton("icons/down_arrow.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
+                saveButton.setToolTipText("New version");
+		saveButton.addActionListener(this);
+                                                
 		copyOptionsPanel.add(undoButton = GetButton("icons/undo.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
-                undoButton.setToolTipText("Undo changes");
+                undoButton.setToolTipText("Previous version");
 		undoButton.addActionListener(this);
                 undoButton.setEnabled(false);
                 
+                cGridBag updown = new cGridBag().setVertical(true);
+		updown.add(restoreButton = GetButton("icons/restore.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
+                restoreButton.setToolTipText("Restore current");
+		restoreButton.addActionListener(this);
+                restoreButton.setEnabled(false);
+                
+		updown.add(replaceButton = GetButton("icons/replace.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
+                replaceButton.setToolTipText("Replace current");
+		replaceButton.addActionListener(this);
+                replaceButton.setEnabled(false);
+                
+                copyOptionsPanel.add(updown);
+                
 		copyOptionsPanel.add(redoButton = GetButton("icons/redo.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
-                redoButton.setToolTipText("Redo changes");
+                redoButton.setToolTipText("Next version");
 		redoButton.addActionListener(this);
                 redoButton.setEnabled(false);
                 
-		copyOptionsPanel.add(saveButton = GetButton("icons/down_arrow.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
-                saveButton.setToolTipText("Save changes");
-		saveButton.addActionListener(this);
-                
-                copyOptionsPanel.add(liveCB = GetToggleButton("icons/run.png", Globals.isLIVE())); //, oe.aConstraints);
+                oe.toolbarPanel.add(liveCB = GetToggleButton("icons/run.png", Globals.isLIVE())); //, oe.aConstraints);
                 liveCB.setToolTipText("Enable animation");
                                 liveCB.addItemListener(this);
 
-		copyOptionsPanel.add(oneStepButton = GetButton("icons/step.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
+		oe.toolbarPanel.add(oneStepButton = GetButton("icons/step.png", !Grafreed.NIMBUSLAF)); //, oe.aConstraints);
                 oneStepButton.setToolTipText("Animate one step forward");
 		oneStepButton.addActionListener(this);
                 
-                copyOptionsPanel.add(fastCB = GetToggleButton("icons/runfast.png", CameraPane.FAST)); //, constraints);
+                oe.toolbarPanel.add(fastCB = GetToggleButton("icons/runfast.png", CameraPane.FAST)); //, constraints);
                 fastCB.setToolTipText("Fast mode");
                                 fastCB.addItemListener(this);
                                 
@@ -820,6 +834,9 @@
 
                 oe.treePanel.add(copyOptionsPanel);
                 oe.treePanel.Return();
+        cGridBag sliderPane = AddSlider(oe.treePanel, "Version", 0, 10, 0);
+            versionField = (cNumberSlider)sliderPane.getComponent(1);
+            sliderPane.preferredHeight = 1;
                 
 //        mainPanel.setDividerLocation(0.5); //1.0);
 //        mainPanel.setResizeWeight(0.5);
@@ -945,9 +962,9 @@
                 toggleSwitchCB.setToolTipText("Choose a single item");
                                 toggleSwitchCB.addItemListener(this);
                                 
-                panel.add(autosaveCB = new cCheckBox("Auto-save", Globals.SAVEONMAKE)); //, constraints);
-                autosaveCB.setToolTipText("On structure change");
-                                autosaveCB.addItemListener(this);
+                panel.add(autokeepCB = new cCheckBox("Auto-replace", Globals.REPLACEONMAKE)); //, constraints);
+                autokeepCB.setToolTipText("On structure change");
+                                autokeepCB.addItemListener(this);
 
                 panel.Return();
             if (Globals.ADVANCED)
@@ -1024,7 +1041,7 @@
         
         cCheckBox oeilCB;
         cCheckBox shadowCB;
-        cCheckBox autosaveCB;
+        cCheckBox autokeepCB;
         cCheckBox lookAtCB;
         
 //        static int COLOR = 1;
@@ -1136,9 +1153,9 @@
             {
                 Globals.FREEZEONMOVE ^= true;
             }
-            else if(e.getSource() == autosaveCB)
+            else if(e.getSource() == autokeepCB)
             {
-                Globals.SAVEONMAKE ^= true;
+                Globals.REPLACEONMAKE ^= true;
             }
             else if(e.getSource() == lookAtCB)
             {
@@ -2225,15 +2242,29 @@
 		} else
 		if (source == undoButton)
 		{
-			if (!Undo())
-                            java.awt.Toolkit.getDefaultToolkit().beep();
+                        // Go to previous version
+			//if (!Undo())
+                            //java.awt.Toolkit.getDefaultToolkit().beep();
+			Undo();
+		} else
+		if (source == restoreButton)
+		{
+                        // Restore current version
+			Restore();
+		} else
+		if (source == replaceButton)
+		{
+                        // Overwrite current version
+			Replace();
 		} else
 		if (source == redoButton)
 		{
+                        // Go to next version
 			Redo();
 		} else
 		if (source == saveButton)
 		{
+                        // Save a new version
 			if (!Save(true))
                             java.awt.Toolkit.getDefaultToolkit().beep();
 		} else
@@ -3479,6 +3510,9 @@
                     }
                     
                     copy = group;
+                    
+                    SetUndoStates();
+
                     //Globals.theRenderer.object = group;
                     if(!useclient)
                     {
@@ -4942,10 +4976,10 @@
 	
 	void paste(boolean expand)
 	{
-        if (Globals.SAVEONMAKE)
+        if (Globals.REPLACEONMAKE)
             Save();
-        boolean keep = Globals.SAVEONMAKE;
-        Globals.SAVEONMAKE = false;
+        boolean keep = Globals.REPLACEONMAKE;
+        Globals.REPLACEONMAKE = false;
 	//	if (GrafreeD.clipboard == null)
 	//		return;
 		boolean first = true;
@@ -5005,7 +5039,7 @@
                         Grafreed.clipboard.get(0).parent = keepparent;
 		}
 		
-        Globals.SAVEONMAKE = keep;
+        Globals.REPLACEONMAKE = keep;
 		ResetModel();
 		refreshContents();
 	}
@@ -5141,10 +5175,10 @@
         
 	void group(Object3D csg, boolean grab)
 	{
-        if (Globals.SAVEONMAKE)
+        if (Globals.REPLACEONMAKE)
             Save();
-        boolean keep = Globals.SAVEONMAKE;
-        Globals.SAVEONMAKE = false;
+        boolean keep = Globals.REPLACEONMAKE;
+        Globals.REPLACEONMAKE = false;
 		if (//false) // why??
                     !group.selection.isEmpty())
                 {
@@ -5258,15 +5292,15 @@
                 //node.add(csg);
 		//makeSomething(node);
 		makeSomething(csg);
-        Globals.SAVEONMAKE = keep;
+        Globals.REPLACEONMAKE = keep;
 	}
 	
         void Ungroup(Object3D g)
         {
-        if (Globals.SAVEONMAKE)
+        if (Globals.REPLACEONMAKE)
             Save();
-        boolean keep = Globals.SAVEONMAKE;
-        Globals.SAVEONMAKE = false;
+        boolean keep = Globals.REPLACEONMAKE;
+        Globals.REPLACEONMAKE = false;
             if (g instanceof HiddenObject)
             {
                 HiddenObject h = (HiddenObject) g;
@@ -5283,7 +5317,7 @@
                     objEditor.makeSomething(g.get(i), false);
                 }
             }
-        Globals.SAVEONMAKE = keep;
+        Globals.REPLACEONMAKE = keep;
         }
         
 	void ungroup()
diff --git a/ObjEditor.java b/ObjEditor.java
index b1bbdf6..174c859 100644
--- a/ObjEditor.java
+++ b/ObjEditor.java
@@ -816,6 +816,28 @@
             frame.validate();
         }
 
+    private byte[] CompressCopy()
+    {
+        boolean temp = CameraPane.SWITCH;
+        CameraPane.SWITCH = false;
+        
+        copy.ExtractBigData(versiontable);
+        // if (copy == client)
+        
+        byte[] versions[] = copy.versions;
+        copy.versions = null;
+        
+        byte[] compress = Compress(copy);
+        
+        copy.versions = versions;
+        
+        copy.RestoreBigData(versiontable);
+        
+        CameraPane.SWITCH = temp;
+        
+        return compress;
+    }
+
     private JTextPane createTextPane()
     {
 // TEXTAREA       String[] initString =
@@ -3574,11 +3596,11 @@
         return null;
     }
     
-    java.util.Hashtable<java.util.UUID, Object3D> hashtable = new java.util.Hashtable<java.util.UUID, Object3D>();
     
     public void Save()
     {
-        Save(true);
+        //Save(true);
+        Replace();
     }
     
     private boolean Equal(byte[] compress, byte[] name)
@@ -3597,25 +3619,20 @@
         return true;
     }
 
+    java.util.Hashtable<java.util.UUID, Object3D> versiontable = new java.util.Hashtable<java.util.UUID, Object3D>();
+    
     public boolean Save(boolean user)
     {
         System.err.println("Save");
         
         cRadio tab = GetCurrentTab();
         
-        boolean temp = CameraPane.SWITCH;
-        CameraPane.SWITCH = false;
-        
-        copy.ExtractBigData(hashtable);
-        
-        byte[] compress = Compress(copy);
-
-        CameraPane.SWITCH = temp;
+        byte[] compress = CompressCopy();
         
         boolean thesame = false;
         
         // Quick heuristic using length. Works only when stream is compressed.
-        if (tab.undoindex > 0 && tab.graphs[tab.undoindex-1] != null && Equal(compress, tab.graphs[tab.undoindex-1]))
+        if (copy.versionindex > 0 && copy.versions[copy.versionindex-1] != null && Equal(compress, copy.versions[copy.versionindex-1]))
         {
             thesame = true;
         }
@@ -3623,23 +3640,23 @@
         //EditorFrame.m_MainFrame.requestFocusInWindow();
         if (!thesame)
         {
-            //tab.user[tab.undoindex] = user;
-            boolean increment = tab.graphs[tab.undoindex] == null;
+            //tab.user[tab.versionindex] = user;
+            //boolean increment = true; // tab.graphs[tab.versionindex] == null;
             
-            tab.graphs[tab.undoindex] = compress;
+            copy.versions[++copy.versionindex] = compress;
             
-            if (increment)
-                tab.undoindex++;
+            // if (increment)
+            //     tab.versionindex++;
         }
 
-        copy.RestoreBigData(hashtable);
+        //copy.RestoreBigData(versiontable);
 
         //assert(hashtable.isEmpty());
         
-        for (int i = tab.undoindex; i < tab.graphs.length; i++)
+        for (int i = copy.versionindex+1; i < copy.versions.length; i++)
         {
             //tab.user[i] = false;
-        //    tab.graphs[i] = null;
+            copy.versions[i] = null;
         }
 
         SetUndoStates();
@@ -3649,7 +3666,7 @@
         {
             try
             {
-                FileOutputStream ostream = new FileOutputStream("save" + tab.undoindex);
+                FileOutputStream ostream = new FileOutputStream("save" + copy.versionindex);
                 ObjectOutputStream p = new ObjectOutputStream(ostream);
 
                 p.writeObject(copy);
@@ -3673,7 +3690,7 @@
         boolean temp = CameraPane.SWITCH;
         CameraPane.SWITCH = false;
         
-        copy.ExtractBigData(hashtable);
+        copy.ExtractBigData(versiontable);
         
         copy.clear();
         
@@ -3682,7 +3699,7 @@
             copy.add(obj.get(i));
         }
         
-        copy.RestoreBigData(hashtable);
+        copy.RestoreBigData(versiontable);
         
         CameraPane.SWITCH = temp;
         
@@ -3710,14 +3727,18 @@
     }
     
     cButton undoButton;
+    cButton restoreButton;
+    cButton replaceButton;
     cButton redoButton;
         
     void SetUndoStates()
     {
         cRadio tab = GetCurrentTab();
         
-        undoButton.setEnabled(tab.undoindex > 0);
-        redoButton.setEnabled(tab.graphs[tab.undoindex + 1] != null);
+        restoreButton.setEnabled(copy.versionindex != -1);
+        replaceButton.setEnabled(copy.versionindex != -1);
+        undoButton.setEnabled(copy.versionindex > 0);
+        redoButton.setEnabled(copy.versions[copy.versionindex + 1] != null);
     }
     
     public boolean Undo()
@@ -3726,28 +3747,62 @@
         
         cRadio tab = GetCurrentTab();
         
-        if (tab.undoindex == 0)
+        if (copy.versionindex == 0)
         {
             java.awt.Toolkit.getDefaultToolkit().beep();
             return false;
         }
 
-        if (tab.graphs[tab.undoindex] == null) // || !tab.user[tab.undoindex])
+//        if (tab.graphs[tab.versionindex] == null) // || !tab.user[tab.versionindex])
+//        {
+//            if (Save(false))
+//                tab.versionindex -= 1;
+//            else
+//            {
+//                if (tab.versionindex <= 0)
+//                    return false;
+//                else
+//                    tab.versionindex -= 1;
+//            }
+//        }
+
+        copy.versionindex -= 1;
+
+        CopyChanged((Object3D)Uncompress(copy.versions[copy.versionindex]));
+        
+        return true;
+    }
+
+    public boolean Restore()
+    {
+        System.err.println("Restore");
+        
+        cRadio tab = GetCurrentTab();
+        
+        if (copy.versionindex == -1 || copy.versions[copy.versionindex] == null)
         {
-            if (Save(false))
-                tab.undoindex -= 1;
-            else
-            {
-                if (tab.undoindex <= 0)
-                    return false;
-                else
-                    tab.undoindex -= 1;
-            }
+            java.awt.Toolkit.getDefaultToolkit().beep();
+            return false;
         }
 
-        tab.undoindex -= 1;
+        CopyChanged((Object3D)Uncompress(copy.versions[copy.versionindex]));
+        
+        return true;
+    }
 
-        CopyChanged((Object3D)Uncompress(tab.graphs[tab.undoindex]));
+    public boolean Replace()
+    {
+        System.err.println("Replace");
+        
+        cRadio tab = GetCurrentTab();
+        
+        if (copy.versionindex == -1 || copy.versions[copy.versionindex] == null)
+        {
+            // No version yet. OK. java.awt.Toolkit.getDefaultToolkit().beep();
+            return false;
+        }
+
+        copy.versions[copy.versionindex] = CompressCopy();
         
         return true;
     }
@@ -3756,18 +3811,18 @@
     {
         cRadio tab = GetCurrentTab();
         
-        if (tab.graphs[tab.undoindex + 1] == null)
+        if (copy.versions[copy.versionindex + 1] == null)
         {
             java.awt.Toolkit.getDefaultToolkit().beep();
             return;
         }
 
-        tab.undoindex += 1;
+        copy.versionindex += 1;
 
-        CopyChanged((Object3D)Uncompress(tab.graphs[tab.undoindex]));
+        CopyChanged((Object3D)Uncompress(copy.versions[copy.versionindex]));
         
-        //if (!tab.user[tab.undoindex])
-        //    tab.graphs[tab.undoindex] = null;
+        //if (!tab.user[tab.versionindex])
+        //    tab.graphs[tab.versionindex] = null;
     }
 
         void ImportGFD()
@@ -4063,9 +4118,22 @@
         //copy.Touch();
     }
 
+    cNumberSlider versionField;
+    
     public void stateChanged(ChangeEvent e)
     {
         //    assert(false);
+        if (e.getSource() == versionField)
+        {
+            int version = versionField.getInteger();
+            
+            if (copy.versions[version] != null)
+            {
+                CopyChanged((Object3D)Uncompress(copy.versions[copy.versionindex = version]));
+            }
+            
+            return;
+        }
 
         if (freezematerial)
         {
@@ -4414,7 +4482,7 @@
 
     void makeSomething(Object3D thing, boolean resetmodel) // deselect)
     {
-        if (Globals.SAVEONMAKE) // && resetmodel)
+        if (Globals.REPLACEONMAKE) // && resetmodel)
             Save();
         //Tween.set(thing, 0).target(1).start(tweenManager);
         //Tween.to(thing, 0, 0.5f).target(0).start(tweenManager);
@@ -4786,6 +4854,11 @@
                 c.addChild(csg);
             }
 
+            copy.versions = readobj.versions;
+            copy.versionindex = readobj.versionindex;
+            
+            SetUndoStates();
+            
             ResetModel();
             copy.HardTouch(); // recompile?
             refreshContents();
diff --git a/Object3D.java b/Object3D.java
index 750acd4..b3a45b6 100644
--- a/Object3D.java
+++ b/Object3D.java
@@ -29,6 +29,9 @@
     Object3D saveskeleton;
     //
     
+    byte[] versions[] = new byte[100][];
+    int versionindex = -1;    
+        
     ScriptNode scriptnode;
 
         void InitOthers()
@@ -215,6 +218,7 @@
 //            o.transientrep = this.bRep.support;
 //            o.bRep.support = null;
 //        }
+        o.selection = this.selection;
         
         if (this.support != null)
         {
@@ -268,6 +272,8 @@
         {
             this.support.bRep = o.transientrep;
         }
+        
+        this.selection = o.selection;
 // July 2019        if (this.bRep != null)
 //            this.bRep.support = o.transientrep;
     //    this.support = o.support;
diff --git a/cRadio.java b/cRadio.java
index af43de0..2fd1e50 100644
--- a/cRadio.java
+++ b/cRadio.java
@@ -35,10 +35,10 @@
             camera = (Camera)Grafreed.clone(c);
     }
     
-    byte[] graphs[] = new byte[100][];
+    //byte[] versions[] = new byte[100][];
     //boolean[] user = new boolean[100];
     
-    int undoindex = 0;
+    //int versionindex = -1;
     
     // Patch to avoid bug with transparency.
     boolean hadMaterial;
diff --git a/cToggleButton.java b/cToggleButton.java
index 7f9252b..a9a562a 100644
--- a/cToggleButton.java
+++ b/cToggleButton.java
@@ -19,8 +19,10 @@
         this.image = icon.getImage();
         iconWidth = icon.getIconWidth();
 
-        //setBorder(new javax.swing.border.EmptyBorder(8, 0, 8, 24)); // top, left, bottom, right
-        setBorder(new javax.swing.border.EtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.BLACK));
+        setMargin(new java.awt.Insets(11, 11, 11, 11));
+        //setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));
+        // setBorder(new javax.swing.border.EmptyBorder(8, 0, 8, 24)); // top, left, bottom, right
+        // setBorder(new javax.swing.border.EtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.BLACK));
     }
     
     private java.awt.Image image;
@@ -35,6 +37,7 @@
             //if (getWidth() > image.getWidth(null) + 8)
             {
                 g.drawImage(image, this.getWidth()/2 - 12, this.getHeight()/2 - 12, this);
+                g.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
             }
         }
     }
diff --git a/icons/arrow-down-01-512.png b/icons/arrow-down-01-512.png
new file mode 100644
index 0000000..e23260a
--- /dev/null
+++ b/icons/arrow-down-01-512.png
Binary files differ
diff --git a/icons/replace.png b/icons/replace.png
new file mode 100644
index 0000000..ccb60a6
--- /dev/null
+++ b/icons/replace.png
Binary files differ
diff --git a/icons/restore.png b/icons/restore.png
new file mode 100644
index 0000000..3384246
--- /dev/null
+++ b/icons/restore.png
Binary files differ

--
Gitblit v1.6.2