Files
VirtualChemistry.Demo/Scenes/AtomActions.cs
2024-06-21 20:57:19 +08:00

67 lines
1.6 KiB
C#
Executable File

using Godot;
using System.Collections.Generic;
using VirtualChemistry.Chemistry.Bonds.Implements;
public partial class AtomActions : MenuButton
{
public Atom Atom => GetParent<Atom>();
private Dictionary<int, BaseBond> BondMap { get; set; } = new();
private CompoundConstructor CompoundConstructor => GlobalScene.CompoundConstructor;
public void BuildMenu()
{
PopupMenu menu = GetPopup();
menu.Clear();
foreach (var w in menu.GetSignalConnectionList(PopupMenu.SignalName.IdPressed))
menu.Disconnect(PopupMenu.SignalName.IdPressed, w["callable"].AsCallable());
menu.AddItem("Remove Atom", 0);
int idx = 1;
BondMap.Clear();
foreach (BaseBond b in Atom.BaseAtom.UnconnectedBonds)
{
BondMap[idx] = b;
menu.AddItem(b.BondType, idx);
if(CompoundConstructor.ConnectPending == b)
menu.SetItemDisabled(idx, true);
idx++;
}
menu.Connect(PopupMenu.SignalName.IdPressed, Callable.From((int id) => MenuHandle(id)));
}
private void MenuHandle(int idx)
{
if (idx == 0)
{
CompoundConstructor.RemoveAtomOf(Atom);
return;
}
if (CompoundConstructor.ConnectPending == BaseBond.Null)
{
CompoundConstructor.ConnectPending = BondMap[idx];
CompoundConstructor.PendingAtom = Atom;
}
else
{
CompoundConstructor.ConnectAtoms(Atom, BondMap[idx]);
}
BuildMenu();
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
BuildMenu();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override Variant _GetDragData(Vector2 atPosition)
{
return GetParent();
}
}