Skip to content
Snippets Groups Projects
texture_atlas.rs 2.47 KiB
Newer Older
Matthew's avatar
Matthew committed
use bevy::{
StarToaster's avatar
StarToaster committed
    prelude::{App as BevyApp, AssetServer, Commands, ImageSettings, Res, ResMut},
Matthew's avatar
Matthew committed
    DefaultPlugins,
};
StarToaster's avatar
StarToaster committed
use kayak_ui::prelude::{widgets::*, KStyle, *};
Matthew's avatar
Matthew committed

fn startup(
    mut commands: Commands,
StarToaster's avatar
StarToaster committed
    mut font_mapping: ResMut<FontMapping>,
Matthew's avatar
Matthew committed
    asset_server: Res<AssetServer>,
) {
StarToaster's avatar
StarToaster committed
    font_mapping.set_default(asset_server.load("roboto.kayak_font"));

    commands.spawn(UICameraBundle::new());
StarToaster's avatar
StarToaster committed
    let image_handle = asset_server.load("texture_atlas.png");
Matthew's avatar
Matthew committed

    //texture_atlas.png uses 16 pixel sprites and is 272x128 pixels
    let tile_size = 16;
    let columns = 272 / tile_size;
    let rows = 128 / tile_size;
    let atlas = bevy::sprite::TextureAtlas::from_grid(
StarToaster's avatar
StarToaster committed
        image_handle.clone(),
        bevy::prelude::Vec2::splat(tile_size as f32),
        columns,
        rows,
StarToaster's avatar
StarToaster committed
        None,
        None,
Matthew's avatar
Matthew committed

    //The sign in the top right of the image would be index 16
    let sign_index = 16;
    //The flower is in the 6(-1) row and 15 collumn
    let flower_index = columns * 5 + 15;

StarToaster's avatar
StarToaster committed
    let mut widget_context = Context::new();
    let parent_id = None;

    let atlas_styles = KStyle {
        position_type: StyleProp::Value(PositionType::ParentDirected),
        width: StyleProp::Value(Units::Pixels(200.0)),
        height: StyleProp::Value(Units::Pixels(200.0)),
        ..KStyle::default()
    };
StarToaster's avatar
StarToaster committed
    let rect = atlas.textures[sign_index];
    let sign_position = rect.min;
    let sign_size = rect.max - rect.min;
StarToaster's avatar
StarToaster committed
    let rect = atlas.textures[flower_index];
    let flower_position = rect.min;
    let flower_size = rect.max - rect.min;
StarToaster's avatar
StarToaster committed
    rsx! {
        <KayakAppBundle>
            <TextureAtlasBundle
                atlas={TextureAtlas {
                    handle: image_handle.clone(),
                    position: sign_position,
                    tile_size: sign_size,
                }}
                styles={atlas_styles.clone()}
            />
            <TextureAtlasBundle
                atlas={TextureAtlas {
                    handle: image_handle.clone(),
                    position: flower_position,
                    tile_size: flower_size,
                }}
                styles={atlas_styles.clone()}
            />
        </KayakAppBundle>
    }
StarToaster's avatar
StarToaster committed
    commands.insert_resource(widget_context);
Matthew's avatar
Matthew committed
}

fn main() {
    BevyApp::new()
        .insert_resource(ImageSettings::default_nearest())
Matthew's avatar
Matthew committed
        .add_plugins(DefaultPlugins)
StarToaster's avatar
StarToaster committed
        .add_plugin(ContextPlugin)
        .add_plugin(KayakWidgets)
Matthew's avatar
Matthew committed
        .add_startup_system(startup)
StarToaster's avatar
StarToaster committed
        .run()
Matthew's avatar
Matthew committed
}