Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions app/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub async fn run(
.ui_renderer(true)
.with_asset_cache(assets)
.headless(headless)
.debug(is_debug)
.update_title_with_fps_stats(false)
.run(move |app, _runtime| {
*app.world.resource_mut(window_title()) = "Ambient".to_string();
Expand All @@ -89,26 +90,34 @@ pub async fn run(
}

#[element_component]
fn TitleUpdater(hooks: &mut Hooks) -> Element {
fn TitleUpdater(hooks: &mut Hooks, debug: bool) -> Element {
let (net, _) = use_remote_resource(hooks, client_network_stats()).expect("No game client");

let world = &hooks.world;
let title = world.resource(window_title());
let fps = world
.get_cloned(hooks.world.resource_entity(), fps_stats())
.ok()
.filter(|f| !f.fps().is_nan());

let title = match (fps, net) {
(None, None) => title.clone(),
(Some(fps), None) => format!("{} [{}]", title, fps.dump_both()),
(None, Some(net)) => format!("{} [{}]", title, net),
(Some(fps), Some(net)) => format!("{} [{}, {}]", title, fps.dump_both(), net),
};
world
.resource(window_ctl())
.send(WindowCtl::SetTitle(title))
.ok();

if debug {
let fps = world
.get_cloned(hooks.world.resource_entity(), fps_stats())
.ok()
.filter(|f| !f.fps().is_nan());

let title = match (fps, net) {
(None, None) => title.clone(),
(Some(fps), None) => format!("{} [{}]", title, fps.dump_both()),
(None, Some(net)) => format!("{} [{}]", title, net),
(Some(fps), Some(net)) => format!("{} [{}, {}]", title, fps.dump_both(), net),
};
world
.resource(window_ctl())
.send(WindowCtl::SetTitle(title))
.ok();
} else {
world
.resource(window_ctl())
.send(WindowCtl::SetTitle(title.clone()))
.ok();
}

Element::new()
}
Expand Down Expand Up @@ -173,7 +182,7 @@ fn MainApp(
cert,
create_rpc_registry: cb(shared::create_server_rpc_registry),
inner: Dock::el(vec![
TitleUpdater.el(),
TitleUpdater::el(show_debug),
if let Some(golden_image_cmd) = golden_image_cmd.filter(|_| loaded) {
GoldenImageTest::el(golden_image_output_dir, golden_image_cmd)
} else {
Expand Down
46 changes: 46 additions & 0 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use winit::{
dpi::PhysicalPosition,
event::{ElementState, Event, KeyboardInput, ModifiersState, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
platform::macos::WindowExtMacOS,
window::{CursorGrabMode, Fullscreen, Window, WindowBuilder},
};

Expand Down Expand Up @@ -190,6 +191,7 @@ pub struct AppBuilder {
pub main_renderer: bool,
pub examples_systems: bool,
pub headless: Option<UVec2>,
debug: bool,
pub update_title_with_fps_stats: bool,
#[cfg(target_os = "unknown")]
pub parent_element: Option<web_sys::HtmlElement>,
Expand Down Expand Up @@ -221,6 +223,7 @@ impl AppBuilder {
main_renderer: true,
examples_systems: false,
headless: None,
debug: false,
update_title_with_fps_stats: true,
#[cfg(target_os = "unknown")]
parent_element: None,
Expand Down Expand Up @@ -268,6 +271,11 @@ impl AppBuilder {
self
}

pub fn debug(mut self, debug: bool) -> Self {
self.debug = debug;
self
}

pub fn update_title_with_fps_stats(mut self, value: bool) -> Self {
self.update_title_with_fps_stats = value;
self
Expand All @@ -290,6 +298,7 @@ impl AppBuilder {

let settings = SettingsKey.get(&assets);

#[cfg(not(target_os = "windows"))]
let (window, event_loop) = if self.headless.is_some() {
(None, None)
} else {
Expand All @@ -299,6 +308,26 @@ impl AppBuilder {
height: settings.resolution().1,
});
let window = Arc::new(window.build(&event_loop).unwrap());
window.set_simple_fullscreen(!self.debug);
(Some(window), Some(event_loop))
};

#[cfg(target_os = "windows")]
let (window, event_loop) = if self.headless.is_some() {
(None, None)
} else {
let event_loop = self.event_loop.unwrap_or_else(EventLoop::new);

let monitor = event_loop.primary_monitor().unwrap();
let video_mode = monitor.video_modes().next().unwrap();
let window = WindowBuilder::new()
.with_inner_size(winit::dpi::LogicalSize {
width: settings.resolution().0,
height: settings.resolution().1,
})
.with_fullscreen(Some(Fullscreen::Exclusive(video_mode)));
let window = Arc::new(window.build(&event_loop).unwrap());
window.set_simple_fullscreen(!self.debug);
(Some(window), Some(event_loop))
};

Expand Down Expand Up @@ -588,6 +617,23 @@ impl App {
},
control_flow,
);
} else if let Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Escape),
..
},
..
},
..
} = &event
{
if let Some(window) = self.window.as_ref() {
window.set_simple_fullscreen(false);
}
} else if let Some(event) = event.to_static() {
self.handle_static_event(&event, control_flow);
}
Expand Down