Skip to content

Commit

Permalink
Update do load obf and obz boards
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-kierepka-hl committed Oct 6, 2024
1 parent 3c3ba69 commit 1207864
Show file tree
Hide file tree
Showing 21 changed files with 704 additions and 406 deletions.
5 changes: 5 additions & 0 deletions ChatAAC/ChatAAC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.3" />
<PackageReference Include="Avalonia.Desktop" Version="11.1.3" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.1.3" />
<PackageReference Include="Avalonia.Svg" Version="11.1.0.1" />
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0.1" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.3" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.3" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.1.0.4" />
<PackageReference Include="Avalonia.Xaml.Interactivity" Version="11.1.0.4" />
<PackageReference Include="BinToss.GroupBox.Avalonia" Version="1.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="8.0.8" />
Expand All @@ -32,6 +36,7 @@
<PackageReference Include="ReactiveUI" Version="20.1.63" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="RestSharp" Version="112.0.0" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
<PackageReference Include="System.Speech" Version="8.0.0" />
</ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions ChatAAC/Converters/BooleanToClassConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// BooleanToClassConverter.cs
using Avalonia.Data.Converters;
using System;
using System.Globalization;

namespace ChatAAC.Converters
{
public class BooleanToClassConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
return value != null ? "symbol action" : "symbol";
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
24 changes: 24 additions & 0 deletions ChatAAC/Converters/ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Globalization;
using System.Linq;
using Avalonia.Data.Converters;
using Avalonia.Media;

namespace ChatAAC.Converters;

public class ColorConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not string colorString) return Colors.Transparent;
var rgb = colorString.Split(['(', ',', ')'], StringSplitOptions.RemoveEmptyEntries)
.Skip(1) // Skip "rgb"
.Select(int.Parse).ToList();
return Color.FromArgb(255, (byte)rgb[0], (byte)rgb[1], (byte)rgb[2]);
}

public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
44 changes: 39 additions & 5 deletions ChatAAC/Converters/StringToBitmapConverter.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,60 @@
using Avalonia;
using Avalonia.Data.Converters;
using Avalonia.Media.Imaging;
using System;
using System.Globalization;
using System.IO;
using SkiaSharp;
using Svg.Skia;

namespace ChatAAC.Converters;

public class StringToBitmapConverter : IValueConverter
{
public static StringToBitmapConverter Instance = new StringToBitmapConverter();

public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not string path || !File.Exists(path)) return null;
try
{
return new Bitmap(path);
// Zakładając, że ścieżka wskazuje na plik SVG
if (!path.EndsWith(".svg", StringComparison.OrdinalIgnoreCase)) return new Bitmap(path);
using var stream = File.OpenRead(path);

using var svg = new SKSvg();
svg.Load(stream);

if (svg.Picture == null) return null;

// Ustawienie docelowego rozmiaru obrazu
const int targetWidth = 250;
const int targetHeight = 250;

// Obliczanie skali, aby zachować proporcje obrazu
float scaleX = targetWidth / svg.Picture.CullRect.Width;
float scaleY = targetHeight / svg.Picture.CullRect.Height;
float scale = Math.Min(scaleX, scaleY);

// Tworzenie i skalowanie bitmapy
var scaledSize = new SKImageInfo(targetWidth, targetHeight);
using var bitmap = new SKBitmap(scaledSize);
using var canvas = new SKCanvas(bitmap);
canvas.Clear(SKColors.Transparent);

var matrix = SKMatrix.CreateScale(scale, scale);
canvas.DrawPicture(svg.Picture, ref matrix);
canvas.Flush();

// Konwersja SKBitmap na Bitmapę Avalonii
using var image = SKImage.FromBitmap(bitmap);
using var data = image.Encode(SKEncodedImageFormat.Png, 100);
using var ms = new MemoryStream();
data.SaveTo(ms);
ms.Seek(0, SeekOrigin.Begin);
return new Bitmap(ms);
}
catch (Exception ex)
{
Console.WriteLine($"Błąd podczas tworzenia Bitmap z {path}: {ex.Message}");
// Możesz zwrócić domyślny obraz lub null
Console.WriteLine($"Błąd podczas tworzenia Bitmap z SVG: {ex.Message}");
return null;
}
}
Expand Down
59 changes: 59 additions & 0 deletions ChatAAC/Helpers/ButtonStyleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Avalonia;
using Avalonia.Controls;
using System;

