Module vpeol_2d

Source
Expand description

§Viewport Editing Overlay for 2D games.

Use this module to implement simple 2D editing for 2D games.

To use add the egui and Yoleck plugins to the Bevy app, as well as the plugin of this module:

app.add_plugins(EguiPlugin {
    enable_multipass_for_primary_context: true,
});
app.add_plugins(YoleckPluginForEditor);
// Use `Vpeol2dPluginForGame` instead when setting up for game.
app.add_plugins(Vpeol2dPluginForEditor);

Add the following components to the camera entity:

  • VpeolCameraState in order to select and drag entities.
  • Vpeol2dCameraControl in order to pan and zoom the camera with the mouse. This one can be skipped if there are other means to control the camera inside the editor, or if no camera control inside the editor is desired.
commands
    .spawn(Camera2d::default())
    .insert(VpeolCameraState::default())
    .insert(Vpeol2dCameraControl::default());

Entity selection by clicking on it is supported by just adding the plugin. To implement dragging, there are two options:

  1. Add the Vpeol2dPosition Yoleck component and use it as the source of position (there are also Vpeol2dRotatation and Vpeol2dScale, but they don’t currently get editing support from vpeol_2d)
    app.add_yoleck_entity_type({
        YoleckEntityType::new("Example")
            .with::<Vpeol2dPosition>() // vpeol_2d dragging
            .with::<Example>() // entity's specific data and systems
    });
  2. Use data passing. vpeol_2d will pass a Vec3 to the entity being dragged:
    fn edit_example(mut edit: YoleckEdit<(Entity, &mut Example)>, passed_data: Res<YoleckPassedData>) {
        let Ok((entity, mut example)) = edit.single_mut() else { return };
        if let Some(pos) = passed_data.get::<Vec3>(entity) {
            example.position = pos.truncate();
        }
    }
    
    fn populate_example(mut populate: YoleckPopulate<&Example>) {
        populate.populate(|_ctx, mut cmd, example| {
            cmd.insert(Transform::from_translation(example.position.extend(0.0)));
            cmd.insert(Sprite {
                // Actual sprite data
                ..Default::default()
            });
        });
    }

Structs§

Vpeol2dCameraControl
Pan and zoom a camera entity with the mouse while inisde the editor.
Vpeol2dPluginForEditor
Add the systems required for 2D editing.
Vpeol2dPluginForGame
Add the systems required for loading levels that use vpeol_2d components
Vpeol2dPosition
A position component that’s edited and populated by vpeol_2d.
Vpeol2dRotatation
A rotation component that’s populated (but not edited) by vpeol_2d.
Vpeol2dScale
A scale component that’s populated (but not edited) by vpeol_2d.