Skip to content

Commit

Permalink
Reworked control checking to fix some bugs
Browse files Browse the repository at this point in the history
There was still some problems with key-combos/key modifiers so I rewrote the IsControl function in Config.cs.
I also changed some of the default key bindings.
  • Loading branch information
Torrunt committed Jan 18, 2018
1 parent 59935dc commit 79d2305
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
68 changes: 43 additions & 25 deletions vimage/Source/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class Config
public static bool CtrlDown = false;
public static bool ShiftDown = false;
public static bool AltDown = false;
public static bool RCtrlDown = false;
public static bool RShiftDown = false;
public static bool RAltDown = false;

public List<int> Control_Drag = new List<int>();
public List<int> Control_Close = new List<int>();
Expand Down Expand Up @@ -335,12 +338,12 @@ public void SetDefaultControls()
SetControls(Control_Delete, "DELETE");
SetControls(Control_Copy, "CTRL+C");
SetControls(Control_CopyAsImage, "ALT+C");
SetControls(Control_OpenDuplicateImage, "D");
SetControls(Control_OpenDuplicateImage, "C");
SetControls(Control_RandomImage, "M");
SetControls(Control_MoveLeft, "CTRL+LEFT");
SetControls(Control_MoveRight, "CTRL+RIGHT");
SetControls(Control_MoveUp, "CTRL+UP");
SetControls(Control_MoveDown, "CTRL+DOWN");
SetControls(Control_MoveLeft, "CTRL+LEFT", "RCTRL+LEFT");
SetControls(Control_MoveRight, "CTRL+RIGHT", "RCTRL+RIGHT");
SetControls(Control_MoveUp, "CTRL+UP", "RCTRL+UP");
SetControls(Control_MoveDown, "CTRL+DOWN", "RCTRL+DOWN");
}
public void SetDefaultContextMenu()
{
Expand Down Expand Up @@ -871,31 +874,41 @@ public static bool IsControl(Keyboard.Key keyCode, List<int> Control)
if (Control.Count == 0)
return false;

// key-combo?
if (Control.Count > 2 && Control[0] == -2 && (keyCode == (Keyboard.Key)Control[1] ||
((Keyboard.Key)Control[1] == Keyboard.Key.LControl && !CtrlDown) ||
((Keyboard.Key)Control[1] == Keyboard.Key.LShift && !ShiftDown) ||
((Keyboard.Key)Control[1] == Keyboard.Key.LAlt && !AltDown)))
int index = Control.IndexOf((int)keyCode);
if (index == -1)
return false;

// not key-combo but Ctrl, Shift or Alt is down?
if (Control[0] != -2)
int t = 0;
bool value = false;
do
{
if ((CtrlDown && Control.Contains((int)Keyboard.Key.LControl)) ||
(ShiftDown && Control.Contains((int)Keyboard.Key.LShift)) ||
(AltDown && Control.Contains((int)Keyboard.Key.LAlt)))
return true;
// key-combo?
if (index >= 1 && Control[index - 1] == -2)
value = false;
else if (index > 1 && Control[index - 2] == -2)
{
// key-combo
value = (((Keyboard.Key)Control[index - 1] == Keyboard.Key.LControl && CtrlDown) ||
((Keyboard.Key)Control[index - 1] == Keyboard.Key.LShift && ShiftDown) ||
((Keyboard.Key)Control[index - 1] == Keyboard.Key.LAlt && AltDown) ||
((Keyboard.Key)Control[index - 1] == Keyboard.Key.RControl && RCtrlDown) ||
((Keyboard.Key)Control[index - 1] == Keyboard.Key.RShift && RShiftDown) ||
((Keyboard.Key)Control[index - 1] == Keyboard.Key.RAlt && RAltDown));
}
else if (!KeyModifier(keyCode) && (CtrlDown || ShiftDown || AltDown || RCtrlDown || RShiftDown || RAltDown))
value = false; // don't activate non key-combos if key modifier is down
else
value = true;

if (CtrlDown || ShiftDown || AltDown)
return false;
// loop if there might be second binding using the same keyCode (eg: CTRL+UP and RCTRL+UP)
if (!value)
index = Control.IndexOf((int)keyCode, index + 1);
else
index = -1;
t++;
}
while (index != -1);

foreach (Keyboard.Key key in Control)
{
if (keyCode == key)
return true;
}
return false;
return value;
}
/// <summary> Returns true if Mouse.Button is one of Control bindings. </summary>
public static bool IsControl(Mouse.Button code, List<int> Control)
Expand Down Expand Up @@ -1472,6 +1485,11 @@ public static string KeyToString(Keyboard.Key key)
return "";
}

public static bool KeyModifier(Keyboard.Key key)
{
return key == Keyboard.Key.LControl || key == Keyboard.Key.LShift || key == Keyboard.Key.LAlt ||
key == Keyboard.Key.RControl || key == Keyboard.Key.RShift || key == Keyboard.Key.RAlt;
}

private static string VariableAmountOfStrings(int amount, string s)
{
Expand Down
28 changes: 21 additions & 7 deletions vimage/Source/ImageViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,28 @@ private void ControlUp(object code)
DoCustomAction((Config.CustomActions.Where(a => (a as dynamic).name == (Config.CustomActionBindings[i] as dynamic).name).First() as dynamic).func);
}

ZoomFaster = false;
ZoomAlt = false;
FitToMonitorAlt = false;
DragLimitToBoundsMod = false;
// Zooming - release
if (Config.IsControl(code, Config.Control_ZoomFaster))
ZoomFaster = false;
if (Config.IsControl(code, Config.Control_ZoomAlt))
ZoomAlt = false;
if (Config.IsControl(code, Config.Control_DragLimitToMonitorBounds))
DragLimitToBoundsMod = false;
if (Config.IsControl(code, Config.Control_FitToMonitorAlt))
FitToMonitorAlt = false;

if ((Keyboard.Key)code == Keyboard.Key.LControl)
Config.CtrlDown = false;
if ((Keyboard.Key)code == Keyboard.Key.LShift)
Config.ShiftDown = false;
if ((Keyboard.Key)code == Keyboard.Key.LAlt)
Config.AltDown = false;
if ((Keyboard.Key)code == Keyboard.Key.RControl)
Config.RCtrlDown = false;
if ((Keyboard.Key)code == Keyboard.Key.RShift)
Config.RShiftDown = false;
if ((Keyboard.Key)code == Keyboard.Key.RAlt)
Config.RAltDown = false;
}
private void ControlDown(object code)
{
Expand All @@ -497,11 +508,8 @@ private void ControlDown(object code)
ZoomFaster = true;
if (Config.IsControl(code, Config.Control_ZoomAlt))
ZoomAlt = true;

if (Config.IsControl(code, Config.Control_DragLimitToMonitorBounds))
DragLimitToBoundsMod = true;

// Fit To Monitor Height Alternative
if (Config.IsControl(code, Config.Control_FitToMonitorAlt))
FitToMonitorAlt = true;

Expand Down Expand Up @@ -536,6 +544,12 @@ private void ControlDown(object code)
Config.ShiftDown = true;
if ((Keyboard.Key)code == Keyboard.Key.LAlt)
Config.AltDown = true;
if ((Keyboard.Key)code == Keyboard.Key.RControl)
Config.RCtrlDown = true;
if ((Keyboard.Key)code == Keyboard.Key.RShift)
Config.RShiftDown = true;
if ((Keyboard.Key)code == Keyboard.Key.RAlt)
Config.RAltDown = true;
}


Expand Down

0 comments on commit 79d2305

Please sign in to comment.