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

Tabs content state saving and restoring (tab sessions). #1152

Merged
merged 22 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
503cfc9
Extracted "Tab" and "TabTitleConverter" classes to the separate file.
VladiStep Dec 24, 2022
35f8652
Added new classes for the last tab content state; documented all tabs…
VladiStep Dec 24, 2022
53a6e0d
Code editor fixes and improvements
VladiStep Jan 10, 2023
cb36096
Merge branch 'master' into tabsState
VladiStep Jan 10, 2023
d68164f
Improved code editor caret position saving and restoring.
VladiStep Jan 11, 2023
af84ab0
Merge branch 'tabsState' of https://github.com/VladiStep/UndertaleMod…
VladiStep Jan 11, 2023
2336ce2
Code editor tab restores its content state even if it's the same code…
VladiStep Jan 12, 2023
ad4e926
Implemented room editor tab content saving and restoring, reduced the…
VladiStep Jan 13, 2023
7ef74f7
Implemented font editor tab content saving and restoring.
VladiStep Jan 13, 2023
943ab7c
Implemented general info editor tab content saving and restoring.
VladiStep Jan 14, 2023
86bfc73
Implemented global init editor tab content saving and restoring.
VladiStep Jan 14, 2023
2420164
Fixed #1154
VladiStep Jan 14, 2023
a3a6a3b
Implemented sprite editor tab content saving and restoring.
VladiStep Jan 15, 2023
3d6745e
Implemented background (GMS 2 tile set) editor tab content saving and…
VladiStep Jan 16, 2023
3aeeae8
Fixed #1155, simplified the tab state methods.
VladiStep Jan 16, 2023
4e6fb13
Implemented texture group info editor tab content saving and restoring.
VladiStep Jan 16, 2023
2287d0a
Closed #1156, fixed #1157
VladiStep Jan 17, 2023
dccaf47
Merge branch 'master' into tabsState
VladiStep Jan 21, 2023
a940c6f
Fixed #1129
VladiStep Mar 3, 2023
2e1641e
Merged "master" into tabsState.
VladiStep Mar 9, 2023
f0eee24
Fixed bug with room editor tab not restoring the opened object.
VladiStep Mar 9, 2023
b3f31d6
The code scroll position saves separately.
VladiStep Mar 22, 2023
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
17 changes: 9 additions & 8 deletions UndertaleModTool/Editors/UndertaleCodeEditor.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<local:DataUserControl x:Class="UndertaleModTool.UndertaleCodeEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UndertaleModTool"
xmlns:undertale="clr-namespace:UndertaleModLib.Models;assembly=UndertaleModLib"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{d:DesignInstance undertale:UndertaleCode}" DataContextChanged="UserControl_DataContextChanged">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UndertaleModTool"
xmlns:undertale="clr-namespace:UndertaleModLib.Models;assembly=UndertaleModLib"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{d:DesignInstance undertale:UndertaleCode}"
DataContextChanged="UserControl_DataContextChanged" Loaded="UndertaleCodeEditor_Loaded" Unloaded="UndertaleCodeEditor_Unloaded">
<UserControl.CommandBindings>
<CommandBinding Command="{x:Static local:UndertaleCodeEditor.Compile}" Executed="Command_Compile" />
</UserControl.CommandBindings>
Expand Down
151 changes: 132 additions & 19 deletions UndertaleModTool/Editors/UndertaleCodeEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ public partial class UndertaleCodeEditor : DataUserControl
public bool DecompiledYet = false;
public bool DecompiledSkipped = false;
public SearchPanel DecompiledSearchPanel;
public static (int Line, int Column, double ScrollPos) OverriddenDecompPos;

public bool DisassemblyFocused = false;
public bool DisassemblyChanged = false;
public bool DisassembledYet = false;
public bool DisassemblySkipped = false;
public SearchPanel DisassemblySearchPanel;
public static (int Line, int Column, double ScrollPos) OverriddenDisasmPos;

public static RoutedUICommand Compile = new RoutedUICommand("Compile code", "Compile", typeof(UndertaleCodeEditor));

