Struct widgetry::mapspace::World

source ·
pub struct World<ID: ObjectID> {
    objects: HashMap<ID, Object<ID>>,
    quadtree: QuadTree<ID>,
    draw_master_batches: Vec<ToggleZoomed>,
    hovering: Option<ID>,
    draw_hovering: Option<Drawable>,
    dragging_from: Option<(Pt2D, bool)>,
}
Expand description

A World manages objects that exist in “map-space”, the zoomable and pannable canvas. These objects can be drawn, hovered on, clicked, dragged, etc.

Fields§

§objects: HashMap<ID, Object<ID>>§quadtree: QuadTree<ID>§draw_master_batches: Vec<ToggleZoomed>§hovering: Option<ID>§draw_hovering: Option<Drawable>§dragging_from: Option<(Pt2D, bool)>

Implementations§

source§

impl<ID: ObjectID> World<ID>

source

pub fn new() -> World<ID>

Creates an empty World

source

pub fn add(&mut self, id: ID) -> ObjectBuilder<'_, ID>

Start adding an object to the World. The caller should specify the object with methods on ObjectBuilder, then call build.

source

pub fn delete(&mut self, id: ID)

Delete an object. Not idempotent – this will panic if the object doesn’t exist. Will panic if the object is deleted in the middle of being dragged.

source

pub fn delete_before_replacement(&mut self, id: ID)

Delete an object, with the promise to recreate it with the same ID before the next call to event. This may be called while the object is being hovered on or dragged.

source

pub fn maybe_delete(&mut self, id: ID)

Like delete, but doesn’t crash if the object doesn’t exist

source

pub fn initialize_hover(&mut self, ctx: &EventCtx<'_>)

After adding all objects to a World, call this to initially detect if the cursor is hovering on an object. This may also be called after adding or deleting objects to immediately recalculate hover before the mouse moves.

source

pub fn hack_unset_hovering(&mut self)

Forcibly reset the hovering state to empty. This is a necessary hack when launching a new state that uses DrawBaselayer::PreviousState and has tooltips.

source

pub fn rebuilt_during_drag( &mut self, ctx: &EventCtx<'_>, prev_world: &World<ID> )

If a drag event causes the world to be totally rebuilt, call this with the previous world to preserve the ongoing drag.

This should be called after initialize_hover.

Important: the rebuilt world must include the same object ID that’s currently being dragged from the previous world.

source

pub fn draw_master_batch<I: Into<ToggleZoomedBuilder>>( &mut self, ctx: &EventCtx<'_>, draw: I )

Draw something underneath all objects. This is useful for performance, when a large number of objects never change appearance.

source

pub fn draw_master_batch_built(&mut self, draw: ToggleZoomed)

Like draw_master_batch, but for already-built objects.

source

pub fn event(&mut self, ctx: &mut EventCtx<'_>) -> WorldOutcome<ID>

Let objects in the world respond to something happening.

source

fn calculate_hover(&self, cursor: Pt2D) -> Option<ID>

source

pub fn draw(&self, g: &mut GfxCtx<'_>)

Draw objects in the world that’re currently visible.

source

pub fn get_hovering(&self) -> Option<ID>

Returns the object currently hovered on.

source

pub fn override_tooltip(&mut self, id: &ID, tooltip: Option<Text>) -> bool

Change an object’s tooltip. Returns true for success, false if the object didn’t exist.

source

pub fn calculate_hovering(&self, ctx: &EventCtx<'_>) -> Option<ID>

Calculate the object currently underneath the cursor. This should only be used when the World is not being actively updated by calling event. If another state temporarily needs to disable most interactions with objects, it can poll this instead.

source

pub fn get_hovered_keybindings(&self) -> Option<&Vec<(MultiKey, &'static str)>>

If an object is currently being hovered on, return its keybindings. This should be used to describe interactions; to detect the keypresses, listen for WorldOutcome::Keypress.

source

fn redraw_hovering(&mut self, ctx: &EventCtx<'_>)

source§

impl World<DummyID>

source

pub fn add_unnamed(&mut self) -> ObjectBuilder<'_, DummyID>

Begin adding an unnamed object to the World.

Note: You must call build on this object before calling add_unnamed again. Otherwise, the object IDs will collide.

TODO This will break when objects are deleted!

Auto Trait Implementations§

§

impl<ID> Freeze for World<ID>
where ID: Freeze,

§

impl<ID> RefUnwindSafe for World<ID>
where ID: RefUnwindSafe,

§

impl<ID> !Send for World<ID>

§

impl<ID> !Sync for World<ID>

§

impl<ID> Unpin for World<ID>
where ID: Unpin,

§

impl<ID> UnwindSafe for World<ID>
where ID: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

§

fn is_within(&self, b: &G2) -> bool