remove variable aliasing in test_hud

Former-commit-id: d84d517197643650e8f6dce18e27e67ffd06163d
This commit is contained in:
Imberflur
2019-02-26 13:19:00 -05:00
committed by Imbris
parent 1cce7e1096
commit a20517684a

View File

@ -111,99 +111,90 @@ impl TestHud {
} }
fn update_layout(&mut self) { fn update_layout(&mut self) {
// This is very useful
let TestHud {
ref mut ui,
ref imgs,
ref ids,
ref mut bag_open,
ref mut menu_open,
..
} = *self;
// TODO: write some custom widgets to make this stuff simpler let ref mut ui_widgets = self.ui.set_widgets();
// Check if the bag was clicked // Check if the bag was clicked
// (can't use .was_clicked() because we are changing the image and this is after setting the widget which causes flickering as it takes a frame to change after the mouse button is lifted) // (can't use .was_clicked() because we are changing the image and this is after setting the widget which causes flickering as it takes a frame to change after the mouse button is lifted)
if ui.widget_input(ids.bag).clicks().left().count() % 2 == 1 { if ui_widgets.widget_input(self.ids.bag).clicks().left().count() % 2 == 1 {
*bag_open = !*bag_open; self.bag_open = !self.bag_open;
} }
let ref mut ui_cell = ui.set_widgets();
// Bag contents // Bag contents
if *bag_open { // Note that display_contents is set before checking if the bag was clicked
// this ensures that the contents and open bag img are displayed on the same frame
if self.bag_open {
// Contents // Contents
Image::new(imgs.bag_contents) Image::new(self.imgs.bag_contents)
.w_h(1504.0/4.0, 1760.0/4.0) .w_h(1504.0/4.0, 1760.0/4.0)
.bottom_right_with_margins(88.0, 68.0) .bottom_right_with_margins(88.0, 68.0)
.set(ids.bag_contents, ui_cell); .set(self.ids.bag_contents, ui_widgets);
// Close button
if Button::image(imgs.close_button) // X-Button
if Button::image(self.imgs.close_button)
.w_h(144.0/4.0, 144.0/4.0) .w_h(144.0/4.0, 144.0/4.0)
.hover_image(imgs.close_button_hover) .hover_image(self.imgs.close_button_hover)
.press_image(imgs.close_button_press) .press_image(self.imgs.close_button_press)
.top_right_with_margins_on(ids.bag_contents, 0.0, 12.0) .top_right_with_margins_on(self.ids.bag_contents, 0.0, 12.0)
.set(ids.bag_close, ui_cell) .set(self.ids.bag_close, ui_widgets)
.was_clicked() { .was_clicked() {
*bag_open = false; self.bag_open = false;
} }
} }
// Bag //Bag
let (bag_img, bag_hover_img, bag_press_img) = Button::image(if self.bag_open {self.imgs.bag_open} else {self.imgs.bag})
if *bag_open { .bottom_right_with_margin_on(ui_widgets.window, 20.0)
(imgs.bag_open, imgs.bag_open_hover, imgs.bag_open_press) .hover_image(if self.bag_open {self.imgs.bag_open_hover} else {self.imgs.bag_hover})
} else { .press_image(if self.bag_open {self.imgs.bag_open_press} else {self.imgs.bag_press})
(imgs.bag, imgs.bag_hover, imgs.bag_press) .w_h(420.0/4.0, 480.0/4.0)
}; .set(self.ids.bag, ui_widgets);
Button::image(bag_img)
.wh([416.0/4.0, 480.0/4.0])
.bottom_right_with_margin_on(ui_cell.window, 20.0)
.hover_image(bag_hover_img)
.press_image(bag_press_img)
.set(ids.bag, ui_cell);
// Attempt to make resizable image based container for buttons // Attempt to make resizable image based container for buttons
// Maybe this could be made into a Widget type if it is useful // Maybe this could be made into a Widget type if it is useful
if *menu_open { if self.menu_open {
let num = ids.menu_buttons.len(); let num = self.ids.menu_buttons.len();
// Canvas to hold everything together // Canvas to hold everything together
Canvas::new() Canvas::new()
.w_h(106.0, 54.0 + num as f64 * 30.0) .w_h(106.0, 54.0 + num as f64 * 30.0)
.middle_of(ui_cell.window) .middle_of(ui_widgets.window)
.set(ids.menu_canvas, ui_cell); .set(self.ids.menu_canvas, ui_widgets);
// Top of menu // Top of menu
Image::new(imgs.menu_top) Image::new(self.imgs.menu_top)
.w_h(106.0, 28.0) .w_h(106.0, 28.0)
.mid_top_of(ids.menu_canvas) .mid_top_of(self.ids.menu_canvas)
// Does not work because of bug in conrod, but above line is equivalent // Does not work because of bug in conrod, but above line is equivalent
//.parent(ids.menu_canvas) //.parent(ids.menu_canvas)
//.mid_top() //.mid_top()
.set(ids.menu_top, ui_cell); .set(self.ids.menu_top, ui_widgets);
// Bottom of Menu // Bottom of Menu
// Note: conrod defaults to the last used parent // Note: conrod defaults to the last used parent
Image::new(imgs.menu_bot) Image::new(self.imgs.menu_bot)
.w_h(106.0, 26.0) .w_h(106.0, 26.0)
.mid_bottom() .mid_bottom()
.set(ids.menu_bot, ui_cell); .set(self.ids.menu_bot, ui_widgets);
// Midsection background // Midsection background
Image::new(imgs.menu_mid) Image::new(self.imgs.menu_mid)
.w_h(106.0, num as f64 * 30.0) .w_h(106.0, num as f64 * 30.0)
.mid_bottom_with_margin(26.0) .mid_bottom_with_margin(26.0)
.set(ids.menu_mid, ui_cell); .set(self.ids.menu_mid, ui_widgets);
// Menu buttons // Menu buttons
if num > 0 { if num > 0 {
Button::image(imgs.menu_button) Button::image(self.imgs.menu_button)
.mid_top_with_margin_on(ids.menu_mid, 8.0) .mid_top_with_margin_on(self.ids.menu_mid, 8.0)
.w_h(48.0, 20.0) .w_h(48.0, 20.0)
.label(&format!("Button {}", 1)) .label(&format!("Button {}", 1))
.label_rgb(1.0, 0.4, 1.0) .label_rgb(1.0, 0.4, 1.0)
.label_font_size(7) .label_font_size(7)
.set(ids.menu_buttons[0], ui_cell); .set(self.ids.menu_buttons[0], ui_widgets);
} }
for i in 1..num { for i in 1..num {
Button::image(imgs.menu_button) Button::image(self.imgs.menu_button)
.down(10.0) .down(10.0)
.label(&format!("Button {}", i + 1)) .label(&format!("Button {}", i + 1))
.label_rgb(1.0, 0.4, 1.0) .label_rgb(1.0, 0.4, 1.0)
.label_font_size(7) .label_font_size(7)
.set(ids.menu_buttons[i], ui_cell); .set(self.ids.menu_buttons[i], ui_widgets);
} }
} }
} }
@ -224,4 +215,4 @@ impl TestHud {
pub fn render(&self, renderer: &mut Renderer) { pub fn render(&self, renderer: &mut Renderer) {
self.ui.render(renderer); self.ui.render(renderer);
} }
} }