Browse Source

Fixed templates popup

Krzysztof Krysiński 1 year ago
parent
commit
f79fe801c7

+ 2 - 1
src/PixiEditor.AvaloniaUI/Models/Commands/Templates/Providers/AsepriteProvider.cs

@@ -1,6 +1,7 @@
 using System.Collections.Generic;
 using System.IO;
 using PixiEditor.AvaloniaUI.Models.Commands.Templates.Providers.Parsers;
+using PixiEditor.AvaloniaUI.Models.IO;
 
 namespace PixiEditor.AvaloniaUI.Models.Commands.Templates.Providers;
 
@@ -17,7 +18,7 @@ internal partial class ShortcutProvider
         
         public AsepriteProvider() : base("Aseprite")
         {
-            _parser = new AsepriteKeysParser("AsepriteShortcutMap.json");
+            _parser = new AsepriteKeysParser($"{Paths.InternalResourceDataPath}/ShortcutActionMaps/AsepriteShortcutMap.json");
             LogoPath = "/Images/TemplateLogos/Aseprite.png";
             HoverLogoPath = "/Images/TemplateLogos/Aseprite-Hover.png";
         }

+ 39 - 6
src/PixiEditor.AvaloniaUI/Models/Commands/Templates/Providers/Parsers/KeysParser.cs

@@ -1,5 +1,6 @@
 using System.Collections.Generic;
 using System.IO;
+using Avalonia.Platform;
 using Newtonsoft.Json;
 using PixiEditor.AvaloniaUI.Models.IO;
 
@@ -19,16 +20,36 @@ public abstract class KeysParser
     
     public KeysParser(string mapFileName)
     {
-        // TODO: Fix this, should use avares:// or custom path for external parsers
+        if (mapFileName.StartsWith("avares://"))
+        {
+            SetResourcePathOrThrow(mapFileName);
+        }
+        else
+        {
+            SetPathOrThrow(mapFileName);
+        }
+        
+        MapFileName = mapFileName;
+    }
+
+    private void SetResourcePathOrThrow(string mapFileName)
+    {
+        _fullMapFilePath = mapFileName;
+        if (!AssetLoader.Exists(new Uri(mapFileName)))
+        {
+            throw new FileNotFoundException($"Keys map file '{_fullMapFilePath}' not found in resources.");
+        }
+    }
+
+    private void SetPathOrThrow(string mapFileName)
+    {
         _fullMapFilePath = Path.Combine(Paths.DataFullPath, "ShortcutActionMaps", mapFileName);
         if (!File.Exists(_fullMapFilePath))
         {
             throw new FileNotFoundException($"Keys map file '{_fullMapFilePath}' not found.");
         }
-        
-        MapFileName = mapFileName;
     }
-    
+
     /// <summary>
     ///     Parses custom shortcuts file into ShortcutTemplate.
     /// </summary>
@@ -39,13 +60,25 @@ public abstract class KeysParser
     
     private Dictionary<string, KeyDefinition> LoadKeysMap()
     {
-        string text = File.ReadAllText(_fullMapFilePath);
+        string text = ReadMap();
         var dict = JsonConvert.DeserializeObject<Dictionary<string, KeyDefinition>>(text);
         if(dict == null) throw new Exception("Keys map file is empty.");
         if(dict.ContainsKey("")) dict.Remove("");
         return dict;
     }
-    
+
+    private static string ReadMap()
+    {
+        if (_fullMapFilePath.StartsWith("avares://"))
+        {
+            using Stream stream = AssetLoader.Open(new Uri(_fullMapFilePath));
+            using StreamReader reader = new(stream);
+            return reader.ReadToEnd();
+        }
+
+        return File.ReadAllText(_fullMapFilePath);
+    }
+
     private List<Shortcut> ParseDefaults()
     {
         var defaults = new List<Shortcut>();

+ 3 - 0
src/PixiEditor.AvaloniaUI/Models/IO/Paths.cs

@@ -12,4 +12,7 @@ public static class Paths
     public static string PathToPalettesFolder { get; } = Path.Join(
         Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
         "PixiEditor", "Palettes");
+
+    public static string InternalResourceDataPath { get; } =
+        $"avares://{Assembly.GetExecutingAssembly().GetName().Name}/Data";
 }

