diff --git a/Pinta.Core/Managers/ToolManager.cs b/Pinta.Core/Managers/ToolManager.cs index e401530b9..6a3122625 100644 --- a/Pinta.Core/Managers/ToolManager.cs +++ b/Pinta.Core/Managers/ToolManager.cs @@ -37,6 +37,9 @@ public class ToolManager : IEnumerable int prev_index = -1; private List Tools; + private Dictionary> groupedTools; + private Gdk.Key LastUsedKey; + private int PressedShortcutCounter; public event EventHandler ToolAdded; public event EventHandler ToolRemoved; @@ -44,6 +47,8 @@ public class ToolManager : IEnumerable public ToolManager () { Tools = new List (); + groupedTools = new Dictionary>(); + PressedShortcutCounter = 0; } public void AddTool (BaseTool tool) @@ -58,6 +63,11 @@ public void AddTool (BaseTool tool) if (CurrentTool == null) SetCurrentTool (tool); + + if (!groupedTools.ContainsKey(tool.ShortcutKey)) + groupedTools.Add(tool.ShortcutKey, new List()); + + groupedTools[tool.ShortcutKey].Add(tool); } public void RemoveInstanceOfTool (System.Type tool_type) @@ -73,11 +83,17 @@ public void RemoveInstanceOfTool (System.Type tool_type) SetCurrentTool(new DummyTool()); OnToolRemoved (tool); + + if (groupedTools[tool.ShortcutKey].Count > 1) + groupedTools[tool.ShortcutKey].Remove(tool); + else + groupedTools.Remove(tool.ShortcutKey); + return; } } } - + void HandlePbToolItemClicked (object sender, EventArgs e) { ToggleToolButton tb = (ToggleToolButton)sender; @@ -172,24 +188,32 @@ public void SetCurrentTool (Gdk.Key shortcut) if (tool != null) SetCurrentTool(tool); } - + private BaseTool FindNextTool (Gdk.Key shortcut) { - string key = shortcut.ToString ().ToUpperInvariant (); - - // Begin looking at the tool after the current one - for (int i = index + 1; i < Tools.Count; i++) { - if (Tools[i].ShortcutKey.ToString ().ToUpperInvariant () == key) - return Tools[i]; - } - - // Begin at the beginning and look up to the current tool - for (int i = 0; i < index; i++) { - if (Tools[i].ShortcutKey.ToString ().ToUpperInvariant () == key) - return Tools[i]; + shortcut = shortcut.ToUpper(); + + if (groupedTools.ContainsKey(shortcut)) + { + for (int i = 0; i < groupedTools[shortcut].Count; i++) + { + if (LastUsedKey != shortcut) + { + // Return first tool in group. + PressedShortcutCounter = (1 % groupedTools[shortcut].Count); + LastUsedKey = shortcut; + return groupedTools[shortcut][0]; + } + else if(i == PressedShortcutCounter) + { + var tool = groupedTools[shortcut][PressedShortcutCounter]; + PressedShortcutCounter = (i + 1) % groupedTools[shortcut].Count; + return tool; + } + } } - - return null; + + return null; } private void OnToolAdded (BaseTool tool) @@ -242,4 +266,21 @@ protected override void OnBuildToolBar (Toolbar tb) tool_sep = null; } } + + //Key extensions for more convenient usage of Gdk key enumerator + public static class KeyExtensions + { + public static Gdk.Key ToUpper(this Gdk.Key k1) + { + try + { + return (Gdk.Key)Enum.Parse(typeof(Gdk.Key), k1.ToString().ToUpperInvariant()); + } + catch (ArgumentException) + { + //there is a need to catch argument exception because some buttons have not its UpperCase variant + return k1; + } + } + } } diff --git a/Pinta.Tools/Tools/LassoSelectTool.cs b/Pinta.Tools/Tools/LassoSelectTool.cs index 53b7263c2..d069af75b 100644 --- a/Pinta.Tools/Tools/LassoSelectTool.cs +++ b/Pinta.Tools/Tools/LassoSelectTool.cs @@ -52,7 +52,8 @@ public LassoSelectTool () public override string Name { get { return Catalog.GetString ("Lasso Select"); } } public override string Icon { get { return "Tools.LassoSelect.png"; } } public override string StatusBarText { get { return Catalog.GetString ("Click and drag to draw the outline for a selection area."); } } - public override Gdk.Cursor DefaultCursor { get { return new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Cursor.LassoSelect.png"), 9, 18); } } + public override Gdk.Key ShortcutKey { get { return Gdk.Key.S; } } + public override Gdk.Cursor DefaultCursor { get { return new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Cursor.LassoSelect.png"), 9, 18); } } public override int Priority { get { return 9; } } #endregion