Added plotters example capture + animate_keypress

This commit is contained in:
Jonathan Johnson 2024-05-12 14:23:29 -07:00
parent cf78c9defa
commit 568f2ca327
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
3 changed files with 89 additions and 3 deletions

View file

@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- markdownlint-disable no-duplicate-heading -->
## Unreleased
### Added
- `AnimationRecorder::animate_keypress` is a new helper that animates a single
key press.
## v0.3.0 (2024-05-12)
### Breaking Changes

View file

@ -1,8 +1,11 @@
use std::time::Duration;
use cushy::value::{Dynamic, Source};
use cushy::widget::MakeWidget;
use cushy::widgets::slider::Slidable;
use cushy::widgets::Canvas;
use cushy::Run;
use kludgine::app::winit::keyboard::{Key, NamedKey};
use plotters::prelude::*;
// This is copied from the sierpinski.rs example in the plotters repository.
@ -30,9 +33,8 @@ where
Ok(())
}
fn main() -> cushy::Result<()> {
fn plotters() -> impl MakeWidget {
let depth = Dynamic::new(1);
"Depth"
.and(depth.clone().slider_between(1, 5))
.and(
@ -45,5 +47,51 @@ fn main() -> cushy::Result<()> {
.expand(),
)
.into_rows()
.run()
}
fn main() -> cushy::Result<()> {
plotters().run()
}
#[test]
fn runs() {
cushy::example!(plotters).animated(|r| {
r.wait_for(Duration::from_millis(500)).unwrap();
r.animate_keypress(
kludgine::app::winit::keyboard::PhysicalKey::Code(
kludgine::app::winit::keyboard::KeyCode::ArrowRight,
),
Key::Named(NamedKey::ArrowRight),
None,
Duration::from_millis(250),
)
.unwrap();
r.animate_keypress(
kludgine::app::winit::keyboard::PhysicalKey::Code(
kludgine::app::winit::keyboard::KeyCode::ArrowRight,
),
Key::Named(NamedKey::ArrowRight),
None,
Duration::from_millis(250),
)
.unwrap();
r.animate_keypress(
kludgine::app::winit::keyboard::PhysicalKey::Code(
kludgine::app::winit::keyboard::KeyCode::ArrowRight,
),
Key::Named(NamedKey::ArrowRight),
None,
Duration::from_millis(250),
)
.unwrap();
r.animate_keypress(
kludgine::app::winit::keyboard::PhysicalKey::Code(
kludgine::app::winit::keyboard::KeyCode::ArrowRight,
),
Key::Named(NamedKey::ArrowRight),
None,
Duration::from_secs(1),
)
.unwrap();
});
}

View file

@ -2581,6 +2581,7 @@ impl CushyWindowBuilder {
#[must_use]
pub fn finish_virtual(self, device: &wgpu::Device, queue: &wgpu::Queue) -> VirtualWindow {
let mut state = VirtualState::new();
state.size = self.initial_size;
let mut cushy = self.finish(&mut state, device, queue);
cushy.set_focused(true);
@ -3499,6 +3500,36 @@ where
self.wait_for(over)
}
/// Simulates a key down and key up event with the given information.
pub fn animate_keypress(
&mut self,
physical_key: PhysicalKey,
logical_key: Key,
text: Option<&str>,
duration: Duration,
) -> Result<(), VirtualRecorderError> {
let text = text.map(SmolStr::new);
let half_duration = duration / 2;
let mut event = KeyEvent {
physical_key,
logical_key,
text,
state: ElementState::Pressed,
repeat: false,
location: KeyLocation::Standard,
};
self.recorder
.window
.keyboard_input(DeviceId::Virtual(0), event.clone(), true);
self.wait_for(half_duration)?;
event.state = ElementState::Released;
self.recorder
.window
.keyboard_input(DeviceId::Virtual(0), event.clone(), true);
self.wait_for(half_duration)
}
/// Animates entering the graphemes from `text` over `duration`.
pub fn animate_text_input(
&mut self,