1 // 2 // RoundedLineEditEngine.cs 3 // 4 // Author: 5 // Andrew Davis <andrew.3.1415@gmail.com> 6 // 7 // Copyright (c) 2014 Andrew Davis, GSoC 2014 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 Cairo; 29 using Pinta.Core; 30 using System.Collections.Generic; 31 using System.Linq; 32 using System.Text; 33 using Mono.Unix; 34 35 namespace Pinta.Tools 36 { 37 public class RoundedLineEditEngine: BaseEditEngine 38 { 39 protected override string ShapeName 40 { 41 get 42 { 43 return Catalog.GetString("Rounded Line Shape"); 44 } 45 } 46 47 public const double DefaultRadius = 20d; 48 49 protected double previousRadius = DefaultRadius; 50 51 protected ToolBarComboBox radius; 52 protected ToolBarLabel radius_label; 53 protected ToolBarButton radius_minus; 54 protected ToolBarButton radius_plus; 55 protected Gtk.SeparatorToolItem radius_sep; 56 57 public double Radius 58 { 59 get 60 { 61 double rad; 62 63 if (radius != null) 64 { 65 if (Double.TryParse(radius.ComboBox.ActiveText, out rad)) 66 { 67 if (rad >= 0) 68 { 69 (radius.ComboBox as Gtk.ComboBoxEntry).Entry.Text = rad.ToString(); 70 71 return rad; 72 } 73 else 74 { 75 (radius.ComboBox as Gtk.ComboBoxEntry).Entry.Text = BrushWidth.ToString(); 76 77 return BrushWidth; 78 } 79 } 80 else 81 { 82 (radius.ComboBox as Gtk.ComboBoxEntry).Entry.Text = BrushWidth.ToString(); 83 84 return BrushWidth; 85 } 86 } 87 else 88 { 89 return BrushWidth; 90 } 91 } 92 93 set 94 { 95 if (radius != null) 96 { 97 (radius.ComboBox as Gtk.ComboBoxEntry).Entry.Text = value.ToString(); 98 99 ShapeEngine selEngine = SelectedShapeEngine; 100 101 if (selEngine != null && selEngine.ShapeType == ShapeTypes.RoundedLineSeries) 102 { 103 ((RoundedLineEngine)selEngine).Radius = Radius; 104 105 StorePreviousSettings(); 106 107 DrawActiveShape(false, false, true, false, false); 108 } 109 } 110 } 111 } 112 113 HandleBuildToolBar(Gtk.Toolbar tb)114 public override void HandleBuildToolBar(Gtk.Toolbar tb) 115 { 116 base.HandleBuildToolBar(tb); 117 118 119 if (radius_sep == null) 120 radius_sep = new Gtk.SeparatorToolItem(); 121 122 tb.AppendItem(radius_sep); 123 124 if (radius_label == null) 125 radius_label = new ToolBarLabel(string.Format(" {0}: ", Catalog.GetString("Radius"))); 126 127 tb.AppendItem(radius_label); 128 129 if (radius_minus == null) 130 { 131 radius_minus = new ToolBarButton("Toolbar.MinusButton.png", "", Catalog.GetString("Decrease shape's corner radius")); 132 radius_minus.Clicked += RadiusMinusButtonClickedEvent; 133 } 134 135 tb.AppendItem(radius_minus); 136 137 if (radius == null) 138 { 139 radius = new ToolBarComboBox(65, 16, true, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 140 "10", "11", "12", "13", "14", "15", "20", "25", "30", "40", 141 "50", "60", "70", "80"); 142 143 radius.ComboBox.Changed += (o, e) => 144 { 145 //Go through the Get/Set routine. 146 Radius = Radius; 147 }; 148 } 149 150 tb.AppendItem(radius); 151 152 if (radius_plus == null) 153 { 154 radius_plus = new ToolBarButton("Toolbar.PlusButton.png", "", Catalog.GetString("Increase shape's corner radius")); 155 radius_plus.Clicked += RadiusPlusButtonClickedEvent; 156 } 157 158 tb.AppendItem(radius_plus); 159 } 160 RadiusMinusButtonClickedEvent(object o, EventArgs args)161 private void RadiusMinusButtonClickedEvent(object o, EventArgs args) 162 { 163 if (Math.Truncate(Radius) > 0) 164 { 165 Radius = Math.Truncate(Radius) - 1; 166 } 167 } 168 RadiusPlusButtonClickedEvent(object o, EventArgs args)169 private void RadiusPlusButtonClickedEvent(object o, EventArgs args) 170 { 171 Radius = Math.Truncate(Radius) + 1; 172 } 173 174 RoundedLineEditEngine(ShapeTool passedOwner)175 public RoundedLineEditEngine(ShapeTool passedOwner): base(passedOwner) 176 { 177 178 } 179 CreateShape(bool ctrlKey, bool clickedOnControlPoint, PointD prevSelPoint)180 protected override ShapeEngine CreateShape(bool ctrlKey, bool clickedOnControlPoint, PointD prevSelPoint) 181 { 182 Document doc = PintaCore.Workspace.ActiveDocument; 183 184 ShapeEngine newEngine = new RoundedLineEngine(doc.CurrentUserLayer, null, Radius, owner.UseAntialiasing, 185 BaseEditEngine.OutlineColor, BaseEditEngine.FillColor, owner.EditEngine.BrushWidth); 186 187 AddRectanglePoints(ctrlKey, clickedOnControlPoint, newEngine, prevSelPoint); 188 189 //Set the new shape's DashPattern option. 190 newEngine.DashPattern = dash_pattern_box.comboBox.ComboBox.ActiveText; 191 192 return newEngine; 193 } 194 MovePoint(List<ControlPoint> controlPoints)195 protected override void MovePoint(List<ControlPoint> controlPoints) 196 { 197 MoveRectangularPoint(controlPoints); 198 199 base.MovePoint(controlPoints); 200 } 201 202 UpdateToolbarSettings(ShapeEngine engine)203 public override void UpdateToolbarSettings(ShapeEngine engine) 204 { 205 if (engine != null && engine.ShapeType == ShapeTypes.RoundedLineSeries) 206 { 207 RoundedLineEngine rLEngine = (RoundedLineEngine)engine; 208 209 Radius = rLEngine.Radius; 210 211 base.UpdateToolbarSettings(engine); 212 } 213 } 214 RecallPreviousSettings()215 protected override void RecallPreviousSettings() 216 { 217 Radius = previousRadius; 218 219 base.RecallPreviousSettings(); 220 } 221 StorePreviousSettings()222 protected override void StorePreviousSettings() 223 { 224 previousRadius = Radius; 225 226 base.StorePreviousSettings(); 227 } 228 } 229 } 230