Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: add local variables to watch panel automatically #2451

Draft
wants to merge 3 commits into
base: ags4
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Editor/AGS.Editor/AGSEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ private void _debugger_BreakAtLocation(DebugCallStack callStack)
Factory.GUIController.HideOutputPanel();
Factory.GUIController.ShowCallStack(callStack);
Factory.GUIController.ZoomToFile(callStack.Lines[0].ScriptName, callStack.Lines[0].LineNumber, true, callStack.ErrorMessage);
Factory.GUIController.SetAutoLocalVariables(callStack);
Factory.GUIController.NotifyWatchVariables();
}

Expand Down
20 changes: 20 additions & 0 deletions Editor/AGS.Editor/GUI/GUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class GUIController : IGUIController
private delegate void ShowCallStackDelegate(DebugCallStack callStack);
private delegate void ShowFindSymbolResultsDelegate(List<ScriptTokenReference> results);
private delegate void NotifyWatchVariablesDelegate();
private delegate void NotifySetAutoLocalVariables(DebugCallStack callStack);

private frmMain _mainForm;
private LogPanel _pnlEngineLog;
Expand Down Expand Up @@ -486,6 +487,14 @@ public void HideFindSymbolResults()
_mainForm.pnlFindResults.Hide();
}

public void AddVariableToWatchPanel(string var_name)
{
_mainForm.pnlWatchVariables.AddVariableToWatchList(var_name);
if (_mainForm.pnlWatchVariables.IsHidden)
return;
_mainForm.pnlWatchVariables.Show();
}

public void ShowWatchVariablesPanel(bool ifEnabled)
{
if (ifEnabled && _mainForm.pnlWatchVariables.IsHidden)
Expand Down Expand Up @@ -515,6 +524,17 @@ public void PrintEngineLog(string message, LogGroup group, LogLevel level)
_pnlEngineLog?.WriteLogMessage(message, group, level);
}

public void SetAutoLocalVariables(DebugCallStack callStack)
{
if (_mainForm.pnlWatchVariables.InvokeRequired)
{
_mainForm.pnlWatchVariables.Invoke(new NotifySetAutoLocalVariables(SetAutoLocalVariables), callStack);
return;
}

_mainForm.pnlWatchVariables.SetAutoLocalVariables(callStack);
}

public void NotifyWatchVariables()
{
if (_mainForm.pnlWatchVariables.InvokeRequired)
Expand Down
81 changes: 80 additions & 1 deletion Editor/AGS.Editor/GUI/WatchVariablesPanel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using AGS.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand Down Expand Up @@ -264,6 +265,76 @@ private void UpdateSingleWatch(ListViewItem item)
}
}

private int PerceivedBrightness(Color c)
{
return (int)Math.Sqrt(
c.R * c.R * .299 +
c.G * c.G * .587 +
c.B * c.B * .114);
}

public void SetAutoLocalVariables(DebugCallStack callStack)
{
if (!AGSEditor.Instance.Debugger.IsActive)
return;

string scriptName = callStack.Lines[0].ScriptName;
int lineNumber = callStack.Lines[0].LineNumber;
ScintillaWrapper scintilla = Factory.GUIController.GetScriptEditorControl(scriptName, true) as ScintillaWrapper;
if (scintilla == null)
return;

if (scintilla.CurrentLine != lineNumber)
{
scintilla.GoToLine(lineNumber);
}

List<string> varnames = scintilla.GetListOfLocalVariablesForCurrentPosition(false)
.Select(v => v.VariableName).Distinct().ToList();

listView1.BeginUpdate();

// We will avoid blinking of all "autolocal" by removing the ones not in varnames
// so we can keep the ones that already were added in a previous cycle
// later we will add ONLY the ones that weren't already added
// We also need to keep sync in the items to update.
foreach (ListViewItem itm in listView1.Items)
{
if (itm.Tag as string == "autolocal" && !varnames.Contains(itm.Text))
{
itm.Remove();
}
}

lock (_updateItemLock)
{
_itemsToUpdate.RemoveAll(itm => itm.Tag as string == "autolocal" && !varnames.Contains(itm.Text));

Color c = Color.Empty;
foreach (var v in varnames)
{
if (!listView1.Items.Cast<ListViewItem>().Any(itm => itm.Text == v && itm.Tag as string == "autolocal"))
{
var itm = CreateItem(v);
itm.Tag = "autolocal";
listView1.Items.Insert(0, itm);

if (c == Color.Empty)
{
int brightness = PerceivedBrightness(itm.ForeColor);
c = brightness > 128 ? Color.LightBlue : Color.DarkBlue;
}
itm.ForeColor = c;

_itemsToUpdate.Add(itm);
}
}
}

listView1.EndUpdate();
_updateItemTimer.Start();
}

