FIxed invalid shortcut detection

Made the shortcuts that were invalid
visually different to the others. This is
now working properly!
This commit is contained in:
Terry MacDonald 2021-02-28 20:55:03 +13:00
parent 3a180d8813
commit 7d411b0aff
6 changed files with 105 additions and 8 deletions

View File

@ -308,6 +308,7 @@
<None Include="Resources\Epic.ico" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\warning.png" />
<None Include="Resources\DisplayMagician.ico" />
<Content Include="Resources\redarrows.png" />
</ItemGroup>

View File

@ -90,6 +90,16 @@ namespace DisplayMagician.Properties {
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
public static System.Drawing.Bitmap RedArrows {
get {
object obj = ResourceManager.GetObject("RedArrows", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
/// </summary>
@ -109,5 +119,15 @@ namespace DisplayMagician.Properties {
return ((System.Drawing.Icon)(obj));
}
}
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
public static System.Drawing.Bitmap Warning {
get {
object obj = ResourceManager.GetObject("Warning", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}

View File

@ -133,4 +133,10 @@
<data name="Uplay" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Uplay.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="RedArrows" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\resources\redarrows.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Warning" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\warning.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -9,10 +10,14 @@ using Manina.Windows.Forms;
namespace DisplayMagician.UIForms
{
#pragma warning disable CS3009 // Base type is not CLS-compliant
public class ShortcutILVRenderer : ImageListView.ImageListViewRenderer
#pragma warning restore CS3009 // Base type is not CLS-compliant
{
// Returns item size for the given view mode.
#pragma warning disable CS3001 // Argument type is not CLS-compliant
public override Size MeasureItem(View view)
#pragma warning restore CS3001 // Argument type is not CLS-compliant
{
Size itemPadding = new Size(4, 4);
Size sz = ImageListView.ThumbnailSize +
@ -39,9 +44,20 @@ namespace DisplayMagician.UIForms
base.DrawBackground(g, bounds);
}
// Draws the specified item on the given graphics.
public override void DrawItem(Graphics g, ImageListViewItem item,
ItemState state, Rectangle bounds)
#pragma warning disable CS3001 // Argument type is not CLS-compliant
public override void DrawItem(Graphics g, ImageListViewItem item, ItemState state, Rectangle bounds)
#pragma warning restore CS3001 // Argument type is not CLS-compliant
{
if (g is null)
{
throw new ArgumentNullException(nameof(g));
}
if (item is null)
{
throw new ArgumentNullException(nameof(item));
}
Size itemPadding = new Size(4, 4);
bool alternate = (item.Index % 2 == 1);
@ -106,7 +122,24 @@ namespace DisplayMagician.UIForms
if (img != null)
{
Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(bounds.Location + itemPadding, ImageListView.ThumbnailSize));
g.DrawImage(img, pos);
if (ShortcutLibraryForm.shortcutValidity[item.Text])
{
// Draw the full color image as the shortcuts is not invalid
g.DrawImage(img, pos);
}
else
{
// THe shortcut is invalid
// so we make the image grayscale
Image grayImg = MakeGrayscale(img);
g.DrawImage(grayImg, pos);
// Draw a warning triangle over it
// right in the centre
g.DrawImage(Properties.Resources.Warning, pos.X + 30, pos.Y + 30, 40, 40);
}
// Draw image border
if (Math.Min(pos.Width, pos.Height) > 32)
{
@ -298,5 +331,41 @@ namespace DisplayMagician.UIForms
path.CloseFigure();
return path;
}
public static Image MakeGrayscale(Image original)
{
//create a blank bitmap the same size as original
Image newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
using (Graphics g = Graphics.FromImage(newBitmap))
{
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
using (ImageAttributes attributes = new ImageAttributes())
{
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
}
}
return newBitmap;
}
}
}

View File

@ -19,6 +19,7 @@ namespace DisplayMagician.UIForms
private ShortcutAdaptor _shortcutAdaptor = new ShortcutAdaptor();
private ShortcutItem _selectedShortcut = null;
public static Dictionary<string, bool> shortcutValidity = new Dictionary<string, bool>();
public ShortcutLibraryForm()
{
@ -55,8 +56,8 @@ namespace DisplayMagician.UIForms
ImageListViewItem newItem = null;
ilv_saved_shortcuts.Items.Clear();
shortcutValidity.Clear();
foreach (ShortcutItem loadedShortcut in ShortcutRepository.AllShortcuts.OrderBy(s => s.Name))
{
newItem = new ImageListViewItem(loadedShortcut, loadedShortcut.Name);
@ -65,6 +66,9 @@ namespace DisplayMagician.UIForms
if (_selectedShortcut is ShortcutItem && _selectedShortcut.Equals(loadedShortcut))
newItem.Selected = true;
(bool result, string thing) = loadedShortcut.IsValid();
shortcutValidity[loadedShortcut.Name] = result;
//ilv_saved_profiles.Items.Add(newItem);
ilv_saved_shortcuts.Items.Add(newItem, _shortcutAdaptor);
}
@ -240,10 +244,7 @@ namespace DisplayMagician.UIForms
// Only run the if shortcut is valid
(bool result, string validityMessage) = _selectedShortcut.IsValid();
if (result)
// Run the selected shortcut
btn_run.PerformClick();
else
if (!result)
{
// We tell the user the reason that we couldnt run the shortcut
if (MessageBox.Show($"The shortcut '{_selectedShortcut.Name}' isn't valid for some reason so we cannot run the application or game. Has your hardware or screen layout changed from when the shortcut was made? We recommend that you edit the shortcut to make it valid again, or reverse the hardware changes you made. Do you want to do that now?", $"Edit the '{_selectedShortcut.Name}' Shortcut?", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)