1/* 2* Copyright (c) 2018 (https://github.com/phase1geo/Minder) 3* 4* This program is free software; you can redistribute it and/or 5* modify it under the terms of the GNU General Public 6* License as published by the Free Software Foundation; either 7* version 2 of the License, or (at your option) any later version. 8* 9* This program is distributed in the hope that it will be useful, 10* but WITHOUT ANY WARRANTY; without even the implied warranty of 11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12* General Public License for more details. 13* 14* You should have received a copy of the GNU General Public 15* License along with this program; if not, write to the 16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17* Boston, MA 02110-1301 USA 18* 19* Authored by: Trevor Williams <phase1geo@gmail.com> 20*/ 21 22public class UndoNodeSort : UndoItem { 23 24 private class SortNodes { 25 26 private Array<Node> _nodes; 27 28 /* Stores the given node into this class */ 29 public SortNodes( Node n ) { 30 _nodes = new Array<Node>(); 31 for( int i=0; i<n.children().length; i++ ) { 32 _nodes.append_val( n.children().index( i ) ); 33 } 34 } 35 36 /* Performs an undo operation for the stored nodes */ 37 public void change( Node parent ) { 38 for( int i=0; i<_nodes.length; i++ ) { 39 Node n = _nodes.index( i ); 40 n.detach( n.side ); 41 } 42 for( int i=0; i<_nodes.length; i++ ) { 43 Node n = _nodes.index( i ); 44 n.attach( parent, -1, null ); 45 } 46 } 47 48 } 49 50 private SortNodes _old; 51 private SortNodes? _new = null; 52 private Node _parent; 53 54 /* Default constructor */ 55 public UndoNodeSort( Node parent ) { 56 base( _( "sort nodes" ) ); 57 _parent = parent; 58 _old = new SortNodes( parent ); 59 } 60 61 /* Perform the swap */ 62 private void change( DrawArea da, SortNodes nodes ) { 63 da.animator.add_nodes( da.get_nodes(), "undo sorted nodes" ); 64 nodes.change( _parent ); 65 da.animator.animate(); 66 } 67 68 /* Performs an undo operation for this data */ 69 public override void undo( DrawArea da ) { 70 if( _new == null ) { 71 _new = new SortNodes( _parent ); 72 } 73 change( da, _old ); 74 } 75 76 /* Performs a redo operation */ 77 public override void redo( DrawArea da ) { 78 change( da, _new ); 79 } 80 81} 82