Skip to content

Commit

Permalink
Binding all view controls to models
Browse files Browse the repository at this point in the history
  • Loading branch information
zeh-almeida committed Jan 23, 2023
1 parent 723f884 commit 6216195
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Cpu.Form/Cpu.Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<description>6502 CPU Emulator View</description>
</PropertyGroup>

<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
Expand Down
25 changes: 17 additions & 8 deletions Cpu.Form/CpuView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public partial class CpuView : Form

private IMachine Machine { get; }

private string CurrentProgram { get; set; }

private MachineModel MachineView { get; }

private RunningProgramModel ProgramView { get; }
Expand All @@ -42,7 +40,7 @@ public CpuView(
}
#endregion

#region Updates
#region Bindings
private void BindProgram()
{
this.programText.BindTo(
Expand All @@ -52,6 +50,22 @@ private void BindProgram()
this.executionContent.BindTo(
this.ProgramView,
nameof(RunningProgramModel.Execution));

this.clockButton.BindTo(
this.ProgramView,
nameof(RunningProgramModel.ProgramLoaded));

this.resetButton.BindTo(
this.ProgramView,
nameof(RunningProgramModel.ProgramLoaded));

this.instructionButton.BindTo(
this.ProgramView,
nameof(RunningProgramModel.ProgramLoaded));

this.saveStateToolStripMenuItem.BindTo(
this.ProgramView,
nameof(RunningProgramModel.ProgramLoaded));
}

private void BindState()
Expand Down Expand Up @@ -308,10 +322,5 @@ private void EnableProgramExecution(ReadOnlyMemory<byte> bytes, string programNa

this.MachineView.LoadProgramCommand.Execute(bytes);
this.ProgramView.LoadProgramCommand.Execute(bytes);

this.clockButton.Enabled = true;
this.resetButton.Enabled = true;
this.instructionButton.Enabled = true;
this.saveStateToolStripMenuItem.Enabled = true;
}
}
10 changes: 10 additions & 0 deletions Cpu.Form/Utils/ControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ public static void BindTo(this Button control, object source, string sourcePropN
{
control.BindTo(nameof(Button.Enabled), source, sourcePropName);
}

public static void BindTo(this ToolStripMenuItem control, object source, string sourcePropName)
{
_ = control.DataBindings.Add(
nameof(Button.Enabled),
source,
sourcePropName,
false,
DataSourceUpdateMode.OnPropertyChanged);
}
}
10 changes: 9 additions & 1 deletion Cpu.MVVM/RunningProgramModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public partial class RunningProgramModel : ObservableObject
/// </summary>
[ObservableProperty]
private string _execution = string.Empty;

/// <summary>
/// Checks if the program is loaded and ready to use
/// </summary>
[ObservableProperty]
private bool _programLoaded = false;
#endregion

/// <summary>
Expand Down Expand Up @@ -61,7 +67,7 @@ protected void ClearExecution()
[RelayCommand]
protected void LoadProgram(ReadOnlyMemory<byte> program)
{
var builder = new StringBuilder(program.Length * CharsPer8Bit);
var builder = new StringBuilder(1 + (program.Length * CharsPer8Bit));
var data = program.Span;

foreach (var value in data[ICpuState.MemoryStateOffset..])
Expand All @@ -70,6 +76,7 @@ protected void LoadProgram(ReadOnlyMemory<byte> program)
}

this.Bytes = builder.ToString();
this.ProgramLoaded = true;
}

/// <summary>
Expand All @@ -79,6 +86,7 @@ protected void LoadProgram(ReadOnlyMemory<byte> program)
protected void ClearProgram()
{
this.Bytes = string.Empty;
this.ProgramLoaded = false;
}

/// <summary>
Expand Down

0 comments on commit 6216195

Please sign in to comment.