Skip to content

Commit

Permalink
Restore on-demand data feed template injection and make the templates…
Browse files Browse the repository at this point in the history
… look better by having consistent widths
  • Loading branch information
Nytra committed Aug 25, 2024
1 parent e0a91a5 commit a8cb1e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ internal sealed class ConfigSectionSettingsItems : DataFeedBuildingBlockMonkey<C
private static readonly MethodInfo _generateFlagsEnumFields = AccessTools.Method(typeof(ConfigSectionSettingsItems), nameof(GenerateFlagsEnumFields));
private static readonly MethodInfo _generateItemForConfigKeyMethod = AccessTools.Method(typeof(ConfigSectionSettingsItems), nameof(GenerateItemForConfigKey));
private static readonly MethodInfo _generateQuantityField = AccessTools.Method(typeof(ConfigSectionSettingsItems), nameof(GenerateQuantityField));
private static EnumerateDataFeedParameters<SettingsDataFeed>? _currentParameters;
public override int Priority => 400;

public override IAsyncEnumerable<DataFeedItem> Apply(IAsyncEnumerable<DataFeedItem> current, EnumerateDataFeedParameters<SettingsDataFeed> parameters)
{
_currentParameters = parameters;
var path = parameters.Path;

if (path.Count is < 2 or > 3 || path[0] is not SettingsHelpers.MonkeyLoader)
Expand Down Expand Up @@ -255,11 +257,15 @@ private static DataFeedValueField<T> GenerateValueField<T>(IReadOnlyList<string>
valueField.InitBase(path, groupKeys, configKey);
valueField.InitSetupValue(field => field.SetupConfigKeyField(configKey));

//var valueType = typeof(T);
//if (valueType != typeof(dummy) && (Coder<T>.IsEnginePrimitive || valueType == typeof(Type)))
//{
// settingsData.Mapper?.RunSynchronously(() => EnsureDataFeedValueFieldTemplate(settingsData, valueType));
//}
var valueType = typeof(T);
if (valueType != typeof(dummy) && (Coder<T>.IsEnginePrimitive || valueType == typeof(Type)))
{
if (_currentParameters!.DataFeed is SettingsDataFeed settingsDataFeed)
{
var settingsViewData = SettingsHelpers.GetViewData(_currentParameters!.DataFeed);
settingsViewData.Mapper?.RunSynchronously(() => settingsViewData.EnsureDataFeedValueFieldTemplate(valueType));
}
}

return valueField;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,6 @@ internal SettingsViewData(SettingsDataFeed dataFeed)

RootCategoryView = dataFeed.Slot.GetComponent<RootCategoryView>();

dataFeed.StartTask(async () =>
{
while (CoroutineManager.Manager.Value is null)
{
await Task.Delay(1000);
}
foreach (var primitiveType in GetTemplateTypes())
{
await default(NextUpdate);
EnsureDataFeedValueFieldTemplate(primitiveType);

}
});

var settingsListSlot = dataFeed.Slot.FindChild(s => s.Name == "Settings List", maxDepth: 2);
if (settingsListSlot is null)
return;
Expand All @@ -111,24 +97,6 @@ internal void MoveUpFromCategory(string category)
RootCategoryView.MoveUpInCategory();
}

private static IEnumerable<Type> GetTemplateTypes()
{
var nullableType = typeof(Nullable<>);

foreach (var basePrimitive in GenericTypesAttribute.GetTypes(GenericTypesAttribute.Group.EnginePrimitivesAndEnums))
{
if (basePrimitive.Name == nameof(dummy))
continue;

if (basePrimitive.IsValueType)
yield return nullableType.MakeGenericType(basePrimitive);

yield return basePrimitive;
}

yield return typeof(Type);
}

private void CleanupView()
{
if (!HasRootCategoryView)
Expand All @@ -140,7 +108,10 @@ private void CleanupView()
Logger.Debug(() => "Cached RootCategoryView and subscribed to events.");
}

private void EnsureDataFeedValueFieldTemplate(Type typeToInject)
/// <summary>
/// Ensures a <see cref="FrooxEngine.DataFeedValueField{T}"/> template exists for the given <see cref="System.Type"/> in this <see cref="SettingsViewData">SettingsViewData's</see> <see cref="FrooxEngine.DataFeedItemMapper"/>
/// </summary>
public void EnsureDataFeedValueFieldTemplate(Type typeToInject)
{
if (!HasMapper)
{
Expand Down Expand Up @@ -188,30 +159,24 @@ private void EnsureDataFeedValueFieldTemplate(Type typeToInject)
RadiantUI_Constants.SetupEditorStyle(ui);

ui.ForceNext = template.AttachComponent<RectTransform>();
ui.HorizontalLayout(11.78908f, 11.78908f);
ui.HorizontalLayout(11.78908f, 11.78908f).ForceExpandWidth.Value = false;

ui.PushStyle();
ui.Style.MinWidth = 288f;
var text = ui.Text("Label");
ui.PopStyle();

text.Size.Value = 24f;
text.HorizontalAlign.Value = TextHorizontalAlignment.Left;
ui.Style.MinHeight = 32f;

ui.Spacer(128f);

Component? component = null;
ISyncMember? member = null;
FieldInfo? fieldInfo = null;
Component component;
ISyncMember member;
FieldInfo fieldInfo;

if (typeToInject == typeof(Type))
{
component = template.AttachComponent(typeof(TypeField));
member = component.GetSyncMember("Type");

if (member == null)
{
Logger.Error(() => "Could not get Type sync member from attached TypeField component!");
return;
}

component = template.AttachComponent<TypeField>();
member = ((TypeField)component).Type;
fieldInfo = component.GetSyncMemberFieldInfo("Type");
}
else
Expand All @@ -228,9 +193,10 @@ private void EnsureDataFeedValueFieldTemplate(Type typeToInject)
fieldInfo = component.GetSyncMemberFieldInfo("Value");
}

ui.PushStyle();
ui.Style.FlexibleWidth = 1f;
SyncMemberEditorBuilder.Build(member, null, fieldInfo, ui, 0f);
ui.Style.FlexibleWidth = -1f;
ui.PopStyle();

var memberActions = ui.Root?.GetComponentInChildren<InspectorMemberActions>()?.Slot;
if (memberActions != null)
Expand Down

0 comments on commit a8cb1e5

Please sign in to comment.