侧边栏UI增加可编辑值的选项类型,滤镜预设现在已支持参数的修改

This commit is contained in:
ALIENJACK 2024-12-05 01:31:28 +08:00
parent c421466b84
commit d6fa9772b9
12 changed files with 1582 additions and 44 deletions

View File

@ -26,7 +26,22 @@ namespace AxibugEmuOnline.Client
where t.DeclaringType.IsSubclassOf(typeof(FilterEffect)) where t.DeclaringType.IsSubclassOf(typeof(FilterEffect))
orderby t.MetadataToken orderby t.MetadataToken
select t); select t);
m_editableParamList = parameters.Select(p => new EditableParamerter(p.Name, (ParameterOverride)p.GetValue(this))).ToList();
m_editableParamList = new List<EditableParamerter>();
foreach (var param in parameters)
{
var paramObj = (ParameterOverride)param.GetValue(this);
var rangeAtt = param.GetCustomAttribute<RangeAttribute>();
float min = 0;
float max = 10;
if (rangeAtt != null)
{
min = rangeAtt.min; max = rangeAtt.max;
}
var editableParam = new EditableParamerter(param.Name, paramObj, min, max);
m_editableParamList.Add(editableParam);
}
} }
public class EditableParamerter public class EditableParamerter
@ -42,9 +57,13 @@ namespace AxibugEmuOnline.Client
set set
{ {
valueFieldInfo.SetValue(m_paramObject, value); valueFieldInfo.SetValue(m_paramObject, value);
m_paramObject.overrideState = true;
} }
} }
public EditableParamerter(string name, ParameterOverride paramObject) public object MinValue { get; private set; }
public object MaxValue { get; private set; }
public EditableParamerter(string name, ParameterOverride paramObject, object minValue, object maxValue)
{ {
m_paramObject = paramObject; m_paramObject = paramObject;
Name = name; Name = name;
@ -60,18 +79,16 @@ namespace AxibugEmuOnline.Client
{ {
ValueType = typeof(object); ValueType = typeof(object);
} }
MinValue = minValue;
MaxValue = maxValue;
} }
public void ResetToDefault() => m_paramObject.overrideState = false; public void ResetToDefault() => m_paramObject.overrideState = false;
public string Serilized()
{
return JsonUtility.ToJson(Value);
}
public void Apply(string json) public void Apply(object overrideValue)
{ {
var overrideValue = JsonUtility.FromJson(json, ValueType);
Value = overrideValue; Value = overrideValue;
} }
} }

View File

