From d0d90501a512dce45de38df2acebaa55cc87356c Mon Sep 17 00:00:00 2001 From: hzhang Date: Sat, 22 Jun 2024 02:28:37 +0800 Subject: [PATCH] Construct Compound --- Scenes/Bottle.cs | 36 ++++++++++++++++----------------- Scenes/CompoundConstructor.cs | 9 +++++++-- Scenes/CompoundConstructor.tscn | 2 -- Scenes/StringLoader.cs | 2 +- Scenes/StringLoader.tscn | 16 +++++++-------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Scenes/Bottle.cs b/Scenes/Bottle.cs index 9c5856e..1b48eb4 100755 --- a/Scenes/Bottle.cs +++ b/Scenes/Bottle.cs @@ -10,11 +10,10 @@ public partial class Bottle : MenuButton, IChemicalContainer private HeterogeneousMixture HeterogeneousMixture { get; set; } public double Volume() => ContainerVolume; - public HeterogeneousMixture Content { get; set; } + public HeterogeneousMixture Content { get; set; } = HeterogeneousMixture.Null; public double EnvironmentPressure { get; set; } public double EnvironmentTemperature { get; set; } - public HeterogeneousMixture ChemicalContent { get; set; } = HeterogeneousMixture.Null; - public HomogeneousMixture MainComponent => ChemicalContent.Layers + public HomogeneousMixture MainComponent => Content.Layers .DefaultIfEmpty(HomogeneousMixture.Null) .MaxBy(x => x.Amount); public Compound MainCompound => MainComponent.Compounds @@ -23,12 +22,24 @@ public partial class Bottle : MenuButton, IChemicalContainer private const string Empty = "Empty"; private string PotionName => $"Main: {(MainCompound == Compound.Null ? Empty : MainCompound.Expression)}"; - private string PotionAmount => $"Amount: {(MainCompound == Compound.Null ? 0d : ChemicalContent.Amount)}"; + private string PotionAmount => $"Amount: {(MainCompound == Compound.Null ? 0d : Content.Amount)}"; public Sprite2D ContentTexture { get; set; } - private void BuildMenu() + public void BuildMenu() { + + if (Content == HeterogeneousMixture.Null) + ContentTexture.Modulate = Color.Color8(0, 0, 0, 0); + else + { + ContentTexture.Modulate = Color.Color8( + MainComponent.ColorRed, + MainComponent.ColorGreen, + MainComponent.ColorBlue, + MainComponent.ColorTransparent + ); + } PopupMenu menu = GetPopup(); menu.Clear(); foreach (var w in menu.GetSignalConnectionList(PopupMenu.SignalName.IdPressed)) @@ -55,8 +66,8 @@ public partial class Bottle : MenuButton, IChemicalContainer GlobalScene.MainScene.SwitchToConstructor(); return; case 3: - ChemicalContent = GlobalScene.Flask.Content; - ChemicalContent.Container = this; + Content = GlobalScene.Flask.Content; + Content.Container = this; GlobalScene.Flask.Content = HeterogeneousMixture.Null; GlobalScene.Flask.Update(); return; @@ -71,17 +82,6 @@ public partial class Bottle : MenuButton, IChemicalContainer public override void _Ready() { ContentTexture = GetNode("Content"); - if (ChemicalContent == HeterogeneousMixture.Null) - ContentTexture.Modulate = new Color(0, 0, 0, 0); - else - { - ContentTexture.Modulate = new Color( - MainComponent.ColorRed, - MainComponent.ColorGreen, - MainComponent.ColorBlue, - MainComponent.ColorTransparent - ); - } BuildMenu(); } diff --git a/Scenes/CompoundConstructor.cs b/Scenes/CompoundConstructor.cs index 7143e38..7d3bde6 100755 --- a/Scenes/CompoundConstructor.cs +++ b/Scenes/CompoundConstructor.cs @@ -49,11 +49,12 @@ public partial class CompoundConstructor : Panel BaseAtom a = AtomResolver.Resolve(idx); Compound c = a.GrabCompound; c.Amount = 1; + a.Compound = c; Atom at = ResourceLoader.Load("res://Scenes/Atom.tscn").Instantiate(); at.BaseAtom = a; AddChild(at); at.Position = new(500, 500); - HomogeneousMixture.AddCompound(c); + HomogeneousMixture.AddCompound(c, true, true); Atoms.Add(at); } @@ -112,6 +113,8 @@ public partial class CompoundConstructor : Panel { HeterogeneousMixture.Container = GlobalScene.Demo.SelectedBottle; GlobalScene.Demo.SelectedBottle.Content = HeterogeneousMixture; + GlobalScene.Demo.SelectedBottle.BuildMenu(); + Clear(); } @@ -128,7 +131,8 @@ public partial class CompoundConstructor : Panel foreach (Atom atom in Atoms) { RemoveChild(atom); - + if (!BondMap.ContainsKey(atom)) + continue; foreach (Bond bond in BondMap[atom]) { if (!removedBonds.Contains(bond)) @@ -140,6 +144,7 @@ public partial class CompoundConstructor : Panel Atoms = new(); BondMap = new(); HeterogeneousMixture = new HeterogeneousMixture(); + HomogeneousMixture = HeterogeneousMixture.AddLayer(); } private void ForceClear() => Clear(false); diff --git a/Scenes/CompoundConstructor.tscn b/Scenes/CompoundConstructor.tscn index 1871fdd..d19d08b 100644 --- a/Scenes/CompoundConstructor.tscn +++ b/Scenes/CompoundConstructor.tscn @@ -36,8 +36,6 @@ text = "Clear" layout_mode = 2 text = "Back" -[node name="MovingLayer" type="CanvasLayer" parent="."] - [connection signal="pressed" from="ControlLayer/Controller/BuildMixture" to="." method="Build"] [connection signal="pressed" from="ControlLayer/Controller/Clear" to="." method="ForceClear"] [connection signal="pressed" from="ControlLayer/Controller/Back" to="." method="Back"] diff --git a/Scenes/StringLoader.cs b/Scenes/StringLoader.cs index d73a06a..48c8f42 100644 --- a/Scenes/StringLoader.cs +++ b/Scenes/StringLoader.cs @@ -10,7 +10,7 @@ public partial class StringLoader : Window // Called when the node enters the scene tree for the first time. public override void _Ready() { - Input = GetNode("Input"); + Input = GetNode("V1/Input"); GlobalScene.StringLoader = this; } diff --git a/Scenes/StringLoader.tscn b/Scenes/StringLoader.tscn index 35e23df..24a111e 100644 --- a/Scenes/StringLoader.tscn +++ b/Scenes/StringLoader.tscn @@ -6,7 +6,7 @@ size = Vector2i(500, 250) script = ExtResource("1_qoxxp") -[node name="VBoxContainer" type="VBoxContainer" parent="."] +[node name="V1" type="VBoxContainer" parent="."] anchors_preset = 8 anchor_left = 0.5 anchor_top = 0.5 @@ -19,25 +19,25 @@ offset_bottom = 37.5 grow_horizontal = 2 grow_vertical = 2 -[node name="Label" type="Label" parent="VBoxContainer"] +[node name="Label" type="Label" parent="V1"] layout_mode = 2 text = "Expression:" -[node name="Input" type="TextEdit" parent="VBoxContainer"] +[node name="Input" type="TextEdit" parent="V1"] custom_minimum_size = Vector2(450, 40) layout_mode = 2 -[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"] +[node name="HBoxContainer" type="HBoxContainer" parent="V1"] layout_mode = 2 alignment = 1 -[node name="Load" type="Button" parent="VBoxContainer/HBoxContainer"] +[node name="Load" type="Button" parent="V1/HBoxContainer"] layout_mode = 2 text = "Load" -[node name="Cancel" type="Button" parent="VBoxContainer/HBoxContainer"] +[node name="Cancel" type="Button" parent="V1/HBoxContainer"] layout_mode = 2 text = "Cancel" -[connection signal="pressed" from="VBoxContainer/HBoxContainer/Load" to="." method="Load"] -[connection signal="pressed" from="VBoxContainer/HBoxContainer/Cancel" to="." method="Cancel"] +[connection signal="pressed" from="V1/HBoxContainer/Load" to="." method="Load"] +[connection signal="pressed" from="V1/HBoxContainer/Cancel" to="." method="Cancel"]