Skip to content

Commit

Permalink
improve ui
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed May 7, 2024
1 parent 1a820ed commit da05de2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
11 changes: 4 additions & 7 deletions src/flower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,17 @@ pub fn spawn_flowers(
seed_radius: f32,
seed_distance: f32,
seed_rotation: f32
) {
// Golden ratio

// Starting angle
) {
let start_angle = 0.0;

for i in 1..num_seeds {
let angle = start_angle + ((2.0 * PI)/ (1. / seed_rotation)) * (i as f32);
let radius = 5.0 * (i as f32).sqrt(); // Adjust radius based on index
let x = angle.cos() * radius * seed_distance; // Multiply by 2.0 to space them out more
let radius = 5.0 * (i as f32).sqrt();
let x = angle.cos() * radius * seed_distance;
let y = angle.sin() * radius * seed_distance;

commands.spawn(MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Circle { radius: seed_radius })), // Use smaller radius for points
mesh: Mesh2dHandle(meshes.add(Circle { radius: seed_radius })),
material: materials.add(Color::ORANGE),
transform: Transform::from_xyz(x, y, 0.0),
..Default::default()
Expand Down
24 changes: 21 additions & 3 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::f32::consts::PI;
use bevy::{prelude::*, sprite::{Mesh2dHandle, MaterialMesh2dBundle}};
use crate::{constants::PHI, flower::{clear_flowers, spawn_flowers, FlowerSeed, NumberSeeds, SeedDistance, SeedRadius, SeedRotation}};
use bevy_egui::{egui::{self, InnerResponse, Response, Ui, ScrollArea}, EguiContexts};
use bevy_egui::{egui::{self, InnerResponse, Response, ScrollArea, Ui}, EguiContexts, EguiSettings};

pub struct UiPlugin;

Expand All @@ -21,7 +21,9 @@ pub fn settings_ui(
mut num_seeds: ResMut<NumberSeeds>,
mut seed_radius: ResMut<SeedRadius>,
mut seed_distance: ResMut<SeedDistance>,
flowers: Query<Entity, With<FlowerSeed>>
flowers: Query<Entity, With<FlowerSeed>>,
mut egui_settings: ResMut<EguiSettings>,
keys: Res<ButtonInput<KeyCode>>,
) {
let mut changed = false;
egui::SidePanel::left("settings_ui")
Expand All @@ -32,7 +34,17 @@ pub fn settings_ui(
.auto_shrink(true)
.show(ui, |ui| {
ui.heading("Settings");
let r_changed = ui.add(egui::Slider::new(&mut seed_rotation.0, 0.0..=1.0).text("Rotation per seed")).changed();
let mut r_changed = ui.add(egui::Slider::new(&mut seed_rotation.0, 0.0..=1.0).step_by(0.00001).drag_value_speed(0.0001).text("Rotation per seed")).changed();
ui.horizontal(|ui| {
if ui.button("1/PI").clicked() {
r_changed = true;
seed_rotation.0 = 1.0 / PI;
}
if ui.button("1/PHI").clicked() {
r_changed = true;
seed_rotation.0 = 1.0 / PHI;
}
});
let d_changed = ui.add(egui::Slider::new(&mut seed_distance.0, 0.0..=30.0).text("Seed density")).changed();
let ra_changed = ui.add(egui::Slider::new(&mut seed_radius.0, 0.0..=20.0).text("Seed radius")).changed();
let n_changed = ui.add(egui::Slider::new(&mut num_seeds.0, 0..=1000).text("Number seeds")).changed();
Expand All @@ -43,4 +55,10 @@ pub fn settings_ui(
clear_flowers(&mut commands, flowers);
spawn_flowers(&mut commands, &mut meshes, &mut materials, num_seeds.0, seed_radius.0, seed_distance.0, seed_rotation.0);
}

if keys.pressed(KeyCode::ControlLeft) && keys.just_pressed(KeyCode::Comma) {
egui_settings.scale_factor *= 1.1;
} else if keys.pressed(KeyCode::ControlLeft) && keys.just_pressed(KeyCode::Period) {
egui_settings.scale_factor *= 0.9;
}
}

0 comments on commit da05de2

Please sign in to comment.