mln-rs-engine 0.0.18

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
use std::time::Duration;

use glam::{DQuat, DVec3};
use mln_style::geo::LatLng;
use mln_style::unitbezier::UnitBezier;

use crate::geo::{EdgeInsets, ScreenCoordinate};

/// Various options for describing the viewpoint of a map. All fields are
/// optional.
/// A partial command replaces animations of its specified fields. Omitted
/// fields keep their original animation timing. Flights couple center and
/// zoom; anchored commands couple center with their specified fields.
/// Replacing any member of a coupled group stops that group.
/// Anchor and center points are mutually exclusive, with preference for the
/// center point when both are set.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CameraOptions {
    /// Coordinate at the center of the map.
    pub center: Option<LatLng>,
    /// Altitude of the center of the map, in meters above sea level, which
    /// terrain that clamps the center (the map's `center_clamped_to_ground`)
    /// decides instead.
    pub center_altitude: Option<f64>,
    /// Padding around the interior of the view that affects the frame of
    /// reference for `center`.
    pub padding: Option<EdgeInsets>,
    /// Point of reference for `zoom` and `angle`, assuming an origin at the
    /// top-left corner of the view: a command keeps the location under it in
    /// place, over terrain the ground drawn there.
    pub anchor: Option<ScreenCoordinate>,
    /// Zero-based zoom level. Constrained to the minimum and maximum zoom
    /// levels.
    pub zoom: Option<f64>,
    /// Bearing, measured in degrees from true north. Wrapped to [0, 360).
    pub bearing: Option<f64>,
    /// Pitch toward the horizon measured in degrees, with 0 deg resulting in
    /// a two-dimensional map.
    pub pitch: Option<f64>,
    /// Roll, measured in degrees.
    pub roll: Option<f64>,
    /// Field of view, measured in degrees.
    pub fov: Option<f64>,
}

/// Various options for describing a transition between viewpoints with
/// animation. All fields are optional; the default values depend on how
/// this struct is used.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AnimationOptions {
    /// Time to animate to the viewpoint defined herein.
    pub duration: Option<Duration>,
    /// Average velocity of a `flyTo()` transition, measured in screenfuls per
    /// second, assuming a linear timing curve.
    pub velocity: Option<f64>,
    /// Zero-based zoom level at the peak of the `flyTo()` transition's flight
    /// path.
    pub min_zoom: Option<f64>,
    /// The longest a `flyTo()` transition may take, as GL JS's `maxDuration`:
    /// a flight whose duration, set or derived from the velocity, exceeds it
    /// jumps instead. `None` caps nothing beyond what [`Duration`] holds: a
    /// derived duration it can't hold jumps too. Other camera commands
    /// ignore it.
    pub max_duration: Option<Duration>,
    /// The easing timing curve of the transition.
    pub easing: Option<UnitBezier>,
    /// Caller-chosen identity of the command, which
    /// `Map::cancel_transitions_with_id` matches. Several commands may share
    /// one.
    pub transition_id: Option<u64>,
}

/// How a camera command ended, as the C API's
/// `mln_camera_transition_outcome` reports it. A command that ended in more
/// than one way reports [`Cancelled`](Self::Cancelled) if any of it was
/// cancelled, otherwise [`Closed`](Self::Closed) if any of it was closed,
/// and otherwise [`Replaced`](Self::Replaced) if any of it was replaced.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TransitionOutcome {
    /// The camera finished the change.
    #[default]
    Completed,
    /// `cancel_transitions` or `cancel_transitions_with_id`, a gesture's
    /// begin or cancel phase, free camera options, or a resize that moved the
    /// camera back inside its bounds stopped it. A command that fails before
    /// it moves the camera also reports this.
    Cancelled,
    /// A later command took every field that the command (or a momentum part)
    /// was still animating, so it stopped. A later command that takes only
    /// some of them leaves it animating the rest.
    Replaced,
    /// The map closed before the change ended.
    Closed,
}

