1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-25 07:35:36 +01:00
GiantsTools/Giants.Launcher/Forms/OptionsForm.cs

241 lines
7.8 KiB
C#
Raw Normal View History

2020-08-10 06:58:51 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-08-10 08:24:50 +02:00
using System.Windows.Forms;
2020-08-10 06:58:51 +02:00
namespace Giants.Launcher
{
2020-08-10 08:24:50 +02:00
public partial class OptionsForm : Form
2020-08-10 06:58:51 +02:00
{
2020-08-10 08:24:50 +02:00
private readonly string gamePath = null;
2020-08-10 06:58:51 +02:00
public OptionsForm(string title, string gamePath)
{
this.InitializeComponent();
2020-08-10 06:58:51 +02:00
this.Text = title;
2020-08-10 08:24:50 +02:00
this.gamePath = gamePath;
2020-08-10 06:58:51 +02:00
}
private void OptionsForm_Load(object sender, EventArgs e)
{
// Must come first as other options depend on it
this.PopulateRenderers();
this.PopulateResolution();
this.PopulateAnisotropy();
this.PopulateAntialiasing();
this.SetOptions();
2020-08-10 06:58:51 +02:00
}
private void PopulateRenderers()
{
this.cmbRenderer.Items.Clear();
this.cmbRenderer.Items.AddRange(
GameSettings.CompatibleRenderers
.Disambiguate()
.ToList()
.ToArray());
}
2020-08-10 06:58:51 +02:00
private void SetOptions()
{
var resolutions = (List<ScreenResolution>)this.cmbResolution.DataSource;
this.cmbResolution.SelectedItem = resolutions.Find(r => r.Width == GameSettings.Get<int>(RegistryKeys.VideoWidth) && r.Height == GameSettings.Get<int>(RegistryKeys.VideoHeight));
if (this.cmbResolution.SelectedItem == null)
this.cmbResolution.SelectedIndex = 0;
2020-08-10 06:58:51 +02:00
var antialiasingOptions = (List<KeyValuePair<string, int>>)this.cmbAntialiasing.DataSource;
this.cmbAntialiasing.SelectedItem = antialiasingOptions.Find(o => o.Value == GameSettings.Get<int>(RegistryKeys.Antialiasing));
if (this.cmbAntialiasing.SelectedItem == null)
this.cmbAntialiasing.SelectedIndex = 0;
2020-08-10 06:58:51 +02:00
this.chkUpdates.Checked = GameSettings.Get<int>(RegistryKeys.NoAutoUpdate) != 1;
RendererInfo renderer = GameSettings.CompatibleRenderers.Find(
r => StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(r.FilePath), GameSettings.Get<string>(RegistryKeys.Renderer)) == 0);
if (renderer != null)
{
this.cmbRenderer.SelectedItem = renderer;
}
else
{
renderer = GameSettings.CompatibleRenderers.Find(r => r.FileName == "gg_dx7r.dll");
this.cmbRenderer.SelectedItem = renderer;
}
2020-08-10 06:58:51 +02:00
}
private void PopulateAntialiasing()
{
var antialiasingOptions = new List<KeyValuePair<string, int>>();
antialiasingOptions.Add(new KeyValuePair<string, int>(Resources.OptionNone, 0));
2020-08-10 06:58:51 +02:00
var renderer = (RendererInfo)this.cmbRenderer.SelectedItem;
2020-08-10 06:58:51 +02:00
if (renderer != null)
{
if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA2x))
{
antialiasingOptions.Add(new KeyValuePair<string, int>(string.Format(Resources.OptionSamples, 2), 2));
}
if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA4x))
{
antialiasingOptions.Add(new KeyValuePair<string, int>(string.Format(Resources.OptionSamples, 4), 4));
}
if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA8x))
{
antialiasingOptions.Add(new KeyValuePair<string, int>(string.Format(Resources.OptionSamples, 8), 8));
}
if (renderer.Flags.HasFlag(RendererInfo.RendererFlag.MSAA16x))
{
antialiasingOptions.Add(new KeyValuePair<string, int>(string.Format(Resources.OptionSamples, 16), 16));
}
2020-08-10 06:58:51 +02:00
}
// Try to keep current selection when repopulating
int? currentValue = null;
if (this.cmbAntialiasing.SelectedValue != null)
2020-08-10 06:58:51 +02:00
{
currentValue = (int)this.cmbAntialiasing.SelectedValue;
2020-08-10 06:58:51 +02:00
}
this.cmbAntialiasing.DataSource = antialiasingOptions;
this.cmbAntialiasing.DisplayMember = "Key";
this.cmbAntialiasing.ValueMember = "Value";
2020-08-10 06:58:51 +02:00
if (currentValue != null)
this.cmbAntialiasing.SelectedValue = currentValue;
2020-08-10 06:58:51 +02:00
if (this.cmbAntialiasing.SelectedValue == null)
this.cmbAntialiasing.SelectedIndex = 0;
2020-08-10 06:58:51 +02:00
}
private bool IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
private void PopulateAnisotropy()
{
var anisotropyOptions = new List<KeyValuePair<string, int>>();
anisotropyOptions.Add(new KeyValuePair<string, int>(Resources.OptionNone, 0));
2020-08-10 06:58:51 +02:00
var renderer = (RendererInfo)this.cmbRenderer.SelectedItem;
2020-08-10 06:58:51 +02:00
if (renderer != null)
{
for (int i = 2; i <= renderer.MaxAnisotropy; i++)
{
if (!this.IsPowerOfTwo(i)) continue;
2020-08-10 06:58:51 +02:00
anisotropyOptions.Add(new KeyValuePair<string,int>(string.Format(Resources.OptionSamples, i), i));
2020-08-10 06:58:51 +02:00
}
}
}
private void PopulateResolution()
{
List<ScreenResolution> resolutions = new List<ScreenResolution>();
NativeMethods.DEVMODE devMode = new NativeMethods.DEVMODE();
int i = 0;
while (NativeMethods.EnumDisplaySettings(null, i, ref devMode))
{
if (devMode.dmBitsPerPel == 32 && devMode.dmPelsWidth >= 640 && devMode.dmPelsHeight >= 480)
{
if (resolutions.Find(r => r.Width == devMode.dmPelsWidth && r.Height == devMode.dmPelsHeight) == null)
resolutions.Add(new ScreenResolution(devMode.dmPelsWidth, devMode.dmPelsHeight));
}
i++;
}
resolutions.Sort();
this.cmbResolution.DataSource = resolutions;
2020-08-10 06:58:51 +02:00
}
private void cmbRenderer_SelectedIndexChanged(object sender, EventArgs e)
{
bool windowed = GameSettings.Get<int>(RegistryKeys.Windowed) == 1;
2020-08-10 06:58:51 +02:00
if (windowed)
{
bool borderless = GameSettings.Get<int>(RegistryKeys.BorderlessWindow) == 1;
2020-08-10 06:58:51 +02:00
if (borderless)
this.cmbMode.SelectedIndex = 2;
2020-08-10 06:58:51 +02:00
else
this.cmbMode.SelectedIndex = 1;
2020-08-10 06:58:51 +02:00
}
else
this.cmbMode.SelectedIndex = 0;
2020-08-10 06:58:51 +02:00
var renderer = (RendererInfo)this.cmbRenderer.SelectedItem;
2020-08-10 06:58:51 +02:00
if ((renderer.Flags & RendererInfo.RendererFlag.VSync) != RendererInfo.RendererFlag.VSync)
2020-08-10 06:58:51 +02:00
{
this.chkVSync.Checked = false;
this.chkVSync.Enabled = false;
2020-08-10 06:58:51 +02:00
}
else
{
this.chkVSync.Checked = GameSettings.Get<int>(RegistryKeys.VerticalSync) == 1;
this.chkVSync.Enabled = true;
2020-08-10 06:58:51 +02:00
}
if ((renderer.Flags & RendererInfo.RendererFlag.TripleBuffer) != RendererInfo.RendererFlag.TripleBuffer)
2020-08-10 06:58:51 +02:00
{
this.chkTripleBuffering.Checked = false;
this.chkTripleBuffering.Enabled = false;
2020-08-10 06:58:51 +02:00
}
else
{
this.chkTripleBuffering.Checked = GameSettings.Get<int>(RegistryKeys.TripleBuffering) == 1;
this.chkTripleBuffering.Enabled = true;
2020-08-10 06:58:51 +02:00
}
this.PopulateAntialiasing();
this.PopulateAnisotropy();
2020-08-10 06:58:51 +02:00
}
private void btnOK_Click(object sender, EventArgs e)
{
var renderer = this.cmbRenderer.SelectedItem as RendererInfo;
2020-08-10 06:58:51 +02:00
if (renderer != null)
{
GameSettings.Modify(RegistryKeys.Renderer, renderer.FileName);
2020-08-10 06:58:51 +02:00
}
var resolution = (ScreenResolution)this.cmbResolution.SelectedItem;
2020-08-10 06:58:51 +02:00
if (resolution != null)
{
GameSettings.Modify(RegistryKeys.VideoWidth, resolution.Width);
GameSettings.Modify(RegistryKeys.VideoHeight, resolution.Height);
2020-08-10 06:58:51 +02:00
}
GameSettings.Modify(RegistryKeys.Antialiasing, this.cmbAntialiasing.SelectedValue);
bool windowed = (WindowType)this.cmbMode.SelectedIndex == WindowType.Windowed || (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless;
GameSettings.Modify(RegistryKeys.Windowed, windowed == true ? 1 : 0);
bool borderless = (WindowType)this.cmbMode.SelectedIndex == WindowType.Borderless;
GameSettings.Modify(RegistryKeys.BorderlessWindow, borderless == true ? 1 : 0);
GameSettings.Modify(RegistryKeys.VerticalSync, this.chkVSync.Checked == true ? 1 : 0);
GameSettings.Modify(RegistryKeys.TripleBuffering, this.chkTripleBuffering.Checked == true ? 1 : 0);
GameSettings.Modify(RegistryKeys.NoAutoUpdate, this.chkUpdates.Checked == false ? 1 : 0);
2020-08-10 06:58:51 +02:00
GameSettings.Save();
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
GameSettings.Load(this.gamePath);
2020-08-10 06:58:51 +02:00
}
private void btnResetDefaults_Click(object sender, EventArgs e)
{
GameSettings.SetDefaults(this.gamePath);
this.SetOptions();
2020-08-10 06:58:51 +02:00
}
}
}