1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Editor - Autofit surface on open

Data model picker - Expand on double click
This commit is contained in:
Robert 2022-08-13 17:09:53 +02:00
parent b0c7dd4290
commit e401fdf964
5 changed files with 86 additions and 8 deletions

View File

@ -13,6 +13,8 @@ using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Threading; using Avalonia.Threading;
using Material.Icons.Avalonia; using Material.Icons.Avalonia;
using ReactiveUI; using ReactiveUI;
@ -129,7 +131,7 @@ public class DataModelPicker : TemplatedControl
get => GetValue(DataModelViewModelProperty); get => GetValue(DataModelViewModelProperty);
private set => SetValue(DataModelViewModelProperty, value); private set => SetValue(DataModelViewModelProperty, value);
} }
/// <summary> /// <summary>
/// A list of types to filter the selectable paths on. /// A list of types to filter the selectable paths on.
/// </summary> /// </summary>
@ -246,6 +248,13 @@ public class DataModelPicker : TemplatedControl
DataModelPath = new DataModelPath(dataModelEvent.DataModelPath); DataModelPath = new DataModelPath(dataModelEvent.DataModelPath);
} }
private void DataModelTreeViewOnDoubleTapped(object? sender, RoutedEventArgs e)
{
TreeViewItem? treeViewItem = (e.Source as ILogical)?.FindLogicalAncestorOfType<TreeViewItem>();
if (treeViewItem != null)
treeViewItem.IsExpanded = !treeViewItem.IsExpanded;
}
private void UpdateCurrentPath(bool selectCurrentPath) private void UpdateCurrentPath(bool selectCurrentPath)
{ {
if (DataModelPath == null) if (DataModelPath == null)
@ -278,6 +287,8 @@ public class DataModelPicker : TemplatedControl
{ {
if (_dataModelTreeView != null) if (_dataModelTreeView != null)
_dataModelTreeView.SelectionChanged -= DataModelTreeViewOnSelectionChanged; _dataModelTreeView.SelectionChanged -= DataModelTreeViewOnSelectionChanged;
if (_dataModelTreeView != null)
_dataModelTreeView.DoubleTapped -= DataModelTreeViewOnDoubleTapped;
_currentPathIcon = e.NameScope.Find<MaterialIcon>("CurrentPathIcon"); _currentPathIcon = e.NameScope.Find<MaterialIcon>("CurrentPathIcon");
_currentPathDisplay = e.NameScope.Find<TextBlock>("CurrentPathDisplay"); _currentPathDisplay = e.NameScope.Find<TextBlock>("CurrentPathDisplay");
@ -286,6 +297,8 @@ public class DataModelPicker : TemplatedControl
if (_dataModelTreeView != null) if (_dataModelTreeView != null)
_dataModelTreeView.SelectionChanged += DataModelTreeViewOnSelectionChanged; _dataModelTreeView.SelectionChanged += DataModelTreeViewOnSelectionChanged;
if (_dataModelTreeView != null)
_dataModelTreeView.DoubleTapped += DataModelTreeViewOnDoubleTapped;
} }
#region Overrides of Visual #region Overrides of Visual

View File

@ -38,7 +38,7 @@
</Grid.Transitions> </Grid.Transitions>
<!-- The bottom layer consists of devices --> <!-- The bottom layer consists of devices -->
<ItemsControl Items="{CompiledBinding Devices}" ClipToBounds="False"> <ItemsControl Name="DevicesContainer" Items="{CompiledBinding Devices}" ClipToBounds="False">
<ItemsControl.Styles> <ItemsControl.Styles>
<Style Selector="ContentPresenter"> <Style Selector="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding X}" /> <Setter Property="Canvas.Left" Value="{Binding X}" />

View File

@ -1,15 +1,22 @@
using System;
using System.Linq;
using System.Reactive.Disposables;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.PanAndZoom; using Avalonia.Controls.PanAndZoom;
using Avalonia.Input;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
using Avalonia.Threading;
using ReactiveUI;
namespace Artemis.UI.Screens.ProfileEditor.VisualEditor; namespace Artemis.UI.Screens.ProfileEditor.VisualEditor;
public class VisualEditorView : ReactiveUserControl<VisualEditorViewModel> public class VisualEditorView : ReactiveUserControl<VisualEditorViewModel>
{ {
private readonly ZoomBorder _zoomBorder; private readonly ZoomBorder _zoomBorder;
private bool _movedByUser;
public VisualEditorView() public VisualEditorView()
{ {
@ -17,7 +24,37 @@ public class VisualEditorView : ReactiveUserControl<VisualEditorViewModel>
_zoomBorder = this.Find<ZoomBorder>("ZoomBorder"); _zoomBorder = this.Find<ZoomBorder>("ZoomBorder");
_zoomBorder.PropertyChanged += ZoomBorderOnPropertyChanged; _zoomBorder.PropertyChanged += ZoomBorderOnPropertyChanged;
_zoomBorder.PointerMoved += ZoomBorderOnPointerMoved;
_zoomBorder.PointerWheelChanged += ZoomBorderOnPointerWheelChanged;
UpdateZoomBorderBackground(); UpdateZoomBorderBackground();
this.WhenActivated(d =>
{
ViewModel!.AutoFitRequested += ViewModelOnAutoFitRequested;
Disposable.Create(() => ViewModel.AutoFitRequested -= ViewModelOnAutoFitRequested).DisposeWith(d);
});
this.WhenAnyValue(v => v.Bounds).Subscribe(_ =>
{
if (!_movedByUser)
AutoFit(true);
});
}
private void ZoomBorderOnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
{
_movedByUser = true;
}
private void ZoomBorderOnPointerMoved(object? sender, PointerEventArgs e)
{
if (e.GetCurrentPoint(_zoomBorder).Properties.IsMiddleButtonPressed)
_movedByUser = true;
}
private void ViewModelOnAutoFitRequested(object? sender, EventArgs e)
{
Dispatcher.UIThread.Post(() => AutoFit(false));
} }
private void ZoomBorderOnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) private void ZoomBorderOnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
@ -41,4 +78,27 @@ public class VisualEditorView : ReactiveUserControl<VisualEditorViewModel>
{ {
UpdateZoomBorderBackground(); UpdateZoomBorderBackground();
} }
private void AutoFit(bool skipTransitions)
{
if (ViewModel == null)
return;
double left = ViewModel.Devices.Select(d => d.Rectangle.Left).Min();
double top = ViewModel.Devices.Select(d => d.Rectangle.Top).Min();
double bottom = ViewModel.Devices.Select(d => d.Rectangle.Bottom).Max();
double right = ViewModel.Devices.Select(d => d.Rectangle.Right).Max();
// Add a 10 pixel margin around the rect
Rect scriptRect = new(new Point(left - 10, top - 10), new Point(right + 10, bottom + 10));
// The scale depends on the available space
double scale = Math.Min(3, Math.Min(Bounds.Width / scriptRect.Width, Bounds.Height / scriptRect.Height));
// Pan and zoom to make the script fit
_zoomBorder.Zoom(scale, 0, 0, skipTransitions);
_zoomBorder.Pan(Bounds.Center.X - scriptRect.Center.X * scale, Bounds.Center.Y - scriptRect.Center.Y * scale, skipTransitions);
_movedByUser = false;
}
} }

View File

@ -113,4 +113,11 @@ public class VisualEditorViewModel : ActivatableViewModelBase
visualizerViewModels.Add(_vmFactory.LayerShapeVisualizerViewModel(layer)); visualizerViewModels.Add(_vmFactory.LayerShapeVisualizerViewModel(layer));
visualizerViewModels.Add(_vmFactory.LayerVisualizerViewModel(layer)); visualizerViewModels.Add(_vmFactory.LayerVisualizerViewModel(layer));
} }
public void RequestAutoFit()
{
AutoFitRequested?.Invoke(this, EventArgs.Empty);
}
public event EventHandler? AutoFitRequested;
} }

View File

@ -2,13 +2,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:screens="clr-namespace:Artemis.VisualScripting.Nodes.Operators.Screens"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.VisualScripting.Nodes.Operators.Screens.EnumEqualsNodeCustomView" x:Class="Artemis.VisualScripting.Nodes.Operators.Screens.EnumEqualsNodeCustomView">
x:DataType="screens:EnumEqualsNodeCustomViewModel"> <ComboBox IsEnabled="{Binding EnumValues.Count}"
<ComboBox IsEnabled="{CompiledBinding EnumValues.Count}" Items="{Binding EnumValues}"
Items="{CompiledBinding EnumValues}" SelectedItem="{Binding CurrentValue}"
SelectedItem="{CompiledBinding CurrentValue}"
VirtualizationMode="Simple" VirtualizationMode="Simple"
PlaceholderText="Select a value" PlaceholderText="Select a value"
Classes="condensed" Classes="condensed"