Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ use bevy::{
};
use bevy_egui::{egui, EguiContexts, EguiSet};
use bevy_rapier3d::plugin::PhysicsSet;
use enum_map::{enum_map, EnumMap};
use exe_resource_loader::{ExeResourceCursor, ExeResourceLoader};
use rose_data::{CharacterMotionDatabaseOptions, NpcDatabaseOptions, ZoneId};
use rose_file_readers::{
AruaVfsIndex, HostFilesystemDevice, IrosePhVfsIndex, LtbFile, StbFile, TitanVfsIndex, VfsIndex,
VirtualFilesystem, VirtualFilesystemDevice, ZscFile,
};
use rose_game_common::components::{InventoryPageType, INVENTORY_PAGE_SIZE};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};
Expand Down Expand Up @@ -122,6 +125,24 @@ pub struct AccountConfig {
pub password: String,
}

#[derive(Deserialize, Serialize, Clone)]
#[serde(default)]
pub struct CharacterConfig {
slots: EnumMap<InventoryPageType, Vec<(InventoryPageType, usize)>>,
}

impl Default for CharacterConfig {
fn default() -> Self {
Self {
slots: enum_map! {
page_type => (0..INVENTORY_PAGE_SIZE)
.map(|index| (page_type, index))
.collect(),
},
}
}
}

