mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-01 06:00:46 +00:00
Gigantic code cleanup
Time to enforce some common file layout, as general mess started to annoying me. Sorry in advance for people using custom forks and having merge conflicts, this will help everybody in long-run
This commit is contained in:
@@ -7,28 +7,31 @@ using System.Windows.Forms.Design;
|
||||
|
||||
namespace ConfigGenerator {
|
||||
internal sealed class FlagCheckedListBox : CheckedListBox {
|
||||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
||||
internal Enum EnumValue {
|
||||
get {
|
||||
object e = Enum.ToObject(EnumType, GetCurrentValue());
|
||||
return (Enum) e;
|
||||
}
|
||||
|
||||
set {
|
||||
Items.Clear();
|
||||
_EnumValue = value; // Store the current enum value
|
||||
EnumType = value.GetType(); // Store enum type
|
||||
FillEnumMembers(); // Add items for enum members
|
||||
ApplyEnumValue(); // Check/uncheck items depending on enum value
|
||||
}
|
||||
}
|
||||
|
||||
private Enum _EnumValue;
|
||||
private Type EnumType;
|
||||
private bool IsUpdatingCheckStates;
|
||||
|
||||
internal FlagCheckedListBox() {
|
||||
// This call is required by the Windows.Forms Form Designer.
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
#region Component Designer generated code
|
||||
private void InitializeComponent() {
|
||||
//
|
||||
// FlaggedCheckedListBox
|
||||
//
|
||||
CheckOnClick = true;
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
// Adds an integer value and its associated description
|
||||
private void Add(int v, string c) {
|
||||
FlagCheckedListBoxItem item = new FlagCheckedListBoxItem(v, c);
|
||||
Items.Add(item);
|
||||
}
|
||||
|
||||
protected override void OnItemCheck(ItemCheckEventArgs e) {
|
||||
base.OnItemCheck(e);
|
||||
|
||||
@@ -42,9 +45,44 @@ namespace ConfigGenerator {
|
||||
UpdateCheckedItems(item, e.NewValue);
|
||||
}
|
||||
|
||||
// Adds an integer value and its associated description
|
||||
private void Add(int v, string c) {
|
||||
FlagCheckedListBoxItem item = new FlagCheckedListBoxItem(v, c);
|
||||
Items.Add(item);
|
||||
}
|
||||
|
||||
// Checks/unchecks items based on the current value of the enum variable
|
||||
private void ApplyEnumValue() {
|
||||
int intVal = (int) Convert.ChangeType(_EnumValue, typeof(int));
|
||||
UpdateCheckedItems(intVal);
|
||||
}
|
||||
|
||||
// Adds items to the checklistbox based on the members of the enum
|
||||
private void FillEnumMembers() {
|
||||
foreach (string name in Enum.GetNames(EnumType)) {
|
||||
object val = Enum.Parse(EnumType, name);
|
||||
int intVal = (int) Convert.ChangeType(val, typeof(int));
|
||||
|
||||
Add(intVal, name);
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the current bit value corresponding to all checked items
|
||||
private int GetCurrentValue() => (from object t in Items select t as FlagCheckedListBoxItem).Where((item, i) => (item != null) && GetItemChecked(i)).Aggregate(0, (current, item) => current | item.Value);
|
||||
|
||||
#region Component Designer generated code
|
||||
|
||||
private void InitializeComponent() {
|
||||
//
|
||||
// FlaggedCheckedListBox
|
||||
//
|
||||
CheckOnClick = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Checks/Unchecks items depending on the give bitvalue
|
||||
private void UpdateCheckedItems(int value) {
|
||||
|
||||
IsUpdatingCheckStates = true;
|
||||
|
||||
// Iterate over all items
|
||||
@@ -57,7 +95,6 @@ namespace ConfigGenerator {
|
||||
if (item.Value == 0) {
|
||||
SetItemChecked(i, value == 0);
|
||||
} else {
|
||||
|
||||
// If the bit for the current item is on in the bitvalue, check it
|
||||
if (((item.Value & value) == item.Value) && (item.Value != 0)) {
|
||||
SetItemChecked(i, true);
|
||||
@@ -70,20 +107,17 @@ namespace ConfigGenerator {
|
||||
}
|
||||
|
||||
IsUpdatingCheckStates = false;
|
||||
|
||||
}
|
||||
|
||||
// Updates items in the checklistbox
|
||||
// composite = The item that was checked/unchecked
|
||||
// cs = The check state of that item
|
||||
private void UpdateCheckedItems(FlagCheckedListBoxItem composite, CheckState cs) {
|
||||
|
||||
// If the value of the item is 0, call directly.
|
||||
if (composite.Value == 0) {
|
||||
UpdateCheckedItems(0);
|
||||
}
|
||||
|
||||
|
||||
// Get the total value of all checked items
|
||||
int sum = (from object t in Items select t as FlagCheckedListBoxItem).Where((item, i) => (item != null) && GetItemChecked(i)).Aggregate(0, (current, item) => current | item.Value);
|
||||
|
||||
@@ -98,53 +132,7 @@ namespace ConfigGenerator {
|
||||
|
||||
// Update all items in the checklistbox based on the final bit value
|
||||
UpdateCheckedItems(sum);
|
||||
|
||||
}
|
||||
|
||||
private bool IsUpdatingCheckStates;
|
||||
|
||||
// Gets the current bit value corresponding to all checked items
|
||||
private int GetCurrentValue() => (from object t in Items select t as FlagCheckedListBoxItem).Where((item, i) => (item != null) && GetItemChecked(i)).Aggregate(0, (current, item) => current | item.Value);
|
||||
|
||||
private Type EnumType;
|
||||
private Enum _EnumValue;
|
||||
|
||||
// Adds items to the checklistbox based on the members of the enum
|
||||
private void FillEnumMembers() {
|
||||
foreach (string name in Enum.GetNames(EnumType)) {
|
||||
object val = Enum.Parse(EnumType, name);
|
||||
int intVal = (int) Convert.ChangeType(val, typeof(int));
|
||||
|
||||
Add(intVal, name);
|
||||
}
|
||||
}
|
||||
|
||||
// Checks/unchecks items based on the current value of the enum variable
|
||||
private void ApplyEnumValue() {
|
||||
int intVal = (int) Convert.ChangeType(_EnumValue, typeof(int));
|
||||
UpdateCheckedItems(intVal);
|
||||
|
||||
}
|
||||
|
||||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
|
||||
internal Enum EnumValue {
|
||||
get {
|
||||
object e = Enum.ToObject(EnumType, GetCurrentValue());
|
||||
return (Enum) e;
|
||||
}
|
||||
|
||||
set {
|
||||
|
||||
Items.Clear();
|
||||
_EnumValue = value; // Store the current enum value
|
||||
EnumType = value.GetType(); // Store enum type
|
||||
FillEnumMembers(); // Add items for enum members
|
||||
ApplyEnumValue(); // Check/uncheck items depending on enum value
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Represents an item in the checklistbox
|
||||
@@ -161,13 +149,12 @@ namespace ConfigGenerator {
|
||||
public override string ToString() => Caption;
|
||||
}
|
||||
|
||||
|
||||
// UITypeEditor for flag enums
|
||||
internal sealed class FlagEnumUIEditor : UITypeEditor {
|
||||
internal sealed class FlagEnumUiEditor : UITypeEditor {
|
||||
// The checklistbox
|
||||
private readonly FlagCheckedListBox FlagEnumCb;
|
||||
|
||||
internal FlagEnumUIEditor() {
|
||||
internal FlagEnumUiEditor() {
|
||||
FlagEnumCb = new FlagCheckedListBox { BorderStyle = BorderStyle.None };
|
||||
}
|
||||
|
||||
@@ -190,4 +177,4 @@ namespace ConfigGenerator {
|
||||
|
||||
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) => UITypeEditorEditStyle.DropDown;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user