pub struct Window { /* private fields */ }
Expand description
Simulator window
Implementations§
Source§impl Window
impl Window
Sourcepub fn new(title: &str, output_settings: &OutputSettings) -> Self
pub fn new(title: &str, output_settings: &OutputSettings) -> Self
Creates a new simulator window.
Examples found in repository?
examples/themes.rs (line 38)
11fn main() {
12 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
13
14 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
15 let centered = TextStyleBuilder::new()
16 .baseline(Baseline::Middle)
17 .alignment(Alignment::Center)
18 .build();
19
20 Text::with_text_style(
21 "embedded-graphics",
22 display.bounding_box().center(),
23 large_text,
24 centered,
25 )
26 .draw(&mut display)
27 .unwrap();
28
29 // Uncomment one of the `theme` lines to use a different theme.
30 let output_settings = OutputSettingsBuilder::new()
31 //.theme(BinaryColorTheme::LcdGreen)
32 //.theme(BinaryColorTheme::LcdWhite)
33 .theme(BinaryColorTheme::LcdBlue)
34 //.theme(BinaryColorTheme::OledBlue)
35 //.theme(BinaryColorTheme::OledWhite)
36 .build();
37
38 let mut window = Window::new("Themes", &output_settings);
39 window.show_static(&display);
40}
More examples
examples/input-handling.rs (line 44)
42fn main() -> Result<(), core::convert::Infallible> {
43 let mut display: SimulatorDisplay<Rgb888> = SimulatorDisplay::new(Size::new(800, 480));
44 let mut window = Window::new("Click to move circle", &OutputSettings::default());
45
46 let mut position = Point::new(200, 200);
47 Circle::with_center(position, 200)
48 .into_styled(PrimitiveStyle::with_fill(FOREGROUND_COLOR))
49 .draw(&mut display)?;
50
51 'running: loop {
52 window.update(&display);
53
54 for event in window.events() {
55 match event {
56 SimulatorEvent::Quit => break 'running,
57 SimulatorEvent::KeyDown { keycode, .. } => {
58 let delta = match keycode {
59 Keycode::Left => Point::new(-KEYBOARD_DELTA, 0),
60 Keycode::Right => Point::new(KEYBOARD_DELTA, 0),
61 Keycode::Up => Point::new(0, -KEYBOARD_DELTA),
62 Keycode::Down => Point::new(0, KEYBOARD_DELTA),
63 _ => Point::zero(),
64 };
65 let new_position = position + delta;
66 move_circle(&mut display, position, new_position)?;
67 position = new_position;
68 }
69 SimulatorEvent::MouseButtonUp { point, .. } => {
70 move_circle(&mut display, position, point)?;
71 position = point;
72 }
73 _ => {}
74 }
75 }
76 }
77
78 Ok(())
79}
examples/sdl-audio.rs (line 62)
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
Sourcepub fn update<C>(&mut self, display: &SimulatorDisplay<C>)
pub fn update<C>(&mut self, display: &SimulatorDisplay<C>)
Updates the window.
Examples found in repository?
examples/input-handling.rs (line 52)
42fn main() -> Result<(), core::convert::Infallible> {
43 let mut display: SimulatorDisplay<Rgb888> = SimulatorDisplay::new(Size::new(800, 480));
44 let mut window = Window::new("Click to move circle", &OutputSettings::default());
45
46 let mut position = Point::new(200, 200);
47 Circle::with_center(position, 200)
48 .into_styled(PrimitiveStyle::with_fill(FOREGROUND_COLOR))
49 .draw(&mut display)?;
50
51 'running: loop {
52 window.update(&display);
53
54 for event in window.events() {
55 match event {
56 SimulatorEvent::Quit => break 'running,
57 SimulatorEvent::KeyDown { keycode, .. } => {
58 let delta = match keycode {
59 Keycode::Left => Point::new(-KEYBOARD_DELTA, 0),
60 Keycode::Right => Point::new(KEYBOARD_DELTA, 0),
61 Keycode::Up => Point::new(0, -KEYBOARD_DELTA),
62 Keycode::Down => Point::new(0, KEYBOARD_DELTA),
63 _ => Point::zero(),
64 };
65 let new_position = position + delta;
66 move_circle(&mut display, position, new_position)?;
67 position = new_position;
68 }
69 SimulatorEvent::MouseButtonUp { point, .. } => {
70 move_circle(&mut display, position, point)?;
71 position = point;
72 }
73 _ => {}
74 }
75 }
76 }
77
78 Ok(())
79}
More examples
examples/sdl-audio.rs (line 71)
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
Sourcepub fn show_static<C>(&mut self, display: &SimulatorDisplay<C>)
pub fn show_static<C>(&mut self, display: &SimulatorDisplay<C>)
Shows a static display.
This methods updates the window once and loops until the simulator window is closed.
Examples found in repository?
examples/themes.rs (line 39)
11fn main() {
12 let mut display = SimulatorDisplay::<BinaryColor>::new(Size::new(256, 64));
13
14 let large_text = MonoTextStyle::new(&FONT_10X20, BinaryColor::On);
15 let centered = TextStyleBuilder::new()
16 .baseline(Baseline::Middle)
17 .alignment(Alignment::Center)
18 .build();
19
20 Text::with_text_style(
21 "embedded-graphics",
22 display.bounding_box().center(),
23 large_text,
24 centered,
25 )
26 .draw(&mut display)
27 .unwrap();
28
29 // Uncomment one of the `theme` lines to use a different theme.
30 let output_settings = OutputSettingsBuilder::new()
31 //.theme(BinaryColorTheme::LcdGreen)
32 //.theme(BinaryColorTheme::LcdWhite)
33 .theme(BinaryColorTheme::LcdBlue)
34 //.theme(BinaryColorTheme::OledBlue)
35 //.theme(BinaryColorTheme::OledWhite)
36 .build();
37
38 let mut window = Window::new("Themes", &output_settings);
39 window.show_static(&display);
40}
Sourcepub fn events(&self) -> SimulatorEventsIter<'_> ⓘ
pub fn events(&self) -> SimulatorEventsIter<'_> ⓘ
Returns an iterator of all captured simulator events.
§Panics
Panics if called before update
is called at least
once. Also panics if multiple instances of the iterator are used at the
same time.
Examples found in repository?
examples/input-handling.rs (line 54)
42fn main() -> Result<(), core::convert::Infallible> {
43 let mut display: SimulatorDisplay<Rgb888> = SimulatorDisplay::new(Size::new(800, 480));
44 let mut window = Window::new("Click to move circle", &OutputSettings::default());
45
46 let mut position = Point::new(200, 200);
47 Circle::with_center(position, 200)
48 .into_styled(PrimitiveStyle::with_fill(FOREGROUND_COLOR))
49 .draw(&mut display)?;
50
51 'running: loop {
52 window.update(&display);
53
54 for event in window.events() {
55 match event {
56 SimulatorEvent::Quit => break 'running,
57 SimulatorEvent::KeyDown { keycode, .. } => {
58 let delta = match keycode {
59 Keycode::Left => Point::new(-KEYBOARD_DELTA, 0),
60 Keycode::Right => Point::new(KEYBOARD_DELTA, 0),
61 Keycode::Up => Point::new(0, -KEYBOARD_DELTA),
62 Keycode::Down => Point::new(0, KEYBOARD_DELTA),
63 _ => Point::zero(),
64 };
65 let new_position = position + delta;
66 move_circle(&mut display, position, new_position)?;
67 position = new_position;
68 }
69 SimulatorEvent::MouseButtonUp { point, .. } => {
70 move_circle(&mut display, position, point)?;
71 position = point;
72 }
73 _ => {}
74 }
75 }
76 }
77
78 Ok(())
79}
More examples
examples/sdl-audio.rs (line 73)
31fn main() -> Result<(), core::convert::Infallible> {
32 // Prepare the audio "engine" with gate control
33 let gate = Arc::new(AtomicBool::new(false));
34 let audio_wrapper = AudioWrapper::new(gate.clone());
35
36 let audio_spec = AudioSpecDesired {
37 freq: Some(SAMPLE_RATE),
38 channels: Some(1),
39 samples: Some(32),
40 };
41
42 // Initialize the SDL audio subsystem.
43 //
44 // `sdl2` allows multiple instances of the SDL context to exist, which makes
45 // it possible to access SDL subsystems which aren't used by the simulator.
46 // But keep in mind that only one `EventPump` can exists and the simulator
47 // window creation will fail if the `EventPump` is claimed in advance.
48 let sdl = sdl2::init().unwrap();
49 let audio_subsystem = sdl.audio().unwrap();
50
51 // Start audio playback by opening the device and setting the custom callback.
52 let audio_device = audio_subsystem
53 .open_playback(None, &audio_spec, |_| audio_wrapper)
54 .unwrap();
55 audio_device.resume();
56
57 let output_settings = OutputSettingsBuilder::new()
58 .scale(4)
59 .theme(embedded_graphics_simulator::BinaryColorTheme::OledWhite)
60 .build();
61
62 let mut window = Window::new("Simulator audio example", &output_settings);
63
64 let text_style = MonoTextStyle::new(&FONT_6X10, BinaryColor::On);
65 let text_position = Point::new(25, 30);
66 let text = Text::new("Press space...", text_position, text_style);
67
68 let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));
69 text.draw(&mut display).unwrap();
70 'running: loop {
71 window.update(&display);
72
73 for event in window.events() {
74 match event {
75 SimulatorEvent::Quit => break 'running,
76 SimulatorEvent::KeyDown {
77 keycode, repeat, ..
78 } if keycode == Keycode::Space && !repeat => {
79 gate.store(true, Ordering::SeqCst);
80 display.clear(BinaryColor::On).unwrap();
81 }
82 SimulatorEvent::KeyUp { keycode, .. } => match keycode {
83 Keycode::Space => {
84 gate.store(false, Ordering::SeqCst);
85 display.clear(BinaryColor::Off).unwrap();
86 text.draw(&mut display).unwrap();
87 }
88 _ => {}
89 },
90 _ => {}
91 }
92 }
93 }
94
95 Ok(())
96}
Sourcepub fn set_max_fps(&mut self, max_fps: u32)
pub fn set_max_fps(&mut self, max_fps: u32)
Sets the FPS limit of the window.
Auto Trait Implementations§
impl !Freeze for Window
impl !RefUnwindSafe for Window
impl !Send for Window
impl !Sync for Window
impl Unpin for Window
impl UnwindSafe for Window
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.