Select Git revision
basicReport.html
Forked from
SERG / woRkflow
Source project has a limited visibility.
-
Ben Anderson authoredBen Anderson authored
CentreGraph.xaml.cs 5.23 KiB
using Microsoft.Maui.Graphics;
using Robobin.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace RobobinApp.Views
{
public class GraphicsDrawable : IDrawable
{
private Graph _graph;
public const float NodeRadius = 5f; // Fixed radius for nodes
private static readonly Color DefaultNodeColor = Colors.Red;
private static readonly Color HighlightedNodeColor = Colors.Yellow; // Highlighted node color
private RectF storedDirtyRect;
private Node _highlightedNode;
public void SetGraph(Graph graph)
{
_graph = graph ?? throw new ArgumentNullException(nameof(graph));
_highlightedNode = null;
}
public void HighlightNode(Node node)
{
_highlightedNode = node;
}
public void Draw(ICanvas canvas, RectF dirtyRect)
{
storedDirtyRect = dirtyRect;
if (_graph == null) return;
// Draw the background image
DrawBackground(canvas, dirtyRect);
// Draw the nodes on top of the image
DrawNodes(canvas, NodeRadius, _highlightedNode);
}
private void DrawBackground(ICanvas canvas, RectF dirtyRect)
{
try
{
// Load the embedded image resource
var assembly = typeof(GraphicsDrawable).Assembly;
var resourceStream = assembly.GetManifestResourceStream("Robobin.Resources.Images.lab_map.png");
if (resourceStream != null)
{
var image = Microsoft.Maui.Graphics.Platform.PlatformImage.FromStream(resourceStream);
canvas.DrawImage(image, dirtyRect.Left, dirtyRect.Top, dirtyRect.Width, dirtyRect.Height);
}
else
{
throw new Exception("Embedded resource not found.");
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error loading image: {ex.Message}");
// Fallback: fill background with a color if the image is unavailable
canvas.FillColor = Colors.Black;
canvas.FillRectangle(dirtyRect);
}
}
private void DrawNodes(ICanvas canvas, float nodeRadius, Node highlightedNode)
{
// Calculate offsets to center the nodes
float canvasCenterX = storedDirtyRect.Width * 4.5f / 7;
float canvasCenterY = storedDirtyRect.Height / 4.2f;
// Calculate the center of the graph based on node coordinates
float graphCenterX = (_graph.Nodes.Min(node => node.X) + _graph.Nodes.Max(node => node.X)) / 2;
float graphCenterY = (_graph.Nodes.Min(node => node.Y) + _graph.Nodes.Max(node => node.Y)) / 2;
// Offset to center the graph
float offsetX = canvasCenterX - graphCenterX;
float offsetY = canvasCenterY - graphCenterY;
foreach (var node in _graph.Nodes)
{
// Apply centering offsets
float centeredX = node.X + offsetX;
float centeredY = node.Y + offsetY;
// Set fill color based on whether the node is highlighted
canvas.FillColor = (highlightedNode == node) ? HighlightedNodeColor : DefaultNodeColor;
// Draw the node as a circle
canvas.FillCircle(centeredX, centeredY, nodeRadius);
// Optionally, draw node name or other details
canvas.FontColor = Colors.Black;
canvas.DrawString(node.Name, centeredX + nodeRadius + 2, centeredY - nodeRadius, HorizontalAlignment.Left);
}
}
public Node FindNearestNode(float tapPointX, float tapPointY)
{
// Calculate offsets to center the nodes
float canvasCenterX = storedDirtyRect.Width * 4.5f / 7;
float canvasCenterY = storedDirtyRect.Height / 2;
// Calculate the center of the graph based on node coordinates
float graphCenterX = (_graph.Nodes.Min(node => node.X) + _graph.Nodes.Max(node => node.X)) / 2;
float graphCenterY = (_graph.Nodes.Min(node => node.Y) + _graph.Nodes.Max(node => node.Y)) / 2;
// Offset to center the graph
float offsetX = canvasCenterX - graphCenterX;
float offsetY = canvasCenterY - graphCenterY;
float minDistance = float.MaxValue;
Node nearestNode = null;
foreach (var node in _graph.Nodes)
{
// Apply centering offsets to node positions
float centeredX = node.X + offsetX;
float centeredY = node.Y + offsetY;
// Calculate the distance between the tapped point and the centered node position
float distance = (float)Math.Sqrt(
Math.Pow(centeredX - tapPointX, 2) +
Math.Pow(centeredY - tapPointY, 2)
);
if (distance < minDistance)
{
minDistance = distance;
nearestNode = node;
}
}
return nearestNode;
}
}
}