@ -1,4 +1,5 @@
using System; using AxibugEmuOnline.Client.ClientCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
@ -94,7 +95,8 @@ namespace AxibugEmuOnline.Client
filter = Filters.FirstOrDefault(f => f.Name == value.filterName); filter = Filters.FirstOrDefault(f => f.Name == value.filterName);
if (filter != null) if (filter != null)
{ {
preset = filter.Presets.FirstOrDefault(p => p.Name == value.filterName); string presetName = value.presetName;
preset = filter.Presets.FirstOrDefault(p => p.Name == presetName);
} }
return (filter, preset); return (filter, preset);
@ -126,9 +128,9 @@ namespace AxibugEmuOnline.Client
else Presets = loadedPresets.presets; else Presets = loadedPresets.presets;
} }
private void savePresets() public void SavePresets()
{ {
var json = JsonUtility.ToJson(new FilterPresetList { presets = Presets }); var json = JsonUtility.ToJson(new FilterPresetList(Presets));
PlayerPrefs.SetString($"Filter_{Name}_PresetList", json); PlayerPrefs.SetString($"Filter_{Name}_PresetList", json);
} }
@ -141,7 +143,7 @@ namespace AxibugEmuOnline.Client
newPreset = new FilterPreset(presetName); newPreset = new FilterPreset(presetName);
Presets.Add(newPreset); Presets.Add(newPreset);
savePresets(); SavePresets();
return true; return true;
} }
@ -149,7 +151,7 @@ namespace AxibugEmuOnline.Client
public void RemovePreset(FilterPreset preset) public void RemovePreset(FilterPreset preset)
{ {
if (!Presets.Remove(preset)) return; if (!Presets.Remove(preset)) return;
savePresets(); SavePresets();
EventInvoker.RaiseFilterPresetRemoved(this, preset); EventInvoker.RaiseFilterPresetRemoved(this, preset);
} }
@ -166,11 +168,11 @@ namespace AxibugEmuOnline.Client
{ {
foreach (var param in Paramerters) foreach (var param in Paramerters)
{ {
var json = preset.GetParamValueJson(param.Name); var value = preset.GetParamValue(param.Name, param.ValueType);
if (string.IsNullOrEmpty(json)) if (value == null)
param.ResetToDefault(); param.ResetToDefault();
else else
param.Apply(json); param.Apply(value);
} }
} }
@ -182,6 +184,15 @@ namespace AxibugEmuOnline.Client
private class FilterPresetList private class FilterPresetList
{ {
public List<FilterPreset> presets; public List<FilterPreset> presets;
public FilterPresetList(List<FilterPreset> presets)
{
this.presets = presets;
foreach (var preset in presets)
{
preset.ReadyForJson();
}
}
} }
[Serializable] [Serializable]
@ -201,6 +212,12 @@ namespace AxibugEmuOnline.Client
Name = presetName; Name = presetName;
} }
public void ReadyForJson()
{
m_paramName = m_paramName2ValueJson.Keys.ToList();
m_valueJson = m_paramName2ValueJson.Values.ToList();
}
public string GetParamValueJson(string paramName) public string GetParamValueJson(string paramName)
{ {
prepareCache(); prepareCache();
@ -209,6 +226,34 @@ namespace AxibugEmuOnline.Client
return value; return value;
} }
public object GetParamValue(string paramName, Type valueType)
{
var rawStr = GetParamValueJson(paramName);
if (rawStr == null) return null;
if (valueType == typeof(float))
{
float.TryParse(rawStr, out var floatVal);
return floatVal;
}
else if (valueType.IsEnum)
{
Enum.TryParse(valueType, rawStr,out var enumValue);
return enumValue;
}
else
{
App.log.Error($"尚未支持的滤镜参数类型{valueType}");
return null;
}
}
public void SetParamValue(string paramName, Type valueType, object value)
{
prepareCache();
m_paramName2ValueJson[paramName] = value.ToString();
}
private void prepareCache() private void prepareCache()
{ {
if (m_cacheReady) return; if (m_cacheReady) return;

View File

@ -1,5 +1,6 @@
using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.UI; using AxibugEmuOnline.Client.UI;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
@ -88,12 +89,16 @@ namespace AxibugEmuOnline.Client
private Filter m_filter; private Filter m_filter;
private FilterPreset m_preset; private FilterPreset m_preset;
private OptionUI_MenuItem m_ui; private OptionUI_MenuItem m_ui;
private List<OptionMenu> m_menu;
public Opt_Presets(Filter filter, FilterPreset preset) : base(preset.Name, null) public Opt_Presets(Filter filter, FilterPreset preset) : base(preset.Name, null)
{ {
m_filter = filter; m_filter = filter;
m_preset = preset; m_preset = preset;
m_menu = new List<OptionMenu>();
m_menu.Add(new Opt_Delete(m_filter, m_preset));
foreach (var p in m_filter.Paramerters) m_menu.Add(new Opt_ParamEditor(m_filter, p, m_preset));
} }
public override void OnShow(OptionUI_MenuItem ui) public override void OnShow(OptionUI_MenuItem ui)
@ -122,7 +127,37 @@ namespace AxibugEmuOnline.Client
protected override List<OptionMenu> GetOptionMenus() protected override List<OptionMenu> GetOptionMenus()
{ {
return new List<OptionMenu> { new Opt_Delete(m_filter, m_preset) }; return m_menu;
}
public class Opt_ParamEditor : ValueSetMenu
{
private Filter m_filter;
private FilterEffect.EditableParamerter m_param;
private FilterPreset m_preset;
public Opt_ParamEditor(Filter filter, FilterEffect.EditableParamerter editParam, FilterPreset preset)
: base(editParam.Name)
{
m_filter = filter;
m_param = editParam;
m_preset = preset;
}
public override Type ValueType => m_param.ValueType;
public override object ValueRaw => m_preset.GetParamValue(m_param.Name, ValueType) ?? m_param.Value;
public override void OnValueChanged(object newValue)
{
m_preset.SetParamValue(m_param.Name, ValueType, newValue);
m_filter.SavePresets();
m_param.Apply(newValue);
}
public override object Min => m_param.MinValue;
public override object Max => m_param.MaxValue;
} }
public class Opt_Delete : ExecuteMenu public class Opt_Delete : ExecuteMenu
@ -141,6 +176,8 @@ namespace AxibugEmuOnline.Client
m_filter.RemovePreset(m_preset); m_filter.RemovePreset(m_preset);
} }
} }
} }
} }
} }

View File

