added nature and characteristic selection as dropdown
parent
70a849c7b5
commit
089eb8917a
|
|
@ -1,6 +1,11 @@
|
||||||
use bevy::prelude::*;
|
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;
|
pub struct InspectorPlugin;
|
||||||
|
|
||||||
|
|
@ -8,7 +13,8 @@ impl Plugin for InspectorPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.register_type::<InspectorPokemon>()
|
app.register_type::<InspectorPokemon>()
|
||||||
.insert_resource(InspectorPokemon::default())
|
.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>,
|
pub characteristic: Option<Characteristic>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resource_setup(
|
fn resource_setup(mut inspector: ResMut<InspectorPokemon>, pokemon: Res<Database<Pokemon>>) {
|
||||||
mut inspector: ResMut<InspectorPokemon>,
|
|
||||||
pokemon: Res<Database<Pokemon>>,
|
|
||||||
natures: Res<Database<Nature>>,
|
|
||||||
) {
|
|
||||||
inspector.pokemon = pokemon.map.get("sceptile").cloned();
|
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>,
|
pub stats: HashMap<BaseStat, i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Reflect, FromReflect)]
|
#[derive(Default, Debug, Clone, Reflect, FromReflect, PartialEq)]
|
||||||
pub struct Pokemon {
|
pub struct Pokemon {
|
||||||
pub id: u16,
|
pub id: u16,
|
||||||
pub national: 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 struct Type {
|
||||||
pub id: u8,
|
pub id: u8,
|
||||||
pub name: String,
|
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 struct Nature {
|
||||||
pub id: u8,
|
pub id: u8,
|
||||||
pub name: String,
|
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 {
|
pub struct Characteristic {
|
||||||
stat: BaseStat,
|
pub stat: BaseStat,
|
||||||
name: String,
|
pub name: String,
|
||||||
possible_values: Vec<u8>,
|
pub possible_values: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromJson for Characteristic {
|
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 bevy_egui::{egui, EguiContexts, EguiPlugin};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
use crate::{
|
use crate::{database::Database, inspector::InspectorPokemon, pokemon::*};
|
||||||
database::{load_url_asset, Database},
|
|
||||||
inspector::InspectorPokemon,
|
|
||||||
pokemon::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref BASE_STAT_COLOR_RANGES: Vec<(u8, egui::Color32)> = vec![
|
static ref BASE_STAT_COLOR_RANGES: Vec<(u8, egui::Color32)> = vec![
|
||||||
|
|
@ -37,15 +33,14 @@ impl Plugin for UiPlugin {
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
pub struct UiAssets {
|
pub struct UiAssets {
|
||||||
bar_handle: Handle<Image>,
|
pub bar_handle: Handle<Image>,
|
||||||
sprite_map: HashMap<String, (Handle<Image>, egui::TextureId)>,
|
pub sprite_map: HashMap<String, (Handle<Image>, egui::TextureId)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default, Reflect)]
|
#[derive(Resource, Default, Reflect)]
|
||||||
#[reflect(Resource)]
|
#[reflect(Resource)]
|
||||||
pub struct UiState {
|
pub struct UiState {
|
||||||
name: String,
|
pub name: String,
|
||||||
nature: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_assets(mut ui_assets: ResMut<UiAssets>, assets: Res<AssetServer>) {
|
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(
|
fn handle_state(
|
||||||
ui_state: Res<UiState>,
|
ui_state: Res<UiState>,
|
||||||
pokemon: Res<Database<Pokemon>>,
|
pokemon: Res<Database<Pokemon>>,
|
||||||
nature: Res<Database<Nature>>,
|
|
||||||
assets: Res<AssetServer>,
|
|
||||||
mut inspector: ResMut<InspectorPokemon>,
|
mut inspector: ResMut<InspectorPokemon>,
|
||||||
mut ui_assets: ResMut<UiAssets>,
|
|
||||||
mut contexts: EguiContexts,
|
|
||||||
) {
|
) {
|
||||||
if ui_state.is_changed() {
|
if ui_state.is_changed() {
|
||||||
if let Some(pokemon) = pokemon.map.get(ui_state.name.to_lowercase().as_str()) {
|
if let Some(pokemon) = pokemon.map.get(ui_state.name.to_lowercase().as_str()) {
|
||||||
inspector.pokemon = Some(pokemon.clone());
|
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(
|
fn ui_system(
|
||||||
mut contexts: EguiContexts,
|
mut contexts: EguiContexts,
|
||||||
inspector: Res<InspectorPokemon>,
|
mut inspector: ResMut<InspectorPokemon>,
|
||||||
ui_assets: Res<UiAssets>,
|
ui_assets: Res<UiAssets>,
|
||||||
mut ui_state: ResMut<UiState>,
|
mut ui_state: ResMut<UiState>,
|
||||||
mut rendered_texture_id: Local<egui::TextureId>,
|
mut rendered_texture_id: Local<egui::TextureId>,
|
||||||
mut is_initialized: Local<bool>,
|
mut is_initialized: Local<bool>,
|
||||||
|
characteristics: Res<Database<Characteristic>>,
|
||||||
|
natures: Res<Database<Nature>>,
|
||||||
) {
|
) {
|
||||||
if !*is_initialized {
|
if !*is_initialized {
|
||||||
*is_initialized = true;
|
*is_initialized = true;
|
||||||
|
|
@ -99,25 +75,61 @@ fn ui_system(
|
||||||
}
|
}
|
||||||
|
|
||||||
egui::CentralPanel::default().show(contexts.ctx_mut(), |ui| {
|
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.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.set_width(60.0);
|
ui.set_width(INPUT_LABEL_WIDTH);
|
||||||
ui.label("pokemon:");
|
ui.label("pokemon:");
|
||||||
});
|
});
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.set_width(150.0);
|
ui.set_width(INPUT_TEXT_WIDTH);
|
||||||
ui.text_edit_singleline(&mut ui_state.name);
|
ui.text_edit_singleline(&mut ui_state.name);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.set_width(60.0);
|
ui.set_width(INPUT_LABEL_WIDTH);
|
||||||
ui.label("nature:");
|
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.horizontal(|ui| {
|
ui.horizontal(|ui| {
|
||||||
ui.set_width(150.0);
|
ui.set_width(INPUT_LABEL_WIDTH);
|
||||||
ui.text_edit_singleline(&mut ui_state.nature);
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(pokemon) = inspector.pokemon.as_ref() {
|
if let Some(pokemon) = inspector.pokemon.as_ref() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue