2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-12-17 05:04:56 +01:00

WIP: observatory UI overhaul

This commit is contained in:
Xjph
2023-07-07 08:36:27 -02:30
parent 1d62a0ec1d
commit d99a190869
28 changed files with 1448 additions and 386 deletions

View File

@@ -2,10 +2,12 @@
using Observatory.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
namespace Observatory.UI
{
@@ -22,7 +24,7 @@ namespace Observatory.UI
_adjustPanelsBelow = adjustPanelsBelow;
// Filtered to only settings without SettingIgnore attribute
var settings = PluginManagement.PluginManager.GetSettingDisplayNames(plugin).Where(s => !Attribute.IsDefined(s.Key, typeof (SettingIgnore)));
var settings = PluginManagement.PluginManager.GetSettingDisplayNames(plugin.Settings).Where(s => !Attribute.IsDefined(s.Key, typeof (SettingIgnore)));
CreateControls(settings);
}
@@ -30,35 +32,300 @@ namespace Observatory.UI
private void CreateControls(IEnumerable<KeyValuePair<PropertyInfo, string>> settings)
{
int controlRow = 0;
bool nextColumn = true;
bool recentHalfCol = false;
// Handle bool (checkbox) settings first and keep them grouped together
foreach (var setting in settings.Where(s => s.Key.PropertyType == typeof(bool)))
foreach (var setting in settings)
{
CheckBox checkBox = new()
// Reset the column tracking for checkboxes if this isn't a checkbox
if (setting.Key.PropertyType.Name != "Boolean" && setting.Key.PropertyType.Name != "Button")
recentHalfCol = false;
switch (setting.Key.GetValue(_plugin.Settings))
{
Text = setting.Value,
Checked = (bool?)setting.Key.GetValue(_plugin.Settings) ?? false
};
case bool:
var checkBox = CreateBoolSetting(setting);
controlRow += recentHalfCol ? 0 : 1;
checkBox.Location = GetSettingPosition(controlRow, recentHalfCol);
checkBox.CheckedChanged += (object? _, EventArgs _) =>
{
setting.Key.SetValue(_plugin.Settings, checkBox.Checked);
PluginManagement.PluginManager.GetInstance.SaveSettings(_plugin, _plugin.Settings);
};
recentHalfCol = !recentHalfCol;
checkBox.Location = new Point(nextColumn ? 10 : 130, 3 + controlRow * 29);
controlRow += nextColumn ? 0 : 1;
nextColumn = !nextColumn;
Controls.Add(checkBox);
break;
case string:
var stringLabel = CreateSettingLabel(setting.Value);
var textBox = CreateStringSetting(setting.Key);
controlRow++;
stringLabel.Location = GetSettingPosition(controlRow);
textBox.Location = GetSettingPosition(controlRow, true);
Controls.Add(stringLabel);
Controls.Add(textBox);
Controls.Add(checkBox);
break;
case FileInfo:
var fileLabel = CreateSettingLabel(setting.Value);
var pathTextBox = CreateFilePathSetting(setting.Key);
var pathButton = CreateFileBrowseSetting(setting.Key, pathTextBox);
controlRow++;
fileLabel.Location = GetSettingPosition(controlRow);
pathTextBox.Location = GetSettingPosition(controlRow, true);
pathButton.Location = GetSettingPosition(++controlRow, true);
Controls.Add(fileLabel);
Controls.Add(pathTextBox);
Controls.Add(pathButton);
break;
case int:
// We have two options for integer values:
// 1) A slider (explicit by way of the SettingIntegerUseSlider attribute and bounded to 0..100 by default)
// 2) A numeric up/down (default otherwise, and is unbounded by default).
// Bounds for both can be set via the SettingNumericBounds attribute, only the up/down uses Increment.
var intLabel = CreateSettingLabel(setting.Value);
Control intControl;
controlRow++;
if (System.Attribute.IsDefined(setting.Key, typeof(SettingNumericUseSlider)))
{
intControl = CreateSettingTrackbar(setting.Key);
}
else
{
intControl = CreateSettingNumericUpDown(setting.Key);
}
intLabel.Location = GetSettingPosition(controlRow);
intControl.Location = GetSettingPosition(controlRow, true);
Controls.Add(intLabel);
Controls.Add(intControl);
break;
case Action action:
var button = CreateSettingButton(setting.Value, action);
controlRow += recentHalfCol ? 0 : 1;
button.Location = GetSettingPosition(controlRow, recentHalfCol);
recentHalfCol = !recentHalfCol;
Controls.Add(button);
break;
case Dictionary<string, object> dictSetting:
var dictLabel = CreateSettingLabel(setting.Value);
var dropdown = CreateSettingDropdown(setting.Key, dictSetting);
controlRow++;
dictLabel.Location = GetSettingPosition(controlRow);
dropdown.Location = GetSettingPosition(controlRow, true);
Controls.Add(dictLabel);
Controls.Add(dropdown);
break;
default:
break;
}
}
Height = 3 + controlRow * 29;
}
private static Point GetSettingPosition(int rowNum, bool secondCol = false)
{
return new Point(10 + (secondCol ? 200 : 0), -26 + rowNum * 29);
}
private Label CreateSettingLabel(string settingName)
{
Label label = new()
{
Text = settingName + ": ",
TextAlign = System.Drawing.ContentAlignment.MiddleRight,
Width = 200,
ForeColor = Color.LightGray
};
return label;
}
private ComboBox CreateSettingDropdown(PropertyInfo setting, Dictionary<string, object> dropdownItems)
{
var backingValueName = (SettingBackingValue?)Attribute.GetCustomAttribute(setting, typeof(SettingBackingValue));
var backingValue = from s in PluginManagement.PluginManager.GetSettingDisplayNames(_plugin.Settings)
where s.Value == backingValueName?.BackingProperty
select s.Key;
if (backingValue.Count() != 1)
throw new($"{_plugin.ShortName}: Dictionary settings must have exactly one backing value.");
ComboBox comboBox = new()
{
Width = 200,
DropDownStyle = ComboBoxStyle.DropDownList
};
comboBox.Items.AddRange(dropdownItems.OrderBy(s => s.Key).Select(s => s.Key).ToArray());
string? currentSelection = backingValue.First().GetValue(_plugin.Settings)?.ToString();
if (currentSelection?.Length > 0)
{
comboBox.SelectedItem = currentSelection;
}
// Then the rest
foreach (var setting in settings.Where(s => s.Key.PropertyType != typeof(bool)))
comboBox.SelectedValueChanged += (sender, e) =>
{
backingValue.First().SetValue(_plugin.Settings, comboBox.SelectedItem.ToString());
SaveSettings();
};
return comboBox;
}
private Button CreateSettingButton(string settingName, Action action)
{
Button button = new()
{
Text = settingName
};
button.Click += (sender, e) =>
{
action.Invoke();
SaveSettings();
};
return button;
}
private TrackBar CreateSettingTrackbar(PropertyInfo setting)
{
SettingNumericBounds? bounds = (SettingNumericBounds?)System.Attribute.GetCustomAttribute(setting, typeof(SettingNumericBounds));
TrackBar trackBar = new ()
{
Orientation = Orientation.Horizontal,
TickStyle = TickStyle.Both,
Width = 200,
Minimum = Convert.ToInt32(bounds?.Minimum ?? 0),
Maximum = Convert.ToInt32(bounds?.Maximum ?? 100)
};
trackBar.Value = (int?)setting.GetValue(_plugin.Settings) ?? 0;
trackBar.ValueChanged += (sender, e) =>
{
setting.SetValue(_plugin.Settings, trackBar.Value);
SaveSettings();
};
return trackBar;
}
private NumericUpDown CreateSettingNumericUpDown(PropertyInfo setting)
{
SettingNumericBounds? bounds = (SettingNumericBounds?)System.Attribute.GetCustomAttribute(setting, typeof(SettingNumericBounds));
NumericUpDown numericUpDown = new()
{
}
Width = 200,
Minimum = Convert.ToInt32(bounds?.Minimum ?? Int32.MinValue),
Maximum = Convert.ToInt32(bounds?.Maximum ?? Int32.MaxValue),
Increment = Convert.ToInt32(bounds?.Increment ?? 1)
};
numericUpDown.Value = (int?)setting.GetValue(_plugin.Settings) ?? 0;
numericUpDown.ValueChanged += (sender, e) =>
{
setting.SetValue(_plugin.Settings, numericUpDown.Value);
SaveSettings();
};
return numericUpDown;
}
private CheckBox CreateBoolSetting(KeyValuePair<PropertyInfo, string> setting)
{
CheckBox checkBox = new()
{
Text = setting.Value,
TextAlign= System.Drawing.ContentAlignment.MiddleLeft,
Checked = (bool?)setting.Key.GetValue(_plugin.Settings) ?? false,
Width = 200,
ForeColor = Color.LightGray
};
checkBox.CheckedChanged += (sender, e) =>
{
setting.Key.SetValue(_plugin.Settings, checkBox.Checked);
SaveSettings();
};
return checkBox;
}
private TextBox CreateStringSetting(PropertyInfo setting)
{
TextBox textBox = new()
{
Text = (setting.GetValue(_plugin.Settings) ?? String.Empty).ToString(),
Width = 200
};
textBox.TextChanged += (object? sender, EventArgs e) =>
{
setting.SetValue(_plugin.Settings, textBox.Text);
SaveSettings();
};
return textBox;
}
private TextBox CreateFilePathSetting(PropertyInfo setting)
{
var fileInfo = (FileInfo?)setting.GetValue(_plugin.Settings);
TextBox textBox = new()
{
Text = fileInfo?.FullName ?? string.Empty,
Width = 200
};
textBox.TextChanged += (object? sender, EventArgs e) =>
{
setting.SetValue(_plugin.Settings, new FileInfo(textBox.Text));
SaveSettings();
};
return textBox;
}
private Button CreateFileBrowseSetting(PropertyInfo setting, TextBox textBox)
{
Button button = new()
{
Text = "Browse"
};
button.Click += (object? sender, EventArgs e) =>
{
var currentDir = ((FileInfo?)setting.GetValue(_plugin.Settings))?.DirectoryName;
OpenFileDialog ofd = new OpenFileDialog()
{
Title = "Select File...",
Filter = "Lua files (*.lua)|*.lua|All files (*.*)|*.*",
FilterIndex = 0,
InitialDirectory = currentDir ?? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
var browseResult = ofd.ShowDialog();
if (browseResult == DialogResult.OK)
{
textBox.Text = ofd.FileName;
}
};
return button;
}
private Label CreateHeader(string pluginName)
@@ -92,5 +359,10 @@ namespace Observatory.UI
}
this.Parent?.ResumeLayout();
}
private void SaveSettings()
{
PluginManagement.PluginManager.GetInstance.SaveSettings(_plugin, _plugin.Settings);
}
}
}