@ -15,6 +15,7 @@ namespace AxibugEmuOnline.Client
[Space] [Space]
[Header("模板")] [Header("模板")]
[SerializeField] OptionUI_ExecuteItem TEMPLATE_EXECUTEITEM; [SerializeField] OptionUI_ExecuteItem TEMPLATE_EXECUTEITEM;
[SerializeField] OptionUI_ValueEditItem TEMPLATE_VALUEEDITITEM;
private OptionUI m_child; private OptionUI m_child;
private OptionUI m_parent; private OptionUI m_parent;
@ -64,6 +65,8 @@ namespace AxibugEmuOnline.Client
protected override void Awake() protected override void Awake()
{ {
TEMPLATE_EXECUTEITEM.gameObject.SetActiveEx(false); TEMPLATE_EXECUTEITEM.gameObject.SetActiveEx(false);
TEMPLATE_VALUEEDITITEM.gameObject.SetActiveEx(false);
SelectBorder.gameObject.SetActiveEx(false); SelectBorder.gameObject.SetActiveEx(false);
base.Awake(); base.Awake();
} }
@ -269,15 +272,19 @@ namespace AxibugEmuOnline.Client
{ {
if (menuData is ExecuteMenu executeMenu) if (menuData is ExecuteMenu executeMenu)
{ {
var menuUI = GameObject.Instantiate(TEMPLATE_EXECUTEITEM.gameObject, TEMPLATE_EXECUTEITEM.transform.parent).GetComponent<OptionUI_ExecuteItem>(); var menuUI = Instantiate(TEMPLATE_EXECUTEITEM.gameObject, TEMPLATE_EXECUTEITEM.transform.parent).GetComponent<OptionUI_ExecuteItem>();
menuUI.gameObject.SetActive(true); menuUI.gameObject.SetActive(true);
menuUI.SetData(this, executeMenu); menuUI.SetData(this, executeMenu);
m_runtimeMenuItems.Add(menuUI); m_runtimeMenuItems.Add(menuUI);
} }
else else if (menuData is ValueSetMenu valueSetMenu)
{ {
throw new NotImplementedException($"暂不支持的菜单类型{menuData.GetType().Name}"); var menuUI = Instantiate(TEMPLATE_VALUEEDITITEM.gameObject, TEMPLATE_VALUEEDITITEM.transform.parent).GetComponent<OptionUI_ValueEditItem>();
menuUI.gameObject.SetActive(true);
menuUI.SetData(this, valueSetMenu);
m_runtimeMenuItems.Add(menuUI);
} }
else throw new NotImplementedException($"暂不支持的菜单类型{menuData.GetType().Name}");
} }
private void ReleaseRuntimeMenus() private void ReleaseRuntimeMenus()
@ -305,10 +312,23 @@ namespace AxibugEmuOnline.Client
Hide(); Hide();
} }
protected override void OnCmdSelectItemLeft()
{
var executer = m_runtimeMenuItems[SelectIndex];
if (executer != null)
{
executer.OnLeft();
}
}
protected override void OnCmdSelectItemRight() protected override void OnCmdSelectItemRight()
{ {
var executer = m_runtimeMenuItems[SelectIndex]; var executer = m_runtimeMenuItems[SelectIndex];
if (!executer.IsExpandMenu) return; if (!executer.IsExpandMenu)
{
executer.OnRight();
return;
}
OnCmdEnter(); OnCmdEnter();
} }
@ -384,25 +404,6 @@ namespace AxibugEmuOnline.Client
protected abstract List<OptionMenu> GetOptionMenus(); protected abstract List<OptionMenu> GetOptionMenus();
} }
/// <summary>
/// 带有值类型显示和编辑的菜单
/// </summary>
/// <typeparam name="T"></typeparam>
public class ValueSetMenu<T> : ValueSetMenu
{
public sealed override Type ValueType => typeof(T);
public T Value { get; private set; }
public sealed override object ValueRaw => Value;
public sealed override void OnValueChanged(object newValue)
{
Value = (T)newValue;
}
protected ValueSetMenu(string name) : base(name) { }
}
/// <summary> 不要直接继承这个类 </summary> /// <summary> 不要直接继承这个类 </summary>
public abstract class OptionMenu public abstract class OptionMenu
{ {
@ -421,7 +422,9 @@ namespace AxibugEmuOnline.Client
public virtual void OnShow(OptionUI_MenuItem ui) { } public virtual void OnShow(OptionUI_MenuItem ui) { }
public virtual void OnHide() { } public virtual void OnHide() { }
} }
/// <summary> 不要直接继承这个类 </summary> /// <summary>
/// 带有值类型显示和编辑的菜单
/// </summary>
public abstract class ValueSetMenu : OptionMenu public abstract class ValueSetMenu : OptionMenu
{ {
public ValueSetMenu(string name) : base(name) { } public ValueSetMenu(string name) : base(name) { }
@ -429,7 +432,7 @@ namespace AxibugEmuOnline.Client
public abstract Type ValueType { get; } public abstract Type ValueType { get; }
public abstract object ValueRaw { get; } public abstract object ValueRaw { get; }
public abstract void OnValueChanged(object newValue); public abstract void OnValueChanged(object newValue);
public abstract object Min { get; }
public abstract object Max { get; }
} }
} }

View File

