veloren/voxygen/src/hud/bag.rs

201 lines
6.1 KiB
Rust
Raw Normal View History

2019-07-25 22:52:28 +00:00
use super::{img_ids::Imgs, Event as HudEvent, Fonts, TEXT_COLOR};
use client::Client;
use conrod_core::{
color,
2019-06-29 01:47:38 +00:00
position::Relative,
2019-07-07 15:31:34 +00:00
widget::{self, Button, Image, Rectangle /*, Scrollbar*/},
2019-07-07 14:06:27 +00:00
widget_ids, /*Color, Colorable,*/ Labelable, Positionable, Sizeable, Widget, WidgetCommon,
};
widget_ids! {
struct Ids {
bag_close,
bag_top,
bag_mid,
bag_bot,
inv_alignment,
inv_grid_1,
inv_grid_2,
inv_scrollbar,
2019-07-25 22:52:28 +00:00
inv_slots_0,
map_title,
2019-07-25 22:52:28 +00:00
inv_slots[],
items[],
}
}
#[derive(WidgetCommon)]
pub struct Bag<'a> {
client: &'a Client,
imgs: &'a Imgs,
2019-07-04 20:55:23 +00:00
fonts: &'a Fonts,
#[conrod(common_builder)]
common: widget::CommonBuilder,
}
impl<'a> Bag<'a> {
pub fn new(client: &'a Client, imgs: &'a Imgs, fonts: &'a Fonts) -> Self {
Self {
client,
imgs,
2019-07-04 20:55:23 +00:00
fonts,
common: widget::CommonBuilder::default(),
}
}
}
pub struct State {
ids: Ids,
2019-07-26 17:08:40 +00:00
selected_slot: Option<usize>,
}
const BAG_SCALE: f64 = 4.0;
pub enum Event {
2019-07-25 22:52:28 +00:00
HudEvent(HudEvent),
Close,
}
impl<'a> Widget for Bag<'a> {
type State = State;
type Style = ();
type Event = Option<Event>;
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
State {
ids: Ids::new(id_gen),
2019-07-26 17:08:40 +00:00
selected_slot: None,
}
}
fn style(&self) -> Self::Style {
()
}
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { state, ui, .. } = args;
2019-07-25 22:52:28 +00:00
let mut event = None;
let invs = self.client.inventories();
let inventory = match invs.get(self.client.entity()) {
Some(inv) => inv,
None => return None,
};
// Bag parts
Image::new(self.imgs.bag_bot)
.w_h(61.0 * BAG_SCALE, 9.0 * BAG_SCALE)
.bottom_right_with_margins_on(ui.window, 60.0, 5.0)
.set(state.ids.bag_bot, ui);
Image::new(self.imgs.bag_top)
.w_h(61.0 * BAG_SCALE, 9.0 * BAG_SCALE)
.up_from(state.ids.bag_mid, 0.0)
2019-07-07 13:44:34 +00:00
.set(state.ids.bag_top, ui);
Image::new(self.imgs.bag_mid)
.w_h(61.0 * BAG_SCALE, ((inventory.len() + 4) / 5) as f64 * 44.0)
.up_from(state.ids.bag_bot, 0.0)
.set(state.ids.bag_mid, ui);
// Alignment for Grid
2019-07-07 13:44:34 +00:00
Rectangle::fill_with(
2019-07-25 22:52:28 +00:00
[54.0 * BAG_SCALE, ((inventory.len() + 4) / 5) as f64 * 44.0],
2019-07-07 13:44:34 +00:00
color::TRANSPARENT,
)
.top_left_with_margins_on(state.ids.bag_top, 9.0 * BAG_SCALE, 3.0 * BAG_SCALE)
.scroll_kids()
.scroll_kids_vertically()
.set(state.ids.inv_alignment, ui);
2019-06-29 13:03:48 +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-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
if Button::image(self.imgs.inv_slot)
2019-06-29 13:03:48 +00:00
.top_left_with_margins_on(
state.ids.inv_alignment,
2019-06-29 13:03:48 +00:00
4.0 + y as f64 * (40.0 + 4.0),
4.0 + x as f64 * (40.0 + 4.0),
) // conrod uses a (y,x) format for placing...
2019-07-26 13:42:36 +00:00
.parent(state.ids.inv_alignment) // Avoids the background overlapping available slots
2019-06-29 13:03:48 +00:00
.w_h(40.0, 40.0)
2019-07-26 17:08:40 +00:00
.image_color(if is_selected {
color::WHITE
} else {
color::DARK_YELLOW
})
2019-07-26 13:42:36 +00:00
.floating(true)
2019-07-25 22:52:28 +00:00
.set(state.ids.inv_slots[i], ui)
.was_clicked()
{
2019-07-26 17:08:40 +00:00
let selected_slot = match state.selected_slot {
Some(a) => {
if a == i {
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
}
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
}
// Item
if item.is_some() {
Button::image(self.imgs.potion_red) // TODO: Insert variable image depending on the item displayed in that slot
.w_h(4.0 * 4.4, 7.0 * 4.4) // TODO: Fix height and scale width correctly to that to avoid a stretched item image
.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)
2019-07-26 13:42:36 +00:00
.parent(state.ids.inv_slots[i])
.graphics_for(state.ids.inv_slots[i])
2019-07-25 22:52:28 +00:00
.set(state.ids.items[i], ui);
}
2019-06-29 01:47:38 +00:00
}
2019-07-25 22:52:28 +00:00
// Close button
if Button::image(self.imgs.close_button)
.w_h(28.0, 28.0)
.hover_image(self.imgs.close_button_hover)
.press_image(self.imgs.close_button_press)
.top_right_with_margins_on(state.ids.bag_top, 0.0, 0.0)
.set(state.ids.bag_close, ui)
.was_clicked()
{
2019-07-25 22:52:28 +00:00
event = Some(Event::Close);
}
2019-07-25 22:52:28 +00:00
event
}
}