-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
197 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mod fps; | ||
mod performance_overlay; | ||
|
||
pub use fps::*; | ||
pub use performance_overlay::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use freya_core::plugins::{FreyaPlugin, PluginEvent}; | ||
use freya_engine::prelude::{Color, ParagraphBuilder, ParagraphStyle, TextShadow, TextStyle}; | ||
|
||
#[derive(Default)] | ||
pub struct PerformanceOverlayPlugin { | ||
frames: Vec<Instant>, | ||
started_render: Option<Instant>, | ||
started_layout: Option<Instant>, | ||
finished_layout: Option<Duration>, | ||
} | ||
|
||
impl FreyaPlugin for PerformanceOverlayPlugin { | ||
fn on_event(&mut self, event: &PluginEvent) { | ||
match event { | ||
PluginEvent::StartedLayout(_) => self.started_layout = Some(Instant::now()), | ||
PluginEvent::FinishedLayout(_) => { | ||
self.finished_layout = Some(self.started_layout.unwrap().elapsed()) | ||
} | ||
PluginEvent::StartedRender(_canvas, _font_collection) => { | ||
self.started_render = Some(Instant::now()) | ||
} | ||
PluginEvent::FinishedRender(canvas, font_collection) => { | ||
let started_render = self.started_render.take().unwrap(); | ||
let finished_layout = self.finished_layout.unwrap(); | ||
|
||
let now = Instant::now(); | ||
|
||
self.frames | ||
.retain(|frame| now.duration_since(*frame).as_millis() < 1000); | ||
|
||
self.frames.push(now); | ||
|
||
// Render the texts | ||
let mut paragraph_builder = | ||
ParagraphBuilder::new(&ParagraphStyle::default(), *font_collection); | ||
let mut text_style = TextStyle::default(); | ||
text_style.set_color(Color::from_rgb(63, 255, 0)); | ||
text_style.add_shadow(TextShadow::new( | ||
Color::from_rgb(60, 60, 60), | ||
(0.0, 1.0), | ||
1.0, | ||
)); | ||
paragraph_builder.push_style(&text_style); | ||
|
||
// FPS | ||
let mut text_style = TextStyle::default(); | ||
text_style.set_color(Color::from_rgb(63, 255, 0)); | ||
text_style.add_shadow(TextShadow::new( | ||
Color::from_rgb(60, 60, 60), | ||
(0.0, 1.0), | ||
1.0, | ||
)); | ||
text_style.set_font_size(30.0); | ||
paragraph_builder.push_style(&text_style); | ||
paragraph_builder.add_text(format!("{} \n", self.frames.len())); | ||
|
||
// Rendering time | ||
let mut text_style = TextStyle::default(); | ||
text_style.set_color(Color::from_rgb(63, 255, 0)); | ||
text_style.add_shadow(TextShadow::new( | ||
Color::from_rgb(60, 60, 60), | ||
(0.0, 1.0), | ||
1.0, | ||
)); | ||
text_style.set_font_size(18.0); | ||
paragraph_builder.push_style(&text_style); | ||
paragraph_builder.add_text(format!( | ||
"Rendering: {}ms \n", | ||
started_render.elapsed().as_millis() | ||
)); | ||
|
||
// Layout time | ||
let mut text_style = TextStyle::default(); | ||
text_style.set_color(Color::from_rgb(63, 255, 0)); | ||
text_style.add_shadow(TextShadow::new( | ||
Color::from_rgb(60, 60, 60), | ||
(0.0, 1.0), | ||
1.0, | ||
)); | ||
text_style.set_font_size(18.0); | ||
paragraph_builder.push_style(&text_style); | ||
paragraph_builder.add_text(format!("Layout: {}ms \n", finished_layout.as_millis())); | ||
|
||
let mut paragraph = paragraph_builder.build(); | ||
paragraph.layout(f32::MAX); | ||
paragraph.paint(canvas, (5.0, 0.0)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#![cfg_attr( | ||
all(not(debug_assertions), target_os = "windows"), | ||
windows_subsystem = "windows" | ||
)] | ||
|
||
use freya::prelude::*; | ||
|
||
fn main() { | ||
launch_cfg( | ||
app, | ||
LaunchConfig::<()>::builder() | ||
.with_title("Performance Overlay Plugin") | ||
.with_width(700.) | ||
.with_height(500.) | ||
.with_plugin(PerformanceOverlayPlugin::default()) | ||
.build(), | ||
) | ||
} | ||
|
||
const TIME: i32 = 400; | ||
const TARGET: f64 = 650.0; | ||
|
||
fn app(cx: Scope) -> Element { | ||
let animation = use_animation(cx, || 15.0); | ||
|
||
let progress = animation.value(); | ||
|
||
if !animation.is_animating() { | ||
if progress == 15.0 { | ||
animation.start(Animation::new_sine_in_out(15.0..=TARGET, TIME)); | ||
} else if progress == TARGET { | ||
animation.start(Animation::new_sine_in_out(TARGET..=15.0, TIME)); | ||
} | ||
} | ||
|
||
render!( | ||
rect { | ||
width: "100%", | ||
height: "100%", | ||
main_align: "center", | ||
for i in 0..32 { | ||
rsx!( | ||
rect { | ||
offset_x: "{progress - i as f64 * 10.0}", | ||
key: "{i}", | ||
direction: "horizontal", | ||
rect { | ||
height: "25", | ||
width: "45", | ||
background: "rgb(7, 102, 173)", | ||
corner_radius: "100", | ||
} | ||
rect { | ||
height: "25", | ||
width: "45", | ||
background: "rgb(166, 207, 152)", | ||
corner_radius: "100", | ||
} | ||
rect { | ||
height: "25", | ||
width: "45", | ||
background: "rgb(179, 19, 18)", | ||
corner_radius: "100", | ||
} | ||
rect { | ||
height: "25", | ||
width: "45", | ||
background: "rgb(255, 108, 34)", | ||
corner_radius: "100", | ||
} | ||
} | ||
) | ||
} | ||
} | ||
) | ||
} |