Skip to content

Commit

Permalink
Implemented flip feature for screen mirror (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
StijnOostdam authored Aug 10, 2020
1 parent 30998d6 commit 22cf3fa
Show file tree
Hide file tree
Showing 20 changed files with 801 additions and 593 deletions.
10 changes: 10 additions & 0 deletions Winleafs.Models/Enums/ScreenMirrorFlip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Winleafs.Models.Enums
{
public enum ScreenMirrorFlip
{
None = 0,
Horizontal = 1,
Vertical = 2,
HorizontalVertical = 3
}
}
2 changes: 2 additions & 0 deletions Winleafs.Models/Models/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class Device

public ScreenMirrorAlgorithm ScreenMirrorAlgorithm { get; set; }

public ScreenMirrorFlip ScreenMirrorFlip { get; set; }

public Device()
{
Effects = new List<Effect>();
Expand Down
15 changes: 13 additions & 2 deletions Winleafs.Models/Models/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ namespace Winleafs.Models.Models
public class UserSettings
{
public static readonly string APPLICATIONNAME = "Winleafs";
public static readonly string APPLICATIONVERSION = "v1.1.0";
public static readonly string APPLICATIONVERSION = "v1.1.1";

public static readonly string SettingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), APPLICATIONNAME);

public static readonly string CustomColorNamePreface = "Custom Color - ";
public static readonly string EffectNamePreface = "Winleafs - ";

private static readonly string _settingsFileName = Path.Combine(SettingsFolder, "Settings.txt");
private static readonly string _latestSettingsVersion = "12";
private static readonly string _latestSettingsVersion = "13";

private static UserSettings _settings { get; set; }

Expand Down Expand Up @@ -476,6 +476,17 @@ private static JToken Migration_11_12(JToken jToken)

return jToken;
}

[Migration("12", "13")]
private static JToken Migration_12_13(JToken jToken)
{
foreach (var device in jToken["Devices"])
{
device[nameof(Device.ScreenMirrorFlip)] = (int)ScreenMirrorFlip.None;
}

return jToken;
}
#endregion
}
}
2 changes: 1 addition & 1 deletion Winleafs.Setup/Product.wxs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" UpgradeCode="42ee9157-4382-4852-bed7-72b25fb2d0bf" Version="1.1.0" Language="1033" Name="Winleafs" Manufacturer="Stijn Oostdam">
<Product Id="*" UpgradeCode="42ee9157-4382-4852-bed7-72b25fb2d0bf" Version="1.1.1" Language="1033" Name="Winleafs" Manufacturer="Stijn Oostdam">
<Package Compressed="yes" />
<Media Id="1" Cabinet="myapplication.cab" EmbedCab="yes" />

