pub struct World<ID: ObjectID> {
objects: HashMap<ID, Object<ID>>,
quadtree: QuadTree<ID>,
draw_master_batches: Vec<ToggleZoomed>,
hovering: Option<ID>,
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>
dragging_from: Option<(Pt2D, bool)>
Implementations
sourceimpl<ID: ObjectID> World<ID>
impl<ID: ObjectID> World<ID>
sourcepub fn unbounded() -> World<ID>
pub fn unbounded() -> World<ID>
Creates an empty World
, whose objects can exist anywhere from (0, 0) to the max f64.
sourcepub fn bounded(bounds: &Bounds) -> World<ID>
pub fn bounded(bounds: &Bounds) -> World<ID>
Creates an empty World
, whose objects can exist in the provided rectangular boundary.
sourcepub fn add(&mut self, id: ID) -> ObjectBuilder<'_, ID>
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
.
sourcepub fn delete(&mut self, id: ID)
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.
sourcepub fn delete_before_replacement(&mut self, id: ID)
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.
sourcepub fn initialize_hover(&mut self, ctx: &EventCtx<'_>)
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.
sourcepub fn rebuilt_during_drag(&mut self, prev_world: &World<ID>)
pub fn rebuilt_during_drag(&mut self, 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.
sourcepub fn draw_master_batch<I: Into<ToggleZoomedBuilder>>(
&mut self,
ctx: &EventCtx<'_>,
draw: I
)
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.
sourcepub fn draw_master_batch_built(&mut self, draw: ToggleZoomed)
pub fn draw_master_batch_built(&mut self, draw: ToggleZoomed)
Like draw_master_batch
, but for already-built objects.
sourcepub fn event(&mut self, ctx: &mut EventCtx<'_>) -> WorldOutcome<ID>
pub fn event(&mut self, ctx: &mut EventCtx<'_>) -> WorldOutcome<ID>
Let objects in the world respond to something happening.
fn calculate_hover(&self, cursor: Pt2D) -> Option<ID>
sourcepub fn get_hovering(&self) -> Option<ID>
pub fn get_hovering(&self) -> Option<ID>
Returns the object currently hovered on.
sourcepub fn override_tooltip(&mut self, id: &ID, tooltip: Option<Text>) -> bool
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.
sourcepub fn calculate_hovering(&self, ctx: &EventCtx<'_>) -> Option<ID>
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.
sourceimpl World<DummyID>
impl World<DummyID>
sourcepub fn add_unnamed(&mut self) -> ObjectBuilder<'_, DummyID>
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> 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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
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
. Read more
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn as_any(&self) -> &(dyn Any + 'static)
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. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
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. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self