Skip to content

Commit

Permalink
Merge pull request #1152 from VladiStep/tabsState
Browse files Browse the repository at this point in the history
  • Loading branch information
Grossley authored Mar 23, 2023
2 parents d40d7a0 + b3f31d6 commit 5355cf6
Show file tree
Hide file tree
Showing 10 changed files with 1,247 additions and 288 deletions.
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

0 comments on commit 5355cf6

Please sign in to comment.