Skip to content

Commit

Permalink
Enable DebugMode (#386)
Browse files Browse the repository at this point in the history
* enable debug mode when compiling in RELEASE mode
* add hotkey for enabling debug mode
* add localization for hotkey
* add documentation
  • Loading branch information
FroggieFrog authored Jan 17, 2022
1 parent 620b19f commit bb6e29a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
4 changes: 0 additions & 4 deletions AnnoDesigner.Core/DataStructures/QuadTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ public Quadrant GetContainingQuadrant(Rect bounds)
return null;
}

#if DEBUG
/// <summary>
/// Retrieves a list of Rects that make up this quadrant and the quadrants beneath it.
/// Used when debugging to draw quadrants to a canvas.
Expand Down Expand Up @@ -377,7 +376,6 @@ public IEnumerable<Rect> GetQuadrantRects()
}
}
}
#endif
}

/// <summary>
Expand Down Expand Up @@ -546,7 +544,6 @@ public void AddRange(IEnumerable<T> collection)
}
}

#if DEBUG
/// <summary>
/// Retrieves a list of Rects that make up the Quadrants in this QuadTree.
/// Used when debugging to draw the QuadTree quadrants to a canvas.
Expand All @@ -556,6 +553,5 @@ public IEnumerable<Rect> GetQuadrantRects()
{
return root.GetQuadrantRects();
}
#endif
}
}
71 changes: 36 additions & 35 deletions AnnoDesigner/AnnoCanvas.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public partial class AnnoCanvas : UserControl, IAnnoCanvas, IHotkeySource, IScro
public const string DELETE_OBJECT_UNDER_CURSOR_LOCALIZATION_KEY = "DeleteObjectUnderCursor";
public const string UNDO_LOCALIZATION_KEY = "Undo";
public const string REDO_LOCALIZATION_KEY = "Redo";
public const string ENABLE_DEBUG_MODE_LOCALIZATION_KEY = "EnableDebugMode";

public event EventHandler<UpdateStatisticsEventArgs> StatisticsUpdated;
public event EventHandler<EventArgs> ColorsInLayoutUpdated;
Expand Down Expand Up @@ -534,7 +535,6 @@ public double LinePenThickness

#endregion

#if DEBUG
#region Debug options

/// <summary>
Expand All @@ -546,19 +546,18 @@ public double LinePenThickness
/// </summary>
private readonly SolidColorBrush _debugBrushLight;

private bool debugModeIsEnabled = false;
private readonly bool debugShowObjectPositions = true;
private readonly bool debugShowQuadTreeViz = true;
private readonly bool debugShowSelectionRectCoordinates = true;
private readonly bool debugShowSelectionCollisionRect = true;
private readonly bool debugShowViewportRectCoordinates = true;
private readonly bool debugShowScrollableRectCoordinates = true;
private readonly bool debugShowLayoutRectCoordinates = true;
private readonly bool debugShowMouseGridCoordinates = true;
private readonly bool debugShowObjectCount = true;
private bool _debugModeIsEnabled = false;
private readonly bool _debugShowObjectPositions = true;
private readonly bool _debugShowQuadTreeViz = true;
private readonly bool _debugShowSelectionRectCoordinates = true;
private readonly bool _debugShowSelectionCollisionRect = true;
private readonly bool _debugShowViewportRectCoordinates = true;
private readonly bool _debugShowScrollableRectCoordinates = true;
private readonly bool _debugShowLayoutRectCoordinates = true;
private readonly bool _debugShowMouseGridCoordinates = true;
private readonly bool _debugShowObjectCount = true;

#endregion
#endif

