Skip to content

Commit

Permalink
Some Item cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed May 31, 2024
1 parent e760d67 commit dac5aeb
Show file tree
Hide file tree
Showing 28 changed files with 826 additions and 553 deletions.
8 changes: 5 additions & 3 deletions korangar/src/input/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use cgmath::Vector2;
use korangar_interface::event::ClickAction;
use korangar_interface::ElementEvent;
use korangar_networking::{MarketItem, NoMetadata};
use ragnarok_packets::{
AccountId, BuyOrSellOption, CharacterId, CharacterServerInformation, EntityId, HotbarSlot, PurchaseItem, ShopId, SoldItem, TilePosition,
AccountId, BuyOrSellOption, CharacterId, CharacterServerInformation, EntityId, HotbarSlot, PurchaseItemInformation, ShopId,
SoldItemInformation, TilePosition,
};

use crate::interface::application::{InterfaceSettings, InternalThemeKind};
Expand Down Expand Up @@ -73,15 +75,15 @@ pub enum UserEvent {
character_id: CharacterId,
},
PurchaseItems {
items: Vec<PurchaseItem>,
items: Vec<MarketItem<u32>>,
},
CloseShop,
BuyOrSell {
shop_id: ShopId,
buy_or_sell: BuyOrSellOption,
},
SellItems {
items: Vec<SoldItem>,
items: Vec<SoldItemInformation>,
},
#[cfg(feature = "debug")]
OpenMarkerDetails(MarkerIdentifier),
Expand Down
9 changes: 5 additions & 4 deletions korangar/src/input/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use std::sync::Arc;
use cgmath::Vector2;
use korangar_interface::application::MouseInputModeTrait;
use korangar_interface::elements::{Element, ElementCell};
use korangar_networking::InventoryItem;
use vulkano::image::view::ImageView;

use crate::interface::application::InterfaceSettings;
use crate::interface::resource::{ItemSource, SkillSource};
use crate::inventory::{Item, Skill};
use crate::loaders::{Actions, AnimationState, Sprite};
use crate::inventory::Skill;
use crate::loaders::{Actions, AnimationState, ResourceMetadata, Sprite};

#[derive(Default)]
pub enum MouseInputMode {
MoveItem(ItemSource, Item),
MoveItem(ItemSource, InventoryItem<ResourceMetadata>),
MoveSkill(SkillSource, Skill),
MoveInterface(usize),
ResizeInterface(usize),
Expand Down Expand Up @@ -40,7 +41,7 @@ impl MouseInputMode {

pub fn grabbed(&self) -> Option<Grabbed> {
match self {
MouseInputMode::MoveItem(_, item) => Some(Grabbed::Texture(item.texture.clone())),
MouseInputMode::MoveItem(_, item) => Some(Grabbed::Texture(item.metadata.texture.clone())),
MouseInputMode::MoveSkill(_, skill) => Some(Grabbed::Action(
skill.sprite.clone(),
skill.actions.clone(),
Expand Down
49 changes: 26 additions & 23 deletions korangar/src/interface/elements/containers/cart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ use korangar_interface::state::{PlainRemote, PlainTrackedState, Remote, TrackedS
use korangar_interface::{dimension_bound, size_bound};
use korangar_networking::{ItemQuantity, MarketItem};
use num::Integer;
use ragnarok_packets::{ItemId, PurchaseItem};
use ragnarok_packets::{ItemId, MarketItemInformation};

use crate::graphics::{Color, InterfaceRenderer, Renderer, SpriteRenderer};
use crate::input::{MouseInputMode, UserEvent};
use crate::interface::application::InterfaceSettings;
use crate::interface::layout::{CornerRadius, ScreenClip, ScreenPosition, ScreenSize};
use crate::interface::resource::{ItemSource, Move, PartialMove};
use crate::interface::theme::InterfaceTheme;
use crate::loaders::{FontSize, Scaling};
use crate::FullPurchaseItem;
use crate::loaders::{FontSize, ResourceMetadata, Scaling};

pub struct ItemDisplay {
item: FullPurchaseItem,
item: MarketItem<(ResourceMetadata, u32)>,
state: ElementState<InterfaceSettings>,
}

impl ItemDisplay {
pub fn new(item: FullPurchaseItem) -> Self {
pub fn new(item: MarketItem<(ResourceMetadata, u32)>) -> Self {
Self {
item,
state: ElementState::default(),
Expand Down Expand Up @@ -81,7 +80,7 @@ impl Element<InterfaceSettings> for ItemDisplay {

renderer.renderer.render_sprite(
renderer.render_target,
self.item.texture.clone(),
self.item.metadata.0.texture.clone(),
renderer.position,
ScreenSize::uniform(30.0).scaled(Scaling::new(application.get_scaling_factor())),
renderer.clip,
Expand All @@ -90,10 +89,10 @@ impl Element<InterfaceSettings> for ItemDisplay {
);

renderer.render_text(
&format!("{}", self.item.quantity),
&format!("{}", self.item.metadata.1),
ScreenPosition::default(),
theme.button.foreground_color.get(),
FontSize::new(8.0),
FontSize::new(12.0),
);
}
}
Expand All @@ -105,8 +104,8 @@ pub struct CartItemEntry {

impl CartItemEntry {
fn add_button(
mut cart: PlainTrackedState<Vec<FullPurchaseItem>>,
item: FullPurchaseItem,
mut cart: PlainTrackedState<Vec<MarketItem<(ResourceMetadata, u32)>>>,
item: MarketItem<(ResourceMetadata, u32)>,
amount: u32,
) -> ElementCell<InterfaceSettings> {
let disabled_cart = cart.clone();
Expand All @@ -117,9 +116,9 @@ impl CartItemEntry {
cart.mutate(|cart| {
let purchase = cart.iter_mut().find(|purchase| purchase.item_id == item.item_id).unwrap();

purchase.quantity = purchase.quantity.saturating_sub(amount);
purchase.metadata.1 = purchase.metadata.1.saturating_sub(amount);

if purchase.quantity == 0 {
if purchase.metadata.1 == 0 {
cart.retain(|purchase| purchase.item_id != item.item_id);
}
});
Expand All @@ -131,18 +130,22 @@ impl CartItemEntry {
.get()
.iter()
.find(|cart_item| cart_item.item_id == item.item_id)
.map(|cart_item| amount.saturating_sub(cart_item.quantity) == 0)
.map(|cart_item| amount.saturating_sub(cart_item.metadata.1) == 0)
.unwrap_or(true)
})
.with_width_bound(dimension_bound!(20%))
.build()
.wrap()
}

pub fn new(item: FullPurchaseItem, mut cart: PlainTrackedState<Vec<FullPurchaseItem>>, secondary_color: bool) -> Self {
pub fn new(
item: MarketItem<(ResourceMetadata, u32)>,
mut cart: PlainTrackedState<Vec<MarketItem<(ResourceMetadata, u32)>>>,
secondary_color: bool,
) -> Self {
let elements = vec![
ItemDisplay::new(item.clone()).wrap(),
Headline::new(format!("{}: {}", item.name, item.quantity), size_bound!(!, 16)).wrap(),
Headline::new(format!("{}: {}", item.metadata.0.name, item.metadata.1), size_bound!(!, 16)).wrap(),
Self::add_button(cart.clone(), item.clone(), 1),
Self::add_button(cart.clone(), item.clone(), 10),
Self::add_button(cart.clone(), item.clone(), 100),
Expand Down Expand Up @@ -269,8 +272,8 @@ pub struct CartSum {
}

impl CartSum {
pub fn new(cart: PlainTrackedState<Vec<FullPurchaseItem>>) -> Self {
let total_price = cart.get().iter().map(|item| item.price.0 * item.quantity).sum::<u32>();
pub fn new(cart: PlainTrackedState<Vec<MarketItem<(ResourceMetadata, u32)>>>) -> Self {
let total_price = cart.get().iter().map(|item| item.price.0 * item.metadata.1).sum::<u32>();
let state = ElementState::default();

Self { total_price, state }
Expand Down Expand Up @@ -326,13 +329,13 @@ impl Element<InterfaceSettings> for CartSum {
}

pub struct CartContainer {
cart: PlainTrackedState<Vec<FullPurchaseItem>>,
cart_remote: PlainRemote<Vec<FullPurchaseItem>>,
cart: PlainTrackedState<Vec<MarketItem<(ResourceMetadata, u32)>>>,
cart_remote: PlainRemote<Vec<MarketItem<(ResourceMetadata, u32)>>>,
state: ContainerState<InterfaceSettings>,
}

impl CartContainer {
pub fn new(cart: PlainTrackedState<Vec<FullPurchaseItem>>) -> Self {
pub fn new(cart: PlainTrackedState<Vec<MarketItem<(ResourceMetadata, u32)>>>) -> Self {
let mut elements = cart
.get()
.iter()
Expand All @@ -352,9 +355,9 @@ impl CartContainer {
let items = cart
.get()
.iter()
.map(|item| PurchaseItem {
item_id: item.item_id,
amount: item.quantity,
.map(|item| MarketItem {
metadata: item.metadata.1,
..item.clone()
})
.collect();

Expand Down
19 changes: 14 additions & 5 deletions korangar/src/interface/elements/containers/equipment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use korangar_interface::event::{ChangeEvent, HoverInformation};
use korangar_interface::layout::PlacementResolver;
use korangar_interface::state::{PlainRemote, Remote};
use korangar_interface::{dimension_bound, size_bound};
use korangar_networking::{InventoryItem, InventoryItemDetails};
use ragnarok_packets::EquipPosition;

use crate::graphics::{Color, InterfaceRenderer, Renderer};
Expand All @@ -14,15 +15,15 @@ use crate::interface::elements::ItemBox;
use crate::interface::layout::{ScreenClip, ScreenPosition, ScreenSize};
use crate::interface::resource::ItemSource;
use crate::interface::theme::InterfaceTheme;
use crate::inventory::Item;
use crate::loaders::ResourceMetadata;

pub struct EquipmentContainer {
items: PlainRemote<Vec<Item>>,
items: PlainRemote<Vec<InventoryItem<ResourceMetadata>>>,
state: ContainerState<InterfaceSettings>,
}

impl EquipmentContainer {
pub fn new(items: PlainRemote<Vec<Item>>) -> Self {
pub fn new(items: PlainRemote<Vec<InventoryItem<ResourceMetadata>>>) -> Self {
const SLOT_POSITIONS: [EquipPosition; 9] = [
EquipPosition::HEAD_TOP,
EquipPosition::HEAD_MIDDLE,
Expand Down Expand Up @@ -75,13 +76,21 @@ impl EquipmentContainer {
.with_width(dimension_bound!(!))
.wrap();

let item = items.iter().find(|item| item.equipped_position.contains(slot)).cloned();
let item = items
.iter()
.find(|item| match &item.details {
korangar_networking::InventoryItemDetails::Equippable { equipped_position, .. } => {
equipped_position.contains(slot)
}
_ => false,
})
.cloned();

let item_box = ItemBox::new(
item,
ItemSource::Equipment { position: slot },
Box::new(
move |mouse_mode| matches!(mouse_mode, MouseInputMode::MoveItem(_, item) if item.equip_position.contains(slot)),
move |mouse_mode| matches!(mouse_mode, MouseInputMode::MoveItem(_, InventoryItem { details: InventoryItemDetails::Equippable { equip_position, .. }, ..}) if equip_position.contains(slot)),
),
);

Expand Down
7 changes: 4 additions & 3 deletions korangar/src/interface/elements/containers/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use korangar_interface::event::{ChangeEvent, HoverInformation};
use korangar_interface::layout::PlacementResolver;
use korangar_interface::size_bound;
use korangar_interface::state::{PlainRemote, Remote};
use korangar_networking::InventoryItem;

use crate::graphics::{Color, InterfaceRenderer, Renderer};
use crate::input::MouseInputMode;
Expand All @@ -11,15 +12,15 @@ use crate::interface::elements::ItemBox;
use crate::interface::layout::{CornerRadius, ScreenClip, ScreenPosition, ScreenSize};
use crate::interface::resource::{ItemSource, Move, PartialMove};
use crate::interface::theme::InterfaceTheme;
use crate::inventory::Item;
use crate::loaders::ResourceMetadata;

pub struct InventoryContainer {
items: PlainRemote<Vec<Item>>,
items: PlainRemote<Vec<InventoryItem<ResourceMetadata>>>,
state: ContainerState<InterfaceSettings>,
}

impl InventoryContainer {
pub fn new(items: PlainRemote<Vec<Item>>) -> Self {
pub fn new(items: PlainRemote<Vec<InventoryItem<ResourceMetadata>>>) -> Self {
let elements = {
let items = items.get();

Expand Down
Loading

0 comments on commit dac5aeb

Please sign in to comment.