public void UpdateAllWatches()
{
if (!AGSEditor.Instance.Debugger.IsActive)
Expand Down Expand Up @@ -314,6 +385,14 @@ private void listView1_MouseUp(object sender, MouseEventArgs e)
}
}

public void AddVariableToWatchList(string var_name)
{
ListViewItem item = listView1.Items.Add(CreateItem(var_name));
lock (_updateItemLock)
_itemsToUpdate.Add(item);
_updateItemTimer.Start();
}

private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
ListViewItem item;
Expand Down
29 changes: 25 additions & 4 deletions Editor/AGS.Editor/Panes/ScriptEditorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class ScriptEditorBase : EditorContentPanel
private const string GO_TO_DEFINITION_COMMAND = "ScriptGoToDefiniton";
private const string FIND_ALL_USAGES_COMMAND = "ScriptFindAllUsages";
private const string GO_TO_SPRITE_COMMAND = "ScriptGoToSprite";
private const string ADD_TO_WATCH_PANE_COMMAND = "ScriptAddToWatch";

protected AGSEditor _agsEditor;
// Loaded script reference, is assigned by the child class.
Expand All @@ -54,6 +55,7 @@ public class ScriptEditorBase : EditorContentPanel
_menuCmdCopy,
_menuCmdPaste,
_menuCmdGoToDefinition,
_menuCmdAddToWatchPane,
_menuCmdFindAllUsages,
_menuCmdGoToSprite;

Expand Down Expand Up @@ -97,7 +99,7 @@ protected IScript Script
/// <summary>
/// Lets for a child class to assign an actual Scintilla control.
/// </summary>
protected ScintillaWrapper Scintilla
public ScintillaWrapper Scintilla
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to not be necessary,
but if it's necessary to let make ScintillaWrapper public, then the property setter should still be made protected, to not let external code modify the text control.