#region Constructor
/// <summary>
Expand Down Expand Up @@ -621,6 +620,7 @@ public AnnoCanvas(BuildingPresets presetsToUse,
deleteObjectUnderCursorCommand = new RelayCommand(ExecuteDeleteObjectUnderCursor);
undoCommand = new RelayCommand(ExecuteUndo);
redoCommand = new RelayCommand(ExecuteRedo);
enableDebugModeCommand = new RelayCommand(ExecuteEnableDebugMode);

//Set up default keybindings

Expand Down Expand Up @@ -656,6 +656,9 @@ public AnnoCanvas(BuildingPresets presetsToUse,
var redoBinding = new InputBinding(redoCommand, new PolyGesture(Key.Y, ModifierKeys.Control));
redoHotkey = new Hotkey(REDO_LOCALIZATION_KEY, redoBinding, REDO_LOCALIZATION_KEY);

var enableDebugModeBinding = new InputBinding(enableDebugModeCommand, new PolyGesture(Key.D, ModifierKeys.Control | ModifierKeys.Shift));
enableDebugModeHotkey = new Hotkey(ENABLE_DEBUG_MODE_LOCALIZATION_KEY, enableDebugModeBinding, ENABLE_DEBUG_MODE_LOCALIZATION_KEY);

//We specifically do not add the `InputBinding`s to the `InputBindingCollection` of `AnnoCanvas`, as if we did that,
//`InputBinding.Gesture.Matches()` would be fired for *every* event - MouseWheel, MouseDown, KeyUp, KeyDown, MouseMove etc
//which we don't want, as it produces a noticeable performance impact.
Expand All @@ -674,10 +677,8 @@ public AnnoCanvas(BuildingPresets presetsToUse,
color = Colors.LawnGreen;
color.A = 32;
_influencedBrush = _brushCache.GetSolidBrush(color);
#if DEBUG
_debugBrushLight = Brushes.Blue;
_debugBrushDark = Brushes.DarkBlue;
#endif

sw.Stop();
logger.Trace($"init variables took: {sw.ElapsedMilliseconds}ms");
Expand Down Expand Up @@ -1078,11 +1079,10 @@ protected override void OnRender(DrawingContext drawingContext)

#region Draw debug information

#if DEBUG
if (debugModeIsEnabled)
if (_debugModeIsEnabled)
{
drawingContext.PushTransform(_viewportTransform);
if (debugShowQuadTreeViz)
if (_debugShowQuadTreeViz)
{
var brush = Brushes.Transparent;
var pen = _penCache.GetPen(_debugBrushDark, 2);
Expand All @@ -1092,7 +1092,7 @@ protected override void OnRender(DrawingContext drawingContext)
}
}

if (debugShowSelectionCollisionRect)
if (_debugShowSelectionCollisionRect)
{
var color = _debugBrushLight.Color;
color.A = 0x08;
Expand All @@ -1106,7 +1106,7 @@ protected override void OnRender(DrawingContext drawingContext)
drawingContext.Pop();
var debugText = new List<FormattedText>(3);

if (debugShowViewportRectCoordinates)
if (_debugShowViewportRectCoordinates)
{
//The first time this is called, App.DpiScale is still 0 which causes this code to throw an error
if (App.DpiScale.PixelsPerDip != 0)
Expand All @@ -1124,7 +1124,7 @@ protected override void OnRender(DrawingContext drawingContext)
}
}

if (debugShowScrollableRectCoordinates)
if (_debugShowScrollableRectCoordinates)
{
//The first time this is called, App.DpiScale is still 0 which causes this code to throw an error
if (App.DpiScale.PixelsPerDip != 0)
Expand All @@ -1142,7 +1142,7 @@ protected override void OnRender(DrawingContext drawingContext)
}
}

if (debugShowLayoutRectCoordinates)
if (_debugShowLayoutRectCoordinates)
{
//The first time this is called, App.DpiScale is still 0 which causes this code to throw an error
if (App.DpiScale.PixelsPerDip != 0)
Expand All @@ -1160,7 +1160,7 @@ protected override void OnRender(DrawingContext drawingContext)
}
}

if (debugShowObjectCount)
if (_debugShowObjectCount)
{
//The first time this is called, App.DpiScale is still 0 which causes this code to throw an error
if (App.DpiScale.PixelsPerDip != 0)
Expand All @@ -1179,7 +1179,7 @@ protected override void OnRender(DrawingContext drawingContext)
drawingContext.DrawText(debugText[i], new Point(5, (i * 15) + 5));
}

if (debugShowMouseGridCoordinates)
if (_debugShowMouseGridCoordinates)
{
//The first time this is called, App.DpiScale is still 0 which causes this code to throw an error
if (App.DpiScale.PixelsPerDip != 0)
Expand All @@ -1203,7 +1203,7 @@ protected override void OnRender(DrawingContext drawingContext)
//draw selection rect coords last so they draw over the top of everything else
if (CurrentMode == MouseMode.SelectionRect)
{
if (debugShowSelectionRectCoordinates)
if (_debugShowSelectionRectCoordinates)
{
var rect = _coordinateHelper.ScreenToGrid(_selectionRect, _gridSize);
var top = rect.Top;
Expand All @@ -1223,7 +1223,6 @@ protected override void OnRender(DrawingContext drawingContext)
}
}
}
#endif

#endregion

Expand Down Expand Up @@ -1455,8 +1454,8 @@ private void RenderObjectList(DrawingContext drawingContext, List<LayoutObject>

drawingContext.DrawText(text, textLocation);
}
#if DEBUG
if (debugModeIsEnabled && debugShowObjectPositions)

if (_debugModeIsEnabled && _debugShowObjectPositions)
{
var text = new FormattedText(obj.Position.ToString(), Thread.CurrentThread.CurrentCulture, FlowDirection.LeftToRight,
TYPEFACE, 12, _debugBrushLight,
Expand All @@ -1472,7 +1471,6 @@ private void RenderObjectList(DrawingContext drawingContext, List<LayoutObject>

drawingContext.DrawText(text, textLocation);
}
#endif
}
}

Expand Down Expand Up @@ -2439,13 +2437,7 @@ protected override void OnKeyDown(KeyEventArgs e)
//When an InputBinding is added to the InputBindingsCollection, the `Matches` method is fired for every event - KeyUp,
//KeyDown, MouseUp, MouseMove, MouseWheel etc.
HotkeyCommandManager.HandleCommand(e);
#if DEBUG
if (e.Key == Key.D && Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift))
{
debugModeIsEnabled = !debugModeIsEnabled;
e.Handled = true;
}
#endif

