Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from psyGamer/file-reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
swoolcock authored Feb 25, 2024
2 parents dfc540a + 53400b9 commit 3fdad5a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion TAS.Avalonia/Communication/StudioCommunicationServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace TAS.Avalonia.Communication;
public class StudioCommunicationServer : StudioCommunicationBase {
public event Action<StudioInfo> StateUpdated;
public event Action<Dictionary<HotkeyID, List<Keys>>> BindingsUpdated;
public event Action<Dictionary<int, string>> LinesUpdated;

protected virtual void OnStateUpdated(StudioInfo obj) => StateUpdated?.Invoke(obj);
protected virtual void OnBindingsUpdated(Dictionary<HotkeyID, List<Keys>> obj) => BindingsUpdated?.Invoke(obj);
protected virtual void OnLinesUpdated(Dictionary<int, string> lines) => LinesUpdated?.Invoke(lines);

private string _returnData;

Expand Down Expand Up @@ -99,7 +101,8 @@ private void ProcessVersionInfo(byte[] data) {
}

private void ProcessUpdateLines(byte[] data) {
// Dictionary<int, string> updateLines = BinaryFormatterHelper.FromByteArray<Dictionary<int, string>>(data);
Dictionary<int, string> updateLines = BinaryFormatterHelper.FromByteArray<Dictionary<int, string>>(data);
OnLinesUpdated(updateLines);
// CommunicationWrapper.UpdateLines(updateLines);
}

Expand Down
7 changes: 4 additions & 3 deletions TAS.Avalonia/Controls/EditorControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ public EditorControl() {
};

int prevLine = 0;
(Application.Current as App).CelesteService.Server.StateUpdated += state => {
if (state.CurrentLine == prevLine) return;
(Application.Current as App)!.CelesteService.Server.StateUpdated += state => {
if (state.CurrentLine == -1 || state.CurrentLine == prevLine) return;
prevLine = state.CurrentLine;
Dispatcher.UIThread.InvokeAsync(() =>
{
const int LinesBelow = 3;
Expand Down Expand Up @@ -99,7 +101,6 @@ public EditorControl() {
view.MakeVisible(new Rect(0, lineTop, 0, lineBottom - lineTop));
}
});
prevLine = state.CurrentLine;
};
}

Expand Down
17 changes: 17 additions & 0 deletions TAS.Avalonia/Models/TASDocument.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Avalonia;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using ReactiveUI;

Expand Down Expand Up @@ -30,6 +32,12 @@ private set {
private TASDocument(string contents) {
Document = new TextDocument(contents);
Document.TextChanged += Document_TextChanged;

(Application.Current as App)!.CelesteService.Server.LinesUpdated += OnLinesUpdated;
}

~TASDocument() {
(Application.Current as App)!.CelesteService.Server.LinesUpdated -= OnLinesUpdated;
}

public static TASDocument CreateBlank() => new TASDocument(EmptyDocument);
Expand Down Expand Up @@ -61,4 +69,13 @@ public void Save() {
private void Document_TextChanged(object sender, EventArgs eventArgs) {
Dirty = true;
}

private void OnLinesUpdated(Dictionary<int, string> lines) {
Dispatcher.UIThread.Post(() => {
foreach ((int lineNum, string newText) in lines) {
var line = Document.GetLineByNumber(lineNum + 1); // 0-indexed
Document.Replace(line, newText);
}
});
}
}

0 comments on commit 3fdad5a

Please sign in to comment.