This commit is contained in:
Crispy 2024-04-30 23:24:45 +02:00
parent a808e7996b
commit bff268515d
5 changed files with 1311 additions and 1673 deletions

View file

@ -7,5 +7,4 @@ edition = "2021"
[dependencies]
petri = { path = "../petri" }
bevy = { version = "0.13", features = ["dynamic_linking"] }
owo-colors = "4.0.0"
eframe = "0.27.2"

View file

@ -1,39 +1,102 @@
use owo_colors::OwoColorize;
use petri::{Cell, Dish, CHUNK_SIZE};
use eframe::{
egui::{CentralPanel, Color32, Painter, Rect, SidePanel, Ui, Vec2},
NativeOptions,
};
use petri::{Chunk, Dish, Rule, CHUNK_SIZE};
fn main() {
let mut dish = Dish::new();
loop {
for _ in 0..1000 {
dish.fire_blindly();
}
print_dish(&dish);
wait_for_input()
eframe::run_native(
"V3 World Editor",
NativeOptions::default(),
Box::new(|_cc| Box::new(UScope::new(_cc))),
)
.unwrap();
}
#[derive(Debug)]
struct UScope {
dish: Dish,
}
impl UScope {
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
Self { dish: Dish::new() }
}
}
fn print_dish(dish: &Dish) {
for y in 0..(CHUNK_SIZE / 2) {
for x in 0..CHUNK_SIZE {
render_pixel_pair(dish, x, y);
impl eframe::App for UScope {
fn update(&mut self, ctx: &eframe::egui::Context, _frame: &mut eframe::Frame) {
ctx.request_repaint();
for _ in 0..100 {
self.dish.fire_blindly();
}
println!();
SidePanel::left("left_panel").show(ctx, |ui| {
ui.heading("Rules");
ui.text_edit_singleline(&mut "dummy");
if ui.button("aaa").clicked() {
dbg!(&self.dish.rules);
}
for rule in &mut self.dish.rules {
rule_editor(ui, rule);
}
});
CentralPanel::default().show(ctx, |ui| {
let bounds = ui.available_rect_before_wrap();
let painter = ui.painter_at(bounds);
paint_chunk(painter, &self.dish.chunk);
});
}
println!();
}
fn render_pixel_pair(dish: &Dish, x: usize, y: usize) {
let a = dish.get_cell(x, y * 2) != Some(Cell::EMPTY);
let b = dish.get_cell(x, y * 2 + 1) != Some(Cell::EMPTY);
let char = match (a, b) {
(false, false) => " ",
(false, true) => "",
(true, false) => "",
(true, true) => "",
};
print!("{}", char.fg_rgb::<255, 147, 219>());
fn paint_chunk(painter: Painter, chunk: &Chunk) {
let bounds = painter.clip_rect();
let size = 16.;
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
let cell = &chunk.get_cell(x, y);
let corner = bounds.min + (Vec2::from((x as f32, y as f32)) * size);
let rect = Rect::from_min_size(corner, Vec2::splat(size));
let color = Color32::from_rgb(cell.0, cell.1, cell.2);
painter.rect(rect, 0., color, (1., Color32::GRAY));
}
}
}
pub fn wait_for_input() {
std::io::stdin().read_line(&mut String::new()).unwrap();
fn rule_editor(ui: &mut Ui, rule: &mut Rule) {
let patt_height = rule.from.height();
let patt_width = rule.from.height();
const CSIZE: f32 = 24.;
let (_, bounds) = ui.allocate_space(Vec2::new(
CSIZE * (patt_width * 2 + 1) as f32,
CSIZE * patt_height as f32,
));
for x in 0..patt_width {
for y in 0..patt_height {
let rect = Rect::from_min_size(
bounds.min + Vec2::from((x as f32, y as f32)) * CSIZE,
Vec2::splat(CSIZE),
);
if let Some(cell) = rule.from.get(x, y) {
ui.painter().rect(
rect,
2.,
Color32::from_rgb(cell.0, cell.1, cell.2),
(1., Color32::GRAY),
);
}
if let Some(cell) = rule.to.get(x, y) {
let rect = rect.translate(Vec2::X * (patt_width as f32 + 1.) * CSIZE);
ui.painter().rect(
rect,
2.,
Color32::from_rgb(cell.0, cell.1, cell.2),
(1., Color32::GRAY),
);
}
}
}
}