if (e.Handled)
{
InvalidateVisual();
Expand Down Expand Up @@ -2685,6 +2677,7 @@ public void RegisterHotkeys(HotkeyCommandManager manager)
manager.AddHotkey(deleteObjectUnderCursorHotkey);
manager.AddHotkey(undoHotkey);
manager.AddHotkey(redoHotkey);
manager.AddHotkey(enableDebugModeHotkey);
}

#endregion
Expand Down Expand Up @@ -3015,6 +3008,14 @@ private void ExecuteRedo(object param)
StatisticsUpdated?.Invoke(this, UpdateStatisticsEventArgs.All);
}

private readonly Hotkey enableDebugModeHotkey;
private readonly ICommand enableDebugModeCommand;
private void ExecuteEnableDebugMode(object param)
{
_debugModeIsEnabled = !_debugModeIsEnabled;
ForceRendering();
}

#endregion

#region Helper methods
Expand Down
21 changes: 21 additions & 0 deletions doc/Enable_DebugMode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Enable `DebugMode`

## General information

For further investigation of issues, it is possible to activate a `DebugMode`.

## How to enable/disable

The default Hotkey to enable/disable `DebugMode` is `CTRL + Shift + D`.</br>
You can change the Hotkey via `Tools -> Preferences -> Manage Keybindings` menu.

## Supported features of `DebugMode`

- display coordinates of current Viewport
- display coordinates of current scrollable area
- display coordinates and bounding box of current layout
- display collision rectangle of current selection
- display helper lines of `QuadTree`
- display coordinates of objects (on each object)
- display count of placed objects
- display coordinates of mouse cursor (on mouse cursor)

0 comments on commit bb6e29a

Please sign in to comment.