xml anchor + GuiPanel minor refactor
This commit is contained in:
@@ -17,29 +17,38 @@ use crate::{
|
|||||||
overlay::{FrameMeta, OverlayBackend, OverlayRenderer, ShouldRender},
|
overlay::{FrameMeta, OverlayBackend, OverlayRenderer, ShouldRender},
|
||||||
},
|
},
|
||||||
graphics::{CommandBuffers, ExtentExt},
|
graphics::{CommandBuffers, ExtentExt},
|
||||||
|
gui,
|
||||||
state::AppState,
|
state::AppState,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{asset::GuiAsset, timestep::Timestep};
|
use super::{asset::GuiAsset, timestep::Timestep};
|
||||||
|
|
||||||
|
const MAX_SIZE: u32 = 2048;
|
||||||
|
const MAX_SIZE_VEC2: Vec2 = vec2(MAX_SIZE as _, MAX_SIZE as _);
|
||||||
|
|
||||||
pub struct GuiPanel {
|
pub struct GuiPanel {
|
||||||
pub layout: Layout,
|
pub layout: Layout,
|
||||||
context: WguiContext,
|
context: WguiContext,
|
||||||
timestep: Timestep,
|
timestep: Timestep,
|
||||||
pub max_size: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GuiPanel {
|
impl GuiPanel {
|
||||||
pub fn new_from_template(app: &AppState, max_size: u32, path: &str) -> anyhow::Result<Self> {
|
pub fn new_from_template(app: &AppState, path: &str) -> anyhow::Result<Self> {
|
||||||
let mut me = Self::new_blank(app, max_size)?;
|
let (layout, _state) =
|
||||||
|
wgui::parser::new_layout_from_assets(Box::new(gui::asset::GuiAsset {}), path)?;
|
||||||
|
|
||||||
let parent = me.layout.root_widget;
|
let context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
|
||||||
let _res = wgui::parser::parse_from_assets(&mut me.layout, parent, path)?;
|
let mut timestep = Timestep::new();
|
||||||
|
timestep.set_tps(60.0);
|
||||||
|
|
||||||
Ok(me)
|
Ok(Self {
|
||||||
|
layout,
|
||||||
|
context,
|
||||||
|
timestep,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_blank(app: &AppState, max_size: u32) -> anyhow::Result<Self> {
|
pub fn new_blank(app: &AppState) -> anyhow::Result<Self> {
|
||||||
let layout = Layout::new(Box::new(GuiAsset {}))?;
|
let layout = Layout::new(Box::new(GuiAsset {}))?;
|
||||||
let context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
|
let context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
|
||||||
let mut timestep = Timestep::new();
|
let mut timestep = Timestep::new();
|
||||||
@@ -49,9 +58,12 @@ impl GuiPanel {
|
|||||||
layout,
|
layout,
|
||||||
context,
|
context,
|
||||||
timestep,
|
timestep,
|
||||||
max_size,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_layout(&mut self) -> anyhow::Result<()> {
|
||||||
|
self.layout.update(MAX_SIZE_VEC2, 0.0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OverlayBackend for GuiPanel {
|
impl OverlayBackend for GuiPanel {
|
||||||
@@ -121,6 +133,9 @@ impl InteractionHandler for GuiPanel {
|
|||||||
|
|
||||||
impl OverlayRenderer for GuiPanel {
|
impl OverlayRenderer for GuiPanel {
|
||||||
fn init(&mut self, _app: &mut AppState) -> anyhow::Result<()> {
|
fn init(&mut self, _app: &mut AppState) -> anyhow::Result<()> {
|
||||||
|
if self.layout.content_size.x * self.layout.content_size.y == 0.0 {
|
||||||
|
self.update_layout()?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +153,7 @@ impl OverlayRenderer for GuiPanel {
|
|||||||
self.layout.tick()?;
|
self.layout.tick()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.layout.content_size == vec2(0., 0.) {
|
if self.layout.content_size.x * self.layout.content_size.y == 0.0 {
|
||||||
return Ok(ShouldRender::Unable);
|
return Ok(ShouldRender::Unable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +172,7 @@ impl OverlayRenderer for GuiPanel {
|
|||||||
_alpha: f32,
|
_alpha: f32,
|
||||||
) -> anyhow::Result<bool> {
|
) -> anyhow::Result<bool> {
|
||||||
self.context.update_viewport(tgt.extent_u32arr(), 1.0)?;
|
self.context.update_viewport(tgt.extent_u32arr(), 1.0)?;
|
||||||
self.layout.update(tgt.extent_vec2(), self.timestep.alpha)?;
|
self.layout.update(MAX_SIZE_VEC2, self.timestep.alpha)?;
|
||||||
|
|
||||||
let mut cmd_buf = app
|
let mut cmd_buf = app
|
||||||
.gfx
|
.gfx
|
||||||
@@ -176,8 +191,8 @@ impl OverlayRenderer for GuiPanel {
|
|||||||
fn frame_meta(&mut self) -> Option<FrameMeta> {
|
fn frame_meta(&mut self) -> Option<FrameMeta> {
|
||||||
Some(FrameMeta {
|
Some(FrameMeta {
|
||||||
extent: [
|
extent: [
|
||||||
self.max_size.min(self.layout.content_size.x as _),
|
MAX_SIZE.min(self.layout.content_size.x as _),
|
||||||
self.max_size.min(self.layout.content_size.y as _),
|
MAX_SIZE.min(self.layout.content_size.y as _),
|
||||||
1,
|
1,
|
||||||
],
|
],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|||||||
@@ -1,12 +1,5 @@
|
|||||||
use glam::{Vec3A, vec2};
|
use glam::Vec3A;
|
||||||
use std::sync::{Arc, LazyLock};
|
use std::sync::{Arc, LazyLock};
|
||||||
use wgui::parser::parse_color_hex;
|
|
||||||
use wgui::renderer_vk::text::{FontWeight, TextStyle};
|
|
||||||
use wgui::taffy;
|
|
||||||
use wgui::taffy::prelude::{length, percent};
|
|
||||||
use wgui::widget::rectangle::{Rectangle, RectangleParams};
|
|
||||||
use wgui::widget::text::{TextLabel, TextParams};
|
|
||||||
use wgui::widget::util::WLength;
|
|
||||||
|
|
||||||
use crate::backend::overlay::{OverlayData, OverlayState, Positioning, Z_ORDER_ANCHOR};
|
use crate::backend::overlay::{OverlayData, OverlayState, Positioning, Z_ORDER_ANCHOR};
|
||||||
use crate::gui::panel::GuiPanel;
|
use crate::gui::panel::GuiPanel;
|
||||||
@@ -18,46 +11,7 @@ pub fn create_anchor<O>(app: &mut AppState) -> anyhow::Result<OverlayData<O>>
|
|||||||
where
|
where
|
||||||
O: Default,
|
O: Default,
|
||||||
{
|
{
|
||||||
let mut panel = GuiPanel::new_blank(app, 200)?;
|
let panel = GuiPanel::new_from_template(app, "gui/anchor.xml")?;
|
||||||
|
|
||||||
let (rect, _) = panel.layout.add_child(
|
|
||||||
panel.layout.root_widget,
|
|
||||||
Rectangle::create(RectangleParams {
|
|
||||||
color: wgui::drawing::Color::new(0., 0., 0., 0.),
|
|
||||||
border_color: parse_color_hex("#ffff00").unwrap(),
|
|
||||||
border: 2.0,
|
|
||||||
round: WLength::Percent(1.0),
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.unwrap(),
|
|
||||||
taffy::Style {
|
|
||||||
size: taffy::Size {
|
|
||||||
width: percent(1.0),
|
|
||||||
height: percent(1.0),
|
|
||||||
},
|
|
||||||
align_items: Some(taffy::AlignItems::Center),
|
|
||||||
justify_content: Some(taffy::JustifyContent::Center),
|
|
||||||
padding: length(4.0),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let _ = panel.layout.add_child(
|
|
||||||
rect,
|
|
||||||
TextLabel::create(TextParams {
|
|
||||||
content: "Center".into(),
|
|
||||||
style: TextStyle {
|
|
||||||
weight: Some(FontWeight::Bold),
|
|
||||||
size: Some(36.0),
|
|
||||||
color: parse_color_hex("#ffff00"),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.unwrap(),
|
|
||||||
taffy::style::Style::DEFAULT,
|
|
||||||
);
|
|
||||||
|
|
||||||
panel.layout.update(vec2(2048., 2048.), 0.0)?;
|
|
||||||
|
|
||||||
Ok(OverlayData {
|
Ok(OverlayData {
|
||||||
state: OverlayState {
|
state: OverlayState {
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ pub fn create_custom(
|
|||||||
|
|
||||||
unreachable!();
|
unreachable!();
|
||||||
|
|
||||||
let panel = GuiPanel::new_blank(app, 2048).ok()?;
|
let panel = GuiPanel::new_blank(app).ok()?;
|
||||||
panel.layout.update(vec2(2048., 2048.), 0.0).ok()?;
|
panel.update_layout().ok()?;
|
||||||
|
|
||||||
let state = OverlayState {
|
let state = OverlayState {
|
||||||
name,
|
name,
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ where
|
|||||||
processes: vec![],
|
processes: vec![],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let mut panel = GuiPanel::new_blank(app, 2048)?;
|
let mut panel = GuiPanel::new_blank(app)?;
|
||||||
|
|
||||||
let (background, _) = panel.layout.add_child(
|
let (background, _) = panel.layout.add_child(
|
||||||
panel.layout.root_widget,
|
panel.layout.root_widget,
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
|||||||
toast.title
|
toast.title
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut panel = GuiPanel::new_blank(app, 2048).ok()?;
|
let mut panel = GuiPanel::new_blank(app).ok()?;
|
||||||
|
|
||||||
let (rect, _) = panel
|
let (rect, _) = panel
|
||||||
.layout
|
.layout
|
||||||
@@ -231,10 +231,12 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
panel.update_layout().ok()?;
|
||||||
|
|
||||||
let state = OverlayState {
|
let state = OverlayState {
|
||||||
name: TOAST_NAME.clone(),
|
name: TOAST_NAME.clone(),
|
||||||
want_visible: true,
|
want_visible: true,
|
||||||
spawn_scale: (panel.max_size as f32) * PIXELS_TO_METERS,
|
spawn_scale: panel.layout.content_size.x * PIXELS_TO_METERS,
|
||||||
spawn_rotation,
|
spawn_rotation,
|
||||||
spawn_point,
|
spawn_point,
|
||||||
z_order: Z_ORDER_TOAST,
|
z_order: Z_ORDER_TOAST,
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ pub fn create_watch<O>(app: &mut AppState) -> anyhow::Result<OverlayData<O>>
|
|||||||
where
|
where
|
||||||
O: Default,
|
O: Default,
|
||||||
{
|
{
|
||||||
let mut panel = GuiPanel::new_blank(app, 2048)?;
|
let mut panel = GuiPanel::new_blank(app)?;
|
||||||
|
|
||||||
let (_, _) = panel.layout.add_child(
|
let (_, _) = panel.layout.add_child(
|
||||||
panel.layout.root_widget,
|
panel.layout.root_widget,
|
||||||
@@ -52,7 +52,7 @@ where
|
|||||||
lerp: 1.0,
|
lerp: 1.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
panel.layout.update(vec2(2048., 2048.), 0.0)?;
|
panel.update_layout()?;
|
||||||
|
|
||||||
Ok(OverlayData {
|
Ok(OverlayData {
|
||||||
state: OverlayState {
|
state: OverlayState {
|
||||||
|
|||||||
Reference in New Issue
Block a user