Files
BetterAmongUs/Items/OptionItems/BetterOptionCheckboxItem.cs
T

129 lines
4.2 KiB
C#

using BetterAmongUs.Managers;
using UnityEngine;
namespace BetterAmongUs.Items.OptionItems;
internal class BetterOptionCheckboxItem : BetterOptionItem
{
private ToggleOption? ThisOption;
private bool? IsChecked;
internal override bool ShowChildrenCondition() => IsChecked == true;
internal override bool SelfShowCondition() => ShowCondition != null ? ShowCondition() : base.SelfShowCondition();
internal Func<bool>? ShowCondition = null;
internal BetterOptionItem Create(int id, GameOptionsMenu gameOptionsMenu, string name, bool DefaultValue = true, BetterOptionItem? Parent = null, Func<bool>? selfShowCondition = null)
{
Id = id;
Tab = gameOptionsMenu;
Name = name;
IsChecked = DefaultValue;
ShowCondition = selfShowCondition;
if (gameOptionsMenu == null)
{
Load(DefaultValue);
return this;
}
ToggleOption optionBehaviour = UnityEngine.Object.Instantiate(gameOptionsMenu.checkboxOrigin, Vector3.zero, Quaternion.identity, gameOptionsMenu.settingsContainer);
optionBehaviour.transform.localPosition = new Vector3(0.952f, 2f, -2f);
SetUp(optionBehaviour);
optionBehaviour.OnValueChanged = new Action<OptionBehaviour>((option) => ValueChanged(id, option));
optionBehaviour.LabelBackground.transform.localScale = new Vector3(1.6f, 0.78f);
optionBehaviour.LabelBackground.transform.SetLocalX(-2.4f);
optionBehaviour.TitleText.enableAutoSizing = false;
optionBehaviour.TitleText.transform.SetLocalX(-1.5f);
optionBehaviour.TitleText.alignment = TMPro.TextAlignmentOptions.Right;
optionBehaviour.TitleText.enableWordWrapping = false;
optionBehaviour.TitleText.fontSize = 2.5f;
// Set data
optionBehaviour.CheckMark.GetComponent<SpriteRenderer>().enabled = DefaultValue;
TitleText = optionBehaviour.TitleText;
Option = optionBehaviour;
ThisOption = optionBehaviour;
Load(DefaultValue);
BetterOptionItems.Add(this);
obj = optionBehaviour.gameObject;
if (Parent != null)
{
int Index = 1;
var tempParent = Parent;
while (tempParent.ThisParent != null)
{
tempParent = tempParent.ThisParent;
Index++;
}
optionBehaviour.LabelBackground.GetComponent<SpriteRenderer>().color -= new Color(0.25f, 0.25f, 0.25f, 0f) * Index;
optionBehaviour.LabelBackground.transform.localScale -= new Vector3(0.04f, 0f, 0f) * Index;
optionBehaviour.LabelBackground.transform.position += new Vector3(0.04f, 0f, 0f) * Index;
optionBehaviour.LabelBackground.transform.SetLocalZ(1f);
ThisParent = Parent;
IsChild = true;
Parent.ChildrenList.Add(this);
}
return this;
}
internal void Load(bool DefaultValue)
{
if (BetterDataManager.CanLoadSetting(Id))
{
if (ThisOption == null) return;
var Bool = BetterDataManager.LoadBoolSetting(Id, DefaultValue);
ThisOption.CheckMark.GetComponent<SpriteRenderer>().enabled = Bool;
IsChecked = Bool;
}
else
{
BetterDataManager.SaveSetting(Id, DefaultValue.ToString());
}
}
internal override bool GetBool()
{
if (BetterDataManager.CanLoadSetting(Id))
{
return BetterDataManager.LoadBoolSetting(Id);
}
else
{
return IsChecked ??= false;
}
}
internal override void SetData(OptionBehaviour optionBehaviour)
{
optionBehaviour.data = new BaseGameSetting
{
Title = StringNames.None,
Type = OptionTypes.Checkbox,
};
}
internal override void ValueChanged(int id, OptionBehaviour optionBehaviour)
{
IsChecked = !IsChecked;
BetterDataManager.SaveSetting(Id, IsChecked.ToString());
if (IsParent || IsChild)
{
bool Bool = ShowChildrenCondition();
foreach (var item in ChildrenList)
{
item.obj.SetActive(Bool && item.SelfShowCondition());
}
UpdatePositions();
}
}
}