1 // 2 // LayerManager.cs 3 // 4 // Author: 5 // Jonathan Pobst <monkey@jpobst.com> 6 // 7 // Copyright (c) 2010 Jonathan Pobst 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining a copy 10 // of this software and associated documentation files (the "Software"), to deal 11 // in the Software without restriction, including without limitation the rights 12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 // copies of the Software, and to permit persons to whom the Software is 14 // furnished to do so, subject to the following conditions: 15 // 16 // The above copyright notice and this permission notice shall be included in 17 // all copies or substantial portions of the Software. 18 // 19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 // THE SOFTWARE. 26 27 using System; 28 using System.Collections.Generic; 29 using System.ComponentModel; 30 using System.Collections.Specialized; 31 using System.Linq; 32 using Cairo; 33 using Mono.Unix; 34 35 namespace Pinta.Core 36 { 37 public class LayerManager 38 { 39 #region Public Properties 40 public UserLayer this[int index] 41 { 42 get { return PintaCore.Workspace.ActiveDocument.UserLayers[index]; } 43 } 44 45 public UserLayer CurrentLayer 46 { 47 get { return PintaCore.Workspace.ActiveDocument.CurrentUserLayer; } 48 } 49 50 public int Count { 51 get { return PintaCore.Workspace.ActiveDocument.UserLayers.Count; } 52 } 53 54 public Layer ToolLayer { 55 get { return PintaCore.Workspace.ActiveDocument.ToolLayer; } 56 } 57 58 public Layer SelectionLayer { 59 get { return PintaCore.Workspace.ActiveDocument.SelectionLayer; } 60 } 61 62 public int CurrentLayerIndex { 63 get { return PintaCore.Workspace.ActiveDocument.CurrentUserLayerIndex; } 64 } 65 66 public bool ShowSelectionLayer { 67 get { return PintaCore.Workspace.ActiveDocument.ShowSelectionLayer; } 68 set { PintaCore.Workspace.ActiveDocument.ShowSelectionLayer = value; } 69 } 70 #endregion 71 72 #region Public Methods Clear()73 public void Clear () 74 { 75 PintaCore.Workspace.ActiveDocument.Clear (); 76 } 77 GetLayersToPaint()78 public List<Layer> GetLayersToPaint () 79 { 80 return PintaCore.Workspace.ActiveDocument.GetLayersToPaint (); 81 } 82 SetCurrentLayer(int i)83 public void SetCurrentLayer (int i) 84 { 85 PintaCore.Workspace.ActiveDocument.SetCurrentUserLayer (i); 86 } 87 SetCurrentLayer(UserLayer layer)88 public void SetCurrentLayer(UserLayer layer) 89 { 90 PintaCore.Workspace.ActiveDocument.SetCurrentUserLayer (layer); 91 } 92 FinishSelection()93 public void FinishSelection () 94 { 95 PintaCore.Workspace.ActiveDocument.FinishSelection (); 96 } 97 98 // Adds a new layer above the current one AddNewLayer(string name)99 public UserLayer AddNewLayer(string name) 100 { 101 return PintaCore.Workspace.ActiveDocument.AddNewLayer (name); 102 } 103 104 // Adds a new layer above the current one Insert(UserLayer layer, int index)105 public void Insert(UserLayer layer, int index) 106 { 107 PintaCore.Workspace.ActiveDocument.Insert (layer, index); 108 } 109 IndexOf(UserLayer layer)110 public int IndexOf(UserLayer layer) 111 { 112 return PintaCore.Workspace.ActiveDocument.IndexOf (layer); 113 } 114 115 // Delete the current layer DeleteCurrentLayer()116 public void DeleteCurrentLayer () 117 { 118 PintaCore.Workspace.ActiveDocument.DeleteCurrentLayer (); 119 } 120 121 // Delete the layer DeleteLayer(int index, bool dispose)122 public void DeleteLayer (int index, bool dispose) 123 { 124 PintaCore.Workspace.ActiveDocument.DeleteLayer (index, dispose); 125 } 126 127 // Duplicate current layer DuplicateCurrentLayer()128 public Layer DuplicateCurrentLayer () 129 { 130 return PintaCore.Workspace.ActiveDocument.DuplicateCurrentLayer (); 131 } 132 133 // Flatten current layer MergeCurrentLayerDown()134 public void MergeCurrentLayerDown () 135 { 136 PintaCore.Workspace.ActiveDocument.MergeCurrentLayerDown (); 137 } 138 139 // Move current layer up MoveCurrentLayerUp()140 public void MoveCurrentLayerUp () 141 { 142 PintaCore.Workspace.ActiveDocument.MoveCurrentLayerUp (); 143 } 144 145 // Move current layer down MoveCurrentLayerDown()146 public void MoveCurrentLayerDown () 147 { 148 PintaCore.Workspace.ActiveDocument.MoveCurrentLayerDown (); 149 } 150 151 // Flip image horizontally FlipImageHorizontal()152 public void FlipImageHorizontal () 153 { 154 PintaCore.Workspace.ActiveDocument.FlipImageHorizontal (); 155 } 156 157 // Flip image vertically FlipImageVertical()158 public void FlipImageVertical () 159 { 160 PintaCore.Workspace.ActiveDocument.FlipImageVertical (); 161 } 162 163 // Rotate image 180 degrees (flip H+V) RotateImage180()164 public void RotateImage180 () 165 { 166 PintaCore.Workspace.ActiveDocument.RotateImage180 (); 167 } 168 RotateImageCW()169 public void RotateImageCW () 170 { 171 PintaCore.Workspace.ActiveDocument.RotateImageCW (); 172 } 173 RotateImageCCW()174 public void RotateImageCCW () 175 { 176 PintaCore.Workspace.ActiveDocument.RotateImageCCW (); 177 } 178 179 // Flatten image FlattenImage()180 public void FlattenImage () 181 { 182 PintaCore.Workspace.ActiveDocument.FlattenImage (); 183 } 184 CreateSelectionLayer()185 public void CreateSelectionLayer () 186 { 187 PintaCore.Workspace.ActiveDocument.CreateSelectionLayer (); 188 } 189 DestroySelectionLayer()190 public void DestroySelectionLayer () 191 { 192 PintaCore.Workspace.ActiveDocument.DestroySelectionLayer (); 193 } 194 ResetSelectionPath()195 public void ResetSelectionPath () 196 { 197 PintaCore.Workspace.ActiveDocument.ResetSelectionPaths (); 198 } 199 GetClippedLayer(int index)200 public ImageSurface GetClippedLayer (int index) 201 { 202 return PintaCore.Workspace.ActiveDocument.GetClippedLayer (index); 203 } 204 #endregion 205 206 #region Protected Methods OnLayerAdded()207 protected internal void OnLayerAdded () 208 { 209 if (LayerAdded != null) 210 LayerAdded.Invoke (this, EventArgs.Empty); 211 } 212 OnLayerRemoved()213 protected internal void OnLayerRemoved () 214 { 215 if (LayerRemoved != null) 216 LayerRemoved.Invoke (this, EventArgs.Empty); 217 } 218 OnSelectedLayerChanged()219 protected internal void OnSelectedLayerChanged () 220 { 221 if (SelectedLayerChanged != null) 222 SelectedLayerChanged.Invoke (this, EventArgs.Empty); 223 } 224 #endregion 225 226 #region Private Methods CreateLayer()227 public Layer CreateLayer () 228 { 229 return PintaCore.Workspace.ActiveDocument.CreateLayer (); 230 } 231 CreateLayer(string name, int width, int height)232 public Layer CreateLayer (string name, int width, int height) 233 { 234 return PintaCore.Workspace.ActiveDocument.CreateLayer (name, width, height); 235 } 236 RaiseLayerPropertyChangedEvent(object sender, PropertyChangedEventArgs e)237 internal void RaiseLayerPropertyChangedEvent (object sender, PropertyChangedEventArgs e) 238 { 239 if (LayerPropertyChanged != null) 240 LayerPropertyChanged (sender, e); 241 242 //TODO Get the workspace to subscribe to this event, and invalidate itself. 243 PintaCore.Workspace.Invalidate (); 244 } 245 #endregion 246 247 #region Events 248 public event EventHandler LayerAdded; 249 public event EventHandler LayerRemoved; 250 public event EventHandler SelectedLayerChanged; 251 public event PropertyChangedEventHandler LayerPropertyChanged; 252 #endregion 253 } 254 } 255