Dialog uses new coords

This commit is contained in:
mars 2022-11-06 14:59:38 -07:00
parent 69318a02e5
commit e39d16a516
1 changed files with 74 additions and 68 deletions

View File

@ -31,7 +31,6 @@ impl DialogResponse {
#[derive(Clone)]
pub struct DialogStyle {
pub width: f32,
pub rounding: f32,
pub header: DialogHeaderStyle,
pub body: DialogBodyStyle,
@ -41,8 +40,7 @@ pub struct DialogStyle {
impl Default for DialogStyle {
fn default() -> Self {
Self {
width: 1.2,
rounding: 0.02,
rounding: 5.0,
header: Default::default(),
body: Default::default(),
footer: Default::default(),
@ -64,7 +62,7 @@ impl Default for DialogHeaderStyle {
fn default() -> Self {
Self {
color: Color::WHITE,
height: 0.3,
height: 20.0,
text_font: Font::new(crate::DISPLAY_FONT),
text_color: Color::BLACK,
text_scale_factor: 0.65,
@ -76,20 +74,18 @@ impl Default for DialogHeaderStyle {
#[derive(Clone)]
pub struct DialogBodyStyle {
pub color: Color,
pub height: f32,
pub text_font: Font,
pub text_color: Color,
pub text_scale_factor: f32,
pub text_size: f32,
}
impl Default for DialogBodyStyle {
fn default() -> Self {
Self {
color: Color::WHITE.with_alpha(0xb0),
height: 0.6,
text_font: Font::new(crate::CONTENT_FONT),
text_color: Color::BLACK,
text_scale_factor: 0.15,
text_size: 5.0,
}
}
}
@ -106,9 +102,9 @@ impl Default for DialogFooterStyle {
fn default() -> Self {
Self {
icon_font: Font::new(crate::ICON_FONT),
button_radius: 0.1,
button_radius: 7.5,
color: Color::WHITE,
height: 0.25,
height: 20.0,
}
}
}
@ -122,53 +118,16 @@ pub struct DialogInfo {
pub struct Dialog {
style: DialogStyle,
title: Label,
content: Label,
title: Offset<Label>,
content: Offset<Label>,
content_size: Vec2,
buttons: Vec<DialogButton>,
}
impl Dialog {
pub fn new(style: DialogStyle, info: &DialogInfo) -> Self {
let width2 = style.width / 2.0;
let button_y = -(style.body.height + style.footer.height) / 2.0;
let button_spacing = style.width / info.responses.len() as f32;
let button_spacing2 = button_spacing / 2.0;
let title_scale = style.header.height * style.header.text_scale_factor;
let title_baseline =
style.header.height * style.header.text_baseline + style.body.height / 2.0;
let title = Label::new(
LabelText {
font: style.header.text_font,
text: info.title.to_string(),
},
HorizontalAlignment::Center,
title_scale,
style.header.text_color,
-width2,
width2,
title_baseline,
);
let content_scale = style.body.height * style.body.text_scale_factor;
let content = Label::new(
LabelText {
font: style.body.text_font,
text: info.content.to_string(),
},
HorizontalAlignment::Center,
content_scale,
style.body.text_color,
-width2,
width2,
0.0,
);
let mut buttons = Vec::new();
for (index, response) in info.responses.iter().enumerate() {
let button_x = button_spacing * index as f32 + button_spacing2 - style.width / 2.0;
for response in info.responses.iter() {
let color = response.get_color();
let radius = style.footer.button_radius;
let button_style = RoundButtonStyle {
@ -186,7 +145,7 @@ impl Dialog {
};
let button = RoundButton::new(button_style, Some(text));
let button = Offset::new(button, Vec2::new(button_x, button_y));
let button = Offset::new(button, Vec2::ZERO);
buttons.push(DialogButton {
response: *response,
@ -194,11 +153,67 @@ impl Dialog {
});
}
Self {
let title_scale = style.header.height * style.header.text_scale_factor;
let title = Label::new(
LabelText {
font: style.header.text_font,
text: info.title.to_string(),
},
HorizontalAlignment::Center,
title_scale,
style.header.text_color,
0.0,
0.0,
0.0,
);
let content = Label::new(
LabelText {
font: style.body.text_font,
text: info.content.to_string(),
},
HorizontalAlignment::Center,
style.body.text_size,
style.body.text_color,
0.0,
0.0,
0.0,
);
let mut dialog = Self {
style,
title,
content,
title: Offset::new(title, Vec2::ZERO),
content: Offset::new(content, Vec2::ZERO),
content_size: Vec2::ONE,
buttons,
};
dialog.resize(Vec2::splat(100.0));
dialog
}
pub fn resize(&mut self, size: Vec2) {
let style = &self.style;
let width = size.x;
let width2 = width / 2.0;
let body_height = size.y - style.header.height - style.footer.height;
let body_height = body_height.max(0.0);
let title_baseline = style.header.height * (1.0 - style.header.text_baseline);
let content_baseline = style.header.height + body_height / 2.0;
let button_baseline = style.header.height + body_height + style.footer.height / 2.0;
let button_spacing = width / self.buttons.len() as f32;
let button_spacing2 = button_spacing / 2.0;
self.content_size = Vec2::new(width, body_height);
self.title.set_offset(Vec2::new(width2, title_baseline));
self.content.set_offset(Vec2::new(width2, content_baseline));
for (index, button) in self.buttons.iter_mut().enumerate() {
let button_x = button_spacing * index as f32 + button_spacing2;
button
.button
.set_offset(Vec2::new(button_x, button_baseline));
}
}
}
@ -215,24 +230,15 @@ impl Container for Dialog {
fn draw(&mut self, ctx: &DrawContext) {
let style = &self.style;
let width = style.width;
let width2 = width / 2.0;
let width = self.content_size.x;
let rounding = style.rounding;
let header_xy = Vec2::new(-width2, style.body.height / 2.0);
let header_size = Vec2::new(width, style.header.height);
let header_rect = Rect::from_xy_size(header_xy, header_size);
let header_rect = Rect::from_xy_size(Vec2::ZERO, header_size);
let header_corners = CornerFlags::TOP;
let body_tr = Vec2::new(width2, style.body.height / 2.0);
let body_rect = Rect {
tl: -body_tr,
br: body_tr,
};
let footer_xy = Vec2::new(-width2, -style.body.height / 2.0 - style.footer.height);
let body_rect = Rect::from_xy_size(header_rect.bl(), self.content_size);
let footer_size = Vec2::new(width, style.footer.height);
let footer_rect = Rect::from_xy_size(footer_xy, footer_size);
let footer_rect = Rect::from_xy_size(body_rect.bl(), footer_size);
let footer_corners = CornerFlags::BOTTOM;
ctx.draw_rect(body_rect, style.body.color);