109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using I2.Loc;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Ichni.UI
|
|
{
|
|
public partial class ValueModifier : SettingsUIElementBase
|
|
{
|
|
public int intValue;
|
|
public int intStep;
|
|
public int intMin;
|
|
public int intMax;
|
|
|
|
public ModifyValueButton increaseButton;
|
|
public ModifyValueButton decreaseButton;
|
|
public TMP_Text valueText;
|
|
|
|
public Image barImage;
|
|
|
|
public string prefix;
|
|
public string suffix;
|
|
public bool showPositiveMark;
|
|
|
|
public void SetUp(int initialValue, int step, string title = "")
|
|
{
|
|
intStep = step;
|
|
intMin = int.MinValue;
|
|
intMax = int.MaxValue;
|
|
|
|
base.SetUp(title);
|
|
|
|
increaseButton.SetUpButton(IncreaseValue, IncreaseValue);
|
|
decreaseButton.SetUpButton(DecreaseValue, DecreaseValue);
|
|
SetValue(initialValue);
|
|
UpdateValueText();
|
|
}
|
|
|
|
public void SetMinMax(int min, int max)
|
|
{
|
|
intMin = Convert.ToInt32(min);
|
|
intMax = Convert.ToInt32(max);
|
|
}
|
|
|
|
public void SetPrefixAndSuffix(string prefix, string suffix, bool showPositiveMark)
|
|
{
|
|
this.showPositiveMark = showPositiveMark;
|
|
this.prefix = prefix;
|
|
this.suffix = suffix;
|
|
UpdateValueText();
|
|
}
|
|
}
|
|
|
|
public partial class ValueModifier
|
|
{
|
|
public int GetNearestValue(float percentage)
|
|
{
|
|
int valueRange = intMax - intMin;
|
|
int stepCount = Mathf.RoundToInt(percentage * valueRange / intStep);
|
|
int nearestValue = intMin + stepCount * intStep;
|
|
|
|
return nearestValue;
|
|
}
|
|
}
|
|
|
|
public partial class ValueModifier
|
|
{
|
|
public void SetValue(int value)
|
|
{
|
|
intValue = Convert.ToInt32(value);
|
|
intValue = Mathf.Clamp(intValue, intMin, intMax);
|
|
|
|
UpdateValueText();
|
|
}
|
|
|
|
public void IncreaseValue()
|
|
{
|
|
intValue += intStep;
|
|
intValue = Mathf.Clamp(intValue, intMin, intMax);
|
|
|
|
UpdateValueText();
|
|
}
|
|
|
|
public void DecreaseValue()
|
|
{
|
|
intValue -= intStep;
|
|
intValue = Mathf.Clamp(intValue, intMin, intMax);
|
|
|
|
UpdateValueText();
|
|
}
|
|
|
|
public void UpdateValueText()
|
|
{
|
|
valueText.text = prefix + (showPositiveMark && intValue > 0 ? "+" : "") + intValue.ToString() + suffix;
|
|
barImage.rectTransform.sizeDelta = new Vector2((float)(intValue - intMin) / (intMax - intMin) * 660, 20);
|
|
|
|
updateValueAction?.Invoke();
|
|
}
|
|
|
|
public int GetValue()
|
|
{
|
|
return intValue;
|
|
}
|
|
}
|
|
} |