Extract 'create_effect_button' function from 'update'

This commit is contained in:
Dr. Dystopia
2021-06-11 14:36:48 +02:00
parent 690951648d
commit fc4d17ce2a

View File

@ -9,12 +9,13 @@ use crate::{
GlobalState, GlobalState,
}; };
use crate::hud::BuffInfo;
use common::comp::{BuffKind, Buffs, Energy, Health}; use common::comp::{BuffKind, Buffs, Energy, Health};
use conrod_core::{ use conrod_core::{
color, color,
image::Id, image::Id,
widget::{self, Button, Image, Rectangle, Text}, widget::{self, button::TimesClicked, Button, Image, Rectangle, Text},
widget_ids, Color, Colorable, Positionable, Sizeable, Widget, WidgetCommon, widget_ids, Color, Colorable, Positionable, Sizeable, UiCell, Widget, WidgetCommon,
}; };
widget_ids! { widget_ids! {
struct Ids { struct Ids {
@ -98,7 +99,7 @@ impl<'a> Widget for BuffsBar<'a> {
fn style(&self) -> Self::Style {} fn style(&self) -> Self::Style {}
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event { fn update(mut self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { state, ui, .. } = args; let widget::UpdateArgs { state, ui, .. } = args;
let mut event = Vec::new(); let mut event = Vec::new();
let localized_strings = self.localized_strings; let localized_strings = self.localized_strings;
@ -198,12 +199,7 @@ impl<'a> Widget for BuffsBar<'a> {
.iter() .iter()
.enumerate() .enumerate()
.for_each(|(i, ((id, timer_id), buff))| { .for_each(|(i, ((id, timer_id), buff))| {
let max_duration = buff.data.duration;
let current_duration = buff.dur; let current_duration = buff.dur;
let duration_percentage = current_duration.map_or(1000.0, |cur| {
max_duration
.map_or(1000.0, |max| cur.as_secs_f32() / max.as_secs_f32() * 1000.0)
}) as u32; // Percentage to determine which frame of the timer overlay is displayed
let buff_img = hud::get_buff_image(buff.kind, self.imgs); let buff_img = hud::get_buff_image(buff.kind, self.imgs);
let buff_widget = Image::new(buff_img).w_h(40.0, 40.0); let buff_widget = Image::new(buff_img).w_h(40.0, 40.0);
// Sort buffs into rows of 11 slots // Sort buffs into rows of 11 slots
@ -225,23 +221,22 @@ impl<'a> Widget for BuffsBar<'a> {
) )
.set(*id, ui); .set(*id, ui);
// Create Buff tooltip // Create Buff tooltip
let title = hud::get_buff_title(buff.kind, localized_strings);
let desc_txt = hud::get_buff_desc(buff.kind, buff.data, localized_strings); let desc_txt = hud::get_buff_desc(buff.kind, buff.data, localized_strings);
let remaining_time = hud::get_buff_time(*buff); let remaining_time = hud::get_buff_time(*buff);
let click_to_remove = format!("<{}>", &localized_strings.get("buff.remove")); let click_to_remove = format!("<{}>", &localized_strings.get("buff.remove"));
let desc = format!("{}\n\n{}\n\n{}", desc_txt, remaining_time, click_to_remove);
// Timer overlay // Timer overlay
if Button::image(self.get_duration_image(duration_percentage)) if self
.w_h(40.0, 40.0) .create_effect_button(
.middle_of(*id) buff,
.with_tooltip( current_duration,
self.tooltip_manager, localized_strings,
title, ui,
&desc,
&buffs_tooltip, &buffs_tooltip,
id,
timer_id,
&format!("{}\n\n{}\n\n{}", desc_txt, remaining_time, click_to_remove),
BUFF_COLOR, BUFF_COLOR,
) )
.set(*timer_id, ui)
.was_clicked() .was_clicked()
{ {
event.push(Event::RemoveBuff(buff.kind)); event.push(Event::RemoveBuff(buff.kind));
@ -356,7 +351,6 @@ impl<'a> Widget for BuffsBar<'a> {
.iter() .iter()
.enumerate() .enumerate()
.for_each(|(i, (((id, timer_id), txt_id), buff))| { .for_each(|(i, (((id, timer_id), txt_id), buff))| {
let max_duration = buff.data.duration;
let current_duration = buff.dur; let current_duration = buff.dur;
// Percentage to determine which frame of the timer overlay is displayed // Percentage to determine which frame of the timer overlay is displayed
let buff_img = hud::get_buff_image(buff.kind, self.imgs); let buff_img = hud::get_buff_image(buff.kind, self.imgs);
@ -379,35 +373,31 @@ impl<'a> Widget for BuffsBar<'a> {
) )
.set(*id, ui); .set(*id, ui);
// Create Buff tooltip // Create Buff tooltip
let title = hud::get_buff_title(buff.kind, localized_strings);
let desc_txt = hud::get_buff_desc(buff.kind, buff.data, localized_strings); let desc_txt = hud::get_buff_desc(buff.kind, buff.data, localized_strings);
let remaining_time = hud::get_buff_time(*buff); let remaining_time = hud::get_buff_time(*buff);
let click_to_remove = format!("<{}>", &localized_strings.get("buff.remove")); let click_to_remove = format!("<{}>", &localized_strings.get("buff.remove"));
let desc = if buff.is_buff { let desc;
format!("{}\n\n{}", desc_txt, click_to_remove) let title_col;
if buff.is_buff {
desc = format!("{}\n\n{}", desc_txt, click_to_remove);
title_col = BUFF_COLOR;
} else { } else {
desc_txt.to_string() desc = desc_txt.to_string();
title_col = DEBUFF_COLOR;
}; };
// Timer overlay // Timer overlay
let duration_percentage = current_duration.map_or(1000.0, |cur| { if self
max_duration .create_effect_button(
.map_or(1000.0, |max| cur.as_secs_f32() / max.as_secs_f32() * 1000.0) buff,
}) as u32; current_duration,
if Button::image(self.get_duration_image(duration_percentage)) localized_strings,
.w_h(40.0, 40.0) ui,
.middle_of(*id)
.with_tooltip(
self.tooltip_manager,
title,
&desc,
&buffs_tooltip, &buffs_tooltip,
if buff.is_buff { id,
BUFF_COLOR timer_id,
} else { &desc,
DEBUFF_COLOR title_col,
},
) )
.set(*timer_id, ui)
.was_clicked() .was_clicked()
{ {
event.push(Event::RemoveBuff(buff.kind)); event.push(Event::RemoveBuff(buff.kind));
@ -439,4 +429,35 @@ impl<'a> BuffsBar<'a> {
_ => self.imgs.nothing, _ => self.imgs.nothing,
} }
} }
fn create_effect_button(
&mut self,
buff: &BuffInfo,
current_duration: Option<std::time::Duration>,
localized_strings: &Localization,
ui: &mut UiCell,
buffs_tooltip: &Tooltip,
id: &conrod_core::widget::id::Id,
timer_id: &conrod_core::widget::id::Id,
desc: &String,
title_col: Color,
) -> TimesClicked {
let max_duration = buff.data.duration;
let duration_percentage = current_duration.map_or(1000.0, |cur| {
max_duration.map_or(1000.0, |max| cur.as_secs_f32() / max.as_secs_f32() * 1000.0)
}) as u32; // Percentage to determine which frame of the timer overlay is displayed
let title = hud::get_buff_title(buff.kind, localized_strings);
Button::image(self.get_duration_image(duration_percentage))
.w_h(40.0, 40.0)
.middle_of(*id)
.with_tooltip(
self.tooltip_manager,
title,
&desc,
&buffs_tooltip,
title_col,
)
.set(*timer_id, ui)
}
} }