From 14948253c2a98e63f2fca50a0929e8f369812b9e Mon Sep 17 00:00:00 2001 From: Dominik Kopczynski Date: Wed, 29 Nov 2023 13:40:14 +0100 Subject: [PATCH] structure now aligned to 0/0 --- .../StructureEditor/LipidStructure.cs | 225 +++++++++++++----- .../StructureEditor.Designer.cs | 3 + .../StructureEditor/StructureEditor.cs | 51 ++-- 3 files changed, 203 insertions(+), 76 deletions(-) diff --git a/LipidCreator/StructureEditor/LipidStructure.cs b/LipidCreator/StructureEditor/LipidStructure.cs index 8c0c439..60fe97e 100644 --- a/LipidCreator/StructureEditor/LipidStructure.cs +++ b/LipidCreator/StructureEditor/LipidStructure.cs @@ -6,12 +6,94 @@ using System.Xml.Linq; using System.Linq; using System.Drawing.Drawing2D; +using System.IO; namespace LipidCreatorStructureEditor { public enum NodeState {Enabled, Disabled, Hidden}; + public class SPoint + { + public int X; + public int Y; + public SPoint(int x, int y) + { + X = x; + Y = y; + } + + public static SPoint operator +(SPoint p1, SPoint p2) + { + return new SPoint(p1.X + p2.X, p1.Y + p2.Y); + } + + public Point pI() + { + return new Point(X, Y); + } + } + + + public class SPointF + { + public float X; + public float Y; + + public SPointF(float x, float y) + { + X = x; + Y = y; + } + + public SPointF(Point p) + { + X = p.X; + Y = p.Y; + } + + public static SPointF operator +(SPointF p1, SPointF p2) + { + return new SPointF(p1.X + p2.X, p1.Y + p2.Y); + } + + public static SPointF operator +(SPointF p1, SPoint p2) + { + return new SPointF(p1.X + p2.X, p1.Y + p2.Y); + } + + public static SPointF operator +(SPointF p, RectangleF r) + { + return new SPointF(p.X + r.X, p.Y + r.Y); + } + + public static SPointF operator -(SPointF p1, SPointF p2) + { + return new SPointF(p1.X - p2.X, p1.Y - p2.Y); + } + + public static SPointF operator -(SPointF p1, SPoint p2) + { + return new SPointF(p1.X - p2.X, p1.Y - p2.Y); + } + + public static SPointF operator -(SPointF p, RectangleF r) + { + return new SPointF(p.X - r.X, p.Y - r.Y); + } + + public PointF pF() + { + return new PointF(X, Y); + } + + public Point pI() + { + return new Point((int)X, (int)Y); + } + } + + public class S { public static string space(int n) { @@ -21,21 +103,21 @@ public static string space(int n) public class NodeProjection { - public PointF shift = new PointF(0, 0); + public SPointF shift = new SPointF(0, 0); public NodeState nodeState = NodeState.Enabled; - public PointF previousShift; + public SPointF previousShift; public NodeProjection(NodeState state = NodeState.Enabled) { - previousShift = new PointF(shift.X, shift.Y); + previousShift = new SPointF(shift.X, shift.Y); nodeState = state; } public NodeProjection(NodeProjection copy) { - shift = new PointF(copy.shift.X, copy.shift.Y); + shift = new SPointF(copy.shift.X, copy.shift.Y); nodeState = copy.nodeState; - previousShift = new PointF(shift.X, shift.Y); + previousShift = new SPointF(shift.X, shift.Y); } public void toggleState(){ @@ -49,7 +131,7 @@ public void toggleState(){ public void prepareMove() { - previousShift = new PointF(shift.X, shift.Y); + previousShift = new SPointF(shift.X, shift.Y); } @@ -62,7 +144,7 @@ public void serialize(StringBuilder sb, int id, int tabs = 0) public class StructureNode { public int id; - public PointF position; + public SPointF position; public RectangleF boundingBox = new RectangleF(); public string text = ""; public List decorators = new List(); @@ -70,7 +152,7 @@ public class StructureNode public Dictionary nodeProjections = new Dictionary(); public LipidStructure lipidStructure; - public StructureNode(LipidStructure _lipidStructure, int _id, PointF p) + public StructureNode(LipidStructure _lipidStructure, int _id, SPointF p) { lipidStructure = _lipidStructure; id = _id; @@ -78,7 +160,7 @@ public StructureNode(LipidStructure _lipidStructure, int _id, PointF p) isCarbon = true; } - public StructureNode(LipidStructure _lipidStructure, int _id, PointF p, RectangleF b, string t) + public StructureNode(LipidStructure _lipidStructure, int _id, SPointF p, RectangleF b, string t) { lipidStructure = _lipidStructure; id = _id; @@ -101,7 +183,7 @@ public void prepareMove() } - public void move(PointF shift) + public void move(SPointF shift) { if (lipidStructure.currentProjection == 0) return; nodeProjections[lipidStructure.currentProjection].shift.X = nodeProjections[lipidStructure.currentProjection].previousShift.X + shift.X; @@ -176,10 +258,10 @@ public void serialize(StringBuilder sb, int tabs = 0) public class StructureEdge { - public PointF start; - public PointF end; + public SPointF start; + public SPointF end; - public StructureEdge(PointF s, PointF e) + public StructureEdge(SPointF s, SPointF e) { start = s; end = e; @@ -187,8 +269,8 @@ public StructureEdge(PointF s, PointF e) public StructureEdge(StructureEdge copy) { - start = new PointF(copy.start.X, copy.start.Y); - end = new PointF(copy.end.X, copy.end.Y); + start = new SPointF(copy.start.X, copy.start.Y); + end = new SPointF(copy.end.X, copy.end.Y); } public void serialize(StringBuilder sb, int tabs = 0) @@ -247,7 +329,30 @@ public void toggleState() public void serialize(StringBuilder sb, int tabs = 0) { + sb.Append(S.space(tabs) + String.Format("\n", id, startId, endId, isDoubleBond)); + if (edgeSingle != null) + { + sb.Append(S.space(tabs + 1) + "\n"); + edgeSingle.serialize(sb, tabs + 2); + sb.Append(S.space(tabs + 1) + "\n"); + } + + if (edgesDouble.Count > 0) + { + sb.Append(S.space(tabs + 1) + "\n"); + foreach (var edge in edgesDouble) edge.serialize(sb, tabs + 2); + sb.Append(S.space(tabs + 1) + "\n"); + } + if (bondProjections.Count > 0) + { + sb.Append(S.space(tabs + 1) + "\n"); + foreach (var kvp in bondProjections) sb.Append(S.space(tabs + 2) + String.Format("\n", kvp.Key, kvp.Value)); + sb.Append(S.space(tabs + 1) + "\n"); + } + + + sb.Append(S.space(tabs) + "\n"); } } @@ -275,7 +380,7 @@ public void toggleCharge() public void serialize(StringBuilder sb, int tabs = 0) { - + sb.Append(String.Format(S.space(tabs) + "\n", id, fragmentName, charge)); } } @@ -289,6 +394,7 @@ public class LipidStructure public HashSet additionalBonds = new HashSet(); public Dictionary positiveFragments = new Dictionary(); public Dictionary negativeFragments = new Dictionary(); + public SPointF middlePoint; public Dictionary idToNode = new Dictionary(); public float factor = 2.5f; @@ -343,6 +449,7 @@ public LipidStructure(string file_name, Form form) nodeFont = new Font("Arial", fontSize); decoratorFont = new Font("Arial", (float)(fontSize * 0.5)); + middlePoint = new SPointF((float)(form.Width / 2.0), (float)(form.Height / 2.0)); XDocument doc = XDocument.Load(file_name); graphics = form.CreateGraphics(); @@ -385,7 +492,7 @@ public LipidStructure(string file_name, Form form) if ((new HashSet(){"R1", "R2", "R3", "R4"}).Contains(text)) { - StructureNode sn = new StructureNode(this, nodeId, new PointF(x, y), drawRect, "R"); + StructureNode sn = new StructureNode(this, nodeId, new SPointF(x, y), drawRect, "R"); x -= (float)Math.Abs(x * 0.02); nodes.Add(sn); idToNode.Add(nodeId, sn); @@ -396,12 +503,12 @@ public LipidStructure(string file_name, Form form) string subIndex = text.Substring(1, 1); RectangleF subscript = new RectangleF(x, y, 0, 0); - sn.decorators.Add(new StructureNode(this, -1, new PointF(x, y), subscript, subIndex)); + sn.decorators.Add(new StructureNode(this, -1, new SPointF(x, y), subscript, subIndex)); } else { - StructureNode sn = new StructureNode(this, nodeId, new PointF(x, y), drawRect, text); + StructureNode sn = new StructureNode(this, nodeId, new SPointF(x, y), drawRect, text); nodes.Add(sn); idToNode.Add(nodeId, sn); } @@ -435,7 +542,7 @@ public LipidStructure(string file_name, Form form) float bx = (float)Convert.ToDouble(tokensBB[0]) - 1; float by = (float)Convert.ToDouble(tokensBB[1]) - 1 - (symbol.Equals("+") ? 2 : 0); RectangleF drawRect = new RectangleF(bx, by, 0, 0); - idToNode[parentId].decorators.Add(new StructureNode(this, nodeIdG, new PointF(bx, by), drawRect, symbol)); + idToNode[parentId].decorators.Add(new StructureNode(this, nodeIdG, new SPointF(bx, by), drawRect, symbol)); } catch (Exception){} } @@ -455,11 +562,6 @@ public LipidStructure(string file_name, Form form) Bond bond = null; try { - float xB = nodeB.position.X; - float yB = nodeB.position.Y; - float xE = nodeE.position.X; - float yE = nodeE.position.Y; - // get the edge color bool isDoubleBond = ((string)edge.Attribute("Order") != null) && edge.Attribute("Order").Value.ToString().Equals("2"); @@ -490,8 +592,8 @@ public LipidStructure(string file_name, Form form) float midX = minX + (maxX - minX) / 2.0f; float midY = minY + (maxY - minY) / 2.0f; - float offsetX = (float)form.Size.Width / 2.0f - midX * factor; - float offsetY = (float)form.Size.Height / 2.0f - midY * factor; + float offsetX = /* (float)form.Size.Width / 2.0f */ - midX * factor; + float offsetY = /* (float)form.Size.Height / 2.0f */ - midY * factor; nodeFont = new Font("Arial", fontSize * factor); @@ -500,7 +602,7 @@ public LipidStructure(string file_name, Form form) { float x = node.position.X * factor + offsetX; float y = node.position.Y * factor + offsetY; - node.position = new PointF(x, y); + node.position = new SPointF(x, y); x = node.boundingBox.X * factor + offsetX; y = node.boundingBox.Y * factor + offsetY; @@ -514,7 +616,7 @@ public LipidStructure(string file_name, Form form) { float xd = decorator.position.X * factor + offsetX; float yd = decorator.position.Y * factor + offsetY; - decorator.position = new PointF(xd, yd); + decorator.position = new SPointF(xd, yd); xd = decorator.boundingBox.X * factor + offsetX; yd = decorator.boundingBox.Y * factor + offsetY; @@ -542,12 +644,16 @@ public LipidStructure(string file_name, Form form) computeBonds(); changeFragment(); countNodeConnections(); - - serialize("foo"); } + public void setMiddlePoint(SPointF p) + { + middlePoint = p; + } + + public void serialize(string file_name) { @@ -595,12 +701,13 @@ public void serialize(string file_name) foreach (var kvp in negativeFragments) kvp.Value.serialize(sb, 2); sb.Append(" \n"); } - - sb.Append("\n"); - Console.WriteLine(sb.ToString()); + using (StreamWriter outputFile = new StreamWriter(Path.Combine(".", file_name))) + { + outputFile.WriteLine(sb.ToString()); + } } @@ -704,14 +811,14 @@ public void computeBonds() float y1B = yB - deltaY * dbSpace * factor; float x1E = xE - deltaX * dbSpace * factor; float y1E = yE - deltaY * dbSpace * factor; - bond.edgesDouble.Add(new StructureEdge(new PointF(x1B, y1B), new PointF(x1E, y1E))); + bond.edgesDouble.Add(new StructureEdge(new SPointF(x1B, y1B), new SPointF(x1E, y1E))); float x2B = xB + deltaX * dbSpace * factor; float y2B = yB + deltaY * dbSpace * factor; float x2E = xE + deltaX * dbSpace * factor; float y2E = yE + deltaY * dbSpace * factor; - bond.edgesDouble.Add(new StructureEdge(new PointF(x2B, y2B), new PointF(x2E, y2E))); + bond.edgesDouble.Add(new StructureEdge(new SPointF(x2B, y2B), new SPointF(x2E, y2E))); } else { @@ -720,11 +827,11 @@ public void computeBonds() float y1B = yB - deltaY * (2 * dbSpace * factor); float x1E = xE - deltaX * (2 * dbSpace * factor); float y1E = yE - deltaY * (2 * dbSpace * factor); - bond.edgesDouble.Add(new StructureEdge(new PointF(x1B, y1B), new PointF(x1E, y1E))); - bond.edgesDouble.Add(new StructureEdge(new PointF(xB, yB), new PointF(xE, yE))); + bond.edgesDouble.Add(new StructureEdge(new SPointF(x1B, y1B), new SPointF(x1E, y1E))); + bond.edgesDouble.Add(new StructureEdge(new SPointF(xB, yB), new SPointF(xE, yE))); } - bond.edgeSingle = new StructureEdge(new PointF(xB, yB), new PointF(xE, yE)); + bond.edgeSingle = new StructureEdge(new SPointF(xB, yB), new SPointF(xE, yE)); } foreach (var bond in deleteBonds) @@ -738,16 +845,16 @@ public void computeBonds() - public void addNode(int x, int y) + public void addNode(SPointF pos) { if (currentProjection == 0) return; - + pos = pos - middlePoint; nodeFont = new Font("Arial", fontSize * factor); Size size = TextRenderer.MeasureText(graphics, "C", nodeFont); float w = size.Width * 0.9f; float h = size.Height * 0.9f; - RectangleF drawRect = new RectangleF(x - w * 0.5f, y - h * 0.5f, w, h); - StructureNode sn = new StructureNode(this, ++currentNodeId, new PointF(x, y), drawRect, "C"); + RectangleF drawRect = new RectangleF(pos.X - w * 0.5f, pos.Y - h * 0.5f, w, h); + StructureNode sn = new StructureNode(this, ++currentNodeId, pos, drawRect, "C"); int fragId = currentFragment.id; @@ -816,7 +923,7 @@ public Graphics setClipping(Graphics g, Form form) NodeProjection nodeProjection = node.nodeProjections[currentProjection]; if (!developmentView && (node.nodeProjections[currentProjection].nodeState == NodeState.Hidden || node.isCarbon)) continue; - RectangleF r = new RectangleF(node.boundingBox.X + nodeProjection.shift.X, node.boundingBox.Y + nodeProjection.shift.Y, node.boundingBox.Width, node.boundingBox.Height); + RectangleF r = new RectangleF(node.boundingBox.X + nodeProjection.shift.X + middlePoint.X, node.boundingBox.Y + nodeProjection.shift.Y + middlePoint.Y, node.boundingBox.Width, node.boundingBox.Height); clippingGraphics.SetClip(r); g.SetClip(clippingGraphics, CombineMode.Exclude); @@ -826,7 +933,7 @@ public Graphics setClipping(Graphics g, Form form) NodeProjection decoratorProjection = decorator.nodeProjections[currentProjection]; if (!developmentView && decoratorProjection.nodeState == NodeState.Hidden) continue; - RectangleF rd = new RectangleF(decorator.boundingBox.X + decoratorProjection.shift.X, decorator.boundingBox.Y + decoratorProjection.shift.Y, decorator.boundingBox.Width, decorator.boundingBox.Height); + RectangleF rd = new RectangleF(decorator.boundingBox.X + decoratorProjection.shift.X + middlePoint.X, decorator.boundingBox.Y + decoratorProjection.shift.Y + middlePoint.Y, decorator.boundingBox.Width, decorator.boundingBox.Height); clippingGraphics.SetClip(rd); g.SetClip(clippingGraphics, CombineMode.Exclude); } @@ -919,11 +1026,11 @@ public void drawEdges(Graphics g, Form form) else if (proStart.nodeState == NodeState.Disabled || proEnd.nodeState == NodeState.Disabled) pen = penDisabled; if (bond.bondProjections[currentProjection]) { - foreach (var edge in bond.edgesDouble) g.DrawLine(pen, edge.start, edge.end); + foreach (var edge in bond.edgesDouble) g.DrawLine(pen, (edge.start + middlePoint).pF(), (edge.end + middlePoint).pF()); } else { - g.DrawLine(pen, bond.edgeSingle.start, bond.edgeSingle.end); + g.DrawLine(pen, (bond.edgeSingle.start + middlePoint).pF(), (bond.edgeSingle.end + middlePoint).pF()); } } @@ -972,21 +1079,21 @@ public void drawNodes(Graphics g) if (!node.nodeProjections.ContainsKey(currentProjection)) continue; NodeProjection nodeProjection = node.nodeProjections[currentProjection]; - PointF nodePoint = new PointF(node.boundingBox.X + nodeProjection.shift.X, node.boundingBox.Y + nodeProjection.shift.Y); + SPointF nodeSPoint = new SPointF(node.boundingBox.X + nodeProjection.shift.X, node.boundingBox.Y + nodeProjection.shift.Y) + middlePoint; if (!developmentView && (nodeProjection.nodeState == NodeState.Hidden)) continue; SolidBrush brush = solidBrushEnabled; if (nodeProjection.nodeState == NodeState.Hidden) brush = solidBrushHidden; else if (nodeProjection.nodeState == NodeState.Disabled) brush = solidBrushDisabled; - RectangleF r = new RectangleF(nodePoint.X, nodePoint.Y, node.boundingBox.Width, node.boundingBox.Height); + RectangleF r = new RectangleF(nodeSPoint.X, nodeSPoint.Y, node.boundingBox.Width, node.boundingBox.Height); if (!node.isCarbon || developmentView) g.DrawString(node.text, nodeFont, brush, r, drawFormat); if (nodeProjection.nodeState == NodeState.Enabled) { - minX = Math.Min(minX, (int)nodePoint.X); - maxX = Math.Max(maxX, (int)(nodePoint.X + node.boundingBox.Width)); - minY = Math.Min(minY, (int)nodePoint.Y); - maxY = Math.Max(maxY, (int)(nodePoint.Y + node.boundingBox.Height)); + minX = Math.Min(minX, (int)nodeSPoint.X); + maxX = Math.Max(maxX, (int)(nodeSPoint.X + node.boundingBox.Width)); + minY = Math.Min(minY, (int)nodeSPoint.Y); + maxY = Math.Max(maxY, (int)(nodeSPoint.Y + node.boundingBox.Height)); } if (node.isCarbon) continue; @@ -1001,7 +1108,9 @@ public void drawNodes(Graphics g) if (decoratorProjection.nodeState == NodeState.Hidden) decoBrush = solidBrushHidden; else if (decoratorProjection.nodeState == NodeState.Disabled) decoBrush = solidBrushDisabled; - RectangleF rd = new RectangleF(decorator.boundingBox.X + decoratorProjection.shift.X, decorator.boundingBox.Y + decoratorProjection.shift.Y, decorator.boundingBox.Width, decorator.boundingBox.Height); + SPointF decoratorPoint = decoratorProjection.shift + decorator.boundingBox + middlePoint; + + RectangleF rd = new RectangleF(decoratorPoint.X, decoratorPoint.Y, decorator.boundingBox.Width, decorator.boundingBox.Height); g.DrawString(decorator.text, node.text.Equals("R") ? decoratorFont : nodeFont, decoBrush, rd, drawFormat); } @@ -1031,11 +1140,11 @@ public void drawNodes(Graphics g) foreach (var targetNode in adjacentNodes[node]) { NodeProjection targetProjection = targetNode.nodeProjections[currentProjection]; - PointF targetPoint = new PointF(targetNode.boundingBox.X + targetProjection.shift.X, targetNode.boundingBox.Y + targetProjection.shift.Y); + SPointF targetSPoint = new SPointF(targetNode.boundingBox.X + targetProjection.shift.X, targetNode.boundingBox.Y + targetProjection.shift.Y) + middlePoint; - double angle = Math.Atan((targetPoint.Y - nodePoint.Y) / (targetPoint.X - nodePoint.X)) / Math.PI * 180.0; - if (nodePoint.X <= targetPoint.X && -45 <= angle && angle <= 45) rightfree = false; - else if (nodePoint.X > targetPoint.X && -45 <= angle && angle <= 45) leftfree = false; + double angle = Math.Atan((targetSPoint.Y - nodeSPoint.Y) / (targetSPoint.X - nodeSPoint.X)) / Math.PI * 180.0; + if (nodeSPoint.X <= targetSPoint.X && -45 <= angle && angle <= 45) rightfree = false; + else if (nodeSPoint.X > targetSPoint.X && -45 <= angle && angle <= 45) leftfree = false; } diff --git a/LipidCreator/StructureEditor/StructureEditor.Designer.cs b/LipidCreator/StructureEditor/StructureEditor.Designer.cs index 23e0596..09e40cb 100644 --- a/LipidCreator/StructureEditor/StructureEditor.Designer.cs +++ b/LipidCreator/StructureEditor/StructureEditor.Designer.cs @@ -67,6 +67,7 @@ public static string Show(string messageText = "", string title = "", string pre partial class LipidCreatorStructureEditor { + public ListBox positiveFragmentsListBox = new ListBox(); public ListBox negativeFragmentsListBox = new ListBox(); @@ -121,6 +122,8 @@ private void InitializeComponent() this.MouseUp += mouseUp; this.MouseDown += mouseDown; + this.Resize += resizing; + this.Controls.Add(actionChangeAtomState); actionChangeAtomState.Size = new Size(120, 25); diff --git a/LipidCreator/StructureEditor/StructureEditor.cs b/LipidCreator/StructureEditor/StructureEditor.cs index ed1fdc9..1eac490 100644 --- a/LipidCreator/StructureEditor/StructureEditor.cs +++ b/LipidCreator/StructureEditor/StructureEditor.cs @@ -51,6 +51,10 @@ public LipidCreatorStructureEditor() lipidStructure.addFragment("HG -OH", false); computeFragmentMass(null, null); + + + + lipidStructure.serialize("foo.stXL"); } @@ -75,8 +79,9 @@ private void DrawScene(Graphics g) if (currentNode != null && currentNode.nodeProjections.ContainsKey(lipidStructure.currentProjection)) { NodeProjection nodeProjection = currentNode.nodeProjections[lipidStructure.currentProjection]; + SPointF point = currentNode.position + nodeProjection.shift + lipidStructure.middlePoint; Pen pen = new Pen(Color.DeepSkyBlue, 2); - g.DrawEllipse(pen, currentNode.position.X + nodeProjection.shift.X - cursorCircleRadius, currentNode.position.Y + nodeProjection.shift.Y - cursorCircleRadius, cursorCircleRadius * 2, cursorCircleRadius * 2); + g.DrawEllipse(pen, point.X - cursorCircleRadius, point.Y - cursorCircleRadius, cursorCircleRadius * 2, cursorCircleRadius * 2); } } else if (action == Action.ChangeBond || action == Action.RemoveBond) @@ -97,7 +102,8 @@ private void DrawScene(Graphics g) if (!node.nodeProjections.ContainsKey(lipidStructure.currentProjection)) continue; NodeProjection nodeProjection = node.nodeProjections[lipidStructure.currentProjection]; Pen pen = new Pen(Color.DeepSkyBlue, 2); - g.DrawEllipse(pen, node.position.X + nodeProjection.shift.X - cursorCircleRadius, node.position.Y + nodeProjection.shift.Y - cursorCircleRadius, cursorCircleRadius * 2, cursorCircleRadius * 2); + SPointF n = node.position + nodeProjection.shift + lipidStructure.middlePoint; + g.DrawEllipse(pen, n.X - cursorCircleRadius, n.Y - cursorCircleRadius, cursorCircleRadius * 2, cursorCircleRadius * 2); } } @@ -106,7 +112,8 @@ private void DrawScene(Graphics g) { NodeProjection nodeProjection = drawStart.nodeProjections[lipidStructure.currentProjection]; Graphics clippingGraphics = lipidStructure.setClipping(g, this); - PointF start = new PointF(drawStart.position.X + nodeProjection.shift.X, drawStart.position.Y + nodeProjection.shift.Y); + SPointF d = drawStart.position + nodeProjection.shift + lipidStructure.middlePoint; + PointF start = new PointF(d.X, d.Y); g.DrawLine(lipidStructure.penEnabled, start, mouse); lipidStructure.releaseClipping(g, clippingGraphics); } @@ -130,13 +137,12 @@ private void DrawScene(Graphics g) { if (!node.nodeProjections.ContainsKey(lipidStructure.currentProjection)) continue; NodeProjection nodeProjection = node.nodeProjections[lipidStructure.currentProjection]; - double xm = node.position.X + nodeProjection.shift.X; - double ym = node.position.Y + nodeProjection.shift.Y; + SPointF n = node.position + nodeProjection.shift + lipidStructure.middlePoint; - if (lower.X <= xm && xm <= upper.X && lower.Y <= ym && ym <= upper.Y) + if (lower.X <= n.X && n.X <= upper.X && lower.Y <= n.Y && n.Y <= upper.Y) { Pen pen = new Pen(Color.DeepSkyBlue, 2); - g.DrawEllipse(pen, node.position.X + nodeProjection.shift.X - cursorCircleRadius, node.position.Y + nodeProjection.shift.Y - cursorCircleRadius, cursorCircleRadius * 2, cursorCircleRadius * 2); + g.DrawEllipse(pen, n.X - cursorCircleRadius, n.Y - cursorCircleRadius, cursorCircleRadius * 2, cursorCircleRadius * 2); } } @@ -193,7 +199,7 @@ private void mouseUp(Object sender, MouseEventArgs e) else if (action == Action.DrawAtom && currentNode == null) { var mouse = PointToClient(Cursor.Position); - lipidStructure.addNode(mouse.X, mouse.Y); + lipidStructure.addNode(new SPointF(mouse)); updateStructure(); } break; @@ -244,10 +250,9 @@ private void mouseUp(Object sender, MouseEventArgs e) { if (!node.nodeProjections.ContainsKey(lipidStructure.currentProjection)) continue; NodeProjection nodeProjection = node.nodeProjections[lipidStructure.currentProjection]; - double xm = node.position.X + nodeProjection.shift.X; - double ym = node.position.Y + nodeProjection.shift.Y; + SPointF p = node.position + nodeProjection.shift + lipidStructure.middlePoint; - if (lower.X <= xm && xm <= upper.X && lower.Y <= ym && ym <= upper.Y) + if (lower.X <= p.X && p.X <= upper.X && lower.Y <= p.Y && p.Y <= upper.Y) { node.toggleState(); } @@ -264,10 +269,9 @@ private void mouseUp(Object sender, MouseEventArgs e) { if (!node.nodeProjections.ContainsKey(lipidStructure.currentProjection)) continue; NodeProjection nodeProjection = node.nodeProjections[lipidStructure.currentProjection]; - double xm = node.position.X + nodeProjection.shift.X; - double ym = node.position.Y + nodeProjection.shift.Y; + SPointF p = node.position + nodeProjection.shift + lipidStructure.middlePoint; - if (lower.X <= xm && xm <= upper.X && lower.Y <= ym && ym <= upper.Y) + if (lower.X <= p.X && p.X <= upper.X && lower.Y <= p.Y && p.Y <= upper.Y) { moveNodes.Add(node); } @@ -337,7 +341,7 @@ private void mouseMove(Object sender, MouseEventArgs e) Bond previousBond = currentBond; currentBond = null; - PointF mouse = PointToClient(Cursor.Position); + SPointF mouse = new SPointF(PointToClient(Cursor.Position)); if (action == Action.ChangeAtom || action == Action.ChangeAtomState || action == Action.MoveAtom || action == Action.DrawBond || action == Action.RemoveAtom) @@ -347,7 +351,8 @@ private void mouseMove(Object sender, MouseEventArgs e) { if (!node.nodeProjections.ContainsKey(lipidStructure.currentProjection)) continue; NodeProjection nodeProjection = node.nodeProjections[lipidStructure.currentProjection]; - double dist = Math.Pow(node.position.X + nodeProjection.shift.X - mouse.X, 2) + Math.Pow(node.position.Y + nodeProjection.shift.Y - mouse.Y, 2); + SPointF n = node.position + nodeProjection.shift - mouse + lipidStructure.middlePoint; + double dist = Math.Pow(n.X, 2) + Math.Pow(n.Y, 2); if (dist <= Math.Pow(10 + cursorCircleRadius, 2) && dist < minDist) { minDist = dist; @@ -359,7 +364,8 @@ private void mouseMove(Object sender, MouseEventArgs e) foreach (var decorator in node.decorators) { NodeProjection decoratorProjection = decorator.nodeProjections[lipidStructure.currentProjection]; - dist = Math.Pow(decorator.position.X + decoratorProjection.shift.X - mouse.X, 2) + Math.Pow(decorator.position.Y + decoratorProjection.shift.Y - mouse.Y, 2); + SPointF d = decorator.position + decoratorProjection.shift - mouse + lipidStructure.middlePoint; + dist = Math.Pow(d.X, 2) + Math.Pow(d.Y, 2); if (dist <= Math.Pow(10 + cursorCircleRadius, 2) && dist < minDist) { minDist = dist; @@ -418,7 +424,7 @@ private void mouseMove(Object sender, MouseEventArgs e) { if (moveNode != null && action == Action.MoveAtom) { - PointF shift = new PointF(mouse.X - previousMousePosition.X, mouse.Y - previousMousePosition.Y); + SPointF shift = new SPointF(mouse.X - previousMousePosition.X, mouse.Y - previousMousePosition.Y); if (moveNodes.Count > 0 && moveNodes.Contains(moveNode)) { foreach (var m in moveNodes) m.move(shift); @@ -667,6 +673,15 @@ public void fragmentKeyPressed(object sender, KeyEventArgs e) + private void resizing(object sender, System.EventArgs e) + { + lipidStructure.setMiddlePoint(new SPointF(this.Width / 2.0f, this.Height / 2.0f)); + updateStructure(); + } + + + + private void addNegativeFragment(Object sender, EventArgs e) { if (!lipidStructure.negativeFragments.ContainsKey(FRAGMENT_LABEL))