added nature and characteristic selection as dropdown
parent
70a849c7b5
commit
089eb8917a
|
|
@ -1,6 +1,11 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_egui::EguiContexts;
|
||||
|
||||
use crate::{database::Database, pokemon::*};
|
||||
use crate::{
|
||||
database::{load_url_asset, Database},
|
||||
pokemon::*,
|
||||
ui::UiAssets,
|
||||
};
|
||||
|
||||
pub struct InspectorPlugin;
|
||||
|
||||
|
|
@ -8,7 +13,8 @@ impl Plugin for InspectorPlugin {
|
|||
fn build(&self, app: &mut App) {
|
||||
app.register_type::<InspectorPokemon>()
|
||||
.insert_resource(InspectorPokemon::default())
|
||||
.add_startup_system(resource_setup);
|
||||
.add_startup_system(resource_setup)
|
||||
.add_system(state_changed);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -20,11 +26,29 @@ pub struct InspectorPokemon {
|
|||
pub characteristic: Option<Characteristic>,
|
||||
}
|
||||
|
||||
fn resource_setup(
|
||||
mut inspector: ResMut<InspectorPokemon>,
|
||||
pokemon: Res<Database<Pokemon>>,
|
||||
natures: Res<Database<Nature>>,
|
||||
) {
|
||||
fn resource_setup(mut inspector: ResMut<InspectorPokemon>, pokemon: Res<Database<Pokemon>>) {
|
||||
inspector.pokemon = pokemon.map.get("sceptile").cloned();
|
||||
inspector.nature = natures.map.get("adamant").cloned();
|
||||
}
|
||||
|
||||
fn state_changed(
|
||||
inspector: Res<InspectorPokemon>,
|
||||
assets: Res<AssetServer>,
|
||||
mut ui_assets: ResMut<UiAssets>,
|
||||
mut contexts: EguiContexts,
|
||||
) {
|
||||
if let (Some(pokemon), true) = (inspector.pokemon.as_ref(), inspector.is_changed()) {
|
||||
if !ui_assets.sprite_map.contains_key(&pokemon.key()) {
|
||||
let handle = load_url_asset(
|
||||
pokemon.get_url(),
|
||||
format!("cache/{name}.png", name = pokemon.key()).into(),
|
||||
&assets,
|
||||
);
|
||||
if let Ok(handle) = handle {
|
||||
let weak = handle.clone_weak();
|
||||
ui_assets
|
||||
.sprite_map
|
||||
.insert(pokemon.key(), (handle, contexts.add_image(weak)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ pub struct DerivedStats {
|
|||
pub stats: HashMap<BaseStat, i32>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect)]
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect, PartialEq)]
|
||||
pub struct Pokemon {
|
||||
pub id: u16,
|
||||
pub national: u16,
|
||||
|
|
@ -151,7 +151,7 @@ impl fmt::Display for BaseStat {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect)]
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect, PartialEq)]
|
||||
pub struct Type {
|
||||
pub id: u8,
|
||||
pub name: String,
|
||||
|
|
@ -174,7 +174,7 @@ impl GetKey for Type {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect)]
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect, PartialEq)]
|
||||
pub struct Nature {
|
||||
pub id: u8,
|
||||
pub name: String,
|
||||
|
|
@ -205,11 +205,11 @@ impl GetKey for Nature {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect)]
|
||||
#[derive(Default, Debug, Clone, Reflect, FromReflect, PartialEq)]
|
||||
pub struct Characteristic {
|
||||
stat: BaseStat,
|
||||
name: String,
|
||||
possible_values: Vec<u8>,
|
||||
pub stat: BaseStat,
|
||||
pub name: String,
|
||||
pub possible_values: Vec<u8>,
|
||||
}
|
||||
|
||||
impl FromJson for Characteristic {
|
||||
|
|
|
|||
84
src/ui.rs
84
src/ui.rs
|
|
@ -4,11 +4,7 @@ use bevy::prelude::*;
|
|||
use bevy_egui::{egui, EguiContexts, EguiPlugin};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::{
|
||||
database::{load_url_asset, Database},
|
||||
inspector::InspectorPokemon,
|
||||
pokemon::*,
|
||||
};
|
||||
use crate::{database::Database, inspector::InspectorPokemon, pokemon::*};
|
||||
|
||||
lazy_static! {
|
||||
static ref BASE_STAT_COLOR_RANGES: Vec<(u8, egui::Color32)> = vec![
|
||||
|
|
@ -37,15 +33,14 @@ impl Plugin for UiPlugin {
|
|||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct UiAssets {
|
||||
bar_handle: Handle<Image>,
|
||||
sprite_map: HashMap<String, (Handle<Image>, egui::TextureId)>,
|
||||
pub bar_handle: Handle<Image>,
|
||||
pub sprite_map: HashMap<String, (Handle<Image>, egui::TextureId)>,
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
pub struct UiState {
|
||||
name: String,
|
||||
nature: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
fn load_assets(mut ui_assets: ResMut<UiAssets>, assets: Res<AssetServer>) {
|
||||
|
|
@ -55,43 +50,24 @@ fn load_assets(mut ui_assets: ResMut<UiAssets>, assets: Res<AssetServer>) {
|
|||
fn handle_state(
|
||||
ui_state: Res<UiState>,
|
||||
pokemon: Res<Database<Pokemon>>,
|
||||
nature: Res<Database<Nature>>,
|
||||
assets: Res<AssetServer>,
|
||||
mut inspector: ResMut<InspectorPokemon>,
|
||||
mut ui_assets: ResMut<UiAssets>,
|
||||
mut contexts: EguiContexts,
|
||||
) {
|
||||
if ui_state.is_changed() {
|
||||
if let Some(pokemon) = pokemon.map.get(ui_state.name.to_lowercase().as_str()) {
|
||||
inspector.pokemon = Some(pokemon.clone());
|
||||
if !ui_assets.sprite_map.contains_key(&pokemon.key()) {
|
||||
let handle = load_url_asset(
|
||||
pokemon.get_url(),
|
||||
format!("cache/{name}.png", name = pokemon.key()).into(),
|
||||
&assets,
|
||||
);
|
||||
if let Ok(handle) = handle {
|
||||
let weak = handle.clone_weak();
|
||||
ui_assets
|
||||
.sprite_map
|
||||
.insert(pokemon.key(), (handle, contexts.add_image(weak)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(nature) = nature.map.get(ui_state.nature.to_lowercase().as_str()) {
|
||||
inspector.nature = Some(nature.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn ui_system(
|
||||
mut contexts: EguiContexts,
|
||||
inspector: Res<InspectorPokemon>,
|
||||
mut inspector: ResMut<InspectorPokemon>,
|
||||
ui_assets: Res<UiAssets>,
|
||||
mut ui_state: ResMut<UiState>,
|
||||
mut rendered_texture_id: Local<egui::TextureId>,
|
||||
mut is_initialized: Local<bool>,
|
||||
characteristics: Res<Database<Characteristic>>,
|
||||
natures: Res<Database<Nature>>,
|
||||
) {
|
||||
if !*is_initialized {
|
||||
*is_initialized = true;
|
||||
|
|
@ -99,24 +75,60 @@ fn ui_system(
|
|||
}
|
||||
|
||||
egui::CentralPanel::default().show(contexts.ctx_mut(), |ui| {
|
||||
const INPUT_LABEL_WIDTH: f32 = 100.0;
|
||||
const INPUT_TEXT_WIDTH: f32 = 150.0;
|
||||
ui.horizontal(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_width(60.0);
|
||||
ui.set_width(INPUT_LABEL_WIDTH);
|
||||
ui.label("pokemon:");
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_width(150.0);
|
||||
ui.set_width(INPUT_TEXT_WIDTH);
|
||||
ui.text_edit_singleline(&mut ui_state.name);
|
||||
});
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_width(60.0);
|
||||
ui.set_width(INPUT_LABEL_WIDTH);
|
||||
ui.label("nature:");
|
||||
});
|
||||
egui::ComboBox::new("nature", "")
|
||||
.selected_text(format!(
|
||||
"{}",
|
||||
inspector.nature.as_ref().map_or("-", |c| &c.name)
|
||||
))
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(&mut inspector.nature, None, "-");
|
||||
let mut chars: Vec<_> = natures.map.values().collect();
|
||||
chars.sort_by_key(|c| c.name.clone());
|
||||
for char in chars {
|
||||
ui.selectable_value(&mut inspector.nature, Some(char.clone()), &char.name);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_width(150.0);
|
||||
ui.text_edit_singleline(&mut ui_state.nature);
|
||||
ui.horizontal(|ui| {
|
||||
ui.set_width(INPUT_LABEL_WIDTH);
|
||||
ui.label("characteristic:");
|
||||
});
|
||||
egui::ComboBox::new("characteristic", "")
|
||||
.selected_text(format!(
|
||||
"{}",
|
||||
inspector.characteristic.as_ref().map_or("-", |c| &c.name)
|
||||
))
|
||||
.show_ui(ui, |ui| {
|
||||
ui.selectable_value(&mut inspector.characteristic, None, "-");
|
||||
let mut chars: Vec<_> = characteristics.map.values().collect();
|
||||
chars.sort_by_key(|c| c.name.clone());
|
||||
for char in chars {
|
||||
ui.selectable_value(
|
||||
&mut inspector.characteristic,
|
||||
Some(char.clone()),
|
||||
&char.name,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue