Skip to main content

Module map

Module map 

Source
Expand description

Interactive map widget and the data types it renders.

Map is an egui::Widget that draws a 2D set of nodes (objects::MapPoint), the connection lines between them (objects::MapSegment) and free-floating text labels (objects::MapLabel). Nodes are indexed in a kd-tree so that only the ones inside the current viewport are painted each frame.

§Coordinate model

The widget works with two coordinate spaces:

  • Map coordinates: the logical position of your nodes, as loaded through Map::add_hashmap_points.
  • Screen coordinates: positions inside the widget’s rectangle on screen.

Both are related by the current zoom factor and viewport origin: screen = map * zoom - origin. Use Map::set_zoom, Map::set_pos and Map::set_pos_from_nodeid to control the visible region.

§Connecting nodes with lines

Lines are wired up in three steps:

  1. Create the nodes as a HashMap keyed by node id.
  2. For every connection, choose a unique (usize, usize) id – typically the pair of node ids it joins – and push it into MapPoint::connections of both endpoint nodes.
  3. Load the nodes with Map::add_hashmap_points, then load a HashMap of MapSegment keyed by those same connection ids and add it to the widget with Map::add_hashmap_lines.
use egui_map::map::Map;
use egui_map::map::objects::{MapPoint, MapSegment};
use std::collections::HashMap;

// 1. Create the nodes.
let mut points: HashMap<usize, MapPoint> = HashMap::new();
points.insert(1, MapPoint::new(1, [0.0, 0.0]));
points.insert(2, MapPoint::new(2, [10.0, 10.0]));

// 2. Register the connection id on both endpoints.
for id in [1, 2] {
    points.get_mut(&id).unwrap().connections.push((1, 2));
}

let mut map = Map::new();
map.add_hashmap_points(points);

// 3. Provide the line geometry keyed by the same connection id.
let mut lines: HashMap<(usize, usize), MapSegment> = HashMap::new();
lines.insert((1, 2), MapSegment::new((1, 2), [0.0, 0.0], [10.0, 10.0]));
map.add_hashmap_lines(lines);

A line is only drawn while the zoom level is above MapSettings::line_visible_zoom and its bounding box intersects the viewport. Segments are culled broad-phase with an R-tree built by Map::add_lines, so long lines crossing the view are drawn even when both endpoints lie outside of it.

§Custom node rendering

Install a NodeTemplate implementation with Map::set_node_template to take over the rendering of nodes, selection highlights, notification animations and markers. Note that this replaces all built-in node rendering, including the node name labels: draw them yourself in NodeTemplate::node_ui if you need them.

Modules§

animation
Built-in animation effects for nodes.
objects
Data types consumed by the Map widget.

Structs§

Map
An interactive 2D map widget.
NodeHandle
A borrowed node, obtained from Map::node, that an animation can be attached to.