Expand Down
1,098 changes: 549 additions & 549 deletions Winleafs.Setup/Winleafs.wxs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Winleafs.Wpf/Api/Effects/ScreenMirrorEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ public ScreenMirrorEffect(Orchestrator orchestrator, INanoleafClient nanoleafCli
_nanoleafClient = nanoleafClient;
_device = orchestrator.Device;
_screenMirrorAlgorithm = orchestrator.Device.ScreenMirrorAlgorithm;
var flipType = FlipTypeHelper.ScreenMirrorFlipToFlipType(orchestrator.Device.ScreenMirrorFlip);

try
{
if (_screenMirrorAlgorithm == ScreenMirrorAlgorithm.ScreenMirrorFit)
{
_screenMirrorEffect = new ScreenMirror(orchestrator, nanoleafClient, ScaleType.Fit);
_screenMirrorEffect = new ScreenMirror(orchestrator, nanoleafClient, ScaleType.Fit, flipType);
}
else if (_screenMirrorAlgorithm == ScreenMirrorAlgorithm.ScreenMirrorStretch)
{
_screenMirrorEffect = new ScreenMirror(orchestrator, nanoleafClient, ScaleType.Stretch);
_screenMirrorEffect = new ScreenMirror(orchestrator, nanoleafClient, ScaleType.Stretch, flipType);
}
else if (_screenMirrorAlgorithm == ScreenMirrorAlgorithm.Ambilight)
{
Expand Down
6 changes: 3 additions & 3 deletions Winleafs.Wpf/Api/Effects/ScreenMirrorEffects/ScreenMirror.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class ScreenMirror : IScreenMirrorEffect

private readonly DeviceType _deviceType;

public ScreenMirror(Orchestrator orchestrator, INanoleafClient nanoleafClient, ScaleType scaleType)
public ScreenMirror(Orchestrator orchestrator, INanoleafClient nanoleafClient, ScaleType scaleType, FlipType flipType)
{
_externalControlEndpoint = nanoleafClient.ExternalControlEndpoint;
_panels = new List<ScreenMirrorPanel>();
_deviceType = orchestrator.PanelLayout.DeviceType;

var screenBounds = ScreenBoundsHelper.GetScreenBounds(UserSettings.Settings.ScreenMirrorMonitorIndex);
var panels = orchestrator.PanelLayout.GetScaledPolygons(screenBounds.Width, screenBounds.Height, scaleType);
var panels = orchestrator.PanelLayout.GetScaledPolygons(screenBounds.Width, screenBounds.Height, scaleType, flipType);

switch (_deviceType)
{
Expand All @@ -39,7 +39,7 @@ public ScreenMirror(Orchestrator orchestrator, INanoleafClient nanoleafClient, S
LoadPanelsForSquares(screenBounds, panels);
break;
default:
throw new NotImplementedException($"Screen mirror constructor for device of type {orchestrator.PanelLayout.DeviceType.ToString()} not implemented");
throw new NotImplementedException($"Screen mirror constructor for device of type {orchestrator.PanelLayout.DeviceType} not implemented");
}
}

Expand Down
33 changes: 33 additions & 0 deletions Winleafs.Wpf/Api/Layouts/FlipType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Winleafs.Models.Enums;

namespace Winleafs.Wpf.Api.Layouts
{
public enum FlipType
{
None = 0,
Horizontal = 1,
Vertical = 2,
HorizontalVertical = 3
}

public static class FlipTypeHelper
{
public static FlipType ScreenMirrorFlipToFlipType(ScreenMirrorFlip screenMirrorFlip)
{
switch (screenMirrorFlip)
{
case ScreenMirrorFlip.None:
return FlipType.None;
case ScreenMirrorFlip.Horizontal:
return FlipType.Horizontal;
case ScreenMirrorFlip.Vertical:
return FlipType.Vertical;
case ScreenMirrorFlip.HorizontalVertical:
return FlipType.HorizontalVertical;
default:
throw new NotImplementedException($"Conversion for {nameof(ScreenMirrorFlip)}.{screenMirrorFlip} not implemented.");
}
}
}
}
34 changes: 31 additions & 3 deletions Winleafs.Wpf/Api/Layouts/PanelLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void CreatePolygon(double x, double y, double rotation, int panelId,
/// Returns the panels represented as <see cref="DrawablePanel"/>s scaled to fit the desired width and height.
/// Returns null if there is no layout
/// </summary>
public List<DrawablePanel> GetScaledPolygons(int width, int height, ScaleType scaleType = ScaleType.Fit)
public List<DrawablePanel> GetScaledPolygons(int width, int height, ScaleType scaleType, FlipType flipType)
{
if (_layout == null)
{
Expand Down Expand Up @@ -227,18 +227,46 @@ public List<DrawablePanel> GetScaledPolygons(int width, int height, ScaleType sc
var polygon = new Polygon();
foreach (var point in panel.Polygon.Points)
{
polygon.Points.Add(scaleTransform.Transform(point));
var scaledPoint = scaleTransform.Transform(point);
var flippedPoint = FlipTransform(scaledPoint, flipType, width, height);
polygon.Points.Add(flippedPoint);
}

var scaledMidPoint = scaleTransform.Transform(panel.MidPoint);
var flippedMidPoint = FlipTransform(scaledMidPoint, flipType, width, height);

scaledPolygons.Add(new DrawablePanel
{
MidPoint = scaleTransform.Transform(panel.MidPoint),
MidPoint = flippedMidPoint,
PanelId = panel.PanelId,
Polygon = polygon
});
}

return scaledPolygons;
}

private Point FlipTransform(Point point, FlipType flipType, double maxX, double maxY)
{
switch (flipType)
{
case FlipType.Horizontal:
point.X = maxX - point.X;
break;
case FlipType.Vertical:
point.Y = maxY - point.Y;
break;
case FlipType.HorizontalVertical:
point.X = maxX - point.X;
point.Y = maxY - point.Y;
break;
case FlipType.None:
break; //Do nothing
default:
throw new NotImplementedException($"Flipping for {nameof(FlipType)}.{flipType} not implemented.");
}

return point;
}
}
}
40 changes: 37 additions & 3 deletions Winleafs.Wpf/ViewModels/OptionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public class OptionsViewModel : INotifyPropertyChanged
#endregion

#region Screen mirror
public Dictionary<string, ScreenMirrorAlgorithm> AlgorithmPerDevice { get; set; }
public Dictionary<string, ScreenMirrorAlgorithm> ScreenMirrorAlgorithmPerDevice { get; set; }

public Dictionary<string, ScreenMirrorFlip> ScreenMirrorFlipPerDevice { get; set; }

private int _screenMirrorRefreshRatePerSecond;

Expand Down Expand Up @@ -67,7 +69,7 @@ public string SelectedScreenMirrorAlgorithm
{
_selectedScreenMirrorAlgorithm = ScreenMirrorAlgorithmMapping[value];

AlgorithmPerDevice[_selectedDevice] = _selectedScreenMirrorAlgorithm;
ScreenMirrorAlgorithmPerDevice[_selectedDevice] = _selectedScreenMirrorAlgorithm;

_parent.ScreenMirrorAlgorithmChanged(_selectedScreenMirrorAlgorithm);

Expand All @@ -77,6 +79,27 @@ public string SelectedScreenMirrorAlgorithm

public IEnumerable<string> ScreenMirrorAlgorithms => ScreenMirrorAlgorithmMapping.Keys;
#endregion

#region Screen mirror flip dropdown
//Map display values to enum values
public Dictionary<string, ScreenMirrorFlip> ScreenMirrorFlipMapping { get; set; }

private ScreenMirrorFlip _selectedScreenMirrorFlip;
public string SelectedScreenMirrorFlip
{
get { return EnumLocalizer.GetLocalizedEnum(_selectedScreenMirrorFlip); }
set
{
_selectedScreenMirrorFlip = ScreenMirrorFlipMapping[value];

ScreenMirrorFlipPerDevice[_selectedDevice] = _selectedScreenMirrorFlip;

OnPropertyChanged(nameof(SelectedScreenMirrorFlip));
}
}

public IEnumerable<string> ScreenMirrorFlips => ScreenMirrorFlipMapping.Keys;
#endregion
#endregion

#region Selected device dropdown
Expand All @@ -89,8 +112,11 @@ public string SelectedDevice
{
_selectedDevice = value;

var screenMirrorAlgorithm = AlgorithmPerDevice[_selectedDevice];
var screenMirrorAlgorithm = ScreenMirrorAlgorithmPerDevice[_selectedDevice];
SelectedScreenMirrorAlgorithm = ScreenMirrorAlgorithmMapping.FirstOrDefault(map => map.Value == screenMirrorAlgorithm).Key;

var screenMirrorFlip = ScreenMirrorFlipPerDevice[_selectedDevice];
SelectedScreenMirrorFlip = ScreenMirrorFlipMapping.FirstOrDefault(map => map.Value == screenMirrorFlip).Key;
}
}

Expand All @@ -107,6 +133,14 @@ public OptionsViewModel(OptionsWindow parent)
{ EnumLocalizer.GetLocalizedEnum(ScreenMirrorAlgorithm.ScreenMirrorFit), ScreenMirrorAlgorithm.ScreenMirrorFit },
{ EnumLocalizer.GetLocalizedEnum(ScreenMirrorAlgorithm.ScreenMirrorStretch), ScreenMirrorAlgorithm.ScreenMirrorStretch }
};

ScreenMirrorFlipMapping = new Dictionary<string, ScreenMirrorFlip>()
{
{ EnumLocalizer.GetLocalizedEnum(ScreenMirrorFlip.None), ScreenMirrorFlip.None },
{ EnumLocalizer.GetLocalizedEnum(ScreenMirrorFlip.Horizontal), ScreenMirrorFlip.Horizontal },
{ EnumLocalizer.GetLocalizedEnum(ScreenMirrorFlip.Vertical), ScreenMirrorFlip.Vertical },
{ EnumLocalizer.GetLocalizedEnum(ScreenMirrorFlip.HorizontalVertical), ScreenMirrorFlip.HorizontalVertical }
};
}

protected void OnPropertyChanged(string name)
Expand Down
40 changes: 38 additions & 2 deletions Winleafs.Wpf/Views/EnumResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions Winleafs.Wpf/Views/EnumResources.nl.resx
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,21 @@
<data name="Friday" xml:space="preserve">
<value>Vrijdag</value>
</data>
<data name="Horizontal" xml:space="preserve">
<value>Horizontaal</value>
</data>
<data name="HorizontalVertical" xml:space="preserve">
<value>Horizontaal &amp; verticaal</value>
</data>
<data name="Manual" xml:space="preserve">
<value>Handmatig</value>
</data>
<data name="Monday" xml:space="preserve">
<value>Maandag</value>
</data>
<data name="None" xml:space="preserve">
<value>Geen</value>
</data>
<data name="ProcessEvent" xml:space="preserve">
<value>Procesevent</value>
</data>
Expand All @@ -157,10 +166,10 @@
<value>Schema</value>
</data>
<data name="ScreenMirrorFit" xml:space="preserve">
<value>Scherm spiegelen passend</value>
<value>Passend</value>
</data>
<data name="ScreenMirrorStretch" xml:space="preserve">
<value>Scherm spiegelen rekken</value>
<value>Uitrekken</value>
</data>
<data name="SpotifyEvent" xml:space="preserve">
<value>Spotify event</value>
Expand All @@ -186,6 +195,9 @@
<data name="Unknown" xml:space="preserve">
<value>Onbekend</value>
</data>
<data name="Vertical" xml:space="preserve">
<value>Verticaal</value>
</data>
<data name="Wednesday" xml:space="preserve">
<value>Woensdag</value>
</data>
Expand Down
Loading

0 comments on commit 22cf3fa

Please sign in to comment.