2019-09-25 20:18:40 +00:00
|
|
|
use super::{
|
|
|
|
img_ids::{Imgs, ImgsRot},
|
2019-10-22 18:18:40 +00:00
|
|
|
item_imgs::{ItemImgs, ItemKey},
|
2019-09-26 15:07:09 +00:00
|
|
|
Event as HudEvent, Fonts, TEXT_COLOR,
|
2019-09-25 20:18:40 +00:00
|
|
|
};
|
2019-09-26 14:00:24 +00:00
|
|
|
use crate::ui::{ImageFrame, Tooltip, TooltipManager, Tooltipable};
|
2019-07-25 17:41:06 +00:00
|
|
|
use client::Client;
|
2019-05-04 10:43:37 +00:00
|
|
|
use conrod_core::{
|
2019-10-09 19:28:05 +00:00
|
|
|
color, image,
|
2020-02-01 20:39:39 +00:00
|
|
|
widget::{self, Button, Image, Rectangle},
|
2019-10-09 22:42:39 +00:00
|
|
|
widget_ids, Color, Positionable, Sizeable, Widget, WidgetCommon,
|
2019-05-04 10:43:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
widget_ids! {
|
|
|
|
struct Ids {
|
|
|
|
bag_close,
|
2019-06-30 14:04:10 +00:00
|
|
|
bag_top,
|
|
|
|
bag_mid,
|
|
|
|
bag_bot,
|
2019-05-04 10:43:37 +00:00
|
|
|
inv_alignment,
|
2019-05-06 18:49:12 +00:00
|
|
|
inv_grid_1,
|
|
|
|
inv_grid_2,
|
2019-05-04 10:43:37 +00:00
|
|
|
inv_scrollbar,
|
2019-07-25 22:52:28 +00:00
|
|
|
inv_slots_0,
|
2019-05-04 10:43:37 +00:00
|
|
|
map_title,
|
2019-07-25 22:52:28 +00:00
|
|
|
inv_slots[],
|
|
|
|
items[],
|
2019-09-25 22:16:23 +00:00
|
|
|
tooltip[],
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(WidgetCommon)]
|
2019-10-09 22:42:39 +00:00
|
|
|
#[allow(dead_code)]
|
2019-05-04 10:43:37 +00:00
|
|
|
pub struct Bag<'a> {
|
2019-07-25 17:41:06 +00:00
|
|
|
client: &'a Client,
|
2019-05-04 10:43:37 +00:00
|
|
|
imgs: &'a Imgs,
|
2019-10-09 19:28:05 +00:00
|
|
|
item_imgs: &'a ItemImgs,
|
2019-07-04 20:55:23 +00:00
|
|
|
fonts: &'a Fonts,
|
2019-05-04 10:43:37 +00:00
|
|
|
#[conrod(common_builder)]
|
|
|
|
common: widget::CommonBuilder,
|
2019-09-25 16:51:47 +00:00
|
|
|
rot_imgs: &'a ImgsRot,
|
2019-09-25 20:18:40 +00:00
|
|
|
tooltip_manager: &'a mut TooltipManager,
|
2020-01-23 17:14:02 +00:00
|
|
|
pulse: f32,
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Bag<'a> {
|
2019-09-25 20:18:40 +00:00
|
|
|
pub fn new(
|
|
|
|
client: &'a Client,
|
|
|
|
imgs: &'a Imgs,
|
2019-10-09 19:28:05 +00:00
|
|
|
item_imgs: &'a ItemImgs,
|
2019-09-25 20:18:40 +00:00
|
|
|
fonts: &'a Fonts,
|
|
|
|
rot_imgs: &'a ImgsRot,
|
|
|
|
tooltip_manager: &'a mut TooltipManager,
|
2020-01-23 17:14:02 +00:00
|
|
|
pulse: f32,
|
2019-09-25 20:18:40 +00:00
|
|
|
) -> Self {
|
2019-05-04 10:43:37 +00:00
|
|
|
Self {
|
2019-07-25 17:41:06 +00:00
|
|
|
client,
|
2019-05-04 10:43:37 +00:00
|
|
|
imgs,
|
2019-10-09 19:28:05 +00:00
|
|
|
item_imgs,
|
2019-07-04 20:55:23 +00:00
|
|
|
fonts,
|
2019-05-04 10:43:37 +00:00
|
|
|
common: widget::CommonBuilder::default(),
|
2019-09-25 16:51:47 +00:00
|
|
|
rot_imgs,
|
|
|
|
tooltip_manager,
|
2020-01-23 17:14:02 +00:00
|
|
|
pulse,
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct State {
|
|
|
|
ids: Ids,
|
2019-10-22 18:18:40 +00:00
|
|
|
img_id_cache: Vec<Option<(ItemKey, image::Id)>>,
|
2019-07-26 17:08:40 +00:00
|
|
|
selected_slot: Option<usize>,
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:04:10 +00:00
|
|
|
const BAG_SCALE: f64 = 4.0;
|
|
|
|
|
2019-05-04 10:43:37 +00:00
|
|
|
pub enum Event {
|
2019-07-25 22:52:28 +00:00
|
|
|
HudEvent(HudEvent),
|
2019-05-04 10:43:37 +00:00
|
|
|
Close,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Bag<'a> {
|
2020-02-01 20:39:39 +00:00
|
|
|
type Event = Option<Event>;
|
2019-05-04 10:43:37 +00:00
|
|
|
type State = State;
|
|
|
|
type Style = ();
|
|
|
|
|
|
|
|
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
|
|
|
|
State {
|
|
|
|
ids: Ids::new(id_gen),
|
2019-10-09 19:28:05 +00:00
|
|
|
img_id_cache: Vec::new(),
|
2019-07-26 17:08:40 +00:00
|
|
|
selected_slot: None,
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
fn style(&self) -> Self::Style { () }
|
2019-05-04 10:43:37 +00:00
|
|
|
|
|
|
|
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
|
2019-05-07 05:40:03 +00:00
|
|
|
let widget::UpdateArgs { state, ui, .. } = args;
|
2019-05-04 10:43:37 +00:00
|
|
|
|
2019-09-25 20:18:40 +00:00
|
|
|
let mut event = None;
|
2019-07-25 22:52:28 +00:00
|
|
|
|
|
|
|
let invs = self.client.inventories();
|
|
|
|
let inventory = match invs.get(self.client.entity()) {
|
|
|
|
Some(inv) => inv,
|
|
|
|
None => return None,
|
|
|
|
};
|
2020-01-23 17:14:02 +00:00
|
|
|
|
2019-09-25 16:51:47 +00:00
|
|
|
// Tooltips
|
|
|
|
let item_tooltip = Tooltip::new({
|
|
|
|
// Edge images [t, b, r, l]
|
|
|
|
// Corner images [tr, tl, br, bl]
|
|
|
|
let edge = &self.rot_imgs.tt_side;
|
|
|
|
let corner = &self.rot_imgs.tt_corner;
|
|
|
|
ImageFrame::new(
|
|
|
|
[edge.cw180, edge.none, edge.cw270, edge.cw90],
|
|
|
|
[corner.none, corner.cw270, corner.cw90, corner.cw180],
|
|
|
|
Color::Rgba(0.08, 0.07, 0.04, 1.0),
|
|
|
|
5.0,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.title_font_size(15)
|
2019-10-09 19:28:05 +00:00
|
|
|
.parent(ui.window)
|
|
|
|
.desc_font_size(12)
|
2019-09-25 16:51:47 +00:00
|
|
|
.title_text_color(TEXT_COLOR)
|
2019-09-26 14:00:24 +00:00
|
|
|
.desc_text_color(TEXT_COLOR);
|
2019-07-25 17:41:06 +00:00
|
|
|
|
2019-06-30 14:04:10 +00:00
|
|
|
// Bag parts
|
|
|
|
Image::new(self.imgs.bag_bot)
|
2019-10-09 19:28:05 +00:00
|
|
|
.w_h(58.0 * BAG_SCALE, 9.0 * BAG_SCALE)
|
2019-05-06 18:49:12 +00:00
|
|
|
.bottom_right_with_margins_on(ui.window, 60.0, 5.0)
|
2019-06-30 14:04:10 +00:00
|
|
|
.set(state.ids.bag_bot, ui);
|
2019-09-27 04:23:42 +00:00
|
|
|
let mid_height = ((inventory.len() + 4) / 5) as f64 * 44.0;
|
|
|
|
Image::new(self.imgs.bag_mid)
|
2019-10-09 19:28:05 +00:00
|
|
|
.w_h(58.0 * BAG_SCALE, mid_height)
|
2019-09-27 04:23:42 +00:00
|
|
|
.up_from(state.ids.bag_bot, 0.0)
|
|
|
|
.set(state.ids.bag_mid, ui);
|
2019-06-30 14:04:10 +00:00
|
|
|
Image::new(self.imgs.bag_top)
|
2019-10-09 19:28:05 +00:00
|
|
|
.w_h(58.0 * BAG_SCALE, 9.0 * BAG_SCALE)
|
2019-06-30 14:04:10 +00:00
|
|
|
.up_from(state.ids.bag_mid, 0.0)
|
2019-07-07 13:44:34 +00:00
|
|
|
.set(state.ids.bag_top, ui);
|
2019-05-04 10:43:37 +00:00
|
|
|
|
|
|
|
// Alignment for Grid
|
2019-10-09 19:28:05 +00:00
|
|
|
Rectangle::fill_with([56.0 * BAG_SCALE, mid_height], color::TRANSPARENT)
|
2019-09-27 04:23:42 +00:00
|
|
|
.top_left_with_margins_on(state.ids.bag_mid, 0.0, 3.0 * BAG_SCALE)
|
|
|
|
.scroll_kids_vertically()
|
|
|
|
.set(state.ids.inv_alignment, ui);
|
2019-07-25 22:52:28 +00:00
|
|
|
|
2019-09-27 04:23:42 +00:00
|
|
|
// Create available inventory slot widgets
|
2019-07-25 22:52:28 +00:00
|
|
|
if state.ids.inv_slots.len() < inventory.len() {
|
|
|
|
state.update(|s| {
|
|
|
|
s.ids
|
|
|
|
.inv_slots
|
|
|
|
.resize(inventory.len(), &mut ui.widget_id_generator());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if state.ids.items.len() < inventory.len() {
|
2019-06-29 13:03:48 +00:00
|
|
|
state.update(|s| {
|
|
|
|
s.ids
|
2019-07-25 22:52:28 +00:00
|
|
|
.items
|
|
|
|
.resize(inventory.len(), &mut ui.widget_id_generator());
|
2019-06-29 13:03:48 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-09 19:28:05 +00:00
|
|
|
// Expand img id cache to the number of slots
|
|
|
|
if state.img_id_cache.len() < inventory.len() {
|
|
|
|
state.update(|s| {
|
|
|
|
s.img_id_cache.resize(inventory.len(), None);
|
|
|
|
});
|
|
|
|
}
|
2019-07-25 22:52:28 +00:00
|
|
|
|
|
|
|
// Display inventory contents
|
|
|
|
for (i, item) in inventory.slots().iter().enumerate() {
|
2019-06-29 00:11:53 +00:00
|
|
|
let x = i % 5;
|
2019-06-29 13:03:48 +00:00
|
|
|
let y = i / 5;
|
2019-07-25 22:52:28 +00:00
|
|
|
|
2019-07-26 17:08:40 +00:00
|
|
|
let is_selected = Some(i) == state.selected_slot;
|
|
|
|
|
2019-07-25 22:52:28 +00:00
|
|
|
// Slot
|
2020-01-23 17:14:02 +00:00
|
|
|
|
2019-10-09 19:28:05 +00:00
|
|
|
let slot_widget = Button::image(if !is_selected {
|
|
|
|
self.imgs.inv_slot
|
|
|
|
} else {
|
|
|
|
self.imgs.inv_slot_sel
|
|
|
|
})
|
|
|
|
.top_left_with_margins_on(
|
|
|
|
state.ids.inv_alignment,
|
|
|
|
0.0 + y as f64 * (40.0 + 2.0),
|
|
|
|
0.0 + x as f64 * (40.0 + 2.0),
|
2020-01-23 17:14:02 +00:00
|
|
|
)
|
|
|
|
.wh(if is_selected { [40.0; 2] } else { [40.0; 2] })
|
2019-10-09 19:28:05 +00:00
|
|
|
.image_color(if is_selected {
|
|
|
|
color::WHITE
|
|
|
|
} else {
|
|
|
|
color::DARK_YELLOW
|
|
|
|
});
|
2019-09-26 13:41:04 +00:00
|
|
|
|
2019-09-27 04:23:42 +00:00
|
|
|
let slot_widget_clicked = if let Some(item) = item {
|
2019-09-26 13:41:04 +00:00
|
|
|
slot_widget
|
|
|
|
.with_tooltip(
|
|
|
|
self.tooltip_manager,
|
2019-10-22 18:18:40 +00:00
|
|
|
&item.name(),
|
2020-02-01 20:39:39 +00:00
|
|
|
&format!(
|
|
|
|
"{}",
|
|
|
|
/* item.kind, item.effect(), */ item.description()
|
|
|
|
),
|
2019-09-26 13:41:04 +00:00
|
|
|
&item_tooltip,
|
2019-09-26 15:07:09 +00:00
|
|
|
)
|
2019-09-26 13:41:04 +00:00
|
|
|
.set(state.ids.inv_slots[i], ui)
|
|
|
|
} else {
|
|
|
|
slot_widget.set(state.ids.inv_slots[i], ui)
|
2019-09-27 04:23:42 +00:00
|
|
|
}
|
|
|
|
.was_clicked();
|
2019-09-26 00:25:14 +00:00
|
|
|
|
|
|
|
// Item
|
2019-09-27 04:23:42 +00:00
|
|
|
if slot_widget_clicked {
|
2019-07-26 17:08:40 +00:00
|
|
|
let selected_slot = match state.selected_slot {
|
|
|
|
Some(a) => {
|
|
|
|
if a == i {
|
2019-08-30 20:46:45 +00:00
|
|
|
event = Some(Event::HudEvent(HudEvent::UseInventorySlot(i)));
|
2019-07-26 17:08:40 +00:00
|
|
|
} else {
|
|
|
|
event = Some(Event::HudEvent(HudEvent::SwapInventorySlots(a, i)));
|
|
|
|
}
|
|
|
|
None
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-07-26 17:08:40 +00:00
|
|
|
None if item.is_some() => Some(i),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
state.update(|s| s.selected_slot = selected_slot);
|
2019-07-25 22:52:28 +00:00
|
|
|
}
|
2019-09-26 13:41:04 +00:00
|
|
|
// Item
|
2019-10-22 18:18:40 +00:00
|
|
|
if let Some(kind) = item.as_ref().map(|i| ItemKey::from(i)) {
|
2019-10-09 19:28:05 +00:00
|
|
|
Button::image(match &state.img_id_cache[i] {
|
|
|
|
Some((cached_kind, id)) if cached_kind == &kind => *id,
|
|
|
|
_ => {
|
|
|
|
let id = self
|
|
|
|
.item_imgs
|
|
|
|
.img_id(kind.clone())
|
|
|
|
.unwrap_or(self.imgs.not_found);
|
|
|
|
state.update(|s| s.img_id_cache[i] = Some((kind, id)));
|
|
|
|
id
|
|
|
|
}
|
|
|
|
})
|
2020-01-24 15:10:38 +00:00
|
|
|
.wh(if is_selected { [32.0; 2] } else { [30.0; 2] })
|
2019-10-09 19:28:05 +00:00
|
|
|
.middle_of(state.ids.inv_slots[i]) // TODO: Items need to be assigned to a certain slot and then placed like in this example
|
|
|
|
//.label("5x") // TODO: Quantity goes here...
|
|
|
|
//.label_font_id(self.fonts.opensans)
|
|
|
|
//.label_font_size(12)
|
|
|
|
//.label_x(Relative::Scalar(10.0))
|
|
|
|
//.label_y(Relative::Scalar(-10.0))
|
|
|
|
//.label_color(TEXT_COLOR)
|
|
|
|
//.parent(state.ids.inv_slots[i])
|
|
|
|
.graphics_for(state.ids.inv_slots[i])
|
|
|
|
.set(state.ids.items[i], ui);
|
2019-07-25 22:52:28 +00:00
|
|
|
}
|
2019-06-29 01:47:38 +00:00
|
|
|
}
|
2019-07-25 22:52:28 +00:00
|
|
|
|
2019-10-21 23:40:58 +00:00
|
|
|
// Drop selected item
|
|
|
|
if let Some(to_drop) = state.selected_slot {
|
|
|
|
if ui.widget_input(ui.window).clicks().left().next().is_some() {
|
|
|
|
event = Some(Event::HudEvent(HudEvent::DropInventorySlot(to_drop)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:52:28 +00:00
|
|
|
// Close button
|
2019-05-04 10:43:37 +00:00
|
|
|
if Button::image(self.imgs.close_button)
|
2019-05-06 18:49:12 +00:00
|
|
|
.w_h(28.0, 28.0)
|
2019-05-04 10:43:37 +00:00
|
|
|
.hover_image(self.imgs.close_button_hover)
|
|
|
|
.press_image(self.imgs.close_button_press)
|
2019-06-30 14:04:10 +00:00
|
|
|
.top_right_with_margins_on(state.ids.bag_top, 0.0, 0.0)
|
2019-05-04 10:43:37 +00:00
|
|
|
.set(state.ids.bag_close, ui)
|
|
|
|
.was_clicked()
|
|
|
|
{
|
2019-07-25 22:52:28 +00:00
|
|
|
event = Some(Event::Close);
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
2019-07-25 22:52:28 +00:00
|
|
|
|
|
|
|
event
|
2019-05-04 10:43:37 +00:00
|
|
|
}
|
|
|
|
}
|