Moved corner radius property to be separate from the rect struct

This commit is contained in:
Adalyn Black
2024-07-23 22:28:40 -07:00
committed by galister
parent f27c320231
commit 32e0e7656a
7 changed files with 198 additions and 114 deletions

View File

@@ -39,10 +39,11 @@ impl<D, S> CanvasBuilder<D, S> {
}
// Creates a panel with bg_color inherited from the canvas
pub fn panel(&mut self, x: f32, y: f32, w: f32, h: f32, r: f32) -> &mut Control<D, S> {
pub fn panel(&mut self, x: f32, y: f32, w: f32, h: f32, radius: f32) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r },
rect: Rect { x, y, w, h },
corner_radius: radius,
bg_color: self.bg_color,
on_render_bg: Some(Control::render_rounded_rect),
..Control::new()
@@ -51,10 +52,11 @@ impl<D, S> CanvasBuilder<D, S> {
}
// Creates a label with fg_color, font_size inherited from the canvas
pub fn label(&mut self, x: f32, y: f32, w: f32, h: f32, r: f32, text: Arc<str>) -> &mut Control<D, S> {
pub fn label(&mut self, x: f32, y: f32, w: f32, h: f32, radius: f32, text: Arc<str>) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r },
rect: Rect { x, y, w, h },
corner_radius: radius,
text,
fg_color: self.fg_color,
size: self.font_size,
@@ -72,12 +74,13 @@ impl<D, S> CanvasBuilder<D, S> {
y: f32,
w: f32,
h: f32,
r: f32,
radius: f32,
text: Arc<str>,
) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r },
rect: Rect { x, y, w, h },
corner_radius: radius,
text,
fg_color: self.fg_color,
size: self.font_size,
@@ -91,7 +94,8 @@ impl<D, S> CanvasBuilder<D, S> {
pub fn sprite(&mut self, x: f32, y: f32, w: f32, h: f32) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r: 0. },
rect: Rect { x, y, w, h },
corner_radius: 0.,
on_render_bg: Some(Control::render_sprite_bg),
..Control::new()
});
@@ -102,7 +106,8 @@ impl<D, S> CanvasBuilder<D, S> {
pub fn sprite_interactive(&mut self, x: f32, y: f32, w: f32, h: f32) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r: 0. },
rect: Rect { x, y, w, h },
corner_radius: 0.,
on_render_bg: Some(Control::render_sprite_bg),
on_render_hl: Some(Control::render_sprite_hl),
..Control::new()
@@ -111,12 +116,13 @@ impl<D, S> CanvasBuilder<D, S> {
}
// Creates a button with fg_color, bg_color, font_size inherited from the canvas
pub fn button(&mut self, x: f32, y: f32, w: f32, h: f32, r: f32, text: Arc<str>) -> &mut Control<D, S> {
pub fn button(&mut self, x: f32, y: f32, w: f32, h: f32, radius: f32, text: Arc<str>) -> &mut Control<D, S> {
let idx = self.canvas.controls.len();
self.canvas.interactive_set_idx(x, y, w, h, idx);
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r },
rect: Rect { x, y, w, h },
corner_radius: radius,
text,
fg_color: self.fg_color,
bg_color: self.bg_color,
@@ -136,7 +142,7 @@ impl<D, S> CanvasBuilder<D, S> {
y: f32,
w: f32,
h: f32,
r: f32,
radius: f32,
cap_type: KeyCapType,
label: &[String],
) -> &mut Control<D, S> {
@@ -144,7 +150,8 @@ impl<D, S> CanvasBuilder<D, S> {
self.canvas.interactive_set_idx(x, y, w, h, idx);
self.canvas.controls.push(Control {
rect: Rect { x, y, w, h, r },
rect: Rect { x, y, w, h },
corner_radius: radius,
bg_color: self.bg_color,
on_render_bg: Some(Control::render_rounded_rect),
on_render_hl: Some(Control::render_highlight),
@@ -159,7 +166,6 @@ impl<D, S> CanvasBuilder<D, S> {
y,
w,
h: h - self.font_size as f32,
r: 0.,
};
vec![(render, rect, 1f32)]
}
@@ -170,14 +176,12 @@ impl<D, S> CanvasBuilder<D, S> {
y: y + (self.font_size as f32) + 12.,
w,
h,
r: 0.,
};
let rect1 = Rect {
x: x + w * 0.5 + 12.,
y: y + h - (self.font_size as f32) + 8.,
w,
h,
r: 0.,
};
vec![(render, rect0, 1.0), (render, rect1, 0.8)]
}
@@ -188,14 +192,12 @@ impl<D, S> CanvasBuilder<D, S> {
y: y + 2.0,
w,
h: h * 0.5,
r: 0.,
};
let rect1 = Rect {
x,
y: y + h * 0.5 + 2.0,
w,
h: h * 0.5,
r: 0.,
};
vec![(render, rect1, 1.0), (render, rect0, 0.8)]
}
@@ -206,21 +208,18 @@ impl<D, S> CanvasBuilder<D, S> {
y: y + (self.font_size as f32) + 8.,
w,
h,
r: 0.,
};
let rect1 = Rect {
x: x + 12.,
y: y + h - (self.font_size as f32) + 4.,
w,
h,
r: 0.,
};
let rect2 = Rect {
x: x + w * 0.5 + 8.,
y: y + h - (self.font_size as f32) + 4.,
w,
h,
r: 0.,
};
vec![
(render, rect1, 1.0),

View File

@@ -25,6 +25,7 @@ pub type ControlRendererHl<D, S> = fn(
pub(crate) struct Control<D, S> {
pub state: Option<S>,
pub rect: Rect,
pub corner_radius: f32,
pub fg_color: GuiColor,
pub bg_color: GuiColor,
pub text: Arc<str>,
@@ -52,8 +53,8 @@ impl<D, S> Control<D, S> {
y: 0.,
w: 0.,
h: 0.,
r: 0.,
},
corner_radius: 0.,
fg_color: Vec4::ONE,
bg_color: Vec4::ZERO,
text: Arc::from(""),
@@ -113,7 +114,7 @@ impl<D, S> Control<D, S> {
cmd_buffer: &mut WlxCommandBuffer,
) -> anyhow::Result<()> {
let pass = {
let r = self.rect.r.min(self.rect.w / 2.0).min(self.rect.h / 2.0);
let r = self.corner_radius.min(self.rect.w / 2.0).min(self.rect.h / 2.0);
let rw = r / canvas.width as f32;
let ruw = r / self.rect.w as f32;
let rh = r / canvas.height as f32;

View File

@@ -26,7 +26,6 @@ pub struct Rect {
y: f32,
w: f32,
h: f32,
r: f32,
}
pub struct CanvasData<D> {

View File

@@ -53,18 +53,21 @@ pub struct OverlayListTemplate {
#[serde(tag = "type")]
pub enum ModularElement {
Panel {
rect: [f32; 5],
rect: [f32; 4],
corner_radius: f32,
bg_color: Arc<str>,
},
Label {
rect: [f32; 5],
rect: [f32; 4],
corner_radius: f32,
font_size: isize,
fg_color: Arc<str>,
#[serde(flatten)]
data: LabelContent,
},
CenteredLabel {
rect: [f32; 5],
rect: [f32; 4],
corner_radius: f32,
font_size: isize,
fg_color: Arc<str>,
#[serde(flatten)]
@@ -76,7 +79,8 @@ pub enum ModularElement {
sprite_st: Option<[f32; 4]>,
},
Button {
rect: [f32; 5],
rect: [f32; 4],
corner_radius: f32,
font_size: isize,
fg_color: Arc<str>,
bg_color: Arc<str>,
@@ -86,7 +90,8 @@ pub enum ModularElement {
},
/// Convenience type to save you from having to create a bunch of labels
BatteryList {
rect: [f32; 5],
rect: [f32; 4],
corner_radius: f32,
font_size: isize,
fg_color: Arc<str>,
fg_color_low: Arc<str>,
@@ -96,7 +101,8 @@ pub enum ModularElement {
layout: ListLayout,
},
OverlayList {
rect: [f32; 5],
rect: [f32; 4],
corner_radius: f32,
font_size: isize,
fg_color: Arc<str>,
bg_color: Arc<str>,
@@ -139,32 +145,35 @@ pub fn modular_canvas(
for elem in elements.iter() {
match elem {
ModularElement::Panel {
rect: [x, y, w, h, r],
rect: [x, y, w, h],
corner_radius,
bg_color,
} => {
canvas.bg_color = color_parse(bg_color).unwrap_or(*FALLBACK_COLOR);
canvas.panel(*x, *y, *w, *h, *r);
canvas.panel(*x, *y, *w, *h, *corner_radius);
}
ModularElement::Label {
rect: [x, y, w, h, r],
rect: [x, y, w, h],
corner_radius,
font_size,
fg_color,
data,
} => {
canvas.font_size = *font_size;
canvas.fg_color = color_parse(fg_color).unwrap_or(*FALLBACK_COLOR);
let label = canvas.label(*x, *y, *w, *h, *r, empty_str.clone());
let label = canvas.label(*x, *y, *w, *h, *corner_radius, empty_str.clone());
modular_label_init(label, data);
}
ModularElement::CenteredLabel {
rect: [x, y, w, h, r],
rect: [x, y, w, h],
corner_radius,
font_size,
fg_color,
data,
} => {
canvas.font_size = *font_size;
canvas.fg_color = color_parse(fg_color).unwrap_or(*FALLBACK_COLOR);
let label = canvas.label_centered(*x, *y, *w, *h, *r, empty_str.clone());
let label = canvas.label_centered(*x, *y, *w, *h, *corner_radius, empty_str.clone());
modular_label_init(label, data);
}
ModularElement::Sprite {
@@ -187,7 +196,8 @@ pub fn modular_canvas(
}
},
ModularElement::Button {
rect: [x, y, w, h, r],
rect: [x, y, w, h],
corner_radius,
font_size,
bg_color,
fg_color,
@@ -197,11 +207,12 @@ pub fn modular_canvas(
canvas.bg_color = color_parse(bg_color).unwrap_or(*FALLBACK_COLOR);
canvas.fg_color = color_parse(fg_color).unwrap_or(*FALLBACK_COLOR);
canvas.font_size = *font_size;
let button = canvas.button(*x, *y, *w, *h, *r, text.clone());
let button = canvas.button(*x, *y, *w, *h, *corner_radius, text.clone());
modular_button_init(button, data);
}
ModularElement::BatteryList {
rect: [x, y, w, h, r],
rect: [x, y, w, h],
corner_radius,
font_size,
fg_color,
fg_color_low,
@@ -229,7 +240,7 @@ pub fn modular_canvas(
button_y + 2.,
button_w - 4.,
button_h - 4.,
*r,
*corner_radius,
empty_str.clone(),
);
modular_label_init(
@@ -253,7 +264,8 @@ pub fn modular_canvas(
}
}
ModularElement::OverlayList {
rect: [x, y, w, h, r],
rect: [x, y, w, h],
corner_radius,
font_size,
fg_color,
bg_color,
@@ -278,7 +290,7 @@ pub fn modular_canvas(
button_y + 2.,
button_w - 4.,
button_h - 4.,
*r,
*corner_radius,
screen.name.clone(),
);