+ 2 - 11
src/PixiEditor.AvaloniaUI/Views/Shortcuts/ImportShortcutTemplatePopup.axaml

@@ -1,4 +1,4 @@
-<Window
+<dialogs:PixiEditorPopup
     xmlns="https://github.com/avaloniaui"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -20,16 +20,7 @@
     ui:Translator.Key="IMPORT_FROM_TEMPLATE">
 
     <Grid>
-        <Grid.RowDefinitions>
-            <RowDefinition Height="32" />
-            <RowDefinition Height="*" />
-        </Grid.RowDefinitions>
-        <dialogs:DialogTitleBar
-            DockPanel.Dock="Top"
-            TitleKey="IMPORT_FROM_TEMPLATE"
-            CloseCommand="{x:Static viewModels:SystemCommands.CloseWindowCommand}" />
         <ItemsControl
-            Grid.Row="1"
             ItemsSource="{Binding Templates, ElementName=window}"
             Margin="10,10,10,5">
             <ItemsControl.ItemsPanel>
@@ -50,4 +41,4 @@
             </ItemsControl.ItemTemplate>
         </ItemsControl>
     </Grid>
-</Window>
+</dialogs:PixiEditorPopup>

+ 2 - 6
src/PixiEditor.AvaloniaUI/Views/Shortcuts/ImportShortcutTemplatePopup.axaml.cs

@@ -6,12 +6,13 @@ using PixiEditor.AvaloniaUI.Models.Commands;
 using PixiEditor.AvaloniaUI.Models.Commands.Attributes.Commands;
 using PixiEditor.AvaloniaUI.Models.Commands.Templates;
 using PixiEditor.AvaloniaUI.Models.Dialogs;
+using PixiEditor.AvaloniaUI.Views.Dialogs;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Extensions.Exceptions;
 
 namespace PixiEditor.AvaloniaUI.Views.Shortcuts;
 
