aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/PixelSnapHelpers.cs
blob: abd859533bad007c9513aad00751d5480d7c93b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.Windows;
using System.Windows.Media;

namespace Tango.Scripting.Editors.Utils
{
	/// <summary>
	/// Contains static helper methods for aligning stuff on a whole number of pixels.
	/// </summary>
	public static class PixelSnapHelpers
	{
		/// <summary>
		/// Gets the pixel size on the screen containing visual.
		/// This method does not take transforms on visual into account.
		/// </summary>
		public static Size GetPixelSize(Visual visual)
		{
			if (visual == null)
				throw new ArgumentNullException("visual");
			PresentationSource source = PresentationSource.FromVisual(visual);
			if (source != null) {
				Matrix matrix = source.CompositionTarget.TransformFromDevice;
				return new Size(matrix.M11, matrix.M22);
			} else {
				return new Size(1, 1);
			}
		}
		
		/// <summary>
		/// Aligns <paramref name="value"/> on the next middle of a pixel.
		/// </summary>
		/// <param name="value">The value that should be aligned</param>
		/// <param name="pixelSize">The size of one pixel</param>
		public static double PixelAlign(double value, double pixelSize)
		{
			// 0 -> 0.5
			// 0.1 -> 0.5
			// 0.5 -> 0.5
			// 0.9 -> 0.5
			// 1 -> 1.5
			return pixelSize * (Math.Round((value / pixelSize) + 0.5) - 0.5);
		}
		
		/// <summary>
		/// Aligns the borders of rect on the middles of pixels.
		/// </summary>
		public static Rect PixelAlign(Rect rect, Size pixelSize)
		{
			rect.X = PixelAlign(rect.X, pixelSize.Width);
			rect.Y = PixelAlign(rect.Y, pixelSize.Height);
			rect.Width = Round(rect.Width, pixelSize.Width);
			rect.Height = Round(rect.Height, pixelSize.Height);
			return rect;
		}
		
		/// <summary>
		/// Rounds <paramref name="point"/> to whole number of pixels.
		/// </summary>
		public static Point Round(Point point, Size pixelSize)
		{
			return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
		}
		
		/// <summary>
		/// Rounds val to whole number of pixels.
		/// </summary>
		public static Rect Round(Rect rect, Size pixelSize)
		{
			return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
			                Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
		}
		
		/// <summary>
		/// Rounds <paramref name="value"/> to a whole number of pixels.
		/// </summary>
		public static double Round(double value, double pixelSize)
		{
			return pixelSize * Math.Round(value / pixelSize);
		}
		
		/// <summary>
		/// Rounds <paramref name="value"/> to an whole odd number of pixels.
		/// </summary>
		public static double RoundToOdd(double value, double pixelSize)
		{
			return Round(value - pixelSize, pixelSize * 2) + pixelSize;
		}
	}
}
lTimePaused}" materialDesign:HintAssist.Hint="Filter" /> </StackPanel> </Grid> </Border> <Grid Grid.Row="1" Background="{StaticResource TransparentBackgroundBrush400}"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Grid> <Grid> <Grid> <DataGrid x:Name="grid" Background="Transparent" RenderOptions.EdgeMode="Aliased" RenderOptions.BitmapScalingMode="LowQuality" MaxWidth="{Binding RelativeSource={RelativeSource AncestorType=Grid},Path=ActualWidth}" AutoGenerateColumns="False" SelectionMode="Single" SelectedCellsChanged="grid_SelectedCellsChanged" ItemsSource="{Binding LogsViewSource}" SelectedItem="{Binding SelectedLog}" RowHeight="40" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="True" IsReadOnly="True"> <DataGrid.RowStyle> <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="Foreground" Value="{StaticResource AccentColorBrush}" /> <Setter Property="Cursor" Value="Hand"></Setter> </Trigger> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Transparent"></Setter> </Trigger> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="Transparent"></Setter> </Trigger> <DataTrigger Binding="{Binding CallerMethodName}" Value="OnStartup"> <Setter Property="Background" Value="{StaticResource AccentColorBrush}"></Setter> <Setter Property="Foreground" Value="White" /> <Setter Property="FontWeight" Value="SemiBold"></Setter> <Setter Property="Cursor" Value="Arrow"></Setter> </DataTrigger> <DataTrigger Binding="{Binding LogObject}" Value="External Bridge"> <Setter Property="Foreground" Value="#28A805" /> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowStyle> <DataGrid.CellStyle> <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="VerticalContentAlignment" Value="Center"></Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Transparent"></Setter> </Trigger> <DataTrigger Binding="{Binding CallerMethodName}" Value="OnStartup"> <Setter Property="Background" Value="{StaticResource AccentColorBrush}"></Setter> <Setter Property="Foreground" Value="White" /> <Setter Property="FontWeight" Value="SemiBold"></Setter> <Setter Property="Cursor" Value="Arrow"></Setter> </DataTrigger> </Style.Triggers> </Style> </DataGrid.CellStyle> <DataGrid.Columns> <DataGridTemplateColumn Header="#"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <materialDesign:PackIcon Width="16" Height="16"> <materialDesign:PackIcon.Style> <Style TargetType="materialDesign:PackIcon"> <Setter Property="Kind" Value="Alert"></Setter> <Style.Triggers> <DataTrigger Binding="{Binding Category}" Value="Info"> <Setter Property="Kind" Value="Information"></Setter> <Setter Property="Foreground" Value="DimGray"></Setter> </DataTrigger> <DataTrigger Binding="{Binding Category}" Value="Warning"> <Setter Property="Kind" Value="Alert"></Setter> <Setter Property="Foreground" Value="{StaticResource OrangeBrush}"></Setter> </DataTrigger> <DataTrigger Binding="{Binding Category}" Value="Error"> <Setter Property="Kind" Value="AlertOctagon"></Setter> <Setter Property="Foreground" Value="{StaticResource RedBrush100}"></Setter> </DataTrigger> <DataTrigger Binding="{Binding Category}" Value="Critical"> <Setter Property="Kind" Value="BellPlus"></Setter> <Setter Property="Foreground" Value="Red"></Setter> </DataTrigger> <DataTrigger Binding="{Binding Category}" Value="Debug"> <Setter Property="Kind" Value="Bug"></Setter> <Setter Property="Foreground" Value="{StaticResource GrayBrush300}"></Setter> </DataTrigger> <DataTrigger Binding="{Binding CallerMethodName}" Value="OnStartup"> <Setter Property="Kind" Value="ClockFast"></Setter> <Setter Property="Foreground" Value="White"></Setter> </DataTrigger> </Style.Triggers> </Style> </materialDesign:PackIcon.Style> </materialDesign:PackIcon> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="DATE TIME" Binding="{Binding TimeStamp,StringFormat='MM/dd/yyyy HH:mm:ss.ff'}" /> <DataGridTextColumn Header="FILE" Binding="{Binding RelativeCallerFile}" /> <DataGridTextColumn Header="METHOD" Binding="{Binding CallerMethodName}" /> <DataGridTextColumn Header="LINE" Binding="{Binding CallerLineNumber}" /> <DataGridTemplateColumn Header="MESSAGE" Width="1*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Message}" TextTrimming="CharacterEllipsis"></TextBlock> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Grid> </Grid> </Grid> </Grid> </Grid> </Grid> </UserControl>