Skip to content

Commit ec606d2

Browse files
committed
reworked autoresize to work with render area
1 parent 19d71dc commit ec606d2

14 files changed

+129
-160
lines changed

README.md

+13-14
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn draw_scene_system(
6363
camera_widget: Query<&RatatuiCameraWidget>,
6464
) -> std::io::Result<()> {
6565
ratatui.draw(|frame| {
66-
frame.render_widget(camera_widget.single(), frame.area());
66+
camera_widget.single().render(frame.area(), frame.buffer_mut());
6767
})?;
6868

6969
Ok(())
@@ -97,21 +97,20 @@ commands.spawn((
9797
## autoresize
9898

9999
By default, the size of the texture the camera renders to will stay constant,
100-
and when rendered to the ratatui buffer it will retain its aspect ratio. If you
101-
set the `autoresize` attribute to true, the render texture will be resized to
102-
fit the terminal window, including its aspect ratio.
103-
104-
You can also supply an optional `autoresize_function` that converts the
105-
terminal dimensions to the dimensions that will be used for resizing. This is
106-
useful for situations when you want to maintain a specific aspect ratio or
107-
resize to some fraction of the terminal window.
100+
and when rendered to the ratatui buffer with `RatatuiCameraWidget::render(...)`
101+
it will retain its aspect ratio. If you use the
102+
`RatatuiCameraWidget::render_autoresize` variant instead, whenever the render
103+
texture doesn't match the size of the render area, rendering will be skipped
104+
that frame and a resize of the render texture will be triggered instead.
108105

109106
```rust
110-
RatatuiCamera {
111-
autoresize: true,
112-
autoresize_fn: |(w, h)| (w * 4, h * 3),
113-
..default()
114-
}
107+
ratatui.draw(|frame| {
108+
camera_widget.single().render_autoresize(
109+
frame.area(),
110+
frame.buffer_mut(),
111+
&mut commands,
112+
);
113+
})?;
115114
```
116115

117116
## edge detection

examples/2d_camera.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,26 @@ fn setup_scene_system(
5050
shared::spawn_2d_scene(&mut commands, &mut meshes, &mut materials);
5151

5252
commands.spawn((
53-
RatatuiCamera::autoresize(),
53+
RatatuiCamera::default(),
5454
RatatuiCameraStrategy::Luminance(LuminanceConfig::default()),
5555
Camera2d,
5656
));
5757
}
5858

5959
pub fn draw_scene_system(
60+
mut commands: Commands,
6061
mut ratatui: ResMut<RatatuiContext>,
61-
ratatui_camera_widget: Query<&RatatuiCameraWidget>,
62+
camera_widget: Query<&RatatuiCameraWidget>,
6263
flags: Res<shared::Flags>,
6364
diagnostics: Res<DiagnosticsStore>,
6465
kitty_enabled: Option<Res<KittyEnabled>>,
6566
) -> std::io::Result<()> {
6667
ratatui.draw(|frame| {
6768
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
6869

69-
if let Ok(camera_widget) = ratatui_camera_widget.get_single() {
70-
frame.render_widget(camera_widget, area);
71-
}
70+
camera_widget
71+
.single()
72+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
7273
})?;
7374

7475
Ok(())

examples/3d_camera.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use bevy_ratatui_camera::RatatuiCamera;
1414
use bevy_ratatui_camera::RatatuiCameraPlugin;
1515
use bevy_ratatui_camera::RatatuiCameraWidget;
1616
use log::LevelFilter;
17+
use ratatui::widgets::Widget;
1718

1819
mod shared;
1920

@@ -57,17 +58,15 @@ fn setup_scene_system(
5758

5859
pub fn draw_scene_system(
5960
mut ratatui: ResMut<RatatuiContext>,
60-
ratatui_camera_widget: Query<&RatatuiCameraWidget>,
61+
camera_widget: Query<&RatatuiCameraWidget>,
6162
flags: Res<shared::Flags>,
6263
diagnostics: Res<DiagnosticsStore>,
6364
kitty_enabled: Option<Res<KittyEnabled>>,
6465
) -> std::io::Result<()> {
6566
ratatui.draw(|frame| {
6667
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
6768

68-
if let Ok(camera_widget) = ratatui_camera_widget.get_single() {
69-
frame.render_widget(camera_widget, area);
70-
}
69+
camera_widget.single().render(area, frame.buffer_mut());
7170
})?;
7271

7372
Ok(())

examples/edge_detection.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn setup_scene_system(
5252
shared::spawn_3d_scene(&mut commands, &mut meshes, &mut materials);
5353

5454
commands.spawn((
55-
RatatuiCamera::autoresize(),
55+
RatatuiCamera::default(),
5656
RatatuiCameraStrategy::None,
5757
RatatuiCameraEdgeDetection::default(),
5858
Camera3d::default(),
@@ -61,18 +61,19 @@ fn setup_scene_system(
6161
}
6262

6363
pub fn draw_scene_system(
64+
mut commands: Commands,
6465
mut ratatui: ResMut<RatatuiContext>,
65-
ratatui_camera_widget: Query<&RatatuiCameraWidget>,
66+
camera_widget: Query<&RatatuiCameraWidget>,
6667
flags: Res<shared::Flags>,
6768
diagnostics: Res<DiagnosticsStore>,
6869
kitty_enabled: Option<Res<KittyEnabled>>,
6970
) -> std::io::Result<()> {
7071
ratatui.draw(|frame| {
7172
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
7273

73-
if let Ok(camera_widget) = ratatui_camera_widget.get_single() {
74-
frame.render_widget(camera_widget, area);
75-
}
74+
camera_widget
75+
.single()
76+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
7677
})?;
7778

7879
Ok(())

examples/luminance.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use bevy::winit::WinitPlugin;
1111
use bevy_ratatui::RatatuiPlugins;
1212
use bevy_ratatui::kitty::KittyEnabled;
1313
use bevy_ratatui::terminal::RatatuiContext;
14-
use bevy_ratatui_camera::LuminanceConfig;
1514
use bevy_ratatui_camera::RatatuiCamera;
1615
use bevy_ratatui_camera::RatatuiCameraPlugin;
1716
use bevy_ratatui_camera::RatatuiCameraStrategy;
@@ -52,29 +51,27 @@ fn setup_scene_system(
5251
shared::spawn_3d_scene(&mut commands, &mut meshes, &mut materials);
5352

5453
commands.spawn((
55-
RatatuiCamera::autoresize(),
56-
RatatuiCameraStrategy::Luminance(LuminanceConfig {
57-
luminance_scale: 11.0,
58-
..default()
59-
}),
54+
RatatuiCamera::default(),
55+
RatatuiCameraStrategy::luminance_misc(),
6056
Camera3d::default(),
6157
Transform::from_xyz(2.5, 2.5, 2.5).looking_at(Vec3::ZERO, Vec3::Z),
6258
));
6359
}
6460

6561
pub fn draw_scene_system(
62+
mut commands: Commands,
6663
mut ratatui: ResMut<RatatuiContext>,
67-
ratatui_camera_widget: Query<&RatatuiCameraWidget>,
64+
camera_widget: Query<&RatatuiCameraWidget>,
6865
flags: Res<shared::Flags>,
6966
diagnostics: Res<DiagnosticsStore>,
7067
kitty_enabled: Option<Res<KittyEnabled>>,
7168
) -> std::io::Result<()> {
7269
ratatui.draw(|frame| {
7370
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
7471

75-
if let Ok(camera_widget) = ratatui_camera_widget.get_single() {
76-
frame.render_widget(camera_widget, area);
77-
}
72+
camera_widget
73+
.single()
74+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
7875
})?;
7976

8077
Ok(())

examples/masking.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ fn setup_scene_system(
8080
}
8181

8282
pub fn draw_scene_system(
83+
mut commands: Commands,
8384
mut ratatui: ResMut<RatatuiContext>,
8485
foreground_widget: Query<&RatatuiCameraWidget, With<Foreground>>,
8586
background_widget: Query<&RatatuiCameraWidget, With<Background>>,
@@ -90,8 +91,12 @@ pub fn draw_scene_system(
9091
ratatui.draw(|frame| {
9192
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
9293

93-
frame.render_widget(background_widget.single(), area);
94-
frame.render_widget(foreground_widget.single(), area);
94+
background_widget
95+
.single()
96+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
97+
foreground_widget
98+
.single()
99+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
95100
})?;
96101

97102
Ok(())

examples/multiple.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use bevy::winit::WinitPlugin;
1010
use bevy_ratatui::RatatuiPlugins;
1111
use bevy_ratatui::kitty::KittyEnabled;
1212
use bevy_ratatui::terminal::RatatuiContext;
13-
use bevy_ratatui_camera::LuminanceConfig;
1413
use bevy_ratatui_camera::RatatuiCamera;
1514
use bevy_ratatui_camera::RatatuiCameraPlugin;
1615
use bevy_ratatui_camera::RatatuiCameraStrategy;
@@ -55,7 +54,7 @@ fn setup_scene_system(
5554

5655
commands.spawn((
5756
RatatuiCamera::default(),
58-
RatatuiCameraStrategy::Luminance(LuminanceConfig::default()),
57+
RatatuiCameraStrategy::luminance_misc(),
5958
Camera3d::default(),
6059
Transform::from_xyz(0., 3., 0.).looking_at(Vec3::ZERO, Vec3::Z),
6160
));
@@ -66,26 +65,24 @@ fn setup_scene_system(
6665
));
6766
commands.spawn((
6867
RatatuiCamera::default(),
69-
RatatuiCameraStrategy::Luminance(LuminanceConfig::default()),
68+
RatatuiCameraStrategy::luminance_braille(),
7069
Camera3d::default(),
7170
Transform::from_xyz(2., 2., 2.).looking_at(Vec3::ZERO, Vec3::Z),
7271
));
7372
}
7473

7574
pub fn draw_scene_system(
75+
mut commands: Commands,
7676
mut ratatui: ResMut<RatatuiContext>,
77-
ratatui_camera_widgets: Query<&RatatuiCameraWidget>,
77+
camera_widgets: Query<&RatatuiCameraWidget>,
7878
flags: Res<shared::Flags>,
7979
diagnostics: Res<DiagnosticsStore>,
8080
kitty_enabled: Option<Res<KittyEnabled>>,
8181
) -> std::io::Result<()> {
8282
ratatui.draw(|frame| {
8383
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
8484

85-
let widgets = ratatui_camera_widgets
86-
.iter()
87-
.enumerate()
88-
.collect::<Vec<_>>();
85+
let widgets = camera_widgets.iter().enumerate().collect::<Vec<_>>();
8986

9087
let layout = Layout::new(
9188
Direction::Horizontal,
@@ -94,7 +91,7 @@ pub fn draw_scene_system(
9491
.split(area);
9592

9693
for (i, widget) in widgets {
97-
frame.render_widget(widget, layout[i]);
94+
widget.render_autoresize(layout[i], frame.buffer_mut(), &mut commands);
9895
}
9996
})?;
10097

examples/orthographic.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn setup_scene_system(
5252
shared::spawn_3d_scene(&mut commands, &mut meshes, &mut materials);
5353

5454
commands.spawn((
55-
RatatuiCamera::autoresize(),
55+
RatatuiCamera::default(),
5656
RatatuiCameraStrategy::Luminance(LuminanceConfig::default()),
5757
Camera3d::default(),
5858
Transform::from_xyz(2.5, 2.5, 2.5).looking_at(Vec3::ZERO, Vec3::Z),
@@ -64,18 +64,19 @@ fn setup_scene_system(
6464
}
6565

6666
pub fn draw_scene_system(
67+
mut commands: Commands,
6768
mut ratatui: ResMut<RatatuiContext>,
68-
ratatui_camera_widget: Query<&RatatuiCameraWidget>,
69+
camera_widget: Query<&RatatuiCameraWidget>,
6970
flags: Res<shared::Flags>,
7071
diagnostics: Res<DiagnosticsStore>,
7172
kitty_enabled: Option<Res<KittyEnabled>>,
7273
) -> std::io::Result<()> {
7374
ratatui.draw(|frame| {
7475
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
7576

76-
if let Ok(camera_widget) = ratatui_camera_widget.get_single() {
77-
frame.render_widget(camera_widget, area);
78-
}
77+
camera_widget
78+
.single()
79+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
7980
})?;
8081

8182
Ok(())

examples/shared/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn debug_frame(
8181
) -> Rect {
8282
let mut block = Block::bordered()
8383
.bg(ratatui::style::Color::Rgb(0, 0, 0))
84-
.border_style(Style::default().bg(ratatui::style::Color::Rgb(0, 0, 0)))
84+
.border_style(Style::default().bg(ratatui::style::Color::Black))
8585
.title_bottom("[q for quit]")
8686
.title_bottom("[d for debug]")
8787
.title_bottom("[p for panic]")

examples/subcamera.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ use bevy_ratatui_camera::RatatuiCameraPlugin;
1515
use bevy_ratatui_camera::RatatuiCameraWidget;
1616
use bevy_ratatui_camera::RatatuiSubcamera;
1717
use log::LevelFilter;
18-
use ratatui::layout::Constraint;
19-
use ratatui::layout::Direction;
20-
use ratatui::layout::Layout;
2118

2219
mod shared;
2320

@@ -54,7 +51,7 @@ fn setup_scene_system(
5451

5552
let main_camera_id = commands
5653
.spawn((
57-
RatatuiCamera::autoresize(),
54+
RatatuiCamera::default(),
5855
Camera3d::default(),
5956
Transform::from_xyz(2., 1., 1.).looking_at(Vec3::Y, Vec3::Z),
6057
))
@@ -68,29 +65,19 @@ fn setup_scene_system(
6865
}
6966

7067
pub fn draw_scene_system(
68+
mut commands: Commands,
7169
mut ratatui: ResMut<RatatuiContext>,
72-
ratatui_camera_widgets: Query<&RatatuiCameraWidget>,
70+
camera_widget: Query<&RatatuiCameraWidget>,
7371
flags: Res<shared::Flags>,
7472
diagnostics: Res<DiagnosticsStore>,
7573
kitty_enabled: Option<Res<KittyEnabled>>,
7674
) -> std::io::Result<()> {
7775
ratatui.draw(|frame| {
7876
let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref());
7977

80-
let widgets = ratatui_camera_widgets
81-
.iter()
82-
.enumerate()
83-
.collect::<Vec<_>>();
84-
85-
let layout = Layout::new(
86-
Direction::Horizontal,
87-
vec![Constraint::Fill(1); widgets.len()],
88-
)
89-
.split(area);
90-
91-
for (i, widget) in widgets {
92-
frame.render_widget(widget, layout[i]);
93-
}
78+
camera_widget
79+
.single()
80+
.render_autoresize(area, frame.buffer_mut(), &mut commands);
9481
})?;
9582

9683
Ok(())

0 commit comments

Comments
 (0)