Add initial Inventory menu
This commit is contained in:
parent
7fdf6c6c83
commit
9b711af557
101
src/widgets.rs
101
src/widgets.rs
|
@ -1,5 +1,5 @@
|
|||
use crate::anim::Animation;
|
||||
use crate::draw::DrawContext;
|
||||
use crate::draw::{Corner, DrawContext};
|
||||
use crate::{Color, CursorEventKind, Vec2};
|
||||
use keyframe::functions::*;
|
||||
|
||||
|
@ -223,18 +223,6 @@ impl<T: Widget + Button> Widget for ScrollMenu<T> {
|
|||
return;
|
||||
}
|
||||
|
||||
ctx.draw_triangle(
|
||||
Vec2::new(self.spacing, 0.0),
|
||||
Vec2::new(self.spacing + 0.05, 0.05),
|
||||
Vec2::new(self.spacing + 0.05, -0.05),
|
||||
Color {
|
||||
r: 1.0,
|
||||
g: 1.0,
|
||||
b: 1.0,
|
||||
a: 1.0,
|
||||
},
|
||||
);
|
||||
|
||||
self.for_buttons(|button, _i, y| {
|
||||
let ctx = ctx.with_offset(Vec2::new(0.0, y));
|
||||
|
||||
|
@ -271,6 +259,7 @@ impl<T: Widget + Button> Widget for ScrollMenu<T> {
|
|||
|
||||
pub struct MainMenu {
|
||||
pub menu: ScrollMenu<RoundButton>,
|
||||
pub inventory: Inventory,
|
||||
}
|
||||
|
||||
impl Default for MainMenu {
|
||||
|
@ -283,6 +272,7 @@ impl Default for MainMenu {
|
|||
|
||||
Self {
|
||||
menu: ScrollMenu::new(buttons, 0.2),
|
||||
inventory: Inventory::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -290,7 +280,7 @@ impl Default for MainMenu {
|
|||
impl Widget for MainMenu {
|
||||
fn update(&mut self, dt: f32) {
|
||||
match self.menu.was_clicked() {
|
||||
None => {},
|
||||
None => {}
|
||||
Some(0) => self.menu.close(),
|
||||
Some(button) => self.menu.scroll_to(button),
|
||||
};
|
||||
|
@ -300,9 +290,92 @@ impl Widget for MainMenu {
|
|||
|
||||
fn draw(&mut self, ctx: &DrawContext) {
|
||||
self.menu.draw(&ctx);
|
||||
|
||||
let inventory_ctx = ctx.with_offset(Vec2::new(0.05, 0.0));
|
||||
self.inventory.draw(&inventory_ctx);
|
||||
}
|
||||
|
||||
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
|
||||
self.menu.on_cursor_event(kind, at);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Inventory;
|
||||
|
||||
impl Inventory {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Inventory {
|
||||
fn update(&mut self, dt: f32) {}
|
||||
|
||||
fn draw(&mut self, ctx: &DrawContext) {
|
||||
// style constants
|
||||
let head_radius = 0.05;
|
||||
let head_height = 0.1;
|
||||
let tab_width = 0.2;
|
||||
let tab_height = 0.1;
|
||||
let tab_num = 6;
|
||||
let separator_width = 0.05;
|
||||
let box_size = 0.05;
|
||||
let box_margin = 0.025;
|
||||
let box_padding = 0.01;
|
||||
let box_grid_width = 5;
|
||||
|
||||
let head_color = Color {
|
||||
r: 1.0,
|
||||
g: 1.0,
|
||||
b: 1.0,
|
||||
a: 1.0,
|
||||
};
|
||||
|
||||
// alignment variables
|
||||
let tab_list_height = tab_num as f32 * tab_height;
|
||||
let separator_xy = Vec2::new(tab_width, separator_width);
|
||||
let separator_size = Vec2::new(separator_width, tab_list_height - separator_width);
|
||||
let body_width = box_grid_width as f32 * (box_size + box_padding) + box_margin;
|
||||
let body_height = tab_list_height - box_margin * 2.0;
|
||||
let head_width = tab_width + separator_width + body_width;
|
||||
let head_inner_xy = Vec2::new(0.0, tab_list_height);
|
||||
let head_inner_size = Vec2::new(head_width, head_height - head_radius);
|
||||
let head_edge_xy = Vec2::new(head_radius, tab_list_height + head_height - head_radius);
|
||||
let head_edge_size = Vec2::new(head_width - head_radius * 2.0, head_radius);
|
||||
let head_tl_xy = head_edge_xy;
|
||||
let head_tr_xy = head_tl_xy + Vec2::new(head_edge_size.x, 0.0);
|
||||
|
||||
// draw shapes
|
||||
ctx.draw_rect(separator_xy, separator_size, head_color);
|
||||
ctx.draw_quarter_circle(
|
||||
Corner::BottomRight,
|
||||
separator_xy,
|
||||
separator_width,
|
||||
head_color,
|
||||
);
|
||||
|
||||
ctx.draw_rect(head_inner_xy, head_inner_size, head_color);
|
||||
ctx.draw_rect(head_edge_xy, head_edge_size, head_color);
|
||||
ctx.draw_quarter_circle(Corner::TopLeft, head_tl_xy, head_radius, head_color);
|
||||
ctx.draw_quarter_circle(Corner::TopRight, head_tr_xy, head_radius, head_color);
|
||||
|
||||
// placeholder inventory item boxes
|
||||
let box_grid_stride = box_size + box_padding;
|
||||
let box_grid_height = (body_height / box_grid_stride).floor() as usize;
|
||||
let box_rect_size = Vec2::new(box_size, box_size);
|
||||
let box_grid_xy = Vec2::new(
|
||||
tab_width + separator_width + box_margin,
|
||||
tab_list_height - box_margin - box_size,
|
||||
);
|
||||
let box_radius = 0.01;
|
||||
|
||||
for x in 0..box_grid_width {
|
||||
for y in 0..box_grid_height {
|
||||
let box_rect_xy = Vec2::new(x as f32, -(y as f32)) * box_grid_stride + box_grid_xy;
|
||||
ctx.draw_rounded_rect(box_rect_xy, box_rect_size, box_radius, head_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue