VRDash/VDash/MainWindow.xaml.cs

180 lines
6.1 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Valve.VR;
using VDash.Overlay;
using VDash.Overlay.Config;
using VDash.UI;
using VDash.Utilities;
namespace VDash
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static OverlayManager OverlayManager = new OverlayManager();
public static ProfileManager ProfileManager = new ProfileManager();
public MainWindow()
{
Logger.Setup();
this.GetLogger().Info("VDash is starting up...");
InitializeComponent();
//Connect to VR runtime...
EVRInitError initError = EVRInitError.None;
var ret = OpenVR.Init(ref initError, EVRApplicationType.VRApplication_Overlay);
#if !DEBUG
if (initError != EVRInitError.None)
{
throw new Exception($"Error connecting to VR: {initError.ToString()}");
}
#endif
lvOverlays.ItemsSource = OverlayManager.Overlays;
KeyDown += (s, e) =>
{
if(e.Key == Key.F5)
{
ProfileManager.SaveProfile();
}
if(e.Key == Key.F6 || e.Key == Key.P)
{
var profile = ProfileManager.LoadProfile();
LoadProfile(profile);
}
};
#region Debug
//StartOverlayWindow("Discord");
//StartOverlayWindow("Microsoft Visual Studio");
//StartOverlayWindow("Notepad++");
//StartOverlayWindow("Putty");
//StartOverlayWindow("Paint");
//Overlay.WindowOverlay obsoverlay = new Overlay.WindowOverlay("obs", "obs", new WindowSelectors.FromProcessName("obs64"), false);
//obsoverlay.Create();
//obsoverlay.DestroyOnNoWindowFound = true;
//var bringWindow = new Overlay.Actions.BringWindowToFrontAction(true);
//obsoverlay.OverlayShownActions.Add(bringWindow);
//var displays = User32.GetDisplays();
//MonitorOverlay mo0 = new MonitorOverlay("left", "Left Monitor", displays[0]);
//mo0.Create();
//MonitorOverlay mo1 = new MonitorOverlay("center", "Center Monitor", displays[1]);
//mo1.Create();
//MonitorOverlay mo2= new MonitorOverlay("right", "Right Monitor", displays[2]);
//mo2.Create();
#endregion
}
private void LoadProfile(Profile profile)
{
//Dispose of all overlays and clear list.
foreach (var overlay in OverlayManager.Overlays)
{
overlay.Overlay.Dispose();
}
OverlayManager.Overlays.Clear();
var configs = profile.OverlayConfigs;
foreach (var config in configs)
{
//Get type of overlay based on config type
var attrs = OverlayInfo.GetOverlayInfos();
var overlayInfo = attrs.Single(x => x.Info.ConfigType == config.GetType());
Type overlayType = overlayInfo.OverlayType;
//Create overlay
IOverlay overlay = (IOverlay)Activator.CreateInstance(overlayType, new object[] { config });
overlay.Create();
UI.IOverlayEditor editor = (UI.IOverlayEditor)Activator.CreateInstance(overlayInfo.Info.EditorType, new object[] { overlay.OverlayConfig });
OverlayEditorInfo overlayEditor = new OverlayEditorInfo(new UI.OverlayEditor(editor), overlay);
OverlayManager.Overlays.Add(overlayEditor);
}
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Window w = new Window();
var uc = new UI.CreateNewOverlay();
uc.Created += (snd, overlay) => {
w.Close();
var attrs = OverlayInfo.GetOverlayInfos();
var overlayInfo = attrs.Single(x => x.OverlayType == overlay.GetType());
UI.IOverlayEditor editor = (UI.IOverlayEditor)Activator.CreateInstance(overlayInfo.Info.EditorType, new object[] { overlay.OverlayConfig });
OverlayEditorInfo overlayEditor = new OverlayEditorInfo(new UI.OverlayEditor(editor), overlay);
OverlayManager.Overlays.Add(overlayEditor);
};
w.Content = uc;
w.Width = 500;
w.Height = 400;
w.SizeToContent = System.Windows.SizeToContent.Height;
w.Show();
}
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
if(lvOverlays.SelectedItem == null) { return; }
OverlayEditorInfo overlayEditor = (OverlayEditorInfo)lvOverlays.SelectedItem;
overlayEditor.Overlay.Dispose();
OverlayManager.Overlays.Remove(overlayEditor);
if (bdrContent.Child == overlayEditor.Editor)
{
bdrContent.Child = null;
}
}
private void lvOverlays_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (lvOverlays.SelectedItem == null) { return; }
OverlayEditorInfo overlay = (OverlayEditorInfo)lvOverlays.SelectedItem;
bdrContent.Child = (UserControl)overlay.Editor;
}
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
var profile = ProfileManager.LoadProfile();
LoadProfile(profile);
}
}
public class EditorMap
{
public EditorMap(Type overlayType, Type editorType)
{
OverlayType = overlayType;
EditorType = editorType;
}
public Type OverlayType { get; private set; }
public Type EditorType { get; private set; }
}
}