Skip to content

Commit

Permalink
Small edit mode fixes
Browse files Browse the repository at this point in the history
- Uniformize element render order
- Enable toggle edit mode check on click
- Disable editing with right mouse button
  • Loading branch information
ThoNohT committed Dec 17, 2016
1 parent 4e2965a commit 8e20028
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
1 change: 1 addition & 0 deletions NohBoard/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions NohBoard/Forms/MainForm.Editmode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public partial class MainForm
/// <summary>
/// A stack containing the previous edits made by the user.
/// </summary>
private Stack<KeyboardDefinition> EditHistory = new Stack<KeyboardDefinition>();
private readonly Stack<KeyboardDefinition> EditHistory = new Stack<KeyboardDefinition>();

/// <summary>
/// Turns edit-mode on or off.
Expand All @@ -54,33 +54,35 @@ private void mnuToggleEditMode_Click(object sender, EventArgs e)

private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (!this.mnuToggleEditMode.Checked) return;

this.currentlyManipulating = GlobalSettings.CurrentDefinition.Elements
.FirstOrDefault(x => x.Inside(e.Location));
.LastOrDefault(x => x.Inside(e.Location));
if (this.currentlyManipulating == null) return;

this.currentManipulationPoint = e.Location;
this.EditHistory.Push(GlobalSettings.CurrentDefinition);
GlobalSettings.CurrentDefinition = GlobalSettings.CurrentDefinition.RemoveElement(
this.currentlyManipulating);
GlobalSettings.CurrentDefinition = GlobalSettings.CurrentDefinition
.RemoveElement(this.currentlyManipulating);

this.ResetBackBrushes();
}

private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (!this.mnuToggleEditMode.Checked || this.currentlyManipulating == null) return;

var diff = (TPoint)e.Location - this.currentManipulationPoint;

this.currentlyManipulating = this.currentlyManipulating.Translate(diff.Width, diff.Height);
this.ResetBackBrushes();
this.currentManipulationPoint = e.Location;

}

private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (!this.mnuToggleEditMode.Checked || this.currentlyManipulating == null) return;

GlobalSettings.CurrentDefinition = GlobalSettings.CurrentDefinition.AddElement(this.currentlyManipulating);
Expand Down
88 changes: 40 additions & 48 deletions NohBoard/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,16 @@ private void ResetBackBrushes()
}

// Render the individual keys.
foreach (var def in GlobalSettings.CurrentDefinition.Elements.OfType<KeyboardKeyDefinition>())
def.Render(g, false, shift, caps);
foreach (var def in GlobalSettings.CurrentDefinition.Elements)
{
if (def is KeyboardKeyDefinition) ((KeyboardKeyDefinition)def).Render(g, false, shift, caps);

foreach (var def in GlobalSettings.CurrentDefinition.Elements.OfType<MouseKeyDefinition>())
def.Render(g, false, shift, caps);
if (def is MouseKeyDefinition) ((MouseKeyDefinition)def).Render(g, false, shift, caps);

foreach (var def in GlobalSettings.CurrentDefinition.Elements.OfType<MouseScrollDefinition>())
def.Render(g, 0);
if (def is MouseScrollDefinition) ((MouseScrollDefinition)def).Render(g, 0);

foreach (var def in GlobalSettings.CurrentDefinition.Elements.OfType<MouseSpeedIndicatorDefinition>())
def.Render(g, new SizeF());
// No need to render mouse speed indicators in backbrush.
}

this.backBrushes[shift].Add(caps, new TextureBrush(bmp));
}
Expand Down Expand Up @@ -437,52 +436,45 @@ protected override void OnPaint(PaintEventArgs e)
this.backBrushes[KeyboardState.ShiftDown][KeyboardState.CapsActive],
new Rectangle(0, 0, GlobalSettings.CurrentDefinition.Width, GlobalSettings.CurrentDefinition.Height));

// Render keyboard keys.
var kbDefs = GlobalSettings.CurrentDefinition.Elements.OfType<KeyboardKeyDefinition>();
// Render all keys.
var kbKeys = KeyboardState.PressedKeys;
var onlySingles = true;
foreach (var def in kbDefs
.Where(d => kbKeys.ContainsAll(d.KeyCodes))
.OrderByDescending(d => d.KeyCodes.Count)
.TakeWhile(
d =>
{
if (d.KeyCodes.Count > 1) onlySingles = false;
return onlySingles || d.KeyCodes.Count > 1;
}))
{
def.Render(e.Graphics, true, KeyboardState.ShiftDown, KeyboardState.CapsActive);
}

// Render mouse keys.
onlySingles = true;
var mouseKeys = MouseState.PressedKeys;
foreach (var def in mouseKeys.SelectMany(
keyCode => GlobalSettings.CurrentDefinition.Elements.OfType<MouseKeyDefinition>()
.Where(x => x.KeyCodes.Contains((int)keyCode))))
{
def.Render(e.Graphics, true, KeyboardState.ShiftDown, KeyboardState.CapsActive);
}

// Render mouse scrolls.
onlySingles = true;
var mouseKeys = MouseState.PressedKeys.Select(k => (int)k).ToList();
MouseState.CheckScrollAndMovement();
var scrollCounts = MouseState.ScrollCounts;
for (var i = 0; i < scrollCounts.Count; i++)
var allDefs = GlobalSettings.CurrentDefinition.Elements;
foreach (var def in allDefs)
{
if (scrollCounts[i] == 0)
continue;

foreach (var def in GlobalSettings.CurrentDefinition.Elements.OfType<MouseScrollDefinition>()
.Where(x => x.KeyCodes.Contains(i))
.ToList())
def.Render(e.Graphics, scrollCounts[i]);
}
if (def is KeyboardKeyDefinition)
{
var kkDef = (KeyboardKeyDefinition)def;
if (!kkDef.KeyCodes.All(kbKeys.Contains)) continue;

// Render mouse speeds.
foreach (var def in GlobalSettings.CurrentDefinition.Elements.OfType<MouseSpeedIndicatorDefinition>())
def.Render(e.Graphics, MouseState.AverageSpeed);
if (kkDef.KeyCodes.Count == 1
&& allDefs.OfType<KeyboardKeyDefinition>().Any(
d => d.KeyCodes.Count > 1
&& d.KeyCodes.All(kbKeys.Contains)
&& d.KeyCodes.ContainsAll(kkDef.KeyCodes))) continue;

kkDef.Render(e.Graphics, true, KeyboardState.ShiftDown, KeyboardState.CapsActive);
}
if (def is MouseKeyDefinition)
{
var mkDef = (MouseKeyDefinition)def;
if (mouseKeys.Contains(mkDef.KeyCodes.Single()))
mkDef.Render(e.Graphics, true, KeyboardState.ShiftDown, KeyboardState.CapsActive);
}
if (def is MouseScrollDefinition)
{
var msDef = (MouseScrollDefinition)def;
var scrollCount = scrollCounts[msDef.KeyCodes.Single()];
if (scrollCount > 0) msDef.Render(e.Graphics, scrollCount);
}
if (def is MouseSpeedIndicatorDefinition)
{
((MouseSpeedIndicatorDefinition)def).Render(e.Graphics, MouseState.AverageSpeed);
}
}

base.OnPaint(e);
}

Expand Down

0 comments on commit 8e20028

Please sign in to comment.