From c87b7fcf9e7c0ea2b2565895ca6f434043aa36fe Mon Sep 17 00:00:00 2001 From: Taka Date: Tue, 21 May 2019 15:45:27 +0000 Subject: [PATCH] Added support for shortcuts. --- data/config.json | 21 ++++++++++----- src/ChartImage.cs | 64 ++++++++++++++++++++++++++++++++++++++++++++++ src/MainForm.ui.cs | 22 ++++++++++++++-- 3 files changed, 98 insertions(+), 9 deletions(-) diff --git a/data/config.json b/data/config.json index fbd4c97..753a671 100644 --- a/data/config.json +++ b/data/config.json @@ -2,27 +2,32 @@ "IIFTMQRDCc/leader-creation.png": { "caption": "Leader Creation", - "keywords": [ "LC", "leader" ] + "keywords": [ "LC", "leader" ], + "shortcut": "Alt-L" }, "IIFTMQRDCc/close-combat.png": { "caption": "Close Combat", - "keywords": [ "CC" ] + "keywords": [ "CC" ], + "shortcut": "Ctrl-Shift-C" }, "IIFTMQRDCc/heat-to-kill.png": { "caption": "HEAT To Kill", - "keywords": [ "HEAT", "TK" ] + "keywords": [ "HEAT", "TK" ], + "shortcut": "Ctrl-K" }, "IIFTMQRDCc/iift.png": { "caption": "Incremental IFT", - "keywords": [ "IFT", "IIFT" ] + "keywords": [ "IFT", "IIFT" ], + "shortcut": "Ctrl-Alt-I" }, "IIFTMQRDCc/ap-to-kill.png": { "caption": "AP To Kill", - "keywords": [ "AP", "TK" ] + "keywords": [ "AP", "TK" ], + "shortcut": "Ctrl-K" }, "IIFTMQRDCc/afv-destruction.png": { @@ -41,12 +46,14 @@ "IIFTMQRDCc/apcr-apds-to-kill.png": { "caption": "APCR/APDS To Kill", - "keywords": [ "APCR", "APDS", "TK" ] + "keywords": [ "APCR", "APDS", "TK" ], + "shortcut": "Ctrl-K" }, "IIFTMQRDCc/to-hit.png": { "caption": "To Hit", - "keywords": [ "TH" ] + "keywords": [ "TH" ], + "shortcut": "Ctrl-H" } } diff --git a/src/ChartImage.cs b/src/ChartImage.cs index bf9bc71..462afda 100644 --- a/src/ChartImage.cs +++ b/src/ChartImage.cs @@ -2,6 +2,7 @@ using System ; using System.Text ; using System.IO ; using System.Collections.Generic ; +using System.Windows.Forms ; using System.Drawing ; using Manina.Windows.Forms ; @@ -12,6 +13,9 @@ using log4net ; public class ChartImage { + // NOTE: This table maps shortcuts (e.g. Ctrl-Shift-C) to ChartImage's. + private static Dictionary< Tuple, List > mShortcuts = new Dictionary< Tuple, List >() ; + private string mFullPath ; private dynamic mConfig ; private HashSet mKeywords = new HashSet() ; @@ -27,6 +31,21 @@ public class ChartImage mConfig = new JObject() ; mImage = Image.FromFile( fullPath ) ; + // parse the shortcut + ILog logger = LogManager.GetLogger( "shortcuts" ) ; + string val = mConfig[ "shortcut" ] ; + if ( val != null ) { + Tuple shortcut = parseShortcut( val ) ; + if ( shortcut == null ) + logger.Warn( $"Can't parse shortcut: {val}" ) ; + else { + if ( ! mShortcuts.ContainsKey( shortcut ) ) + mShortcuts[ shortcut ] = new List() ; + logger.Info( $"Registering shortcut: {shortcutString(shortcut.Item1,shortcut.Item2)} => ${caption()}" ) ; + mShortcuts[ shortcut ].Add( this ) ; + } + } + // prepare for search scoring if ( mConfig["keywords"] != null ) { foreach( string kywd in mConfig["keywords"] ) @@ -124,6 +143,51 @@ public class ChartImage return totalScore ; } + private Tuple parseShortcut( string val ) + { + // parse the shortcut + string[] parts = val.ToLower().Split( "-" ) ; + Keys modifiers = 0 ; + for ( int i=0 ; i < parts.Length-1 ; ++i ) { + if ( parts[i] == "ctrl" ) + modifiers |= Keys.Control ; + else if ( parts[i] == "alt" ) + modifiers |= Keys.Alt ; + else if ( parts[i] == "shift" ) + modifiers |= Keys.Shift ; + else + return null ; + } + Keys key ; + bool rc = Enum.TryParse( parts[parts.Length-1], true, out key ) ; + if ( ! rc ) + return null ; + return new Tuple( modifiers, key ) ; + } + + public static List checkShortcut( Keys modifiers, Keys key ) + { + // check if there are any ChartImage's associated with the specified shortcut + List chartImages ; + if ( ! mShortcuts.TryGetValue( new Tuple( modifiers, key ), out chartImages ) ) + return null ; + return chartImages ; + } + + public static string shortcutString( Keys modifiers, Keys key ) + { + // return the shortcut as a string + StringBuilder buf = new StringBuilder() ; + if ( (modifiers & Keys.Control) != 0 ) + buf.Append( "Ctrl-" ) ; + if ( (modifiers & Keys.Shift) != 0 ) + buf.Append( "Shift-" ) ; + if ( (modifiers & Keys.Alt) != 0 ) + buf.Append( "Alt-" ) ; + buf.Append( key ) ; + return buf.ToString() ; + } + public string caption() { string caption = (mConfig != null) ? mConfig["caption"] : null ; diff --git a/src/MainForm.ui.cs b/src/MainForm.ui.cs index 05d1827..6a9ba68 100644 --- a/src/MainForm.ui.cs +++ b/src/MainForm.ui.cs @@ -8,6 +8,7 @@ using System.Windows.Forms ; using System.Diagnostics ; using Manina.Windows.Forms ; +using log4net ; // -------------------------------------------------------------------- @@ -99,10 +100,27 @@ public partial class MainForm : Form private void SearchQuery_KeyDown( object sender, KeyEventArgs e ) { + // check if there are any ChartImage's associated with the keypress + List chartImages = ChartImage.checkShortcut( e.Modifiers, e.KeyCode ) ; + if ( chartImages != null ) { + // yup - show them as search results + ILog logger = LogManager.GetLogger( "shortcuts" ) ; + logger.Info( $"Found ChartImage's for shortcut: {ChartImage.shortcutString(e.Modifiers,e.KeyCode)}" ) ; + foreach ( ChartImage chartImage in chartImages ) + logger.Info( $"- {chartImage.caption()}" ) ; + mSearchQuery.Text = "" ; + loadSearchResults( chartImages ) ; + e.Handled = true ; + return ; + } + // check if we should apply the keypress to the search results - if ( e.KeyCode == Keys.Escape ) + if ( e.KeyCode == Keys.Escape ) { + string prevSearchQuery = mSearchQuery.Text ; mSearchQuery.Text = "" ; - else if ( e.KeyCode == Keys.Left || e.KeyCode == Keys.Right ) { + if ( prevSearchQuery == "" ) + updateSearchResults( "" ) ; + } else if ( e.KeyCode == Keys.Left || e.KeyCode == Keys.Right ) { mSearchResults.Focus() ; SendKeys.SendWait( "{" + e.KeyCode.ToString() + "}" ) ; mSearchQuery.Focus() ;