A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Drawing;
6using System.Linq;
7
8namespace osu.Framework.Platform
9{
10 /// <summary>
11 /// Represents a physical display device on the current system.
12 /// </summary>
13 public sealed class Display : IEquatable<Display>
14 {
15 /// <summary>
16 /// The name of the display, if available. Usually the manufacturer.
17 /// </summary>
18 public string Name { get; }
19
20 /// <summary>
21 /// The current rectangle of the display in screen space.
22 /// Non-zero X and Y values represent a non-primary monitor, and indicate its position
23 /// relative to the primary monitor.
24 /// </summary>
25 public Rectangle Bounds { get; }
26
27 /// <summary>
28 /// The available <see cref="DisplayMode"/>s on this display.
29 /// </summary>
30 public DisplayMode[] DisplayModes { get; }
31
32 /// <summary>
33 /// The zero-based index of the <see cref="Display"/>.
34 /// </summary>
35 public int Index { get; }
36
37 public Display(int index, string name, Rectangle bounds, DisplayMode[] displayModes)
38 {
39 Index = index;
40 Name = name;
41 Bounds = bounds;
42 DisplayModes = displayModes;
43 }
44
45 public override string ToString() => $"Name: {Name ?? "Unknown"}, Bounds: {Bounds}, DisplayModes: {DisplayModes.Length}";
46
47 public bool Equals(Display other)
48 {
49 if (ReferenceEquals(null, other)) return false;
50 if (ReferenceEquals(this, other)) return true;
51
52 return Index == other.Index;
53 }
54
55 /// <summary>
56 /// Attempts to find a <see cref="DisplayMode"/> for the given <see cref="Display"/> that
57 /// closely matches the requested parameters.
58 /// </summary>
59 /// <param name="size">The <see cref="Size"/> to match.</param>
60 /// <param name="bitsPerPixel">The bits per pixel to match. If null, the highest available bits per pixel will be used.</param>
61 /// <param name="refreshRate">The refresh rate in hertz. If null, the highest available refresh rate will be used.</param>
62 public DisplayMode FindDisplayMode(Size size, int? bitsPerPixel = null, int? refreshRate = null) =>
63 DisplayModes.Where(mode => mode.Size.Width <= size.Width && mode.Size.Height <= size.Height &&
64 (bitsPerPixel == null || mode.BitsPerPixel == bitsPerPixel) &&
65 (refreshRate == null || mode.RefreshRate == refreshRate))
66 .OrderByDescending(mode => mode.Size.Width)
67 .ThenByDescending(mode => mode.Size.Height)
68 .ThenByDescending(mode => mode.RefreshRate)
69 .ThenByDescending(mode => mode.BitsPerPixel)
70 .FirstOrDefault();
71 }
72}