@ -35,6 +35,8 @@ namespace AxibugEmuOnline.Client
protected abstract void OnSetData(OptionMenu menuData); protected abstract void OnSetData(OptionMenu menuData);
public abstract void OnExecute(OptionUI optionUI, ref bool cancelHide); public abstract void OnExecute(OptionUI optionUI, ref bool cancelHide);
public virtual void OnLeft() { }
public virtual void OnRight() { }
public abstract void OnFocus(); public abstract void OnFocus();
public virtual void OnHide() { } public virtual void OnHide() { }
} }

View File

@ -0,0 +1,69 @@
using AxibugEmuOnline.Client.ClientCore;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class OptionUI_ValueEditItem : OptionUI_MenuItem<ValueSetMenu>
{
[SerializeField]
OptionUI_ValueEditItem_FloatEdit com_floatEdit;
[SerializeField]
OptionUI_ValueEditItem_EnumEdit com_enumEdit;
IValueEditControl m_currentCom;
protected override void OnSetData(OptionMenu menuData)
{
com_floatEdit.gameObject.SetActive(false);
com_enumEdit.gameObject.SetActive(false);
if (menuData is ValueSetMenu valueMenu)
{
if (valueMenu.ValueType == typeof(float))
{
m_currentCom = com_floatEdit;
}
else if (valueMenu.ValueType.IsEnum)
{
m_currentCom = com_enumEdit;
}
else
{
App.log.Error($"ÉÐδ֧³ÖµÄÊý¾ÝÀàÐÍ:{valueMenu.ValueType}");
return;
}
m_currentCom.gameObject.SetActiveEx(true);
m_currentCom.SetData(valueMenu);
}
base.OnSetData(menuData);
}
public override void OnExecute(OptionUI optionUI, ref bool cancelHide)
{
cancelHide = true;
m_currentCom?.OnExecute();
}
public override void OnLeft()
{
m_currentCom?.OnLeft();
}
public override void OnRight()
{
m_currentCom?.OnRight();
}
}
public interface IValueEditControl
{
void SetData(ValueSetMenu valueMenu);
GameObject gameObject { get; }
void OnLeft();
void OnRight();
void OnExecute();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: deae682aaf23cab458cba1b12cd3a1f7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client
{
public class OptionUI_ValueEditItem_EnumEdit : MonoBehaviour, IValueEditControl
{
[SerializeField]
Text txt_value;
private ValueSetMenu m_valueMenu;
private List<Enum> m_enumValues = new List<Enum>();
private int m_valueIndex;
public void SetData(ValueSetMenu valueMenu)
{
m_valueMenu = valueMenu;
txt_value.text = valueMenu.ValueRaw.ToString();
foreach (Enum enumValue in Enum.GetValues(valueMenu.ValueType))
{
m_enumValues.Add(enumValue);
}
m_valueIndex = m_enumValues.IndexOf((Enum)valueMenu.ValueRaw);
}
public void OnLeft()
{
m_valueIndex--;
if (m_valueIndex < 0) m_valueIndex = m_enumValues.Count - 1;
var value = m_enumValues[m_valueIndex];
txt_value.text = value.ToString();
m_valueMenu.OnValueChanged(value);
}
public void OnRight()
{
m_valueIndex++;
if (m_valueIndex >= m_enumValues.Count) m_valueIndex = 0;
var value = m_enumValues[m_valueIndex];
txt_value.text = value.ToString();
m_valueMenu.OnValueChanged(value);
}
public void OnExecute()
{
OnRight();
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ef1018f09e7c5b049b75128ee308d372

View File

@ -0,0 +1,53 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace AxibugEmuOnline.Client
{
public class OptionUI_ValueEditItem_FloatEdit : MonoBehaviour, IValueEditControl
{
[SerializeField]
Slider slider;
[SerializeField]
Text txt_value;
float m_step;
private ValueSetMenu m_valueMenu;
private void Awake()
{
slider.onValueChanged.AddListener(OnSliderValueChanged);
}
private void OnSliderValueChanged(float value)
{
txt_value.text = $"{value:.00}";
m_valueMenu.OnValueChanged(value);
}
public void SetData(ValueSetMenu valueMenu)
{
m_valueMenu = valueMenu;
slider.minValue = (float)valueMenu.Min;
slider.maxValue = (float)valueMenu.Max;
slider.value = (float)valueMenu.ValueRaw;
m_step = (slider.maxValue - slider.minValue) * 0.05f;
}
public void OnLeft()
{
var newValue = Mathf.Clamp(slider.value - m_step, slider.minValue, slider.maxValue);
slider.value = newValue;
}
public void OnRight()
{
var newValue = Mathf.Clamp(slider.value + m_step, slider.minValue, slider.maxValue);
slider.value = newValue;
}
public void OnExecute()
{
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8c9e9868e38e8ea44a0dad03b987cd73