Files
XRLib/Assets/Scripts/Simulator/Components/ComponentUIBinder.cs
2026-03-17 10:52:33 +09:00

96 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Simulator.Data
{
public class ComponentUIBinder
{
private ProgressPopupView _progressPopupView;
private ConveyorProperty _conveyorProperty;
private NodeProperty _nodeProperty;
private struct BindingEntry
{
public Action<ComponentType, ComponentDataBase> PropertyHandler;
public bool ShowProgress;
}
private readonly Dictionary<ComponentType, BindingEntry> _bindings = new Dictionary<ComponentType, BindingEntry>();
public void Initialize()
{
_progressPopupView = GameObject.FindAnyObjectByType<ProgressPopupView>(FindObjectsInactive.Include);
_conveyorProperty = GameObject.FindAnyObjectByType<ConveyorProperty>();
_nodeProperty = GameObject.FindAnyObjectByType<NodeProperty>();
Register<SourceProperty>(ComponentType.Source, (p, t, d) => p.SetPropertyWindow(t, d), showProgress: true);
Register<SinkProperty>(ComponentType.Sink, (p, t, d) => p.SetPropertyWindow(t, d), showProgress: true);
Register<ProcessorProperty>(ComponentType.Processor, (p, t, d) => p.SetPropertyWindow(t, d), showProgress: true);
Register<RackProperty>(ComponentType.Rack, (p, t, d) => p.SetPropertyWindow(t, d));
Register<QueueProperty>(ComponentType.Queue, (p, t, d) => p.SetPropertyWindow(t, d));
Register<ASRSProperty>(ComponentType.ASRS, (p, t, d) => p.SetPropertyWindow(t, d));
Register<RobotArmProperty>(ComponentType.RobotArm, (p, t, d) => p.SetPropertyWindow(t, d));
}
private void Register<T>(ComponentType type, Action<T, ComponentType, ComponentDataBase> setup, bool showProgress = false) where T : MonoBehaviour
{
var property = GameObject.FindAnyObjectByType<T>();
if (property == null) return;
_bindings[type] = new BindingEntry
{
PropertyHandler = (t, d) => setup(property, t, d),
ShowProgress = showProgress
};
}
public void BindStandardComponent(ComponentBase component)
{
if (!_bindings.TryGetValue(component.componentType, out var entry))
return;
component.onComponentClicked += entry.PropertyHandler;
if (entry.ShowProgress && _progressPopupView != null)
component.onComponentClicked += _progressPopupView.Show;
}
public void UnbindStandardComponent(ComponentBase component)
{
if (!_bindings.TryGetValue(component.componentType, out var entry))
return;
component.onComponentClicked -= entry.PropertyHandler;
if (entry.ShowProgress && _progressPopupView != null)
component.onComponentClicked -= _progressPopupView.Show;
}
public void BindConveyor(ConveyorComponent conveyor)
{
if (_conveyorProperty != null)
conveyor.onConveyorClicked += _conveyorProperty.SetPropertyWindow;
}
public void UnbindConveyor(ConveyorComponent conveyor)
{
if (_conveyorProperty != null)
conveyor.onConveyorClicked -= _conveyorProperty.SetPropertyWindow;
}
public void BindNode(NodeComponent node)
{
if (_nodeProperty != null)
node.onConveyorClicked += _nodeProperty.SetPropertyWindow;
}
public void UnbindNode(NodeComponent node)
{
if (_nodeProperty != null)
node.onConveyorClicked -= _nodeProperty.SetPropertyWindow;
}
}
}