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>
impl<ID: ObjectID> World<ID>
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 maybe_delete(&mut self, id: ID)
pub fn maybe_delete(&mut self, id: ID)
Like delete, but doesn’t crash if the object doesn’t exist
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 hack_unset_hovering(&mut self)
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.
sourcepub fn rebuilt_during_drag(
&mut self,
ctx: &EventCtx<'_>,
prev_world: &World<ID>
)
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.
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.
sourcepub fn get_hovered_keybindings(&self) -> Option<&Vec<(MultiKey, &'static str)>>
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
.
fn redraw_hovering(&mut self, ctx: &EventCtx<'_>)
source§impl 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> 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> 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<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.