xml anchor + GuiPanel minor refactor
This commit is contained in:
@@ -17,29 +17,38 @@ use crate::{
|
||||
overlay::{FrameMeta, OverlayBackend, OverlayRenderer, ShouldRender},
|
||||
},
|
||||
graphics::{CommandBuffers, ExtentExt},
|
||||
gui,
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
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 layout: Layout,
|
||||
context: WguiContext,
|
||||
timestep: Timestep,
|
||||
pub max_size: u32,
|
||||
}
|
||||
|
||||
impl GuiPanel {
|
||||
pub fn new_from_template(app: &AppState, max_size: u32, path: &str) -> anyhow::Result<Self> {
|
||||
let mut me = Self::new_blank(app, max_size)?;
|
||||
pub fn new_from_template(app: &AppState, path: &str) -> anyhow::Result<Self> {
|
||||
let (layout, _state) =
|
||||
wgui::parser::new_layout_from_assets(Box::new(gui::asset::GuiAsset {}), path)?;
|
||||
|
||||
let parent = me.layout.root_widget;
|
||||
let _res = wgui::parser::parse_from_assets(&mut me.layout, parent, path)?;
|
||||
let context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
|
||||
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 context = WguiContext::new(app.gfx.clone(), app.gfx.surface_format, 1.0)?;
|
||||
let mut timestep = Timestep::new();
|
||||
@@ -49,9 +58,12 @@ impl GuiPanel {
|
||||
layout,
|
||||
context,
|
||||
timestep,
|
||||
max_size,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update_layout(&mut self) -> anyhow::Result<()> {
|
||||
self.layout.update(MAX_SIZE_VEC2, 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl OverlayBackend for GuiPanel {
|
||||
@@ -121,6 +133,9 @@ impl InteractionHandler for GuiPanel {
|
||||
|
||||
impl OverlayRenderer for GuiPanel {
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -138,7 +153,7 @@ impl OverlayRenderer for GuiPanel {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -157,7 +172,7 @@ impl OverlayRenderer for GuiPanel {
|
||||
_alpha: f32,
|
||||
) -> anyhow::Result<bool> {
|
||||
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
|
||||
.gfx
|
||||
@@ -176,8 +191,8 @@ impl OverlayRenderer for GuiPanel {
|
||||
fn frame_meta(&mut self) -> Option<FrameMeta> {
|
||||
Some(FrameMeta {
|
||||
extent: [
|
||||
self.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.x as _),
|
||||
MAX_SIZE.min(self.layout.content_size.y as _),
|
||||
1,
|
||||
],
|
||||
..Default::default()
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
use glam::{Vec3A, vec2};
|
||||
use glam::Vec3A;
|
||||
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::gui::panel::GuiPanel;
|
||||
@@ -18,46 +11,7 @@ pub fn create_anchor<O>(app: &mut AppState) -> anyhow::Result<OverlayData<O>>
|
||||
where
|
||||
O: Default,
|
||||
{
|
||||
let mut panel = GuiPanel::new_blank(app, 200)?;
|
||||
|
||||
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)?;
|
||||
let panel = GuiPanel::new_from_template(app, "gui/anchor.xml")?;
|
||||
|
||||
Ok(OverlayData {
|
||||
state: OverlayState {
|
||||
|
||||
@@ -18,8 +18,8 @@ pub fn create_custom(
|
||||
|
||||
unreachable!();
|
||||
|
||||
let panel = GuiPanel::new_blank(app, 2048).ok()?;
|
||||
panel.layout.update(vec2(2048., 2048.), 0.0).ok()?;
|
||||
let panel = GuiPanel::new_blank(app).ok()?;
|
||||
panel.update_layout().ok()?;
|
||||
|
||||
let state = OverlayState {
|
||||
name,
|
||||
|
||||
@@ -51,7 +51,7 @@ where
|
||||
processes: vec![],
|
||||
}));
|
||||
|
||||
let mut panel = GuiPanel::new_blank(app, 2048)?;
|
||||
let mut panel = GuiPanel::new_blank(app)?;
|
||||
|
||||
let (background, _) = panel.layout.add_child(
|
||||
panel.layout.root_widget,
|
||||
|
||||
@@ -169,7 +169,7 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
toast.title
|
||||
};
|
||||
|
||||
let mut panel = GuiPanel::new_blank(app, 2048).ok()?;
|
||||
let mut panel = GuiPanel::new_blank(app).ok()?;
|
||||
|
||||
let (rect, _) = panel
|
||||
.layout
|
||||
@@ -231,10 +231,12 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
},
|
||||
);
|
||||
|
||||
panel.update_layout().ok()?;
|
||||
|
||||
let state = OverlayState {
|
||||
name: TOAST_NAME.clone(),
|
||||
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_point,
|
||||
z_order: Z_ORDER_TOAST,
|
||||
|
||||
@@ -23,7 +23,7 @@ pub fn create_watch<O>(app: &mut AppState) -> anyhow::Result<OverlayData<O>>
|
||||
where
|
||||
O: Default,
|
||||
{
|
||||
let mut panel = GuiPanel::new_blank(app, 2048)?;
|
||||
let mut panel = GuiPanel::new_blank(app)?;
|
||||
|
||||
let (_, _) = panel.layout.add_child(
|
||||
panel.layout.root_widget,
|
||||
@@ -52,7 +52,7 @@ where
|
||||
lerp: 1.0,
|
||||
};
|
||||
|
||||
panel.layout.update(vec2(2048., 2048.), 0.0)?;
|
||||
panel.update_layout()?;
|
||||
|
||||
Ok(OverlayData {
|
||||
state: OverlayState {
|
||||
|
||||
Reference in New Issue
Block a user