Skip to content

Commit

Permalink
move windows to layer system instead of custom
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 7, 2024
1 parent c50c170 commit 5317b57
Show file tree
Hide file tree
Showing 9 changed files with 547 additions and 657 deletions.
78 changes: 38 additions & 40 deletions goryak/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::cell::Cell;
use std::rc::Rc;

use yakui_core::geometry::{Color, Constraints, Dim2, Vec2};
Expand All @@ -12,57 +12,54 @@ use crate::{
blur_bg, icon_button, mincolumn, on_primary_container, outline, primary_container, textc,
};

thread_local! {
/// Remember which windows were drawn. That what we can put them at the bottom of the widget tree to be drawn on top of the rest.
/// We can also have a way to remember which windows are active, so they can be on top too.
static WINDOWS: RefCell<Vec<Window>> = Default::default();
}

pub struct Window {
pub struct Window<'a> {
pub title: &'static str,
pub pad: Pad,
pub radius: f32,
pub opened: &'a mut bool,
}

impl Window {
pub fn show(self, children: impl FnOnce(), on_close: impl FnOnce()) {
impl<'a> Window<'a> {
pub fn show(self, children: impl FnOnce()) {
let dom = context::dom();
let response = dom.begin_widget::<WindowBase>(());

let off = draggable(|| {
blur_bg(primary_container().with_alpha(0.5), self.radius, || {
self.pad.show(|| {
mincolumn(|| {
reflow(Alignment::TOP_RIGHT, Dim2::ZERO, || {
offset(Vec2::new(-25.0, -15.0), || {
constrained(Constraints::tight(Vec2::splat(40.0)), || {
center(|| {
let mut b = Button::unstyled("close");
b.padding = Pad::balanced(4.0, 2.0);
b.border_radius = 10.0;
b.style.fill = Color::CLEAR;
b.style.text.font_size = 20.0;
b.style.text.color = on_primary_container().adjust(0.5);
b.down_style.fill = Color::CLEAR;
b.down_style.text = b.style.text.clone();
b.hover_style.fill = Color::CLEAR;
b.hover_style.text = b.style.text.clone();
b.hover_style.text.font_size = 25.0;
b.hover_style.text.color = on_primary_container();

if icon_button(b).show().clicked {
on_close();
}
if *self.opened {
blur_bg(primary_container().with_alpha(0.5), self.radius, || {
self.pad.show(|| {
mincolumn(|| {
reflow(Alignment::TOP_RIGHT, Dim2::ZERO, || {
offset(Vec2::new(-25.0, -15.0), || {
constrained(Constraints::tight(Vec2::splat(40.0)), || {
center(|| {
let mut b = Button::unstyled("close");
b.padding = Pad::balanced(4.0, 2.0);
b.border_radius = 10.0;
b.style.fill = Color::CLEAR;
b.style.text.font_size = 20.0;
b.style.text.color = on_primary_container().adjust(0.5);
b.down_style.fill = Color::CLEAR;
b.down_style.text = b.style.text.clone();
b.hover_style.fill = Color::CLEAR;
b.hover_style.text = b.style.text.clone();
b.hover_style.text.font_size = 25.0;
b.hover_style.text.color = on_primary_container();

if icon_button(b).show().clicked {
*self.opened = false;
}
});
});
});
});
textc(on_primary_container(), Cow::Borrowed(self.title));
divider(outline(), 10.0, 1.0);
children();
});
textc(on_primary_container(), Cow::Borrowed(self.title));
divider(outline(), 10.0, 1.0);
children();
});
});
});
}
});

response.confirm.set(off.dragging.is_none());
Expand All @@ -78,18 +75,18 @@ impl Window {
struct WindowBase {
props: (),
off: Vec2,
resp: Rc<WindowResp>,
resp: Rc<WindowBaseResponse>,
}

#[derive(Default, Debug)]
struct WindowResp {
struct WindowBaseResponse {
off: Cell<Vec2>,
confirm: Cell<bool>,
}

impl Widget for WindowBase {
type Props<'a> = ();
type Response = Rc<WindowResp>;
type Response = Rc<WindowBaseResponse>;

fn new() -> Self {
Self::default()
Expand All @@ -112,6 +109,7 @@ impl Widget for WindowBase {
}

fn layout(&self, mut ctx: LayoutContext<'_>, _: Constraints) -> Vec2 {
ctx.layout.new_layer(ctx.dom);
let node = ctx.dom.get_current();
if node.children.len() > 1 {
panic!("Window can only have one child");
Expand Down
2 changes: 0 additions & 2 deletions native_app/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::newgui::roadbuild::RoadBuildResource;
use crate::newgui::roadeditor::RoadEditorResource;
use crate::newgui::specialbuilding::SpecialBuildingResource;
use crate::newgui::terraforming::TerraformingResource;
use crate::newgui::window_display::WindowDisplay;
use crate::newgui::windows::economy::EconomyState;
use crate::newgui::windows::settings::{Settings, SettingsState};
use crate::newgui::zoneedit::ZoneEditState;
Expand Down Expand Up @@ -62,7 +61,6 @@ pub fn init() {
register_resource_noserialize::<WorldCommands>();
register_resource_noserialize::<crate::gui::windows::load::LoadState>();
register_resource_noserialize::<crate::uiworld::SaveLoadState>();
register_resource_noserialize::<WindowDisplay>();
register_resource_noserialize::<EconomyState>();
register_resource_noserialize::<SettingsState>();
}
Expand Down
71 changes: 34 additions & 37 deletions native_app/src/newgui/hud/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,43 @@ fn save_window(gui: &mut GuiState, uiw: &UiWorld) {
match *estate {
ExitState::NoExit => {}
ExitState::ExitAsk | ExitState::Saving => {
uiw.window(
Window {
title: "Exit Menu",
pad: Pad::all(15.0),
radius: 10.0,
},
|uiw| {
let mut estate = uiw.write::<ExitState>();
*estate = ExitState::NoExit;
},
|uiw, _sim| {
let mut slstate = uiw.write::<SaveLoadState>();
let mut estate = uiw.write::<ExitState>();
let mut l = List::column();
l.item_spacing = 5.0;
l.main_axis_size = MainAxisSize::Min;
l.show(|| {
if let ExitState::Saving = *estate {
textc(on_secondary_container(), "Saving...");
if !slstate.please_save && !slstate.saving_status.load(Ordering::SeqCst)
{
std::process::exit(0);
}
return;
}
if button_secondary("Save and exit").show().clicked {
if let ExitState::ExitAsk = *estate {
slstate.please_save = true;
*estate = ExitState::Saving;
}
}
if button_secondary("Exit without saving").show().clicked {
let mut opened = true;
Window {
title: "Exit Menu",
pad: Pad::all(15.0),
radius: 10.0,
opened: &mut opened,
}
.show(|| {
let mut l = List::column();
l.item_spacing = 5.0;
l.main_axis_size = MainAxisSize::Min;
l.show(|| {
if let ExitState::Saving = *estate {
textc(on_secondary_container(), "Saving...");
if !slstate.please_save && !slstate.saving_status.load(Ordering::SeqCst) {
std::process::exit(0);
}
if button_secondary("Cancel").show().clicked {
*estate = ExitState::NoExit;
return;
}
if button_secondary("Save and exit").show().clicked {
if let ExitState::ExitAsk = *estate {
slstate.please_save = true;
*estate = ExitState::Saving;
}
});
},
);
}
if button_secondary("Exit without saving").show().clicked {
std::process::exit(0);
}
if button_secondary("Cancel").show().clicked {
*estate = ExitState::NoExit;
}
});
});

if !opened {
*estate = ExitState::NoExit;
}

if uiw
.read::<InputMap>()
Expand Down
8 changes: 1 addition & 7 deletions native_app/src/newgui/hud/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use goryak::debug_layout;
use ordered_float::OrderedFloat;
use std::time::Instant;
use yakui::{reflow, Alignment, Color, Dim2, Vec2};
Expand All @@ -10,14 +9,12 @@ use crate::gui::{GuiState, UiTextures};
use crate::newgui::hud::menu::menu_bar;
use crate::newgui::hud::time_controls::time_controls;
use crate::newgui::hud::toolbox::new_toolbox;
use crate::newgui::window_display::WindowDisplay;
use crate::newgui::windows::settings::Settings;
use crate::uiworld::{SaveLoadState, UiWorld};

mod menu;
mod time_controls;
mod toolbox;
pub mod window_display;
pub mod windows;

/// Root GUI entrypoint
Expand All @@ -35,11 +32,8 @@ pub fn render_newgui(uiworld: &UiWorld, sim: &Simulation) {
menu_bar(uiworld, sim);
uiworld.write::<GuiState>().windows.render(uiworld, sim);
time_controls(uiworld, sim);
WindowDisplay::finish(uiworld, sim);
});
if tweak!(false) {
debug_layout();
}
//goryak::debug_layout();
}

fn auto_save(uiworld: &UiWorld) {
Expand Down
52 changes: 0 additions & 52 deletions native_app/src/newgui/hud/window_display.rs

This file was deleted.

Loading

0 comments on commit 5317b57

Please sign in to comment.