/// How fast each part of a camera change moves, in its own unit per second.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CameraVelocity {
    /// Logical pixels per second that the map's content moves on screen.
    pub pan: ScreenCoordinate,
    /// Zoom levels per second.
    pub zoom: f64,
    /// Degrees per second.
    pub bearing: f64,
    /// Degrees per second.
    pub pitch: f64,
}

/// How a camera command ended, once every field of it has: completed,
/// cancelled, replaced by later commands, or closed with the map. Other
/// commands may still be animating. It replaces C++'s `transitionFinishFn`,
/// which can't tell those apart; the C API reports the same outcomes.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TransitionEnd {
    pub outcome: TransitionOutcome,
    /// How fast a momentum still moved when it was cancelled, as of the last
    /// frame that moved the camera, which the next gesture can add to its own
    /// release. Zero for every other end.
    pub velocity: CameraVelocity,
    /// The generation of the first snapshot of the map that shows where the
    /// change left the camera, as `mln_runtime`'s map handles report it. 0
    /// when the map closed first, and from the map itself, which ends a
    /// change before it counts the change's generation.
    pub generation: u64,
}

impl AnimationOptions {
    #[must_use]
    pub fn new(d: Duration) -> Self {
        AnimationOptions { duration: Some(d), ..Default::default() }
    }
}

/// Various options for accessing physical properties of the underlying camera
/// entity. A direct access to these properties allows more flexible and
/// precise controlling of the camera while also being fully compatible and
/// interchangeable with `CameraOptions`. All fields are optional.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FreeCameraOptions {
    /// Position of the camera in slightly modified web mercator coordinates.
    /// The size of 1 unit is the width of the projected world instead of the
    /// "mercator meter". Coordinate [0, 0, 0] is the north-west corner and
    /// [1, 1, 0] is the south-east corner. Z coordinate is conformal and must
    /// respect minimum and maximum zoom values. Zoom is automatically
    /// computed from the altitude (z).
    pub position: Option<DVec3>,
    /// Orientation of the camera as a unit quaternion.
    pub orientation: Option<DQuat>,
}

/// Where a camera change stands in a gesture, as the C API's
/// `mln_gesture_phase` defines it. `Begin` and `Update` mark a gesture in
/// progress, and `End` and `Cancel` end it; a gesture whose end starts
/// momentum stays in progress until the momentum ends.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum GesturePhase {
    /// The change isn't part of a gesture: a command.
    #[default]
    None,
    /// The gesture starts. This cancels every running camera animation
    /// before the change applies.
    Begin,
    /// The gesture continues. To start a gesture that keeps the running
    /// animations, start it with this phase instead of `Begin`.
    Update,
    /// The gesture's last change, which may carry its momentum.
    End,
    /// The platform cancelled the gesture. This also cancels every running
    /// camera animation after the change applies.
    Cancel,
}

/// A change to the camera from where it is now, as a gesture makes it
/// (the C API's `mln_camera_delta`).
///
/// The pan and the anchored change are separate camera commands, because an
/// explicit center overrides an anchor; the anchored change reads the camera
/// after the pan. The delta's command ends once both have ended.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CameraDelta {
    /// Moves the map's content this far on the screen, as a drag does.
    pub offset: Option<ScreenCoordinate>,
    /// Multiplies the map's scale: 2 zooms in by one level.
    pub scale: Option<f64>,
    /// Degrees to add to the bearing.
    pub bearing: Option<f64>,
    /// Degrees to add to the pitch.
    pub pitch: Option<f64>,
    /// The screen point that `scale`, `bearing` and `pitch` keep in place,
    /// and that a drag's `offset` holds: on the globe the location under it
    /// follows the drag, as the C++ globe's `moveBy(offset, anchor)` does,
    /// and over terrain, in a delta of [`GesturePhase::None`], the ground
    /// drawn under it does, while a flat map moves the same wherever it is
    /// held. It needs one of them, and it combines with `offset` and one of
    /// the others only in a delta without a duration.
    pub anchor: Option<ScreenCoordinate>,
    /// How the gesture keeps moving after this delta, which only a delta in
    /// the [`GesturePhase::End`] phase carries. Its parts' snaps apply only
    /// to the parts the gesture changed. With momentum, `animation` may set
    /// only its transition id, and the command ends when the momentum does.
    pub momentum: Option<crate::camera::CameraMomentum>,
    pub animation: AnimationOptions,
    pub phase: GesturePhase,
}

