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::MapLine) 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 string id and push it into MapPoint::connections of both endpoint nodes.
  3. Load the nodes with Map::add_hashmap_points, then load a HashMap of MapLine keyed by those same connection ids with Map::add_lines.
use egui_map::map::Map;
use egui_map::map::objects::{MapLine, MapPoint, RawPoint};
use std::collections::HashMap;

// 1. Create the nodes.
let mut points: HashMap<usize, MapPoint> = HashMap::new();
points.insert(1, MapPoint::new(1, RawPoint::new(0.0, 0.0)));
points.insert(2, MapPoint::new(2, RawPoint::new(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".to_string());
}

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<String, MapLine> = HashMap::new();
let mut line = MapLine::new(RawPoint::new(0.0, 0.0), RawPoint::new(10.0, 10.0));
line.id = Some("1-2".to_string());
lines.insert("1-2".to_string(), line);
map.add_lines(lines);

A line is only drawn while the zoom level is above MapSettings::line_visible_zoom and at least one of its endpoints is inside the viewport.

§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, used when no custom NodeTemplate is installed.
objects
Data types consumed by the Map widget.

Structs§

Map
An interactive 2D map widget.