-internal partial class ImportShortcutTemplatePopup : Window
+internal partial class ImportShortcutTemplatePopup : PixiEditorPopup
 {
     public IEnumerable<ShortcutProvider> Templates { get; set; }
 
@@ -19,11 +20,6 @@ internal partial class ImportShortcutTemplatePopup : Window
     {
         Templates = ShortcutProvider.GetProviders();
         InitializeComponent();
-        
-        LayoutUpdated += (_, _) =>
-        {
-            MinHeight = Bounds.Height;
-        };
     }
 
     [Command.Internal("PixiEditor.Shortcuts.Provider.ImportDefault")]

+ 21 - 25
src/PixiEditor.AvaloniaUI/Views/Shortcuts/ShortcutsTemplateCard.axaml

@@ -3,40 +3,36 @@
     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:converters="clr-namespace:PixiEditor.AvaloniaUI.Helpers.Converters"
     mc:Ignorable="d"
     d:DesignWidth="800"
-    d:DesignHeight="450"
+    d:DesignHeight="450" Name="card"
     x:Class="PixiEditor.AvaloniaUI.Views.Shortcuts.ShortcutsTemplateCard">
-    <Border BorderThickness="1" Height="150" Width="150" Background="{DynamicResource ThemeBackgroundBrush}"
+    <UserControl.Styles>
+        <Style Selector="Border Image">
+            <Setter Property="Width" Value="72"/>
+            <Setter Property="Height" Value="72"/>
+            <Setter Property="Transitions">
+                <Transitions>
+                    <DoubleTransition Duration="0:0:0.15" Property="Width"/>
+                    <DoubleTransition Duration="0:0:0.15" Property="Height"/>
+                </Transitions>
+            </Setter>
+        </Style>
+        <Style Selector="Border:pointerover Image">
+            <Setter Property="Width" Value="100"/>
+            <Setter Property="Height" Value="100"/>
+        </Style>
+    </UserControl.Styles>
+    <Border BorderThickness="1" Height="150" Width="150" Background="{DynamicResource ThemeBackgroundBrush1}"
             CornerRadius="15" PointerEntered="OnBorderPointerEntered" PointerExited="OnBorderPointerExited">
-        <!-- TODO <Border.Triggers>
-            <EventTrigger RoutedEvent="Border.MouseEnter">
-                <BeginStoryboard>
-                    <Storyboard>
-                        <DoubleAnimation Storyboard.TargetName="img" Storyboard.TargetProperty="Width" 
-                                         From="72" To="100" Duration="0:0:0.15"/>
-                        <DoubleAnimation Storyboard.TargetName="img" Storyboard.TargetProperty="Height" 
-                                         From="72" To="100" Duration="0:0:0.15" />
-                    </Storyboard>
-                </BeginStoryboard>
-            </EventTrigger>
-            <EventTrigger RoutedEvent="Border.MouseLeave">
-                <BeginStoryboard>
-                    <Storyboard>
-                        <DoubleAnimation Storyboard.TargetName="img" Storyboard.TargetProperty="Width" 
-                                         From="100" To="72" Duration="0:0:0.15"/>
-                        <DoubleAnimation Storyboard.TargetName="img" Storyboard.TargetProperty="Height" 
-                                         From="100" To="72" Duration="0:0:0.15" />
-                    </Storyboard>
-                </BeginStoryboard>
-            </EventTrigger>
-        </Border.Triggers>-->
         <Grid>
             <Grid.RowDefinitions>
                 <RowDefinition Height="*"/>
                 <RowDefinition Height="30"/>
             </Grid.RowDefinitions>
-            <Image Grid.Row="0" Grid.RowSpan="2" Name="img" HorizontalAlignment="Center" VerticalAlignment="Center" Height="72" Width="72" Source="{Binding ElementName=card, Path=Logo}"/>
+            <Image Grid.Row="0" Grid.RowSpan="2" Name="img" HorizontalAlignment="Center" VerticalAlignment="Center"
+                   Source="{Binding ElementName=card, Path=Logo, Converter={converters:ImagePathToBitmapConverter}}"/>
             <Label 
                 Grid.Row="1" HorizontalAlignment="Center" FontWeight="Bold" Margin="0" Padding="0"
                 Content="{Binding ElementName=card, Path=TemplateName}"/>

+ 6 - 5
src/PixiEditor.AvaloniaUI/Views/Shortcuts/ShortcutsTemplateCard.axaml.cs

@@ -2,6 +2,7 @@
 using Avalonia.Controls;
 using Avalonia.Input;
 using Avalonia.Media.Imaging;
+using PixiEditor.AvaloniaUI.Helpers.Converters;
 
 namespace PixiEditor.AvaloniaUI.Views.Shortcuts;
 
@@ -16,10 +17,10 @@ public partial class ShortcutsTemplateCard : UserControl
         set { SetValue(TemplateNameProperty, value); }
     }
 
-    public static readonly StyledProperty<string>  LogoProperty = 
+    public static readonly StyledProperty<string> LogoProperty =
         AvaloniaProperty.Register<ShortcutsTemplateCard, string>(nameof(Logo));
 
-    public static readonly StyledProperty<string>  HoverLogoProperty = 
+    public static readonly StyledProperty<string> HoverLogoProperty =
         AvaloniaProperty.Register<ShortcutsTemplateCard, string>(nameof(HoverLogo));
 
     public string HoverLogo
@@ -46,7 +47,7 @@ public partial class ShortcutsTemplateCard : UserControl
             return;
         }
         
-        img.Source = new Bitmap(HoverLogo);
+        img.Source = ImagePathToBitmapConverter.LoadBitmapFromRelativePath(Logo);
     }
 
     private void OnBorderPointerEntered(object? sender, PointerEventArgs e)
@@ -55,8 +56,8 @@ public partial class ShortcutsTemplateCard : UserControl
         {
             return;
         }
-        
-        img.Source = new Bitmap(Logo);
+
+        img.Source = ImagePathToBitmapConverter.LoadBitmapFromRelativePath(HoverLogo);
     }
 }