|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use bevy::app::ScheduleRunnerPlugin; |
| 4 | +use bevy::color::Color; |
| 5 | +use bevy::diagnostic::DiagnosticsStore; |
| 6 | +use bevy::diagnostic::FrameTimeDiagnosticsPlugin; |
| 7 | +use bevy::ecs::system::RegisteredSystemError; |
| 8 | +use bevy::ecs::system::SystemState; |
| 9 | +use bevy::log::LogPlugin; |
| 10 | +use bevy::prelude::*; |
| 11 | +use bevy::utils::error; |
| 12 | +use bevy::winit::WinitPlugin; |
| 13 | +use bevy_ratatui::RatatuiPlugins; |
| 14 | +use bevy_ratatui::event::KeyEvent; |
| 15 | +use bevy_ratatui::kitty::KittyEnabled; |
| 16 | +use bevy_ratatui::terminal::RatatuiContext; |
| 17 | +use bevy_ratatui_camera::LuminanceConfig; |
| 18 | +use bevy_ratatui_camera::RatatuiCamera; |
| 19 | +use bevy_ratatui_camera::RatatuiCameraEdgeDetection; |
| 20 | +use bevy_ratatui_camera::RatatuiCameraPlugin; |
| 21 | +use bevy_ratatui_camera::RatatuiCameraStrategy; |
| 22 | +use bevy_ratatui_camera::RatatuiCameraWidget; |
| 23 | +use crossterm::event::KeyCode; |
| 24 | +use crossterm::event::KeyEventKind; |
| 25 | +use log::LevelFilter; |
| 26 | + |
| 27 | +mod shared; |
| 28 | + |
| 29 | +fn main() { |
| 30 | + shared::setup_tui_logger(LevelFilter::Info); |
| 31 | + |
| 32 | + App::new() |
| 33 | + .add_plugins(( |
| 34 | + DefaultPlugins |
| 35 | + .build() |
| 36 | + .disable::<WinitPlugin>() |
| 37 | + .disable::<LogPlugin>(), |
| 38 | + ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1. / 60.)), |
| 39 | + FrameTimeDiagnosticsPlugin, |
| 40 | + RatatuiPlugins::default(), |
| 41 | + RatatuiCameraPlugin, |
| 42 | + )) |
| 43 | + .init_resource::<shared::Flags>() |
| 44 | + .init_resource::<shared::InputState>() |
| 45 | + .insert_resource(ClearColor(Color::BLACK)) |
| 46 | + .add_systems(Startup, setup_scene_system) |
| 47 | + .add_systems(Update, draw_scene_system.map(error)) |
| 48 | + .add_systems(PreUpdate, shared::handle_input_system) |
| 49 | + .add_systems(Update, shared::rotate_spinners_system) |
| 50 | + .add_systems(Update, handle_input_system.map(error)) |
| 51 | + .run(); |
| 52 | +} |
| 53 | + |
| 54 | +fn setup_scene_system( |
| 55 | + mut commands: Commands, |
| 56 | + mut meshes: ResMut<Assets<Mesh>>, |
| 57 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 58 | +) { |
| 59 | + shared::spawn_3d_scene(&mut commands, &mut meshes, &mut materials); |
| 60 | + |
| 61 | + commands.spawn(( |
| 62 | + RatatuiCamera::default(), |
| 63 | + Camera3d::default(), |
| 64 | + Transform::from_xyz(2.5, 2.5, 2.5).looking_at(Vec3::ZERO, Vec3::Z), |
| 65 | + )); |
| 66 | +} |
| 67 | + |
| 68 | +fn draw_scene_system( |
| 69 | + mut commands: Commands, |
| 70 | + mut ratatui: ResMut<RatatuiContext>, |
| 71 | + camera_widget: Query<&RatatuiCameraWidget>, |
| 72 | + flags: Res<shared::Flags>, |
| 73 | + diagnostics: Res<DiagnosticsStore>, |
| 74 | + kitty_enabled: Option<Res<KittyEnabled>>, |
| 75 | +) -> std::io::Result<()> { |
| 76 | + ratatui.draw(|frame| { |
| 77 | + let area = shared::debug_frame(frame, &flags, &diagnostics, kitty_enabled.as_deref()); |
| 78 | + |
| 79 | + camera_widget |
| 80 | + .single() |
| 81 | + .render_autoresize(area, frame.buffer_mut(), &mut commands); |
| 82 | + })?; |
| 83 | + |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | + |
| 87 | +#[derive(Resource, Default, Clone)] |
| 88 | +pub enum CameraState { |
| 89 | + #[default] |
| 90 | + Start, |
| 91 | + SwitchedStrategy, |
| 92 | + AddedEdges, |
| 93 | + ChangedCharacters, |
| 94 | + ChangedEdgeColor, |
| 95 | +} |
| 96 | + |
| 97 | +pub fn handle_input_system( |
| 98 | + world: &mut World, |
| 99 | + system_state: &mut SystemState<EventReader<KeyEvent>>, |
| 100 | + mut camera_state: Local<CameraState>, |
| 101 | +) -> Result<(), RegisteredSystemError> { |
| 102 | + let mut event_reader = system_state.get_mut(world); |
| 103 | + let events: Vec<_> = event_reader.read().cloned().collect(); |
| 104 | + |
| 105 | + for key_event in events.iter() { |
| 106 | + if let KeyEventKind::Press = key_event.kind { |
| 107 | + if let KeyCode::Char(' ') = key_event.code { |
| 108 | + match *camera_state { |
| 109 | + CameraState::Start => { |
| 110 | + world.run_system_cached(toggle_ratatui_camera_strategy)?; |
| 111 | + *camera_state = CameraState::SwitchedStrategy; |
| 112 | + } |
| 113 | + CameraState::SwitchedStrategy => { |
| 114 | + world.run_system_cached(toggle_edge_detection_system)?; |
| 115 | + *camera_state = CameraState::AddedEdges; |
| 116 | + } |
| 117 | + CameraState::AddedEdges => { |
| 118 | + world.run_system_cached(modify_ratatui_camera_strategy)?; |
| 119 | + *camera_state = CameraState::ChangedCharacters; |
| 120 | + } |
| 121 | + CameraState::ChangedCharacters => { |
| 122 | + world.run_system_cached(modify_edge_detection_system)?; |
| 123 | + *camera_state = CameraState::ChangedEdgeColor; |
| 124 | + } |
| 125 | + CameraState::ChangedEdgeColor => { |
| 126 | + world.run_system_cached(toggle_edge_detection_system)?; |
| 127 | + world.run_system_cached(toggle_ratatui_camera_strategy)?; |
| 128 | + *camera_state = CameraState::Start; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + Ok(()) |
| 136 | +} |
| 137 | + |
| 138 | +fn toggle_edge_detection_system( |
| 139 | + mut commands: Commands, |
| 140 | + ratatui_camera: Query<(Entity, Option<&mut RatatuiCameraEdgeDetection>), With<RatatuiCamera>>, |
| 141 | +) { |
| 142 | + let (entity, edge_detection) = ratatui_camera.single(); |
| 143 | + if edge_detection.is_some() { |
| 144 | + commands |
| 145 | + .entity(entity) |
| 146 | + .remove::<RatatuiCameraEdgeDetection>(); |
| 147 | + } else { |
| 148 | + commands |
| 149 | + .entity(entity) |
| 150 | + .insert(RatatuiCameraEdgeDetection::default()); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +fn modify_edge_detection_system( |
| 155 | + mut ratatui_camera_edge_detection: Query< |
| 156 | + Option<&mut RatatuiCameraEdgeDetection>, |
| 157 | + With<RatatuiCamera>, |
| 158 | + >, |
| 159 | +) { |
| 160 | + if let Some(mut c) = ratatui_camera_edge_detection.single_mut() { |
| 161 | + c.edge_color = Some(ratatui::style::Color::Magenta); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +fn modify_ratatui_camera_strategy(mut ratatui_camera_strategy: Query<&mut RatatuiCameraStrategy>) { |
| 166 | + let RatatuiCameraStrategy::Luminance(ref mut luminance_config) = |
| 167 | + *ratatui_camera_strategy.single_mut() |
| 168 | + else { |
| 169 | + return; |
| 170 | + }; |
| 171 | + |
| 172 | + luminance_config.luminance_characters = vec!['.', 'o', 'O', '0']; |
| 173 | +} |
| 174 | + |
| 175 | +fn toggle_ratatui_camera_strategy( |
| 176 | + mut commands: Commands, |
| 177 | + mut ratatui_camera: Query<(Entity, &RatatuiCameraStrategy)>, |
| 178 | +) { |
| 179 | + let (entity, strategy) = ratatui_camera.single_mut(); |
| 180 | + commands.entity(entity).insert(match strategy { |
| 181 | + RatatuiCameraStrategy::HalfBlocks => { |
| 182 | + RatatuiCameraStrategy::Luminance(LuminanceConfig::default()) |
| 183 | + } |
| 184 | + RatatuiCameraStrategy::Luminance(_) => RatatuiCameraStrategy::HalfBlocks, |
| 185 | + RatatuiCameraStrategy::None => RatatuiCameraStrategy::None, |
| 186 | + }); |
| 187 | +} |
0 commit comments