Expand Down Expand Up @@ -184,6 +186,12 @@ public UndertaleCodeEditor()
textArea.SelectionCornerRadius = 0;
}

private void UndertaleCodeEditor_Unloaded(object sender, RoutedEventArgs e)
{
OverriddenDecompPos = default;
OverriddenDisasmPos = default;
}

private void SearchPanel_LostFocus(object sender, RoutedEventArgs e)
{
SearchPanel panel = sender as SearchPanel;
Expand All @@ -205,27 +213,57 @@ private void SearchPanel_LostFocus(object sender, RoutedEventArgs e)
noMatchesTT.IsOpen = false;
}

private void UndertaleCodeEditor_Loaded(object sender, RoutedEventArgs e)
{
FillInCodeViewer();
}
private void FillInCodeViewer(bool overrideFirst = false)
{
UndertaleCode code = DataContext as UndertaleCode;
if (DisassemblyTab.IsSelected && code != CurrentDisassembled)
{
if (!overrideFirst)
{
DisassembleCode(code, !DisassembledYet);
DisassembledYet = true;
}
else
DisassembleCode(code, true);
}
if (DecompiledTab.IsSelected && code != CurrentDecompiled)
{
if (!overrideFirst)
{
_ = DecompileCode(code, !DecompiledYet);
DecompiledYet = true;
}
else
_ = DecompileCode(code, true);
}
}

private async void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
UndertaleCode code = this.DataContext as UndertaleCode;
Directory.CreateDirectory(MainPath);
Directory.CreateDirectory(TempPath);
if (code == null)
return;

DecompiledSearchPanel.Close();
DisassemblySearchPanel.Close();

await DecompiledLostFocusBody(sender, null);
DisassemblyEditor_LostFocus(sender, null);
if (DisassemblyTab.IsSelected && code != CurrentDisassembled)
{
DisassembleCode(code, !DisassembledYet);
DisassembledYet = true;
}
if (DecompiledTab.IsSelected && code != CurrentDecompiled)

if (!IsLoaded)
{
_ = DecompileCode(code, !DecompiledYet);
DecompiledYet = true;
// If it's not loaded, then "FillInCodeViewer()" will be executed on load.
// This prevents a bug with freezing on code opening.
return;
}

FillInCodeViewer();
}

