Added support for shortcuts.

master
Pacman Ghost 5 years ago
parent 5a284e8414
commit c87b7fcf9e
  1. 21
      data/config.json
  2. 64
      src/ChartImage.cs
  3. 22
      src/MainForm.ui.cs

@ -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"
}
}

@ -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<Keys,Keys>, List<ChartImage> > mShortcuts = new Dictionary< Tuple<Keys,Keys>, List<ChartImage> >() ;
private string mFullPath ;
private dynamic mConfig ;
private HashSet<string> mKeywords = new HashSet<string>() ;
@ -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<Keys,Keys> shortcut = parseShortcut( val ) ;
if ( shortcut == null )
logger.Warn( $"Can't parse shortcut: {val}" ) ;
else {
if ( ! mShortcuts.ContainsKey( shortcut ) )
mShortcuts[ shortcut ] = new List<ChartImage>() ;
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<Keys,Keys> 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<Keys,Keys>( modifiers, key ) ;
}
public static List<ChartImage> checkShortcut( Keys modifiers, Keys key )
{
// check if there are any ChartImage's associated with the specified shortcut
List<ChartImage> chartImages ;
if ( ! mShortcuts.TryGetValue( new Tuple<Keys,Keys>( 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 ;

@ -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<ChartImage> 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() ;

Loading…
Cancel
Save