| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use bevy::{ |
| 10 |
app::AppExit, |
| 11 |
input::common_conditions::{input_just_pressed, input_just_released}, |
| 12 |
prelude::*, |
| 13 |
window::{CursorOptions, PrimaryWindow, WindowLevel}, |
| 14 |
}; |
| 15 |
|
| 16 |
#[cfg(target_os = "macos")] |
| 17 |
use bevy::window::CompositeAlphaMode; |
| 18 |
|
| 19 |
fn main() { |
| 20 |
App::new() |
| 21 |
.add_plugins(DefaultPlugins.set(WindowPlugin { |
| 22 |
primary_window: Some(Window { |
| 23 |
title: "Bevy Desk Toy".into(), |
| 24 |
transparent: true, |
| 25 |
#[cfg(target_os = "macos")] |
| 26 |
composite_alpha_mode: CompositeAlphaMode::PostMultiplied, |
| 27 |
..default() |
| 28 |
}), |
| 29 |
..default() |
| 30 |
})) |
| 31 |
.insert_resource(ClearColor(WINDOW_CLEAR_COLOR)) |
| 32 |
.insert_resource(WindowTransparency(false)) |
| 33 |
.insert_resource(CursorWorldPos(None)) |
| 34 |
.add_systems(Startup, setup) |
| 35 |
.add_systems( |
| 36 |
Update, |
| 37 |
( |
| 38 |
get_cursor_world_pos, |
| 39 |
update_cursor_hit_test, |
| 40 |
( |
| 41 |
start_drag.run_if(input_just_pressed(MouseButton::Left)), |
| 42 |
end_drag.run_if(input_just_released(MouseButton::Left)), |
| 43 |
drag.run_if(resource_exists::<DragOperation>), |
| 44 |
quit.run_if(input_just_pressed(MouseButton::Right)), |
| 45 |
toggle_transparency.run_if(input_just_pressed(KeyCode::Space)), |
| 46 |
move_pupils.after(drag), |
| 47 |
), |
| 48 |
) |
| 49 |
.chain(), |
| 50 |
) |
| 51 |
.run(); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
#[derive(Resource)] |
| 56 |
struct WindowTransparency(bool); |
| 57 |
|
| 58 |
|
| 59 |
#[derive(Resource)] |
| 60 |
struct CursorWorldPos(Option<Vec2>); |
| 61 |
|
| 62 |
|
| 63 |
#[derive(Resource)] |
| 64 |
struct DragOperation(Vec2); |
| 65 |
|
| 66 |
|
| 67 |
#[derive(Component)] |
| 68 |
struct InstructionsText; |
| 69 |
|
| 70 |
|
| 71 |
#[derive(Component)] |
| 72 |
struct BevyLogo; |
| 73 |
|
| 74 |
|
| 75 |
#[derive(Component)] |
| 76 |
struct Pupil { |
| 77 |
|
| 78 |
eye_radius: f32, |
| 79 |
|
| 80 |
pupil_radius: f32, |
| 81 |
|
| 82 |
velocity: Vec2, |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
const BEVY_LOGO_RADIUS: f32 = 128.0; |
| 88 |
|
| 89 |
|
| 90 |
const BIRDS_EYES: [(f32, f32, f32); 3] = [ |
| 91 |
(145.0 - 128.0, -(56.0 - 128.0), 12.0), |
| 92 |
(198.0 - 128.0, -(87.0 - 128.0), 10.0), |
| 93 |
(222.0 - 128.0, -(140.0 - 128.0), 8.0), |
| 94 |
]; |
| 95 |
|
| 96 |
const WINDOW_CLEAR_COLOR: Color = Color::srgb(0.2, 0.2, 0.2); |
| 97 |
|
| 98 |
|
| 99 |
fn setup( |
| 100 |
mut commands: Commands, |
| 101 |
asset_server: Res<AssetServer>, |
| 102 |
mut meshes: ResMut<Assets<Mesh>>, |
| 103 |
mut materials: ResMut<Assets<ColorMaterial>>, |
| 104 |
) { |
| 105 |
|
| 106 |
commands.spawn(Camera2d); |
| 107 |
|
| 108 |
|
| 109 |
let font = asset_server.load("fonts/FiraSans-Bold.ttf"); |
| 110 |
let text_style = TextFont { |
| 111 |
font: font.clone().into(), |
| 112 |
font_size: FontSize::Px(25.0), |
| 113 |
..default() |
| 114 |
}; |
| 115 |
commands.spawn(( |
| 116 |
Text2d::new("Press Space to play on your desktop! Press it again to return.\nRight click Bevy logo to exit."), |
| 117 |
text_style.clone(), |
| 118 |
Transform::from_xyz(0.0, -300.0, 100.0), |
| 119 |
InstructionsText, |
| 120 |
)); |
| 121 |
|
| 122 |
|
| 123 |
let circle = meshes.add(Circle { radius: 1.0 }); |
| 124 |
|
| 125 |
let outline_material = materials.add(Color::BLACK); |
| 126 |
let sclera_material = materials.add(Color::WHITE); |
| 127 |
let pupil_material = materials.add(Color::srgb(0.2, 0.2, 0.2)); |
| 128 |
let pupil_highlight_material = materials.add(Color::srgba(1.0, 1.0, 1.0, 0.2)); |
| 129 |
|
| 130 |
|
| 131 |
commands |
| 132 |
.spawn(( |
| 133 |
Sprite::from_image(asset_server.load("branding/icon.png")), |
| 134 |
BevyLogo, |
| 135 |
)) |
| 136 |
.with_children(|commands| { |
| 137 |
|
| 138 |
for (x, y, radius) in BIRDS_EYES { |
| 139 |
let pupil_radius = radius * 0.6; |
| 140 |
let pupil_highlight_radius = radius * 0.3; |
| 141 |
let pupil_highlight_offset = radius * 0.3; |
| 142 |
|
| 143 |
commands.spawn(( |
| 144 |
Mesh2d(circle.clone()), |
| 145 |
MeshMaterial2d(outline_material.clone()), |
| 146 |
Transform::from_xyz(x, y - 1.0, 1.0) |
| 147 |
.with_scale(Vec2::splat(radius + 2.0).extend(1.0)), |
| 148 |
)); |
| 149 |
|
| 150 |
|
| 151 |
commands.spawn(( |
| 152 |
Transform::from_xyz(x, y, 2.0), |
| 153 |
Visibility::default(), |
| 154 |
children![ |
| 155 |
|
| 156 |
( |
| 157 |
Mesh2d(circle.clone()), |
| 158 |
MeshMaterial2d(sclera_material.clone()), |
| 159 |
Transform::from_scale(Vec3::new(radius, radius, 0.0)), |
| 160 |
), |
| 161 |
|
| 162 |
( |
| 163 |
Transform::from_xyz(0.0, 0.0, 1.0), |
| 164 |
Visibility::default(), |
| 165 |
Pupil { |
| 166 |
eye_radius: radius, |
| 167 |
pupil_radius, |
| 168 |
velocity: Vec2::ZERO, |
| 169 |
}, |
| 170 |
children![ |
| 171 |
|
| 172 |
( |
| 173 |
Mesh2d(circle.clone()), |
| 174 |
MeshMaterial2d(pupil_material.clone()), |
| 175 |
Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::new( |
| 176 |
pupil_radius, |
| 177 |
pupil_radius, |
| 178 |
1.0, |
| 179 |
)), |
| 180 |
), |
| 181 |
|
| 182 |
( |
| 183 |
Mesh2d(circle.clone()), |
| 184 |
MeshMaterial2d(pupil_highlight_material.clone()), |
| 185 |
Transform::from_xyz( |
| 186 |
-pupil_highlight_offset, |
| 187 |
pupil_highlight_offset, |
| 188 |
1.0, |
| 189 |
) |
| 190 |
.with_scale(Vec3::new( |
| 191 |
pupil_highlight_radius, |
| 192 |
pupil_highlight_radius, |
| 193 |
1.0, |
| 194 |
)), |
| 195 |
) |
| 196 |
], |
| 197 |
) |
| 198 |
], |
| 199 |
)); |
| 200 |
} |
| 201 |
}); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
fn get_cursor_world_pos( |
| 206 |
mut cursor_world_pos: ResMut<CursorWorldPos>, |
| 207 |
primary_window: Single<&Window, With<PrimaryWindow>>, |
| 208 |
q_camera: Single<(&Camera, &GlobalTransform)>, |
| 209 |
) { |
| 210 |
let (main_camera, main_camera_transform) = *q_camera; |
| 211 |
|
| 212 |
cursor_world_pos.0 = primary_window.cursor_position().and_then(|cursor_pos| { |
| 213 |
main_camera |
| 214 |
.viewport_to_world_2d(main_camera_transform, cursor_pos) |
| 215 |
.ok() |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
fn update_cursor_hit_test( |
| 221 |
cursor_world_pos: Res<CursorWorldPos>, |
| 222 |
primary_window: Single<(&Window, &mut CursorOptions), With<PrimaryWindow>>, |
| 223 |
bevy_logo_transform: Single<&Transform, With<BevyLogo>>, |
| 224 |
) { |
| 225 |
let (window, mut cursor_options) = primary_window.into_inner(); |
| 226 |
|
| 227 |
if window.decorations { |
| 228 |
cursor_options.hit_test = true; |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
let Some(cursor_world_pos) = cursor_world_pos.0 else { |
| 234 |
return; |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
cursor_options.hit_test = bevy_logo_transform |
| 239 |
.translation |
| 240 |
.truncate() |
| 241 |
.distance(cursor_world_pos) |
| 242 |
< BEVY_LOGO_RADIUS; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
fn start_drag( |
| 247 |
mut commands: Commands, |
| 248 |
cursor_world_pos: Res<CursorWorldPos>, |
| 249 |
bevy_logo_transform: Single<&Transform, With<BevyLogo>>, |
| 250 |
) { |
| 251 |
|
| 252 |
let Some(cursor_world_pos) = cursor_world_pos.0 else { |
| 253 |
return; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
let drag_offset = bevy_logo_transform.translation.truncate() - cursor_world_pos; |
| 258 |
|
| 259 |
|
| 260 |
if drag_offset.length() < BEVY_LOGO_RADIUS { |
| 261 |
commands.insert_resource(DragOperation(drag_offset)); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
fn end_drag(mut commands: Commands) { |
| 267 |
commands.remove_resource::<DragOperation>(); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
fn drag( |
| 272 |
drag_offset: Res<DragOperation>, |
| 273 |
cursor_world_pos: Res<CursorWorldPos>, |
| 274 |
time: Res<Time>, |
| 275 |
mut bevy_transform: Single<&mut Transform, With<BevyLogo>>, |
| 276 |
mut q_pupils: Query<&mut Pupil>, |
| 277 |
) { |
| 278 |
|
| 279 |
let Some(cursor_world_pos) = cursor_world_pos.0 else { |
| 280 |
return; |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
let new_translation = cursor_world_pos + drag_offset.0; |
| 285 |
|
| 286 |
|
| 287 |
let drag_velocity = |
| 288 |
(new_translation - bevy_transform.translation.truncate()) / time.delta_secs(); |
| 289 |
|
| 290 |
|
| 291 |
bevy_transform.translation = new_translation.extend(bevy_transform.translation.z); |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
for mut pupil in &mut q_pupils { |
| 297 |
pupil.velocity -= drag_velocity; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
fn quit( |
| 303 |
cursor_world_pos: Res<CursorWorldPos>, |
| 304 |
mut app_exit: MessageWriter<AppExit>, |
| 305 |
bevy_logo_transform: Single<&Transform, With<BevyLogo>>, |
| 306 |
) { |
| 307 |
|
| 308 |
let Some(cursor_world_pos) = cursor_world_pos.0 else { |
| 309 |
return; |
| 310 |
}; |
| 311 |
|
| 312 |
|
| 313 |
if bevy_logo_transform |
| 314 |
.translation |
| 315 |
.truncate() |
| 316 |
.distance(cursor_world_pos) |
| 317 |
< BEVY_LOGO_RADIUS |
| 318 |
{ |
| 319 |
app_exit.write(AppExit::Success); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
fn toggle_transparency( |
| 325 |
mut commands: Commands, |
| 326 |
mut window_transparency: ResMut<WindowTransparency>, |
| 327 |
mut q_instructions_text: Query<&mut Visibility, With<InstructionsText>>, |
| 328 |
mut primary_window: Single<&mut Window, With<PrimaryWindow>>, |
| 329 |
) { |
| 330 |
|
| 331 |
window_transparency.0 = !window_transparency.0; |
| 332 |
|
| 333 |
|
| 334 |
for mut visibility in &mut q_instructions_text { |
| 335 |
*visibility = if window_transparency.0 { |
| 336 |
Visibility::Hidden |
| 337 |
} else { |
| 338 |
Visibility::Visible |
| 339 |
}; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
let clear_color; |
| 345 |
( |
| 346 |
primary_window.decorations, |
| 347 |
primary_window.window_level, |
| 348 |
clear_color, |
| 349 |
) = if window_transparency.0 { |
| 350 |
(false, WindowLevel::AlwaysOnTop, Color::NONE) |
| 351 |
} else { |
| 352 |
(true, WindowLevel::Normal, WINDOW_CLEAR_COLOR) |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
commands.insert_resource(ClearColor(clear_color)); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
fn move_pupils(time: Res<Time>, mut q_pupils: Query<(&mut Pupil, &mut Transform)>) { |
| 361 |
for (mut pupil, mut transform) in &mut q_pupils { |
| 362 |
|
| 363 |
let wiggle_radius = pupil.eye_radius - pupil.pupil_radius; |
| 364 |
|
| 365 |
let z = transform.translation.z; |
| 366 |
|
| 367 |
let mut translation = transform.translation.truncate(); |
| 368 |
|
| 369 |
pupil.velocity *= ops::powf(0.04f32, time.delta_secs()); |
| 370 |
|
| 371 |
translation += pupil.velocity * time.delta_secs(); |
| 372 |
|
| 373 |
|
| 374 |
if translation.length() > wiggle_radius { |
| 375 |
translation = translation.normalize() * wiggle_radius; |
| 376 |
|
| 377 |
pupil.velocity *= -0.75; |
| 378 |
} |
| 379 |
|
| 380 |
transform.translation = translation.extend(z); |
| 381 |
} |
| 382 |
} |