Skip to content

Commit

Permalink
generic fpga gui progress
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Aug 2, 2024
1 parent 637b055 commit e202812
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/OneWare.UniversalFpgaProjectSystem/Fpga/Gui/FpgaGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ public class FpgaGui
public int Width { get; set; }

public int Height { get; set; }

public string? Image { get; set; }

public FpgaGuiElement[]? Elements { get; set; }
}
10 changes: 10 additions & 0 deletions src/OneWare.UniversalFpgaProjectSystem/Fpga/Gui/FpgaGuiElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace OneWare.UniversalFpgaProjectSystem.Fpga.Gui;

public class FpgaGuiElement
{
public string? Type { get; set; }

public int X { get; set; }

public int Y { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using OneWare.UniversalFpgaProjectSystem.Fpga.Gui;

namespace OneWare.UniversalFpgaProjectSystem.ViewModels.FpgaGuiElements;

public class FpgaGuiElementButtonViewModel : FpgaGuiElementViewModelBase
{
public FpgaGuiElementButtonViewModel(FpgaGuiElement element) : base(element)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CommunityToolkit.Mvvm.ComponentModel;
using OneWare.UniversalFpgaProjectSystem.Fpga.Gui;

namespace OneWare.UniversalFpgaProjectSystem.ViewModels.FpgaGuiElements;

public class FpgaGuiElementViewModelBase : ObservableObject
{
public FpgaGuiElement Element { get; }

public FpgaGuiElementViewModelBase(FpgaGuiElement element)
{
Element = element;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
using System.Collections.ObjectModel;
using System.Text.Json;
using CommunityToolkit.Mvvm.ComponentModel;
using OneWare.Essentials.Helpers;
using OneWare.Essentials.Services;
using OneWare.UniversalFpgaProjectSystem.Fpga.Gui;
using OneWare.UniversalFpgaProjectSystem.Models;
using OneWare.UniversalFpgaProjectSystem.ViewModels.FpgaGuiElements;
using Prism.Ioc;

namespace OneWare.UniversalFpgaProjectSystem.ViewModels;

public class GenericFpgaViewModel : FpgaViewModelBase
{
private FpgaGui? _fpgaGui;
private readonly string _guiPath;

private readonly IDisposable? _fileWatcher;

private bool _isLoading;

private int _width;

private int _height;

private string? _image;

public GenericFpgaViewModel(FpgaModel fpgaModel, string guiPath) : base(fpgaModel)
{
Expand All @@ -23,27 +33,73 @@ public GenericFpgaViewModel(FpgaModel fpgaModel, string guiPath) : base(fpgaMode
_fileWatcher = FileSystemWatcherHelper.WatchFile(guiPath, () => _ = LoadGuiAsync());
}

public FpgaGui? FpgaGui
public bool IsLoading
{
get => _isLoading;
set => SetProperty(ref _isLoading, value);
}

public int Width
{
get => _width;
set => SetProperty(ref _width, value);
}

public int Height
{
get => _fpgaGui;
set => SetProperty(ref _fpgaGui, value);
get => _height;
set => SetProperty(ref _height, value);
}

public string? Image
{
get => _image;
set => SetProperty(ref _image, value);
}

public ObservableCollection<FpgaGuiElementViewModelBase> Elements { get; } = new();

private async Task LoadGuiAsync()
{
Console.WriteLine("load");
IsLoading = true;
Width = 0;
Height = 0;
Image = null;
Elements.Clear();

try
{
await using var stream = File.OpenRead(_guiPath);
FpgaGui = await JsonSerializer.DeserializeAsync<FpgaGui>(stream, new JsonSerializerOptions()
var gui = await JsonSerializer.DeserializeAsync<FpgaGui>(stream, new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true
});

if (gui == null)
{
return;
}

Width = gui.Width;
Height = gui.Height;
Image = gui.Image != null ? Path.Combine(Path.GetDirectoryName(_guiPath)!, gui.Image) : null;

if (gui.Elements != null)
{
foreach (var element in gui.Elements)
{
if (element.Type == "button")
{
Elements.Add(new FpgaGuiElementButtonViewModel(element));
}
}
}
}
catch (Exception e)
{
ContainerLocator.Container.Resolve<ILogger>().Error(e.Message, e);
}
IsLoading = false;
}

public override void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:fpgaGuiElements="clr-namespace:OneWare.UniversalFpgaProjectSystem.ViewModels.FpgaGuiElements"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="OneWare.UniversalFpgaProjectSystem.Views.FpgaGuiElements.FpgaGuiElementButtonView" x:DataType="fpgaGuiElements:FpgaGuiElementButtonViewModel">
<Button Height="10" Width="10" Background="Red"/>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace OneWare.UniversalFpgaProjectSystem.Views.FpgaGuiElements;

public partial class FpgaGuiElementButtonView : UserControl
{
public FpgaGuiElementButtonView()
{
InitializeComponent();
}
}
37 changes: 29 additions & 8 deletions src/OneWare.UniversalFpgaProjectSystem/Views/GenericFpgaView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:OneWare.UniversalFpgaProjectSystem.ViewModels"
xmlns:controls="clr-namespace:OneWare.Essentials.Controls;assembly=OneWare.Essentials"
xmlns:converters="clr-namespace:OneWare.Essentials.Converters;assembly=OneWare.Essentials"
xmlns:fpgaGuiElements="clr-namespace:OneWare.UniversalFpgaProjectSystem.ViewModels.FpgaGuiElements"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="OneWare.UniversalFpgaProjectSystem.Views.GenericFpgaView" x:DataType="viewModels:GenericFpgaViewModel">
x:Class="OneWare.UniversalFpgaProjectSystem.Views.GenericFpgaView"
x:DataType="viewModels:GenericFpgaViewModel">
<Panel MinHeight="100" MinWidth="100">
<controls:Spinner IsVisible="{Binding FpgaGui, Converter={x:Static ObjectConverters.IsNull}}" Height="20" Width="20" />

<Panel DataContext="{Binding FpgaGui}" IsVisible="{Binding ., Converter={x:Static ObjectConverters.IsNotNull}}">
<Border Height="{Binding Height}" Width="{Binding Width}" Background="Red">

</Border>
<controls:Spinner IsVisible="{Binding IsLoading}" Height="20" Width="20" />

<Panel IsVisible="{Binding !IsLoading}">
<Image Height="{Binding Height}" Width="{Binding Width}"
Source="{Binding Image, Converter={x:Static converters:SharedConverters.PathToBitmapConverter}}"
Stretch="Fill" />

<ItemsControl ItemsSource="{Binding Elements}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Height="{Binding Height}"
Width="{Binding Width}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Styles>
<Style Selector="ContentPresenter" x:DataType="fpgaGuiElements:FpgaGuiElementViewModelBase">
<Setter Property="Canvas.Left" Value="{Binding Element.X}" />
<Setter Property="Canvas.Top" Value="{Binding Element.Y}" />
</Style>
</ItemsControl.Styles>
</ItemsControl>
<Canvas Background="Transparent">

</Canvas>
</Panel>
</Panel>
</UserControl>
</UserControl>

0 comments on commit e202812

Please sign in to comment.