Skip to content

Commit

Permalink
display fps in ui and toggle fps recording
Browse files Browse the repository at this point in the history
  • Loading branch information
fralonra committed Jan 4, 2024
1 parent 6409976 commit 10f741b
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 37 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ name = "wgshadertoy"
version = "0.3.2"
homepage = "https://github.com/fralonra/wgshadertoy"

[features]
fps = []

[dependencies]
anyhow = "1.0"
Expand All @@ -15,7 +13,10 @@ egui-winit = "0.23"
env_logger = "0.9"
font-kit = "0.11"
futures = "0.3"
i18n-embed = { version = "0.14", features = ["fluent-system", "desktop-requester"] }
i18n-embed = { version = "0.14", features = [
"fluent-system",
"desktop-requester",
] }
i18n-embed-fl = "0.7"
image = "0.24"
lazy_static = "1.4"
Expand Down
1 change: 1 addition & 0 deletions i18n/ar/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = خروج
menu_preferences = التفضيلات
menu_languages = اللغات
menu_language_system = النظام الافتراضي
menu_record_fps = سجل إطارا في الثانية
## Menu Help
menu_help = مساعدة
menu_about = حول
Expand Down
1 change: 1 addition & 0 deletions i18n/de/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Beenden
menu_preferences = Einstellungen
menu_languages = Sprachen
menu_language_system = Systemstandard
menu_record_fps = Nehmen Sie FPS auf
## Menu Help
menu_help = Hilfe
menu_about = Über
Expand Down
1 change: 1 addition & 0 deletions i18n/en/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Quit
menu_preferences = Preferences
menu_languages = Languages
menu_language_system = System Default
menu_record_fps = Record FPS
## Menu Help
menu_help = Help
menu_about = About
Expand Down
1 change: 1 addition & 0 deletions i18n/es/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Salir
menu_preferences = Preferencias
menu_languages = Idiomas
menu_language_system = Sistema Predeterminado
menu_record_fps = Grabar FPS
## Menu Help
menu_help = Ayuda
menu_about = Acerca de
Expand Down
1 change: 1 addition & 0 deletions i18n/fr/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Quitter
menu_preferences = Préférences
menu_languages = Langues
menu_language_system = Par défaut du système
menu_record_fps = Enregistrer des FPS
## Menu Help
menu_help = Aide
menu_about = À propos
Expand Down
1 change: 1 addition & 0 deletions i18n/it/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Esci
menu_preferences = Preferenze
menu_languages = Lingue
menu_language_system = Predefinito del sistema
menu_record_fps = Registra FPS
## Menu Help
menu_help = Aiuto
menu_about = Informazioni
Expand Down
1 change: 1 addition & 0 deletions i18n/ja/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = 終了
menu_preferences = 設定
menu_languages = 言語
menu_language_system = システムデフォルト
menu_record_fps = FPSを記録する
## Menu Help
menu_help = ヘルプ
menu_about = このプログラムについて
Expand Down
1 change: 1 addition & 0 deletions i18n/ko/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = 종료
menu_preferences = 환경 설정
menu_languages = 언어
menu_language_system = 시스템 기본값
menu_record_fps = FPS 기록
## Menu Help
menu_help = 도움말
menu_about = 소개
Expand Down
1 change: 1 addition & 0 deletions i18n/pt/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Sair
menu_preferences = Preferências
menu_languages = Idiomas
menu_language_system = Sistema Padrão
menu_record_fps = Gravar FPS
## Menu Help
menu_help = Ajuda
menu_about = Sobre
Expand Down
1 change: 1 addition & 0 deletions i18n/ru/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = Выйти
menu_preferences = Настройки
menu_languages = Языки
menu_language_system = Системный
menu_record_fps = Запись кадров в секунду
## Menu Help
menu_help = Помощь
menu_about = О программе
Expand Down
1 change: 1 addition & 0 deletions i18n/zh-CN/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = 退出
menu_preferences = 首选项
menu_languages = 语言
menu_language_system = 系统语言
menu_record_fps = 记录 FPS
# Menu Help
menu_help = 帮助
menu_about = 关于
Expand Down
1 change: 1 addition & 0 deletions i18n/zh-TW/wgshadertoy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ menu_quit = 退出
menu_preferences = 偏好設定
menu_languages = 語言
menu_language_system = 系統預設
menu_record_fps = 記錄 FPS
## Menu Help
menu_help = 幫助
menu_about = 關於
Expand Down
25 changes: 18 additions & 7 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(feature = "fps")]
use crate::fps_counter::FpsCounter;
use crate::{
event::{AppResponse, AppStatus, EventProxyWinit, UserEvent},
fps_counter::FpsCounter,
fs::{create_file, select_file, select_texture, write_file},
preferences::Preferences,
ui::{EditContext, Ui, UiState},
};
use anyhow::{bail, Result};
Expand All @@ -23,9 +23,10 @@ use winit::{event::WindowEvent, event_loop::EventLoop, window::Window};
pub struct Core {
cursor: [f32; 2],
event_proxy: EventProxyWinit<UserEvent>,
#[cfg(feature = "fps")]
fps: Option<usize>,
fps_counter: FpsCounter,
has_validation_error: bool,
preferences: Preferences,
runtime: Runtime,
size: (f32, f32),
state: State,
Expand Down Expand Up @@ -82,12 +83,13 @@ impl Core {
let initial_status = AppStatus::Info("Shader compiled successfully!".to_owned());

Ok(Self {
cursor: Default::default(),
cursor: [0.0, 0.0],
event_proxy,
#[cfg(feature = "fps")]
fps: None,
fps_counter: FpsCounter::new(),
has_validation_error: false,
runtime,
preferences: Preferences::default(),
size: (width, height),
state,
status: initial_status,
Expand Down Expand Up @@ -327,8 +329,15 @@ impl Core {
}
}

#[cfg(feature = "fps")]
log::info!("FPS: {}", self.fps_counter.tick());
if self.preferences.record_fps {
let fps = self.fps_counter.tick();

log::info!("FPS: {}", fps);

self.fps = Some(fps);
} else if self.fps.is_some() {
self.fps = None;
}
}

pub fn resize(&mut self, width: f32, height: f32, scale_factor: f32) {
Expand Down Expand Up @@ -386,6 +395,7 @@ impl Core {
let ui_state = UiState {
can_capture: self.runtime.is_capture_supported(),
file_saved: self.wgs_path.is_some(),
fps: self.fps,
is_paused: self.runtime.is_paused(),
status: self.status.clone(),
texture_addable: self.runtime.wgs().textures_ref().len() + 1
Expand All @@ -396,6 +406,7 @@ impl Core {

let full_output = self.ui.prepare(
raw_input,
&mut self.preferences,
&mut self.ui_edit_context,
&self.event_proxy,
ui_state,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ mod egui_winit_wgpu_context;
mod event;
mod example;
mod fonts;
#[cfg(feature = "fps")]
mod fps_counter;
mod fs;
mod i18n;
mod preferences;
mod shortcut;
mod ui;
mod window;
Expand Down
4 changes: 4 additions & 0 deletions src/preferences.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[derive(Default)]
pub struct Preferences {
pub record_fps: bool,
}
75 changes: 49 additions & 26 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
example::Example,
fonts::{load_font, load_system_font},
i18n::{select_locales, select_system_locales, LANGUAGES},
preferences::Preferences,
shortcut::Shortcut,
};
use egui::{
Expand Down Expand Up @@ -74,12 +75,13 @@ impl Ui {
pub fn prepare(
&mut self,
raw_input: RawInput,
preferences: &mut Preferences,
edit_context: &mut EditContext,
event_proxy: &impl EventProxy<UserEvent>,
state: UiState,
) -> FullOutput {
self.context.run(raw_input, |ctx| {
self.ui(ctx, edit_context, event_proxy, state);
self.ui(ctx, preferences, edit_context, event_proxy, state);
})
}

Expand All @@ -94,6 +96,7 @@ impl Ui {
fn ui(
&self,
ctx: &Context,
preferences: &mut Preferences,
edit_context: &mut EditContext,
event_proxy: &impl EventProxy<UserEvent>,
state: UiState,
Expand Down Expand Up @@ -247,6 +250,15 @@ impl Ui {
}
}
});

ui.separator();

if ui
.checkbox(&mut preferences.record_fps, fl!("menu_record_fps"))
.clicked()
{
ui.close_menu();
}
});

ui.menu_button(fl!("menu_help"), |ui| {
Expand All @@ -259,31 +271,41 @@ impl Ui {
});
});

TopBottomPanel::bottom("status").show(ctx, |ui| match state.status {
AppStatus::Info(message) => {
ui.label(message);
}
AppStatus::Warning(message) => {
ui.colored_label(
if is_dark {
Color32::KHAKI
} else {
Color32::DARK_RED
},
message,
);
}
AppStatus::Error(message) => {
ui.colored_label(
if is_dark {
Color32::LIGHT_RED
} else {
Color32::DARK_RED
},
message,
);
}
_ => {}
TopBottomPanel::bottom("status").show(ctx, |ui| {
ui.horizontal(|ui| {
if let Some(fps) = state.fps {
ui.label(format!("{}fps", fps));

ui.separator();
}

match state.status {
AppStatus::Info(message) => {
ui.label(message);
}
AppStatus::Warning(message) => {
ui.colored_label(
if is_dark {
Color32::KHAKI
} else {
Color32::DARK_RED
},
message,
);
}
AppStatus::Error(message) => {
ui.colored_label(
if is_dark {
Color32::LIGHT_RED
} else {
Color32::DARK_RED
},
message,
);
}
_ => {}
}
});
});

CentralPanel::default().show(ctx, |ui| {
Expand Down Expand Up @@ -401,6 +423,7 @@ impl Ui {
pub struct UiState {
pub can_capture: bool,
pub file_saved: bool,
pub fps: Option<usize>,
pub is_paused: bool,
pub status: AppStatus,
pub texture_addable: bool,
Expand Down

0 comments on commit 10f741b

Please sign in to comment.