Files
XRLib/Assets/Scripts/Simulator/Components/ComponentUIBinder.cs

96 lines
3.6 KiB
C#
Raw Permalink Normal View History

2026-03-17 10:52:33 +09:00
using System;
using System.Collections.Generic;
2026-02-26 14:04:33 +09:00
using UnityEngine;
namespace Simulator.Data
{
public class ComponentUIBinder
{
private ProgressPopupView _progressPopupView;
private ConveyorProperty _conveyorProperty;
private NodeProperty _nodeProperty;
2026-03-17 10:52:33 +09:00
private struct BindingEntry
2026-02-26 14:04:33 +09:00
{
2026-03-17 10:52:33 +09:00
public Action<ComponentType, ComponentDataBase> PropertyHandler;
public bool ShowProgress;
2026-02-26 14:04:33 +09:00
}
2026-03-17 10:52:33 +09:00
private readonly Dictionary<ComponentType, BindingEntry> _bindings = new Dictionary<ComponentType, BindingEntry>();
public void Initialize()
2026-02-26 14:04:33 +09:00
{
2026-03-17 10:52:33 +09:00
_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));
2026-02-26 14:04:33 +09:00
}
2026-03-17 10:52:33 +09:00
private void Register<T>(ComponentType type, Action<T, ComponentType, ComponentDataBase> setup, bool showProgress = false) where T : MonoBehaviour
2026-02-26 17:09:39 +09:00
{
2026-03-17 10:52:33 +09:00
var property = GameObject.FindAnyObjectByType<T>();
if (property == null) return;
_bindings[type] = new BindingEntry
2026-02-26 17:09:39 +09:00
{
2026-03-17 10:52:33 +09:00
PropertyHandler = (t, d) => setup(property, t, d),
ShowProgress = showProgress
};
2026-02-26 17:09:39 +09:00
}
2026-03-17 10:52:33 +09:00
public void BindStandardComponent(ComponentBase component)
2026-02-26 17:09:39 +09:00
{
2026-03-17 10:52:33 +09:00
if (!_bindings.TryGetValue(component.componentType, out var entry))
return;
component.onComponentClicked += entry.PropertyHandler;
if (entry.ShowProgress && _progressPopupView != null)
component.onComponentClicked += _progressPopupView.Show;
2026-02-26 17:09:39 +09:00
}
2026-03-17 10:52:33 +09:00
public void UnbindStandardComponent(ComponentBase component)
2026-02-26 17:09:39 +09:00
{
2026-03-17 10:52:33 +09:00
if (!_bindings.TryGetValue(component.componentType, out var entry))
return;
component.onComponentClicked -= entry.PropertyHandler;
if (entry.ShowProgress && _progressPopupView != null)
component.onComponentClicked -= _progressPopupView.Show;
2026-02-26 17:09:39 +09:00
}
2026-02-26 14:04:33 +09:00
public void BindConveyor(ConveyorComponent conveyor)
{
if (_conveyorProperty != null)
conveyor.onConveyorClicked += _conveyorProperty.SetPropertyWindow;
}
2026-03-17 10:52:33 +09:00
public void UnbindConveyor(ConveyorComponent conveyor)
{
if (_conveyorProperty != null)
conveyor.onConveyorClicked -= _conveyorProperty.SetPropertyWindow;
}
2026-02-26 14:04:33 +09:00
public void BindNode(NodeComponent node)
{
if (_nodeProperty != null)
node.onConveyorClicked += _nodeProperty.SetPropertyWindow;
}
2026-03-17 10:52:33 +09:00
public void UnbindNode(NodeComponent node)
{
if (_nodeProperty != null)
node.onConveyorClicked -= _nodeProperty.SetPropertyWindow;
}
2026-02-26 14:04:33 +09:00
}
}