/// How [`CameraUpdate`] moves the camera.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CameraUpdateMode {
    /// At once; the animation's duration is ignored.
    #[default]
    Jump,
    Ease,
    Fly,
}

/// Moves the camera to absolute values, as the C API's
/// `mln_camera_update` does: the counterpart of [`CameraDelta`] for
/// commands and gestures that know where the camera should go.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CameraUpdate {
    pub mode: CameraUpdateMode,
    pub camera: CameraOptions,
    pub animation: AnimationOptions,
    pub phase: GesturePhase,
}

/// Why a [`CameraDelta`] was rejected.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
pub enum CameraError {
    /// An offset, anchor, bearing or pitch isn't finite.
    #[error("camera delta offset, anchor, bearing and pitch must be finite")]
    NotFinite,
    /// The scale isn't finite and positive.
    #[error("camera delta scale must be finite and positive")]
    InvalidScale,
    /// An anchor needs an offset, scale, bearing or pitch to hold in place.
    #[error("a camera delta anchor requires scale, bearing, or pitch, or an offset to hold")]
    AnchorWithoutChange,
    /// Only a delta in the end phase carries momentum.
    #[error("only a camera delta in the end phase carries momentum")]
    MomentumOutsideEnd,
    /// A delta with momentum sets only its animation's transition id: the
    /// momentum, not an animation, moves the camera.
    #[error("a camera delta with momentum sets only the transition ID of its animation")]
    MomentumWithAnimation,
    /// An anchor combines with an offset and a scale, bearing or pitch only
    /// in an immediate delta: an anchored ease couples the center with its
    /// fields, so it would replace an animated pan.
    #[error("a camera delta anchor combines with offset only in an immediate delta")]
    AnimatedAnchoredOffset,
}

impl CameraDelta {
    /// Checks the delta. An anchor with only an offset is the point a drag
    /// holds, which the globe needs.
    pub fn validate(&self) -> Result<(), CameraError> {
        let finite_point = |p: Option<ScreenCoordinate>| p.is_none_or(|p| p.x.is_finite() && p.y.is_finite());
        if !finite_point(self.offset)
            || !finite_point(self.anchor)
            || !self.bearing.is_none_or(f64::is_finite)
            || !self.pitch.is_none_or(f64::is_finite)
        {
            return Err(CameraError::NotFinite);
        }
        if self.scale.is_some_and(|scale| !scale.is_finite() || scale <= 0.0) {
            return Err(CameraError::InvalidScale);
        }
        let anchored = self.scale.is_some() || self.bearing.is_some() || self.pitch.is_some();
        if self.anchor.is_some() && !anchored && self.offset.is_none() {
            return Err(CameraError::AnchorWithoutChange);
        }
        if self.momentum.is_some() {
            if self.phase != GesturePhase::End {
                return Err(CameraError::MomentumOutsideEnd);
            }
            let a = &self.animation;
            if a.duration.is_some()
                || a.velocity.is_some()
                || a.min_zoom.is_some()
                || a.max_duration.is_some()
                || a.easing.is_some()
            {
                return Err(CameraError::MomentumWithAnimation);
            }
        }
        if self.offset.is_some()
            && self.anchor.is_some()
            && anchored
            && self.animation.duration.is_some_and(|duration| duration > Duration::ZERO)
        {
            return Err(CameraError::AnimatedAnchoredOffset);
        }
        Ok(())
    }
}