pub struct Map {
pub settings: MapSettings,
/* private fields */
}Expand description
An interactive 2D map widget.
Map renders a set of nodes (objects::MapPoint), connection lines
(objects::MapSegment) and text labels (objects::MapLabel). The user can
pan the view by dragging and zoom with the mouse wheel (hold Ctrl — or
Cmd on macOS — to zoom faster), or use the built-in zoom slider drawn at
the top-right corner of the widget.
The map is fed through Map::add_hashmap_points, which also builds the
internal kd-tree used for viewport culling and nearest-node hover queries.
Behavior and appearance are configured through the public
settings field (see objects::MapSettings).
Rendering of nodes and their visual effects (selection highlight,
notifications and markers) can be fully customized by installing a
objects::NodeTemplate implementation with Map::set_node_template;
likewise, a right-click context menu can be provided with
Map::set_context_manager.
§Examples
use egui_map::map::Map;
use egui_map::map::objects::{MapPoint, RawPoint};
use std::collections::HashMap;
let mut points = HashMap::new();
points.insert(1, MapPoint::new(1, RawPoint::new(0.0, 0.0)));
let mut map = Map::new();
map.add_hashmap_points(points);
// Every frame, inside your egui update logic:
ui.add(&mut map);Fields§
§settings: MapSettingsBehavior and appearance configuration (zoom limits, visibility
thresholds and per-theme styles). See objects::MapSettings.
Implementations§
Source§impl Map
impl Map
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty map widget with default MapSettings.
The widget displays nothing until nodes are loaded with
Map::add_hashmap_points.
Sourcepub fn add_hashmap_points(&mut self, hash_map: HashMap<usize, MapPoint>)
pub fn add_hashmap_points(&mut self, hash_map: HashMap<usize, MapPoint>)
Loads the node set and (re)builds the spatial index.
This replaces any previously loaded points, computes the bounding box of the whole set, centers the view on its midpoint and refreshes the list of visible nodes. It must be called at least once before the widget can display anything.
The kd-tree built here is what enables viewport culling and nearest-neighbor hover lookups, so calling this method on every frame is discouraged; call it only when the node set changes.
§Examples
use egui_map::map::Map;
use egui_map::map::objects::{MapPoint, RawPoint};
use std::collections::HashMap;
let mut points = 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)));
let mut map = Map::new();
map.add_hashmap_points(points);
// The view is centered on the midpoint of the loaded nodes.
assert_eq!(map.get_pos(), [5.0, 5.0]);Sourcepub fn set_pos_from_nodeid(&mut self, node_id: usize)
pub fn set_pos_from_nodeid(&mut self, node_id: usize)
Centers the view on the node with the given id.
Does nothing if no points have been loaded yet or if node_id is
unknown.
Sourcepub fn get_pos(&self) -> [f32; 2]
pub fn get_pos(&self) -> [f32; 2]
Returns the map coordinates the view is currently centered on.
Sourcepub fn add_labels(&mut self, labels: Vec<MapLabel>)
pub fn add_labels(&mut self, labels: Vec<MapLabel>)
Replaces the set of free-floating text labels drawn on the map.
Labels are only rendered while the zoom level is below
MapSettings::line_visible_zoom.
Sourcepub fn add_lines(&mut self, lines: HashMap<String, RawLine>)
pub fn add_lines(&mut self, lines: HashMap<String, RawLine>)
Replaces the set of connection lines between nodes.
Lines are keyed by a connection id that the endpoint nodes must
reference through MapPoint::connections — push each line’s key into
the connections of the nodes it joins. The segments are stored in an
R-tree keyed by bounding box: a line is drawn while its bounding box
intersects the viewport and the zoom level is above
MapSettings::line_visible_zoom.
See the module-level example for the complete wiring.
Sourcepub fn set_zoom(&mut self, value: f32)
pub fn set_zoom(&mut self, value: f32)
Sets the zoom factor.
Values outside the MapSettings::min_zoom..=MapSettings::max_zoom
range are ignored.
Sourcepub fn notify(&mut self, id_node: usize, time: Instant)
pub fn notify(&mut self, id_node: usize, time: Instant)
Triggers a notification highlight on the node id_node.
By default the notification is rendered as a pulsing circle that starts
at time and plays for about 3.5 seconds; calling notify again for
the same node restarts the animation. The effect can be customized with
objects::NodeTemplate::notification_ui.
Sourcepub fn line_at(&self, point: [f32; 2], tolerance: f32) -> Option<Rc<str>>
pub fn line_at(&self, point: [f32; 2], tolerance: f32) -> Option<Rc<str>>
Returns the id of the line closest to point, in map coordinates,
when it lies within tolerance map units of the segment.
Broad-phase candidates are taken from the segment R-tree built by
Map::add_lines; the exact point-to-segment distance is then
computed against the line geometry and the closest match wins. Returns
None when no lines are loaded or every segment is farther than
tolerance. A negative tolerance behaves like 0.0.
To hit-test a mouse click, convert the screen position to map
coordinates first (map = (screen + origin) / zoom, see the
coordinate model) and pick a tolerance scaled
by 1.0 / zoom so it stays constant in screen pixels.
Sourcepub fn set_context_manager(&mut self, manager: Rc<dyn ContextMenuManager>)
pub fn set_context_manager(&mut self, manager: Rc<dyn ContextMenuManager>)
Installs a right-click context menu whose contents are built by the
given ContextMenuManager implementation.
Sourcepub fn set_node_template(&mut self, template: Rc<dyn NodeTemplate>)
pub fn set_node_template(&mut self, template: Rc<dyn NodeTemplate>)
Replaces the built-in node rendering with a custom NodeTemplate
implementation.
The template takes over the drawing of nodes, selection highlights,
notification animations and markers — including the node name labels,
which the widget no longer draws once a template is installed. See the
NodeTemplate examples for custom shapes and animations.
Sourcepub fn update_marker(&mut self, id: usize, node_id: usize)
pub fn update_marker(&mut self, id: usize, node_id: usize)
Adds the marker id, or moves it, so it points to the node node_id.
Markers are drawn as a blinking ring around the target node unless a
custom objects::NodeTemplate::marker_ui is installed.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Map
impl !Send for Map
impl !Sync for Map
impl !UnwindSafe for Map
impl Freeze for Map
impl Unpin for Map
impl UnsafeUnpin for Map
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more