This commit is contained in:
h z
2024-06-26 14:23:02 +08:00
parent cbe5887f5f
commit b8af5f3ccd
22 changed files with 467 additions and 174 deletions

View File

@@ -1,34 +0,0 @@
using Godot;
public partial class Bond : Line2D
{
public Atom AtomFrom { get; set; }
public Atom AtomTo { get; set; }
private Label BondTypeA { get; set; }
private Label BondTypeB { get; set; }
public string TypeA { get; set; }
public string TypeB { get; set; }
public override void _Ready()
{
BondTypeA = GetNode<Label>("BondTypeA");
BondTypeB = GetNode<Label>("BondTypeB");
}
public void BondUpdate()
{
Vector2 v1 = AtomFrom.Position + new Vector2(32, 32);
Vector2 v2 = AtomTo.Position + new Vector2(32, 32);
Vector2 m = (v1 + v2) / 2;
Points = new[] { v1, m, v2 };
BondTypeA.Position = m - new Vector2(10, 10);
BondTypeB.Position = m + new Vector2(10, 10);
BondTypeA.Text = TypeA;
BondTypeB.Text = TypeB;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}

View File

@@ -1,18 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://ngc8dex7loqv"]
[ext_resource type="Script" path="res://Scenes/Bond.cs" id="1_rpgkt"]
[node name="Bond" type="Line2D"]
default_color = Color(1, 0.0392157, 1, 0.690196)
joint_mode = 2
begin_cap_mode = 2
end_cap_mode = 2
script = ExtResource("1_rpgkt")
[node name="BondTypeA" type="Label" parent="."]
offset_right = 40.0
offset_bottom = 23.0
[node name="BondTypeB" type="Label" parent="."]
offset_right = 40.0
offset_bottom = 23.0

View File

@@ -65,6 +65,8 @@ public partial class Bottle : MenuButton, IChemicalContainer
GlobalScene.MainScene.SwitchToConstructor();
break;
case 3:
if (GlobalScene.Flask.Content.Layers.Count == 0)
return;
Content = GlobalScene.Flask.Content;
Content.Container = this;
GlobalScene.Flask.Content = HeterogeneousMixture.Null;

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Godot;
using VirtualChemDemo;
using VirtualChemDemo.Concepts;
using VirtualChemistry.Chemistry.Atoms.Implements;
using VirtualChemistry.Chemistry.Atoms.Resolver;
using VirtualChemistry.Chemistry.Bonds.Implements;
@@ -15,7 +18,7 @@ public partial class CompoundConstructor : Panel
public HashSet<Atom> Atoms { get; set; } = new();
public BaseBond ConnectPending { get; set; } = BaseBond.Null;
public Atom PendingAtom { get; set; }
public Dictionary<Atom, HashSet<Bond>> BondMap { get; set; } = new();
public Dictionary<Atom, HashSet<Bond2D>> BondMap { get; set; } = new();
// Called when the node enters the scene tree for the first time.
public override void _Ready()
@@ -25,9 +28,65 @@ public partial class CompoundConstructor : Panel
HomogeneousMixture = HeterogeneousMixture.AddLayer();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
private static class AtomPositionFixMemory
{
public static Atom[] SavedAtoms { get; set; } = null;
private static int I { get; set; }
private static int J { get; set; }
public static int Max => SavedAtoms.Length;
public static void Reset()
{
SavedAtoms = null;
I = 0;
J = 0;
}
public static void OneStep()
{
if (J >= Max)
{
J = 0;
I++;
}
if (I >= Max)
{
Reset();
return;
}
if (I == J)
{
J++;
return;
}
Atom a1 = SavedAtoms[I];
Atom a2 = SavedAtoms[J];
if ((a1.Position - a2.Position).Length() < 64)
{
Vector2 pd = a1.Position - a2.Position;
if (pd == Vector2.Zero)
pd = new Vector2(J % 3 - 2, I % 3 - 1);
a1.Position += pd;
a2.Position -= pd;
if (GlobalScene.CompoundConstructor.BondMap.TryGetValue(a1, out var bonds1))
foreach (Bond2D b1 in bonds1)
b1.BondUpdate();
if(GlobalScene.CompoundConstructor.BondMap.TryGetValue(a2, out var bonds2))
foreach (Bond2D b2 in bonds2)
b2.BondUpdate();
}
J++;
}
}
public override void _Process(double delta)
{
if (AtomPositionFixMemory.SavedAtoms == null)
AtomPositionFixMemory.SavedAtoms = Atoms.ToArray();
AtomPositionFixMemory.OneStep();
}
public override bool _CanDropData(Vector2 atPosition, Variant data)
@@ -40,7 +99,7 @@ public partial class CompoundConstructor : Panel
Atom a = data.As<Atom>();
a.Position = atPosition;
if(BondMap.ContainsKey(a))
foreach (Bond b in BondMap[a])
foreach (Bond2D b in BondMap[a])
b.BondUpdate();
}
@@ -56,6 +115,7 @@ public partial class CompoundConstructor : Panel
at.Position = new(500, 500);
HomogeneousMixture.AddCompound(c, true, true);
Atoms.Add(at);
AtomPositionFixMemory.Reset();
}
public void RemoveAtomOf(Atom atom)
@@ -63,11 +123,11 @@ public partial class CompoundConstructor : Panel
foreach (BaseBond b in atom.BaseAtom.ConnectedBonds)
b.Disconnect();
Bond[] toRemove = Array.Empty<Bond>();
if (BondMap.TryGetValue(atom, out HashSet<Bond> value))
Bond2D[] toRemove = Array.Empty<Bond2D>();
if (BondMap.TryGetValue(atom, out HashSet<Bond2D> value))
toRemove = value.ToArray();
foreach (Bond b in toRemove)
foreach (Bond2D b in toRemove)
{
BondMap[b.AtomFrom].Remove(b);
BondMap[b.AtomTo].Remove(b);
@@ -79,29 +139,32 @@ public partial class CompoundConstructor : Panel
atom.BaseAtom.Compound = Compound.Null;
RemoveChild(atom);
Atoms.Remove(atom);
AtomPositionFixMemory.Reset();
}
public void ConnectAtoms(Atom a, BaseBond b)
{
b.Connect(ConnectPending);
Bond bond = ResourceLoader.Load<PackedScene>("res://Scenes/Bond.tscn").Instantiate<Bond>();
Bond2D bond = Bond2D.Resolve(b, ConnectPending);
bond.AtomFrom = PendingAtom;
bond.AtomTo = a;
bond.TypeA = ConnectPending.BondType;
bond.TypeB = b.BondType;
bond.BondFrom = ConnectPending;
bond.BondTo = b;
if (!BondMap.ContainsKey(a))
BondMap[a] = new HashSet<Bond>();
BondMap[a] = new HashSet<Bond2D>();
BondMap[a].Add(bond);
if (!BondMap.ContainsKey(PendingAtom!))
BondMap[PendingAtom] = new HashSet<Bond>();
BondMap[PendingAtom] = new HashSet<Bond2D>();
BondMap[PendingAtom].Add(bond);
ConnectPending = BaseBond.Null;
a.AtomActions.BuildMenu();
PendingAtom.AtomActions.BuildMenu();
PendingAtom = null;
AddChild(bond);
bond.BondUpdate();
foreach (Bond2D bd in BondMap[a])
bd.BondUpdate();
}
private void Back()
@@ -111,11 +174,14 @@ public partial class CompoundConstructor : Panel
private void Build()
{
if(HomogeneousMixture.Compounds.Count==0)
return;
HeterogeneousMixture.Container = GlobalScene.Demo.SelectedBottle;
GlobalScene.Demo.SelectedBottle.Content = HeterogeneousMixture;
GlobalScene.Demo.SelectedBottle.BuildMenu();
Clear();
Back();
}
private void Clear(bool save = true)
@@ -127,13 +193,13 @@ public partial class CompoundConstructor : Panel
return;
}
HashSet<Bond> removedBonds = new();
HashSet<Bond2D> removedBonds = new();
foreach (Atom atom in Atoms)
{
RemoveChild(atom);
if (!BondMap.ContainsKey(atom))
continue;
foreach (Bond bond in BondMap[atom])
foreach (Bond2D bond in BondMap[atom])
{
if (!removedBonds.Contains(bond))
RemoveChild(bond);

View File

@@ -7,7 +7,7 @@ public partial class Flask : TextureRect, IChemicalContainer
{
public double ContainerVolume { get; set; } = 0.5;
private TextureRect InnerLayer { get; set; }
public double Volume() => ContainerVolume;
public double Volume() => GlobalScene.ElasticContainer ? Content.Volume : ContainerVolume;
public HeterogeneousMixture Content { get; set; }
public double EnvironmentPressure { get; set; }
@@ -55,6 +55,11 @@ public partial class Flask : TextureRect, IChemicalContainer
InnerLayer = GetNode<TextureRect>("InnerLayer");
}
private void ShowAll()
{
GlobalScene.MainControlPanel.BuildAllTree();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{

View File

@@ -21,3 +21,13 @@ offset_right = 40.0
offset_bottom = 40.0
mouse_filter = 2
texture = ExtResource("1_b34pm")
[node name="ShowAll" type="Button" parent="."]
layout_mode = 0
offset_left = 185.0
offset_top = 107.0
offset_right = 229.0
offset_bottom = 138.0
text = "Show All"
[connection signal="pressed" from="ShowAll" to="." method="ShowAll"]

View File

@@ -8,7 +8,7 @@ public partial class FlaskContent : ColorRect
public HomogeneousMixture Mixture { get; set; } = HomogeneousMixture.Null;
private const double ContainerHeight = 512d;
public double ContainerVolume => Mixture.HeterogeneousMixture.Container.Volume();
private double ContainerVolume => Mixture.HeterogeneousMixture.Container.Volume();
public double Volume => Mixture.Volume;
private TextureButton Mask { get; set; }
@@ -31,22 +31,21 @@ public partial class FlaskContent : ColorRect
double height = (Volume / ContainerVolume) * ContainerHeight;
Position = new Vector2(Position.X, 512-(float)(StartFrom + height));
Size = new Vector2(Size.X, (float)height);
//Mask.Size = Size;
Color = Color.Color8(
Mixture.ColorRed,
Mixture.ColorGreen,
Mixture.ColorGreen,
Mixture.ColorTransparent
);
/*
Animation a = AP.GetAnimation("HeightChange");
/*Animation a = AP.GetAnimation("HeightChange");
a.TrackSetKeyValue(0, 0, Size);
a.TrackSetKeyValue(0, 1, new Vector2(Size.X, (float)height));
AP.Play("HeightChange");
AP.Play("PositionChange");
a = AP.GetAnimation("PositionChange");
a.TrackSetKeyValue(0,0,Position);
a.TrackSetKeyValue(0,1, new Vector2(Position.X, -(float)StartFrom));
a.TrackSetKeyValue(0,1, new Vector2(Position.X, 512-(float)(StartFrom + height)));
Color = Color.Color8(Mixture.ColorRed, Mixture.ColorGreen, Mixture.ColorGreen, Mixture.ColorTransparent);
AP.Play();*/
}

View File

@@ -16,7 +16,7 @@ public partial class MainControlPanel : HBoxContainer
private Label FreeVolume { get; set; }
private Label Stiffness { get; set; }
private Tree Compounds { get; set; }
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
EnvTemperature = GetNode<HSlider>("V1/EnvTemperature");
@@ -53,13 +53,43 @@ public partial class MainControlPanel : HBoxContainer
BuildTree();
}
public void BuildAllTree()
{
Compounds.Clear();
TreeItem root = Compounds.CreateItem();
root.SetText(0, "Mixtures: ");
int i = 1;
foreach (HomogeneousMixture h in GlobalScene.Flask.Content.LayerOrder)
{
TreeItem sRoot = Compounds.CreateItem(root);
sRoot.SetText(0,$"Mixture{i} Amt{h.Amount}");
foreach (var group in h.Compounds.GroupBy(x => x.Expression))
{
TreeItem t = Compounds.CreateItem(root);
t.SetText(0, group.Key);
t.SetSelectable(0, false);
int j = 1;
foreach (Compound c in group)
{
TreeItem x = Compounds.CreateItem(t);
x.SetText(0, $"{j}th isomer");
x.SetMeta("isoExp", c.IsoRepresentation);
j++;
}
i++;
}
}
}
private void BuildTree()
{
Compounds.Clear();
var groups = SelectedMixture.Compounds.GroupBy(x => x.Expression);
TreeItem root = Compounds.CreateItem(null);
root.SetText(0, "Compounds: ");
foreach (var group in groups)
{
TreeItem t = Compounds.CreateItem();
TreeItem t = Compounds.CreateItem(root);
t.SetText(0, group.Key);
t.SetSelectable(0, false);
int i = 1;
@@ -108,4 +138,14 @@ public partial class MainControlPanel : HBoxContainer
GlobalScene.Flask.ContainerVolume = vol;
GlobalScene.Flask.Update();
}
private void SetElasticContainer(bool on)
{
GlobalScene.ElasticContainer = on;
if (on)
ContainerVolume.Editable = false;
else
ContainerVolume.Editable = true;
}
}

View File

@@ -44,6 +44,12 @@ min_value = 0.2
max_value = 20.0
step = 0.2
value = 1.0
ticks_on_borders = true
[node name="ElasticContainer" type="CheckBox" parent="V1"]
layout_mode = 2
tooltip_text = "Volume of container always equal to the volume of content"
text = "Elastic Container"
[node name="VSeparator" type="VSeparator" parent="."]
layout_mode = 2
@@ -138,12 +144,8 @@ layout_mode = 2
[node name="V3" type="VBoxContainer" parent="."]
layout_mode = 2
[node name="Label" type="Label" parent="V3"]
layout_mode = 2
text = "Compounents"
[node name="Compounds" type="Tree" parent="V3"]
custom_minimum_size = Vector2(150, 200)
custom_minimum_size = Vector2(250, 200)
layout_mode = 2
[node name="VSeparator4" type="VSeparator" parent="."]
@@ -154,4 +156,5 @@ layout_mode = 2
[connection signal="pressed" from="V1/RemoveBottom" to="." method="RemoveBottom"]
[connection signal="value_changed" from="V1/EnvTemperature" to="." method="SetTemperature"]
[connection signal="value_changed" from="V1/ContainerVolume" to="." method="SetVolume"]
[connection signal="toggled" from="V1/ElasticContainer" to="." method="SetElasticContainer"]
[connection signal="item_selected" from="V3/Compounds" to="." method="IsomerSelectedHandler"]

View File

@@ -4,12 +4,12 @@ using System;
public partial class MainScene : Node2D
{
public Node CurrentScene { get; set; }
public VirtualChemDemo Demo { get; set; }
public VirtualChemistryDemo Demo { get; set; }
public override void _Ready()
{
GlobalScene.MainScene = this;
Demo = GetNode<VirtualChemDemo>("Demo");
Demo = GetNode<VirtualChemistryDemo>("Demo");
CurrentScene = Demo;
}

View File

@@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://cgdqxc2mx6w8l"]
[ext_resource type="Script" path="res://Scenes/MainScene.cs" id="1_uwony"]
[ext_resource type="PackedScene" uid="uid://br78rurjakp3a" path="res://Scenes/VirtualChemDemo.tscn" id="2_vpu75"]
[ext_resource type="PackedScene" uid="uid://br78rurjakp3a" path="res://Scenes/VirtualChemistryDemo.tscn" id="2_vpu75"]
[node name="MainScene" type="Node2D"]
script = ExtResource("1_uwony")

9
Scenes/MultiBond.cs Normal file
View File

@@ -0,0 +1,9 @@
using VirtualChemDemo.Concepts;
public partial class MultiBond : Bond2D
{
public override void BondUpdate()
{
throw new System.NotImplementedException();
}
}

25
Scenes/SelfBond.cs Normal file
View File

@@ -0,0 +1,25 @@
using System.Collections.Generic;
using Godot;
using VirtualChemDemo.Concepts;
namespace VirtualChemDemo.Scenes;
public partial class SelfBond : Bond2D
{
public override void BondUpdate()
{
Reindex();
List<Vector2> points = new();
Vector2 pcenter = new(30, 0);
pcenter = pcenter.Rotated((float)Index * Mathf.Pi * 2f / (float)Multi);
Vector2 center = AtomFrom.Position + new Vector2(32, 32) + pcenter;
Vector2 d = AtomFrom.Position + new Vector2(32, 32) - center;
for (int i = 0; i <= 24; i++)
points.Add(center + d.Rotated(i*Mathf.Pi*2/24));
BondTypeA.Position = points[12] + new Vector2(-20, 0);
BondTypeB.Position = points[12] + new Vector2(20, 0);
Points = points.ToArray();
BondTypeA.Text = BondFrom.BondType;
BondTypeB.Text = BondTo.BondType;
}
}

97
Scenes/SingleBond.cs Executable file
View File

@@ -0,0 +1,97 @@
using System;
using Godot;
using VirtualChemDemo.Concepts;
public partial class SingleBond : Bond2D
{
private static int BIndex(int index, int multi)
{
if (multi == 1)
return 0;
if (multi % 2 == 1)
{
int center = multi / 2 + 1;
return center - index;
}
if (multi % 2 == 0)
{
double center = multi / 2 + 0.5;
return (int)(center > index ? Math.Ceiling(center - index) : Math.Floor(center - index));
}
return 0;
}
private void MakeCurve(float curveRate=20f, int cDir = 1)
{
Vector2 Start = AtomFrom.Position + new Vector2(32, 32);;
Vector2 End = AtomTo.Position + new Vector2(32, 32);;
Vector2[] xs = new Vector2[17];
int[] x2 = new[] { 4, 12 };
int[] x3 = new[] { 2, 6, 10, 14};
int[] x4 = new[] { 1, 3, 5, 7, 9, 11, 13, 15 };
Vector2 vd = End - Start;
Vector2 nor = vd.Rotated(Mathf.Pi / 2 * cDir);
Vector2 center = vd / 2 + 4f * nor / curveRate;
xs[0] = Start;
xs[16] = End;
xs[8] = center + xs[0];
foreach (int i in x2)
{
Vector2 v = xs[i + 4] - xs[i - 4];
Vector2 n = v.Rotated(Mathf.Pi / 2 * cDir);
Vector2 w = v / 2 + 2f * n / curveRate;
xs[i] = w + xs[i - 4];
}
foreach (int i in x3)
{
Vector2 v = xs[i + 2] - xs[i - 2];
Vector2 n = v.Rotated(Mathf.Pi / 2 * cDir);
Vector2 w = v / 2 + n / curveRate;
xs[i] = w + xs[i - 2];
}
foreach (int i in x4)
{
Vector2 v = xs[i + 1] - xs[i - 1];
Vector2 n = v.Rotated(Mathf.Pi / 2 * cDir);
Vector2 w = v / 2 + n / curveRate;
xs[i] = w + xs[i - 1];
}
BondTypeA.Position = xs[10];
BondTypeB.Position = xs[6];
Points = xs;
}
public override void BondUpdate()
{
Reindex();
int bindex = BIndex(Index, Multi);
if (bindex == 0)
{
Vector2 v1 = AtomFrom.Position + new Vector2(32, 32);
Vector2 v2 = AtomTo.Position + new Vector2(32, 32);
Vector2 m = (v1 + v2) / 2;
Points = new[] { v1, m, v2 };
BondTypeA.Position = m - new Vector2(10, 10);
BondTypeB.Position = m + new Vector2(10, 10);
}
else
{
float curveRate = 25 - Math.Abs(bindex)*5;
int cDir = bindex > 0 ? 1 : -1;
MakeCurve(curveRate, cDir);
}
BondTypeA.Text = BondFrom.BondType;
BondTypeB.Text = BondTo.BondType;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}

View File

@@ -4,10 +4,7 @@ using VirtualChemistry.Chemistry.Mixtures.Resolver;
public partial class StringLoader : Window
{
public TextEdit Input { get; set; }
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Input = GetNode<TextEdit>("V1/Input");
@@ -21,6 +18,7 @@ public partial class StringLoader : Window
HeterogeneousMixture mixture = HeterogeneousMixtureResolver.Resolve(expression);
GlobalScene.Demo.SelectedBottle.Content = mixture;
mixture.Container = GlobalScene.Demo.SelectedBottle;
GlobalScene.Demo.SelectedBottle.BuildMenu();
Visible = false;
}

View File

@@ -1,87 +0,0 @@
[gd_scene load_steps=6 format=3 uid="uid://br78rurjakp3a"]
[ext_resource type="Script" path="res://Scenes/VirtualChemDemo.cs" id="1_aartk"]
[ext_resource type="PackedScene" uid="uid://re3bwidsx0do" path="res://Scenes/MainControlPanel.tscn" id="2_cfpbe"]
[ext_resource type="PackedScene" uid="uid://u7apjw1vvak3" path="res://Scenes/Bottle.tscn" id="2_ei0nr"]
[ext_resource type="PackedScene" uid="uid://bde5n0vfm6jvs" path="res://Scenes/Flask.tscn" id="3_p6s6c"]
[ext_resource type="PackedScene" uid="uid://cubdalatqln4x" path="res://Scenes/StringLoader.tscn" id="5_5y6ld"]
[node name="VicrtualChemDemo" type="Node2D"]
script = ExtResource("1_aartk")
[node name="VBoxContainer" type="VBoxContainer" parent="."]
offset_right = 40.0
offset_bottom = 40.0
[node name="HSeparator" type="HSeparator" parent="VBoxContainer"]
layout_mode = 2
[node name="MainControlPanel" parent="VBoxContainer" instance=ExtResource("2_cfpbe")]
layout_mode = 2
[node name="HSeparator2" type="HSeparator" parent="VBoxContainer"]
layout_mode = 2
[node name="Potions" type="HBoxContainer" parent="."]
offset_left = 70.0
offset_top = 553.0
offset_right = 670.0
offset_bottom = 621.0
[node name="Prev" type="Button" parent="Potions"]
layout_mode = 2
text = "<-"
[node name="Items" type="HBoxContainer" parent="Potions"]
layout_mode = 2
[node name="MenusButton" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle2" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle3" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle4" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle5" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle6" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Bottle7" parent="Potions/Items" instance=ExtResource("2_ei0nr")]
layout_mode = 2
[node name="Next" type="Button" parent="Potions"]
layout_mode = 2
text = "->"
[node name="Flask" parent="." instance=ExtResource("3_p6s6c")]
offset_left = 747.0
offset_top = 57.0
offset_right = 875.0
offset_bottom = 569.0
[node name="HRepr" type="HBoxContainer" parent="."]
offset_left = 32.0
offset_top = 272.0
offset_right = 702.0
offset_bottom = 352.0
[node name="Label" type="Label" parent="HRepr"]
layout_mode = 2
text = "Full Expression"
[node name="FullRepr" type="RichTextLabel" parent="HRepr"]
custom_minimum_size = Vector2(550, 80)
layout_mode = 2
[node name="ExpressionLoader" parent="." instance=ExtResource("5_5y6ld")]
position = Vector2i(250, 200)

View File

@@ -1,6 +1,6 @@
using Godot;
public partial class VirtualChemDemo : Node2D
public partial class VirtualChemistryDemo : Node2D
{
public Flask Flask { get; set; }
public RichTextLabel FullRepr { get; set; }