From 719d6842c3e7ceab772fd2129f99dd97d1c56906 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 14 Sep 2020 17:57:38 +0200 Subject: [PATCH] GraphView: Move initial node positioning logic to its own class Right now we're just placing it in a grid, but we could also place them in a circle as is done by Obsidian. --- lib/core/graph.dart | 47 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/core/graph.dart b/lib/core/graph.dart index d8695a3d..cf458648 100644 --- a/lib/core/graph.dart +++ b/lib/core/graph.dart @@ -39,10 +39,11 @@ class Graph extends ChangeNotifier { Map> _neighbours = {}; Map _nodeIndexes; - var x = 1050.0; - var y = 1050.0; + GraphNodeLayout initLayouter; Graph.fromFolder(NotesFolder folder) { + initLayouter = GraphNodeLayout(maxHeight: 2000, maxWidth: 2000); + print("Building graph .... "); _addFolder(folder).then((_) { print("Done Building graph"); @@ -88,16 +89,8 @@ class Graph extends ChangeNotifier { var i = nodes.indexWhere((n) => n.note.filePath == note.filePath); if (i == -1) { var node = Node(note); - node.x = x; - node.y = y; + initLayouter.positionNode(node); - if (x >= 500) { - x = 50; - y += 50; - } - x += 50; - - //print('${node.label} -> ${node.x} ${node.y}'); nodes.add(node); return node; } @@ -202,6 +195,38 @@ class Graph extends ChangeNotifier { } } +class GraphNodeLayout { + final double maxWidth; + final double maxHeight; + + double x = 0.0; + double y = 0.0; + + double startX = 500.0; + double startY = 500.0; + + double gap = 70; + double nodeSize = 50; + + GraphNodeLayout({@required this.maxWidth, @required this.maxHeight}) { + x = startX; + y = startY; + } + + void positionNode(Node node) { + node.x = x; + node.y = y; + + print('INIT ${node.label} -> ${node.x} ${node.y}'); + + x += gap; + if (x + nodeSize >= maxWidth) { + x = startX; + y += gap; + } + } +} + // // Basic Force Directed Layout //