namespace ChatAAC.Helpers
{
public static class ButtonStyleHelper
{
public static readonly AttachedProperty<object> ActionProperty =
AvaloniaProperty.RegisterAttached<Button, object>("LoadBoard", typeof(ButtonStyleHelper));

public static object GetAction(Button button)
{
return button.GetValue(ActionProperty);
}

public static void SetAction(Button button, object? value)
{
button.SetValue(ActionProperty!, value);
UpdateButtonClasses(button, value);
}

private static void UpdateButtonClasses(Button button, object? actionValue)
{
if (actionValue is not null)
{
button.Classes.Add("action");
}
else
{
button.Classes.Remove("action");
}

if (!button.Classes.Contains("symbol"))
{
button.Classes.Add("symbol");
}
}

static ButtonStyleHelper()
{
ActionProperty.Changed.Subscribe(new AnonymousObserver<AvaloniaPropertyChangedEventArgs<object>>(
e =>
{
if (e.Sender is Button button)
{
UpdateButtonClasses(button, e.NewValue.Value);
}
}));
}
}

internal class AnonymousObserver<T>(Action<T> onNext) : IObserver<T>
{
public void OnCompleted() { }
public void OnError(Exception error) { }
public void OnNext(T value) => onNext(value);
}
}
33 changes: 31 additions & 2 deletions ChatAAC/Models/Obf/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ChatAAC.Models.Obf;
// Class for Button
public class Button
{
[JsonPropertyName("id")] public string Id { get; set; } = string.Empty;
[JsonPropertyName("id")] public int Id { get; set; }

[JsonPropertyName("label")] public string Label { get; set; } = string.Empty;

Expand All @@ -17,7 +17,36 @@ public class Button

[JsonPropertyName("vocalization")] public string Vocalization { get; set; } = string.Empty;

[JsonPropertyName("load_board")] public LoadBoard LoadBoard { get; set; } = new();
[JsonPropertyName("load_board")] public LoadBoard? LoadBoard { get; set; }

[JsonPropertyName("action")] public string Action { get; set; } = string.Empty;

[JsonIgnore] public Image? Image { get; set; }

private const int ImageWidth = 260;
private const int ImageHeight = 290;

[JsonIgnore]
public int Width
{
get
{
var width = Image?.Width + 10;
if (width <= 10) width = 90;
if (width > ImageWidth) width = ImageWidth;
return width ?? ImageWidth;
}
}

[JsonIgnore]
public int Height
{
get
{
var height = Image?.Height + 30;
if (height <= 30) height = 110;
if (height > ImageHeight) height = ImageHeight;
return height ?? ImageHeight;
}
}
}
14 changes: 14 additions & 0 deletions ChatAAC/Models/Obf/ExtCoughDropSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace ChatAAC.Models.Obf;

public class ExtCoughDropSettings
{
[JsonPropertyName("private")]
public bool Private { get; set; }

[JsonPropertyName("key")] public string Key { get; set; } = string.Empty;

[JsonPropertyName("word_suggestions")]
public bool WordSuggestions { get; set; }
}
2 changes: 1 addition & 1 deletion ChatAAC/Models/Obf/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public class Grid

[JsonPropertyName("columns")] public int Columns { get; set; }

[JsonPropertyName("order")] public List<List<string>> Order { get; set; } = new();
[JsonPropertyName("order")] public List<List<int?>> Order { get; set; } = new();
}
8 changes: 8 additions & 0 deletions ChatAAC/Models/Obf/Image.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using System.Text.Json.Serialization;

namespace ChatAAC.Models.Obf;

// Class for Image
public class Image
{
[JsonPropertyName("id")] public string Id { get; set; } = string.Empty;

[JsonPropertyName("url")] public string Url { get; set; } = string.Empty;

[JsonPropertyName("data_url")] public string DataUrl { get; set; } = string.Empty;
[JsonPropertyName("data")] public string Data { get; set; } = string.Empty;

[JsonPropertyName("content_type")] public string ContentType { get; set; } = string.Empty;

[JsonPropertyName("width")] public int Width { get; set; }

[JsonPropertyName("height")] public int Height { get; set; }

[JsonPropertyName("license")] public License License { get; set; } = new();

[JsonPropertyName("path")] public string Path { get; set; } = string.Empty;

[JsonIgnore] public string ImagePath { get; set; } = string.Empty;
}
14 changes: 14 additions & 0 deletions ChatAAC/Models/Obf/License.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace ChatAAC.Models.Obf;

public class License
{
[JsonPropertyName("type")] public string Type { get; set; } = string.Empty;

[JsonPropertyName("copyright_notice_url")] public string CopyrightNoticeUrl { get; set; } = string.Empty;

[JsonPropertyName("author_name")] public string AuthorName { get; set; } = string.Empty;

[JsonPropertyName("author_url")] public string AuthorUrl { get; set; } = string.Empty;
}
12 changes: 12 additions & 0 deletions ChatAAC/Models/Obf/Manifest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace ChatAAC.Models.Obf;

public class Manifest
{
[JsonPropertyName("format")] public string Format { get; set; } = string.Empty;

[JsonPropertyName("root")] public string Root { get; set; } = string.Empty;

[JsonPropertyName("paths")] public Paths Paths { get; set; } = new();
}
13 changes: 11 additions & 2 deletions ChatAAC/Models/Obf/ObfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class ObfFile
{
[JsonPropertyName("format")] public string Format { get; set; } = string.Empty;

[JsonPropertyName("license")] public License License { get; set; } = new();

[JsonPropertyName("id")] public string Id { get; set; } = string.Empty;

[JsonPropertyName("locale")] public string Locale { get; set; } = string.Empty;
Expand All @@ -17,10 +19,17 @@ public class ObfFile
[JsonPropertyName("description_html")] public string DescriptionHtml { get; set; } = string.Empty;

[JsonPropertyName("grid")] public Grid Grid { get; set; } = new();

[JsonPropertyName("buttons")] public List<Button> Buttons { get; set; } = new();

[JsonPropertyName("images")] public List<Image> Images { get; set; } = new();

[JsonPropertyName("sounds")] public List<Sound> Sounds { get; set; } = new();

[JsonPropertyName("default_layout")] public string DefaultLayout { get; set; } = string.Empty;

[JsonPropertyName("url")] public string Url { get; set; } = string.Empty;

[JsonPropertyName("data_url")] public string DataUrl { get; set; } = string.Empty;

[JsonPropertyName("ext_coughdrop_settings")]
public ExtCoughDropSettings ExtCoughDropSettings { get; set; } = new();
}
Loading

0 comments on commit 1207864

Please sign in to comment.