{
get { return _scintilla; }
set
Expand Down Expand Up @@ -163,13 +165,16 @@ private void InitEditorMenus()

_menuCmdGoToDefinition = new MenuCommand(GO_TO_DEFINITION_COMMAND, "Go to Definition", Keys.F12);
_menuCmdGoToDefinition.Enabled = false;
_menuCmdAddToWatchPane = new MenuCommand(ADD_TO_WATCH_PANE_COMMAND, "Add to Watch", null);
_menuCmdAddToWatchPane.Enabled = false;
_menuCmdFindAllUsages = new MenuCommand(FIND_ALL_USAGES_COMMAND, "Find All Usages", Keys.Shift | Keys.F12);
_menuCmdFindAllUsages.Enabled = false;
_menuCmdGoToSprite = new MenuCommand(GO_TO_SPRITE_COMMAND, "Go to Sprite", Keys.Shift | Keys.F7);
_menuCmdGoToSprite.Enabled = false;

_extraMenu.Commands.Add(MenuCommand.Separator);
_extraMenu.Commands.Add(_menuCmdGoToDefinition);
_extraMenu.Commands.Add(_menuCmdAddToWatchPane);
_extraMenu.Commands.Add(_menuCmdFindAllUsages);
_extraMenu.Commands.Add(_menuCmdGoToSprite);

Expand Down Expand Up @@ -246,7 +251,11 @@ protected override void OnCommandClick(string command)
}
else if (IsContextCommand(command))
{
if (command == GO_TO_DEFINITION_COMMAND ||
if (command == ADD_TO_WATCH_PANE_COMMAND)
{
Factory.GUIController.AddVariableToWatchPanel(_goToDefinition);
}
else if (command == GO_TO_DEFINITION_COMMAND ||
command == FIND_ALL_USAGES_COMMAND)
{
string[] structAndMember = _goToDefinition.Split('.');
Expand Down Expand Up @@ -340,6 +349,7 @@ protected virtual void UpdateUICommands()
(_menuCmdUndo.Enabled != canUndo) ||
(_menuCmdRedo.Enabled != canRedo) ||
(_menuCmdGoToDefinition.Enabled != canGoToDefinition) ||
(_menuCmdAddToWatchPane.Enabled != canGoToDefinition) ||
(_menuCmdFindAllUsages.Enabled != canGoToDefinition) ||
(_menuCmdGoToSprite.Enabled != canGoToSprite);

Expand All @@ -350,7 +360,9 @@ protected virtual void UpdateUICommands()
_menuCmdPaste.Enabled = canPaste;
_menuCmdUndo.Enabled = canUndo;
_menuCmdRedo.Enabled = canRedo;
_menuCmdGoToDefinition.Enabled = _menuCmdFindAllUsages.Enabled = canGoToDefinition;
_menuCmdGoToDefinition.Enabled = canGoToDefinition;
_menuCmdAddToWatchPane.Enabled = canGoToDefinition;
_menuCmdFindAllUsages.Enabled = canGoToDefinition;
_menuCmdGoToSprite.Enabled = canGoToSprite;

Factory.ToolBarManager.RefreshCurrentPane();
Expand All @@ -365,7 +377,10 @@ protected static bool IsStandardEditCommand(string c)

protected static bool IsContextCommand(string c)
{
return (c == GO_TO_DEFINITION_COMMAND) || (c == FIND_ALL_USAGES_COMMAND) || (c == GO_TO_SPRITE_COMMAND);
return (c == GO_TO_DEFINITION_COMMAND) ||
(c == FIND_ALL_USAGES_COMMAND) ||
(c == GO_TO_SPRITE_COMMAND) ||
(c == ADD_TO_WATCH_PANE_COMMAND);
}

protected void UpdateScriptDocumentContext(int clickedPositionInDocument)
Expand Down Expand Up @@ -421,12 +436,18 @@ private void scintilla_ConstructContextMenu(ContextMenuStrip menuStrip, int clic
UpdateScriptDocumentContext(clickedPositionInDocument);

string typeName = _goToDefinition != null ? (" of " + _goToDefinition) : string.Empty;
string varName = _goToDefinition != null ? (_goToDefinition + " to Watch Panel") : string.Empty;

menuItem = new ToolStripMenuItem(_menuCmdGoToDefinition.Name + typeName, null, onClick, GO_TO_DEFINITION_COMMAND);
menuItem.ShortcutKeys = _menuCmdGoToDefinition.ShortcutKey;
menuItem.Enabled = (_goToDefinition != null);
menuStrip.Items.Add(menuItem);

menuItem = new ToolStripMenuItem("Add " + varName, null, onClick, ADD_TO_WATCH_PANE_COMMAND);
menuItem.ShortcutKeys = _menuCmdAddToWatchPane.ShortcutKey;
menuItem.Enabled = (_goToDefinition != null);
menuStrip.Items.Add(menuItem);

menuItem = new ToolStripMenuItem(_menuCmdFindAllUsages.Name + typeName, null, onClick, FIND_ALL_USAGES_COMMAND);
menuItem.ShortcutKeys = _menuCmdFindAllUsages.ShortcutKey;
menuItem.Enabled = (_goToDefinition != null);
Expand Down