From dee11d51990ddaec48fff1fcdc25fe5d6d2e8b52 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Wed, 24 Jan 2024 13:04:21 +0100 Subject: [PATCH] better time controls --- assets/SpaceMono-Regular.ttf | 3 ++ assets_gui/src/yakui_gui.rs | 6 ++-- engine/src/yakui.rs | 8 ++++- goryak/src/lib.rs | 2 +- goryak/src/text.rs | 7 ++++ goryak/src/util.rs | 52 +++++++++++++++++++++++++++--- native_app/src/newgui/topgui.rs | 57 ++++++++++++++++++++++----------- 7 files changed, 108 insertions(+), 27 deletions(-) create mode 100755 assets/SpaceMono-Regular.ttf diff --git a/assets/SpaceMono-Regular.ttf b/assets/SpaceMono-Regular.ttf new file mode 100755 index 00000000..a715a65b --- /dev/null +++ b/assets/SpaceMono-Regular.ttf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:74ea804954f6f3e3ed4c748da603547ff2f88b03f614c7ad85231449d33c51be +size 90904 diff --git a/assets_gui/src/yakui_gui.rs b/assets_gui/src/yakui_gui.rs index 7ab10c24..7ff37c5a 100644 --- a/assets_gui/src/yakui_gui.rs +++ b/assets_gui/src/yakui_gui.rs @@ -70,10 +70,10 @@ impl State { l.main_axis_alignment = MainAxisAlignment::Center; Pad::all(5.0).show(|| { l.show(|| { - if button_primary("Dark theme").clicked { + if button_primary("Dark theme").show().clicked { set_theme(Theme::Dark); } - if button_primary("Light theme").clicked { + if button_primary("Light theme").show().clicked { set_theme(Theme::Light); } }); @@ -197,7 +197,7 @@ impl State { params }); }); - if button_primary("Generate LODs").clicked { + if button_primary("Generate LODs").show().clicked { let asset_path = &props.asset_path; self.actions diff --git a/engine/src/yakui.rs b/engine/src/yakui.rs index 3d0a696b..06669345 100644 --- a/engine/src/yakui.rs +++ b/engine/src/yakui.rs @@ -25,9 +25,15 @@ impl YakuiWrapper { FontSettings::default(), ) .unwrap(); - fonts.add(font, Some("icons")); + let font = Font::from_bytes( + include_bytes!("../../assets/SpaceMono-Regular.ttf").as_slice(), + FontSettings::default(), + ) + .unwrap(); + fonts.add(font, Some("monospace")); + let platform = yakui_winit::YakuiWinit::new(el); let mut renderer = yakui_wgpu::YakuiWgpu::new(&gfx.device, &gfx.queue); diff --git a/goryak/src/lib.rs b/goryak/src/lib.rs index 6cc4f539..97065a06 100644 --- a/goryak/src/lib.rs +++ b/goryak/src/lib.rs @@ -30,4 +30,4 @@ pub use text::*; pub use theme::*; pub use util::*; -const DEFAULT_FONT_SIZE: f32 = 14.0; +pub const DEFAULT_FONT_SIZE: f32 = 14.0; diff --git a/goryak/src/text.rs b/goryak/src/text.rs index a975ce43..f8de5e06 100644 --- a/goryak/src/text.rs +++ b/goryak/src/text.rs @@ -1,8 +1,15 @@ use crate::DEFAULT_FONT_SIZE; use std::borrow::Cow; use yakui_core::Response; +use yakui_widgets::font::FontName; use yakui_widgets::widgets::{Text, TextResponse}; pub fn text>>(text: S) -> Response { Text::new(DEFAULT_FONT_SIZE, text.into()).show() } + +pub fn monospace>>(text: S) -> Response { + let mut t = Text::new(DEFAULT_FONT_SIZE, text.into()); + t.style.font = FontName::new("monospace"); + t.show() +} diff --git a/goryak/src/util.rs b/goryak/src/util.rs index 6e575e42..8073c2e1 100644 --- a/goryak/src/util.rs +++ b/goryak/src/util.rs @@ -1,9 +1,9 @@ use std::borrow::Cow; use std::panic::Location; -use yakui_core::geometry::{Color, Constraints, Vec2}; +use yakui_core::geometry::{Color, Constraints, FlexFit, Vec2}; use yakui_core::widget::{LayoutContext, PaintContext, Widget}; -use yakui_core::{Response, WidgetId}; +use yakui_core::{context, Response, WidgetId}; use yakui_widgets::util::widget; use yakui_widgets::widgets::{Button, ButtonResponse, Text}; @@ -31,7 +31,51 @@ pub fn labelc(c: Color, text: impl Into>) { t.show(); } -pub fn button_primary(text: impl Into) -> Response { +#[derive(Debug)] +pub struct FixedSizeWidget { + props: Vec2, +} + +pub fn fixed_spacer(size: impl Into) -> Response<::Response> { + widget::(size.into()) +} + +impl Widget for FixedSizeWidget { + type Props<'a> = Vec2; + type Response = (); + + fn new() -> Self { + Self { props: Vec2::ZERO } + } + + fn update(&mut self, props: Self::Props<'_>) -> Self::Response { + self.props = props; + } + + fn flex(&self) -> (u32, FlexFit) { + (0, FlexFit::Tight) + } + + fn layout(&self, _ctx: LayoutContext<'_>, _constraints: Constraints) -> Vec2 { + self.props + } + + fn paint(&self, _ctx: PaintContext<'_>) {} +} + +pub fn widget_inner(children: F, props: T::Props<'_>) -> U +where + T: Widget, + F: FnOnce() -> U, +{ + let dom = context::dom(); + let response = dom.begin_widget::(props); + let r = children(); + dom.end_widget::(response.id); + r +} + +pub fn button_primary(text: impl Into) -> Button { let mut b = Button::styled(text.into()); b.style.fill = primary(); b.style.text.color = on_primary(); @@ -39,7 +83,7 @@ pub fn button_primary(text: impl Into) -> Response { b.hover_style.text.color = on_primary(); b.down_style.fill = primary().adjust(1.3); b.down_style.text.color = on_primary(); - b.show() + b } pub fn button_secondary(text: impl Into) -> Response { diff --git a/native_app/src/newgui/topgui.rs b/native_app/src/newgui/topgui.rs index e653b1b0..a9d3715f 100644 --- a/native_app/src/newgui/topgui.rs +++ b/native_app/src/newgui/topgui.rs @@ -1,10 +1,13 @@ -use yakui::widgets::{List, Pad}; +use yakui::widgets::{List, Pad, PadWidget}; use yakui::{ - colored_box, column, draggable, offset, pad, reflow, row, use_state, Alignment, Color, Dim2, - MainAxisAlignment, MainAxisSize, Vec2, + colored_box, column, constrained, draggable, offset, pad, reflow, row, spacer, use_state, + Alignment, Color, Constraints, CrossAxisAlignment, Dim2, MainAxisAlignment, MainAxisSize, Vec2, }; -use goryak::{blur_bg, button_primary, constrained_viewport, labelc, on_primary_container, text}; +use goryak::{ + blur_bg, button_primary, constrained_viewport, labelc, monospace, on_primary_container, + widget_inner, +}; use prototypes::GameTime; use simulation::map_dynamic::ElectricityFlow; use simulation::Simulation; @@ -73,51 +76,69 @@ impl Gui { if *warp == 0 { yakui::canvas(|ctx| { + let w = ctx.layout.viewport().size().length() * 0.002; yakui::shapes::outline( ctx.paint, ctx.layout.viewport(), - 2.0, - Color::rgba(255, 0, 0, 196), + w, + Color::rgba(255, 0, 0, 128), ); }); } let mut time_text = || { row(|| { - text(format!(" Day {}", time.day)); - - text(format!( + monospace(format!("Day {}", time.day)); + spacer(1); + monospace(format!( "{:02}:{:02}:{:02}", time.hour, time.minute, time.second )); }); row(|| { - if button_primary("||").clicked { + let time_button = |text: &str| { + widget_inner::( + || { + let mut b = button_primary(text); + b.padding = Pad::balanced(10.0, 3.0); + b.show() + }, + Pad::all(3.0), + ) + }; + + if time_button("||").clicked { *depause_warp = *warp; *warp = 0; } - if button_primary("1x").clicked { + if time_button("1x").clicked { *warp = 1; } - if button_primary("3x").clicked { + if time_button("3x").clicked { *warp = 3; } - if button_primary("Max").clicked { + if time_button("Max").clicked { *warp = 1000; } }); }; - reflow(Alignment::TOP_LEFT, Dim2::pixels(0.0, 40.0), || { + reflow(Alignment::TOP_LEFT, Dim2::pixels(0.0, 30.0), || { constrained_viewport(|| { let mut l = List::row(); l.main_axis_alignment = MainAxisAlignment::End; l.show(|| { blur_bg(goryak::primary_container().with_alpha(0.5), 10.0, || { - pad(Pad::all(3.0), || { - let mut l = List::column(); - l.main_axis_size = MainAxisSize::Min; - l.show(|| time_text()); + pad(Pad::all(10.0), || { + constrained( + Constraints::loose(Vec2::new(200.0, f32::INFINITY)), + || { + let mut l = List::column(); + l.cross_axis_alignment = CrossAxisAlignment::Stretch; + l.main_axis_size = MainAxisSize::Min; + l.show(|| time_text()); + }, + ); }); }); });