DisplayMagician/DisplayMagicianShared/UserControls/DisplayView.cs

248 lines
9.7 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.Drawing;
using System.Windows.Forms;
using DisplayMagicianShared.Topology;
2017-02-26 19:23:31 +00:00
namespace DisplayMagicianShared.UserControls
2017-02-26 19:23:31 +00:00
{
public partial class DisplayView : UserControl
{
private ProfileItem _profile;
2017-02-26 19:23:31 +00:00
public DisplayView()
{
InitializeComponent();
ResizeRedraw = true;
}
public int PaddingX { get; set; } = 100;
public int PaddingY { get; set; } = 100;
public ProfileItem Profile
2017-02-26 19:23:31 +00:00
{
2018-10-20 00:27:25 +00:00
get => _profile;
2017-02-26 19:23:31 +00:00
set
{
_profile = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (_profile != null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
DrawView(e.Graphics);
2018-10-20 00:27:25 +00:00
}
else
{
DrawEmptyView(e.Graphics);
}
2017-02-26 19:23:31 +00:00
}
2020-10-07 07:08:36 +00:00
private void DrawPath(Graphics g, Path path)
2017-02-26 19:23:31 +00:00
{
var res = ProfileIcon.NormalizeResolution(path);
var rect = new Rectangle(path.Position, res);
g.FillRectangle(new SolidBrush(Color.FromArgb(15, Color.White)), rect);
g.DrawRectangle(Pens.Black, rect);
DrawString(g, path.Position.IsEmpty ? "[Primary]" : $"[{path.Position.X}, {path.Position.Y}]", rect.Size,
2018-10-20 00:27:25 +00:00
new PointF(rect.X + PaddingY / 2, rect.Y + PaddingY / 2), StringAlignment.Near, StringAlignment.Near);
2017-02-26 19:23:31 +00:00
var str = $"DISPLAY #{path.SourceId + 1}{Environment.NewLine}{rect.Width}×{rect.Height}";
2018-10-20 00:27:25 +00:00
var strSize = DrawString(g, str, rect.Size, new PointF(rect.X - PaddingX / 2, rect.Y + PaddingY / 2),
2017-02-26 19:23:31 +00:00
StringAlignment.Near, StringAlignment.Far);
2020-05-12 10:46:23 +00:00
var rows = rect.Width < rect.Height ? path.TargetDisplays.Length : 1;
var cols = rect.Width >= rect.Height ? path.TargetDisplays.Length : 1;
2018-10-20 00:27:25 +00:00
2020-05-12 10:46:23 +00:00
for (var i = 0; i < path.TargetDisplays.Length; i++)
2018-10-20 00:27:25 +00:00
{
2020-05-12 10:46:23 +00:00
DrawTarget(g, path, path.TargetDisplays[i],
2018-10-20 00:27:25 +00:00
new Rectangle(rect.X + PaddingX, rect.Y + strSize.Height + PaddingY, rect.Width - 2 * PaddingX,
rect.Height - strSize.Height - 2 * PaddingY),
2017-02-26 19:23:31 +00:00
rows > 1 ? i : 0, cols > 1 ? i : 0, rows, cols);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
private Size DrawString(
Graphics g,
string str,
SizeF drawingSize = default(SizeF),
PointF? drawingPoint = null,
StringAlignment vertical = StringAlignment.Center,
2017-02-26 19:23:31 +00:00
StringAlignment horizontal = StringAlignment.Center)
{
var format = new StringFormat(StringFormat.GenericTypographic)
{
Alignment = horizontal,
LineAlignment = vertical,
FormatFlags = StringFormatFlags.NoClip
};
var stringSize = g.MeasureString(str, Font, drawingSize, format);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (drawingPoint != null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
g.DrawString(str, Font, new SolidBrush(ForeColor), new RectangleF(drawingPoint.Value, drawingSize),
format);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
return new Size((int) stringSize.Width, (int) stringSize.Height);
}
2020-10-07 07:08:36 +00:00
private void DrawSurroundTopology(Graphics g, PathTarget target, Rectangle rect)
2017-02-26 19:23:31 +00:00
{
g.DrawRectangle(Pens.Black, rect);
2018-10-20 00:27:25 +00:00
var targetSize = new Size(rect.Width / target.SurroundTopology.Columns,
rect.Height / target.SurroundTopology.Rows);
2017-02-26 19:23:31 +00:00
for (var i = 0; i < target.SurroundTopology.Displays.Length; i++)
{
var display = target.SurroundTopology.Displays[i];
2018-10-20 00:27:25 +00:00
var row = i / target.SurroundTopology.Columns;
var col = i % target.SurroundTopology.Columns;
var targetPosition = new Point(targetSize.Width * col + rect.X, targetSize.Height * row + rect.Y);
2017-02-26 19:23:31 +00:00
var targetRect = new Rectangle(targetPosition, targetSize);
g.DrawRectangle(Pens.Black, targetRect);
switch (display.Rotation)
{
case Rotation.Rotate90:
DrawString(g, "90°", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X - PaddingX / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-02-26 19:23:31 +00:00
StringAlignment.Far);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
break;
case Rotation.Rotate180:
DrawString(g, "180°", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X - PaddingX / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-02-26 19:23:31 +00:00
StringAlignment.Far);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
break;
case Rotation.Rotate270:
DrawString(g, "270°", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X - PaddingX / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-02-26 19:23:31 +00:00
StringAlignment.Far);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
break;
}
if (!display.Overlap.IsEmpty)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
DrawString(g, $"[{-display.Overlap.X}, {-display.Overlap.Y}]", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X + PaddingY / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-08-10 14:21:45 +00:00
StringAlignment.Near);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
// Invert to real monitor resolution
var res = ProfileIcon.NormalizeResolution(target.SurroundTopology.Resolution, display.Rotation);
var str = $"{display.DisplayName}{Environment.NewLine}{res.Width}×{res.Height}";
DrawString(g, str, targetRect.Size, targetRect.Location);
}
}
2018-10-20 00:27:25 +00:00
private void DrawTarget(
Graphics g,
2020-10-07 07:08:36 +00:00
Path path,
PathTarget target,
2018-10-20 00:27:25 +00:00
Rectangle rect,
int row,
int col,
int rows,
2017-02-26 19:23:31 +00:00
int cols)
{
2018-10-20 00:27:25 +00:00
var targetSize = new Size(rect.Width / cols, rect.Height / rows);
var targetPosition = new Point(targetSize.Width * col + rect.X, targetSize.Height * row + rect.Y);
2017-02-26 19:23:31 +00:00
var targetRect = new Rectangle(targetPosition, targetSize);
if (target.SurroundTopology != null)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
g.FillRectangle(new SolidBrush(Color.FromArgb(150, 106, 185, 0)), targetRect);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
//else if (target.EyefinityTopology != null)
// g.FillRectangle(new SolidBrush(Color.FromArgb(150, 99, 0, 0)), targetRect);
2020-05-12 10:46:23 +00:00
else if (path.TargetDisplays.Length > 1)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
g.FillRectangle(new SolidBrush(Color.FromArgb(150, 255, 97, 27)), targetRect);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
else if (path.Position == Point.Empty)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
g.FillRectangle(new SolidBrush(Color.FromArgb(150, 0, 174, 241)), targetRect);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
else
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 155, 155, 155)), targetRect);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
g.DrawRectangle(Pens.Black, targetRect);
var str = $"{target.DisplayName}{Environment.NewLine}{path.Resolution.Width}×{path.Resolution.Height}";
switch (target.Rotation)
{
case Rotation.Rotate90:
DrawString(g, "90°", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X - PaddingX / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-02-26 19:23:31 +00:00
StringAlignment.Far);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
break;
case Rotation.Rotate180:
DrawString(g, "180°", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X - PaddingX / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-02-26 19:23:31 +00:00
StringAlignment.Far);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
break;
case Rotation.Rotate270:
DrawString(g, "270°", targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X - PaddingX / 2, targetRect.Y + PaddingY / 2), StringAlignment.Near,
2017-02-26 19:23:31 +00:00
StringAlignment.Far);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
break;
}
if (target.SurroundTopology != null)
{
var strSize = DrawString(g, str, targetRect.Size,
2018-10-20 00:27:25 +00:00
new PointF(targetRect.X + PaddingX / 2, targetRect.Y + PaddingY / 2),
2017-02-26 19:23:31 +00:00
StringAlignment.Near, StringAlignment.Near);
DrawSurroundTopology(g, target,
new Rectangle(
targetRect.X + PaddingX,
targetRect.Y + strSize.Height + PaddingY,
2018-10-20 00:27:25 +00:00
targetRect.Width - 2 * PaddingX,
targetRect.Height - strSize.Height - 2 * PaddingY));
2017-02-26 19:23:31 +00:00
}
else
{
DrawString(g, str, targetRect.Size, targetRect.Location);
}
}
private void DrawView(Graphics g)
{
2020-10-07 07:08:36 +00:00
var viewSize = ProfileIcon.CalculateViewSize(_profile.Paths, true, PaddingX, PaddingY);
2018-10-20 00:27:25 +00:00
var factor = Math.Min(Width / viewSize.Width, Height / viewSize.Height);
2017-02-26 19:23:31 +00:00
g.ScaleTransform(factor, factor);
2018-10-20 00:27:25 +00:00
var xOffset = (Width / factor - viewSize.Width) / 2f;
var yOffset = (Height / factor - viewSize.Height) / 2f;
2017-02-26 19:23:31 +00:00
g.TranslateTransform(-viewSize.X + xOffset, -viewSize.Y + yOffset);
2020-10-07 07:08:36 +00:00
foreach (var path in _profile.Paths)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
DrawPath(g, path);
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
private void DrawEmptyView(Graphics g)
{
RectangleF rect = g.VisibleClipBounds;
g.FillRectangle(new SolidBrush(Color.FromArgb(15, Color.White)), rect);
g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height);
}
2017-02-26 19:23:31 +00:00
}
}