#[derive(Default, Deserialize, Serialize, Clone)]
#[serde(default)]
pub struct AutoLoginConfig {
Expand Down Expand Up @@ -312,6 +333,7 @@ impl Default for GraphicsConfig {
#[serde(default)]
pub struct Config {
pub account: AccountConfig,
pub character: HashMap<String, CharacterConfig>,
pub auto_login: AutoLoginConfig,
pub filesystem: FilesystemConfig,
pub game: GameConfig,
Expand All @@ -322,6 +344,20 @@ pub struct Config {
pub hotkeys: HotkeysConfig,
}

impl Config {
pub fn get_inventory_slots(
&mut self,
username: String,
page: InventoryPageType,
) -> &mut Vec<(InventoryPageType, usize)> {
&mut self
.character
.entry(username)
.or_insert_with(|| CharacterConfig::default())
.slots[page]
}
}

pub fn load_config(path: &PathBuf) -> Config {
let toml_str = match std::fs::read_to_string(path) {
Ok(toml_str) => toml_str,
Expand Down
57 changes: 37 additions & 20 deletions src/ui/ui_inventory_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ use bevy::{
prelude::{Assets, EventWriter, Events, Local, Query, Res, ResMut, With, World},
};
use bevy_egui::{egui, EguiContexts};
use enum_map::{enum_map, EnumMap};

use rose_data::{AmmoIndex, EquipmentIndex, Item, VehiclePartIndex};
use rose_game_common::components::{
Equipment, Inventory, InventoryPageType, ItemSlot, INVENTORY_PAGE_SIZE,
CharacterInfo, Equipment, Inventory, InventoryPageType, ItemSlot,
};
use std::path::Path;

use crate::{
components::{Cooldowns, PlayerCharacter},
events::{NumberInputDialogEvent, PlayerCommandEvent},
resources::{GameData, UiResources},
save_config,
ui::{
tooltips::{PlayerTooltipQuery, PlayerTooltipQueryItem},
ui_add_item_tooltip,
widgets::{DataBindings, Dialog, Widget},
DialogInstance, DragAndDropId, DragAndDropSlot, UiSoundEvent, UiStateDragAndDrop,
UiStateWindows,
},
Config,
};

const IID_BTN_CLOSE: i32 = 10;
Expand All @@ -47,31 +48,27 @@ const IID_PANE_INVEN: i32 = 300;

pub struct UiStateInventory {
dialog_instance: DialogInstance,
item_slot_map: EnumMap<InventoryPageType, Vec<ItemSlot>>,
current_equipment_tab: i32,
current_vehicle_tab: i32,
current_inventory_tab: i32,
minimised: bool,
config_changed: bool,
}

impl Default for UiStateInventory {
fn default() -> Self {
Self {
dialog_instance: DialogInstance::new("DLGITEM.XML"),
item_slot_map: enum_map! {
page_type => (0..INVENTORY_PAGE_SIZE)
.map(|index| ItemSlot::Inventory(page_type, index))
.collect(),
},
current_equipment_tab: IID_TAB_EQUIP_AVATAR,
current_vehicle_tab: IID_TAB_INVEN_PAT,
current_inventory_tab: IID_TAB_INVEN_EQUIP,
minimised: false,
config_changed: false,
}
}
}

const EQUIPMENT_GRID_SLOTS: [(rose_game_common::components::ItemSlot, egui::Pos2); 14] = [
const EQUIPMENT_GRID_SLOTS: [(ItemSlot, egui::Pos2); 14] = [
(
ItemSlot::Equipment(EquipmentIndex::Face),
egui::pos2(19.0, 67.0),
Expand Down Expand Up @@ -222,9 +219,10 @@ fn ui_add_inventory_slot(
player_tooltip_data: Option<&PlayerTooltipQueryItem>,
game_data: &GameData,
ui_resources: &UiResources,
item_slot_map: &mut EnumMap<InventoryPageType, Vec<ItemSlot>>,
config_changed: &mut bool,
ui_state_dnd: &mut UiStateDragAndDrop,
player_command_events: &mut EventWriter<PlayerCommandEvent>,
config: &mut Config,
) {
let drag_accepts = match inventory_slot {
ItemSlot::Inventory(page_type, _) => match page_type {
Expand Down Expand Up @@ -433,23 +431,28 @@ fn ui_add_inventory_slot(
swap_inventory_slots
{
if page_a == page_b {
let inventory_map = &mut item_slot_map[page_a];
let inventory_map =
config.get_inventory_slots(player.character_info.name.clone(), page_a);

let source_index = inventory_map
.iter()
.position(|slot| slot == &ItemSlot::Inventory(page_a, slot_a));
.position(|slot| slot == &(page_a, slot_a));
let destination_index = inventory_map
.iter()
.position(|slot| slot == &ItemSlot::Inventory(page_b, slot_b));
.position(|slot| slot == &(page_b, slot_b));

if let (Some(source_index), Some(destination_index)) = (source_index, destination_index)
{
inventory_map.swap(source_index, destination_index);
*config_changed = true;
}
}
}
}

#[derive(WorldQuery)]
pub struct PlayerQuery<'w> {
character_info: &'w CharacterInfo,
equipment: &'w Equipment,
inventory: &'w Inventory,
cooldowns: &'w Cooldowns,
Expand All @@ -466,6 +469,7 @@ pub fn ui_inventory_system(
dialog_assets: Res<Assets<Dialog>>,
game_data: Res<GameData>,
ui_resources: Res<UiResources>,
mut config: ResMut<Config>,
mut player_command_events: EventWriter<PlayerCommandEvent>,
mut number_input_dialog_events: EventWriter<NumberInputDialogEvent>,
) {
Expand Down Expand Up @@ -547,9 +551,10 @@ pub fn ui_inventory_system(
player_tooltip_data.as_ref(),
&game_data,
&ui_resources,
&mut ui_state_inventory.item_slot_map,
&mut ui_state_inventory.config_changed,
&mut ui_state_dnd,
&mut player_command_events,
&mut config,
);
}
}
Expand Down Expand Up @@ -578,9 +583,10 @@ pub fn ui_inventory_system(
player_tooltip_data.as_ref(),
&game_data,
&ui_resources,
&mut ui_state_inventory.item_slot_map,
&mut ui_state_inventory.config_changed,
&mut ui_state_dnd,
&mut player_command_events,
&mut config,
);
}
}
Expand All @@ -598,12 +604,15 @@ pub fn ui_inventory_system(

for row in 0..6 {
for column in 0..5 {
let inventory_slot =
ui_state_inventory.item_slot_map[current_page][column + row * 5];
let page_slots = config.get_inventory_slots(
player.character_info.name.clone(),
current_page,
);
let (page_type, index) = page_slots[column + row * 5];

ui_add_inventory_slot(
ui,
inventory_slot,
ItemSlot::Inventory(page_type, index),
egui::pos2(
12.0 + column as f32 * 41.0,
y_start + row as f32 * 41.0,
Expand All @@ -612,9 +621,10 @@ pub fn ui_inventory_system(
player_tooltip_data.as_ref(),
&game_data,
&ui_resources,
&mut ui_state_inventory.item_slot_map,
&mut ui_state_inventory.config_changed,
&mut ui_state_dnd,
&mut player_command_events,
&mut config,
);
}

Expand All @@ -637,6 +647,13 @@ pub fn ui_inventory_system(
);
});

if !ui_state_windows.inventory_open && ui_state_inventory.config_changed {
let path = config.filesystem.config_path.clone();
save_config(&config, Path::new(&path));

ui_state_inventory.config_changed = false;
}

if response_close_button.map_or(false, |r| r.clicked()) {
ui_state_windows.inventory_open = false;
}
Expand Down
Loading