private async void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
Expand All @@ -242,7 +280,6 @@ private async void UserControl_DataContextChanged(object sender, DependencyPrope
{
DecompiledSkipped = true;
await DecompiledLostFocusBody(sender, null);

}
else if (DisassemblyTab.IsSelected && DisassemblyFocused && DisassemblyChanged &&
CurrentDisassembled is not null && CurrentDisassembled != code)
Expand All @@ -256,6 +293,8 @@ private async void UserControl_DataContextChanged(object sender, DependencyPrope

DecompiledYet = false;
DisassembledYet = false;
CurrentDecompiled = null;
CurrentDisassembled = null;

if (MainWindow.CodeEditorDecompile != Unstated) //if opened from the code search results "link"
{
Expand All @@ -278,16 +317,7 @@ private async void UserControl_DataContextChanged(object sender, DependencyPrope
MainWindow.CodeEditorDecompile = Unstated;
}
else
{
if (DisassemblyTab.IsSelected && code != CurrentDisassembled)
{
DisassembleCode(code, true);
}
if (DecompiledTab.IsSelected && code != CurrentDecompiled)
{
_ = DecompileCode(code, true);
}
}
FillInCodeViewer(true);
}

public static readonly RoutedEvent CtrlKEvent = EventManager.RegisterRoutedEvent(
Expand Down Expand Up @@ -316,6 +346,42 @@ public async Task SaveChanges()
await CompileCommandBody(null, null);
}

public void RestoreState(CodeTabState tabState)
{
if (tabState.IsDecompiledOpen)
CodeModeTabs.SelectedItem = DecompiledTab;
else
CodeModeTabs.SelectedItem = DisassemblyTab;

TextEditor textEditor = DecompiledEditor;
(int linePos, int columnPos, double scrollPos) = tabState.DecompiledCodePosition;
RestoreCaretPosition(textEditor, linePos, columnPos, scrollPos);

textEditor = DisassemblyEditor;
(linePos, columnPos, scrollPos) = tabState.DisassemblyCodePosition;
RestoreCaretPosition(textEditor, linePos, columnPos, scrollPos);
}
private static void RestoreCaretPosition(TextEditor textEditor, int linePos, int columnPos, double scrollPos)
{
if (linePos <= textEditor.LineCount)
{
int lineLen = textEditor.Document.GetLineByNumber(linePos).Length;
textEditor.TextArea.Caret.Line = linePos;
if (columnPos != -1)
textEditor.TextArea.Caret.Column = columnPos;
else
textEditor.TextArea.Caret.Column = lineLen + 1;

textEditor.ScrollToLine(linePos);
textEditor.ScrollToVerticalOffset(scrollPos);
}
else
{
textEditor.CaretOffset = textEditor.Text.Length;
textEditor.ScrollToEnd();
}
}

private static void FillObjectDicts()
{
var data = mainWindow.Data;
Expand Down Expand Up @@ -383,6 +449,25 @@ private void DisassembleCode(UndertaleCode code, bool first)

string text;

int currLine = 1;
int currColumn = 1;
double scrollPos = 0;
if (!first)
{
var caret = DisassemblyEditor.TextArea.Caret;
currLine = caret.Line;
currColumn = caret.Column;
scrollPos = DisassemblyEditor.VerticalOffset;
}
else if (OverriddenDisasmPos != default)
{
currLine = OverriddenDisasmPos.Line;
currColumn = OverriddenDisasmPos.Column;
scrollPos = OverriddenDisasmPos.ScrollPos;

OverriddenDisasmPos = default;
}

DisassemblyEditor.TextArea.ClearSelection();
if (code.ParentEntry != null)
{
Expand All @@ -401,6 +486,9 @@ private void DisassembleCode(UndertaleCode code, bool first)

DisassemblyEditor.Document.BeginUpdate();
DisassemblyEditor.Document.Text = text;

RestoreCaretPosition(DisassemblyEditor, currLine, currColumn, scrollPos);

DisassemblyEditor.Document.EndUpdate();

if (first)
Expand Down Expand Up @@ -472,7 +560,28 @@ private string UpdateGettextJSON(string json)
private async Task DecompileCode(UndertaleCode code, bool first, LoaderDialog existingDialog = null)
{
DecompiledEditor.IsReadOnly = true;

int currLine = 1;
int currColumn = 1;
double scrollPos = 0;
if (!first)
{
var caret = DecompiledEditor.TextArea.Caret;
currLine = caret.Line;
currColumn = caret.Column;
scrollPos = DecompiledEditor.VerticalOffset;
}
else if (OverriddenDecompPos != default)
{
currLine = OverriddenDecompPos.Line;
currColumn = OverriddenDecompPos.Column;
scrollPos = OverriddenDecompPos.ScrollPos;

OverriddenDecompPos = default;
}

DecompiledEditor.TextArea.ClearSelection();

if (code.ParentEntry != null)
{
DecompiledEditor.Text = "// This code entry is a reference to an anonymous function within " + code.ParentEntry.Name.Content + ", view it there";
Expand Down Expand Up @@ -613,6 +722,8 @@ private async Task DecompileCode(UndertaleCode code, bool first, LoaderDialog ex
CurrentLocals.Add(local.Name.Content);
}

RestoreCaretPosition(DecompiledEditor, currLine, currColumn, scrollPos);

if (existingDialog is not null) //if code was edited (and compiles after it)
{
dataa.GMLCacheChanged.Add(code.Name.Content);
Expand All @@ -621,10 +732,12 @@ private async Task DecompileCode(UndertaleCode code, bool first, LoaderDialog ex
openSaveDialog = mainWindow.IsSaving;
}
}

DecompiledEditor.Document.EndUpdate();
DecompiledEditor.IsReadOnly = false;
if (first)
DecompiledEditor.Document.UndoStack.ClearAll();

DecompiledChanged = false;

CurrentDecompiled = code;
Expand Down
3 changes: 2 additions & 1 deletion UndertaleModTool/Editors/UndertaleFontEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
</Viewbox>

<TextBlock Grid.Row="11" Grid.Column="0" Margin="3,30,3,3">Glyphs:</TextBlock>
<local:DataGridDark Grid.Row="12" Grid.ColumnSpan="2" MaxHeight="370" Margin="3" ItemsSource="{Binding Glyphs, Mode=OneWay}" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="Column" SelectionMode="Single" SelectionUnit="FullRow"
<local:DataGridDark Grid.Row="12" Grid.ColumnSpan="2" MaxHeight="370" Margin="3" x:Name="GlyphsGrid" ItemsSource="{Binding Glyphs, Mode=OneWay}"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="Column" SelectionMode="Single" SelectionUnit="FullRow"
ScrollViewer.CanContentScroll="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling">
Expand Down
5 changes: 3 additions & 2 deletions UndertaleModTool/Editors/UndertaleGeneralInfoEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@
<TextBox Grid.Row="20" Grid.Column="1" Margin="3" Text="{Binding GeneralInfo.DebuggerPort}"/>

<TextBlock Grid.Row="21" Grid.Column="0" Margin="3">Room order</TextBlock>
<Expander Grid.Row="21" Grid.Column="1" Margin="3" Header="List">
<local:DataGridDark ItemsSource="{Binding GeneralInfo.RoomOrder}" MaxHeight="369" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="None" SelectionMode="Single" SelectionUnit="FullRow"
<Expander Grid.Row="21" Grid.Column="1" Margin="3" Header="List" Name="RoomListExpander">
<local:DataGridDark ItemsSource="{Binding GeneralInfo.RoomOrder}" MaxHeight="369" x:Name="RoomListGrid"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="None" SelectionMode="Single" SelectionUnit="FullRow"
ScrollViewer.CanContentScroll="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling">
Expand Down
3 changes: 2 additions & 1 deletion UndertaleModTool/Editors/UndertaleGlobalInitEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<DockPanel MaxHeight="{Binding ViewportHeight, Mode=OneWay, RelativeSource={RelativeSource AncestorType=ScrollViewer}}">
<TextBlock Text="Global initialization scripts" FontWeight="Bold" DockPanel.Dock="Top"/>
<Separator DockPanel.Dock="Top"/>
<local:DataGridDark DockPanel.Dock="Top" Margin="3" ItemsSource="{Binding GlobalInits}" AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="None" SelectionMode="Single" SelectionUnit="FullRow"
<local:DataGridDark DockPanel.Dock="Top" Margin="3" ItemsSource="{Binding GlobalInits}" x:Name="ScriptsGrid"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" HeadersVisibility="None" SelectionMode="Single" SelectionUnit="FullRow"
ScrollViewer.CanContentScroll="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.VirtualizationMode="Recycling">
Expand Down
7 changes: 6 additions & 1 deletion UndertaleModTool/Editors/UndertaleRoomEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ private void ExportAsPNG_Click(object sender, RoutedEventArgs e)

private void UndertaleRoomEditor_Loaded(object sender, RoutedEventArgs e)
{
RoomRootItem.IsSelected = true;
if (ObjectEditor.Content is null)
RoomRootItem.IsSelected = true;
}
private void UndertaleRoomEditor_Unloaded(object sender, RoutedEventArgs e)
{
Expand All @@ -147,6 +148,10 @@ private void UndertaleRoomEditor_DataContextChanged(object sender, DependencyPro
ScrollViewer viewer = MainWindow.FindVisualChild<ScrollViewer>(RoomObjectsTree);
viewer.ScrollToVerticalOffset(0);
viewer.ScrollToHorizontalOffset(0);

RoomGraphics.ClearValue(LayoutTransformProperty);
RoomGraphicsScroll.ScrollToVerticalOffset(0);
RoomGraphicsScroll.ScrollToHorizontalOffset(0);
}

UndertaleCachedImageLoader.Reset();
Expand Down
Loading