store cell data in engine instead of editor

This commit is contained in:
Crispy 2024-05-07 18:40:41 +02:00
parent c440833755
commit d1d033565e
4 changed files with 50 additions and 59 deletions

View file

@ -1 +1 @@
{"cell_types":[{"color":[0,0,0,255],"name":"air"},{"color":[255,147,219,255],"name":"seed"},{"color":[136,242,224,255],"name":"probe"},{"color":[4,164,205,255],"name":"tail"},{"color":[195,109,225,255],"name":"corner"},{"color":[204,123,0,255],"name":"wall"}],"groups":[{"cells":[5],"name":"void","void":true},{"cells":[3,0,4,5],"name":"tail or empty","void":true},{"cells":[4,2],"name":"","void":false}],"rules":[{"base":{"contents":[["Any","None"],["Any",{"One":2}],["Any","None"],["Any",{"One":2}],[{"One":1},{"One":3}],["Any",{"One":2}],["Any","None"],["Any",{"One":2}],["Any","None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"create probes","rotate":false},{"base":{"contents":[[{"One":3},"None"],[{"One":2},{"One":3}],[{"One":0},{"One":2}]],"height":1,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"probe_movement","rotate":true},{"base":{"contents":[["Any","None"],[{"Group":1},"None"],["Any","None"],[{"Group":1},"None"],[{"One":3},{"One":0}],[{"Group":1},"None"],["Any","None"],[{"Group":1},"None"],["Any","None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"tail_death","rotate":false},{"base":{"contents":[[{"One":0},{"One":2}],["Any","None"],[{"One":2},{"One":3}],[{"Group":0},"None"],[{"One":0},{"One":2}],["Any","None"]],"height":3,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"edge","rotate":true},{"base":{"contents":[[{"One":2},{"One":0}],[{"Group":2},"None"]],"height":2,"width":1},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"merge","rotate":true},{"base":{"contents":[[{"One":2},{"One":4}],[{"Group":0},"None"],[{"Group":0},"None"],["Any","None"]],"height":2,"width":2},"enabled":true,"failrate":0,"flip_x":true,"flip_y":false,"name":"mark corner","rotate":true},{"base":{"contents":[[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},{"One":1}],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"]],"height":3,"width":3},"enabled":false,"failrate":255,"flip_x":false,"flip_y":false,"name":"spawn","rotate":false},{"base":{"contents":[[{"One":3},"None"],[{"One":2},{"One":3}],[{"One":3},"None"]],"height":1,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"clean up","rotate":true}]} {"rules":[{"name":"create probes","base":{"width":3,"height":3,"contents":[["Any","None"],["Any",{"One":2}],["Any","None"],["Any",{"One":2}],[{"One":1},{"One":3}],["Any",{"One":2}],["Any","None"],["Any",{"One":2}],["Any","None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"probe_movement","base":{"width":3,"height":1,"contents":[[{"One":3},"None"],[{"One":2},{"One":3}],[{"One":0},{"One":2}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":true,"failrate":0},{"name":"tail_death","base":{"width":3,"height":3,"contents":[["Any","None"],[{"Group":1},"None"],["Any","None"],[{"Group":1},"None"],[{"One":3},{"One":0}],[{"Group":1},"None"],["Any","None"],[{"Group":1},"None"],["Any","None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"edge","base":{"width":2,"height":3,"contents":[[{"One":0},{"One":2}],["Any","None"],[{"One":2},{"One":3}],[{"Group":0},"None"],[{"One":0},{"One":2}],["Any","None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":true,"failrate":0},{"name":"merge","base":{"width":1,"height":2,"contents":[[{"One":2},{"One":0}],[{"Group":2},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":true,"failrate":0},{"name":"mark corner","base":{"width":2,"height":2,"contents":[[{"One":2},{"One":4}],[{"Group":0},"None"],[{"Group":0},"None"],["Any","None"]]},"enabled":true,"flip_x":true,"flip_y":false,"rotate":true,"failrate":0},{"name":"spawn","base":{"width":3,"height":3,"contents":[[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},{"One":1}],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"],[{"One":0},"None"]]},"enabled":false,"flip_x":false,"flip_y":false,"rotate":false,"failrate":255},{"name":"clean up","base":{"width":3,"height":1,"contents":[[{"One":3},"None"],[{"One":2},{"One":3}],[{"One":3},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":true,"failrate":0}],"types":[{"name":"air","color":[0,0,0]},{"name":"seed","color":[255,147,219]},{"name":"probe","color":[136,242,224]},{"name":"tail","color":[4,164,205]},{"name":"corner","color":[195,109,225]},{"name":"wall","color":[204,123,0]}],"groups":[{"name":"void","void":true,"cells":[5]},{"name":"tail or empty","void":true,"cells":[3,0,4,5]},{"name":"","void":false,"cells":[4,2]}]}

View file

@ -1 +1 @@
{"cell_types":[{"color":[0,0,0,255],"name":"air"},{"color":[255,147,219,255],"name":"seed"},{"color":[146,242,225,255],"name":"copy"},{"color":[81,235,104,255],"name":"left"},{"color":[181,219,160,255],"name":"execute"},{"color":[215,172,127,255],"name":"right"},{"color":[36,117,244,255],"name":"alive"}],"groups":[{"cells":[],"name":"edge","void":true},{"cells":[0],"name":"dead","void":true},{"cells":[1,5],"name":"to left","void":false}],"rules":[{"base":{"contents":[[{"One":1},{"One":0}],[{"One":0},{"One":1}]],"height":1,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"init","rotate":false},{"base":{"contents":[[{"One":1},"None"],[{"One":1},{"One":0}]],"height":1,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"merge seeds","rotate":true},{"base":{"contents":[[{"Group":2},{"One":2}],[{"Group":0},"None"]],"height":1,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"begin copy","rotate":false},{"base":{"contents":[[{"One":2},{"One":3}],["Any","None"],["Any",{"Copy":[0,1]}]],"height":3,"width":1},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"copy","rotate":false},{"base":{"contents":[[{"One":0},{"One":2}],[{"One":3},{"One":0}]],"height":1,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"move left","rotate":false},{"base":{"contents":[[{"Group":0},"None"],[{"One":3},{"One":4}]],"height":1,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"begin execute","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":0}],["Any","None"],[{"One":6},"None"],[{"One":6},"None"],[{"One":6},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 111","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"One":6},"None"],[{"One":6},"None"],[{"Group":1},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 110","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"One":6},"None"],[{"One":0},"None"],[{"One":6},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 101","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":0}],["Any","None"],[{"One":6},"None"],[{"One":0},"None"],[{"Group":1},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 100","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"Group":1},"None"],[{"One":6},"None"],[{"One":6},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 011","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"Group":1},"None"],[{"One":6},"None"],[{"Group":1},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 010","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"Group":1},"None"],[{"One":0},"None"],[{"One":6},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 001","rotate":false},{"base":{"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":0}],["Any","None"],[{"Group":1},"None"],[{"One":0},"None"],[{"Group":1},"None"]],"height":3,"width":3},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"execute 111","rotate":false},{"base":{"contents":[[{"One":5},{"One":0}],[{"One":0},{"One":4}]],"height":1,"width":2},"enabled":true,"failrate":0,"flip_x":false,"flip_y":false,"name":"move right","rotate":false}]} {"rules":[{"name":"init","base":{"width":2,"height":1,"contents":[[{"One":1},{"One":0}],[{"One":0},{"One":1}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"merge seeds","base":{"width":2,"height":1,"contents":[[{"One":1},"None"],[{"One":1},{"One":0}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":true,"failrate":0},{"name":"begin copy","base":{"width":2,"height":1,"contents":[[{"Group":2},{"One":2}],[{"Group":0},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"copy","base":{"width":1,"height":3,"contents":[[{"One":2},{"One":3}],["Any","None"],["Any",{"Copy":[0,1]}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"move left","base":{"width":2,"height":1,"contents":[[{"One":0},{"One":2}],[{"One":3},{"One":0}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"begin execute","base":{"width":2,"height":1,"contents":[[{"Group":0},"None"],[{"One":3},{"One":4}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 111","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":0}],["Any","None"],[{"One":6},"None"],[{"One":6},"None"],[{"One":6},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 110","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"One":6},"None"],[{"One":6},"None"],[{"Group":1},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 101","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"One":6},"None"],[{"One":0},"None"],[{"One":6},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 100","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":0}],["Any","None"],[{"One":6},"None"],[{"One":0},"None"],[{"Group":1},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 011","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"Group":1},"None"],[{"One":6},"None"],[{"One":6},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 010","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"Group":1},"None"],[{"One":6},"None"],[{"Group":1},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 001","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":6}],["Any","None"],[{"Group":1},"None"],[{"One":0},"None"],[{"One":6},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"execute 111","base":{"width":3,"height":3,"contents":[["Any","None"],[{"One":4},{"One":5}],["Any","None"],["Any","None"],["Any",{"One":0}],["Any","None"],[{"Group":1},"None"],[{"One":0},"None"],[{"Group":1},"None"]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0},{"name":"move right","base":{"width":2,"height":1,"contents":[[{"One":5},{"One":0}],[{"One":0},{"One":4}]]},"enabled":true,"flip_x":false,"flip_y":false,"rotate":false,"failrate":0}],"types":[{"name":"air","color":[0,0,0]},{"name":"seed","color":[255,147,219]},{"name":"copy","color":[146,242,225]},{"name":"left","color":[81,235,104]},{"name":"execute","color":[181,219,160]},{"name":"right","color":[215,172,127]},{"name":"alive","color":[36,117,244]}],"groups":[{"name":"edge","void":true,"cells":[]},{"name":"dead","void":true,"cells":[0]},{"name":"to left","void":false,"cells":[1,5]}]}

View file

@ -6,11 +6,13 @@ pub const CHUNK_SIZE: usize = 32;
#[derive(Default, Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] #[derive(Default, Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
pub struct Cell(pub u16); pub struct Cell(pub u16);
#[derive(Debug)] #[derive(Debug, Serialize, Deserialize)]
pub struct Dish { pub struct Dish {
#[serde(skip)]
pub chunk: Chunk, pub chunk: Chunk,
pub rules: Vec<Rule>, pub rules: Vec<Rule>,
pub cell_groups: Vec<CellGroup>, pub types: Vec<CellData>,
pub groups: Vec<CellGroup>,
} }
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Default, Serialize, Deserialize)]
@ -20,7 +22,13 @@ pub struct CellGroup {
pub cells: Vec<Cell>, pub cells: Vec<Cell>,
} }
#[derive(Debug)] #[derive(Default, Debug, Serialize, Deserialize)]
pub struct CellData {
pub name: String,
pub color: [u8; 3],
}
#[derive(Debug, Default)]
pub struct Chunk { pub struct Chunk {
pub contents: Box<[[Cell; CHUNK_SIZE]; CHUNK_SIZE]>, pub contents: Box<[[Cell; CHUNK_SIZE]; CHUNK_SIZE]>,
} }
@ -342,7 +350,11 @@ impl Dish {
Self { Self {
chunk: Chunk::new().fill_random(), chunk: Chunk::new().fill_random(),
rules: default_rules, rules: default_rules,
cell_groups: vec![CellGroup { types: vec![
CellData::new("air", 0, 0, 0),
CellData::new("pink_sand", 255, 147, 219),
],
groups: vec![CellGroup {
name: "empty".into(), name: "empty".into(),
void: true, void: true,
cells: vec![Cell(0)], cells: vec![Cell(0)],
@ -414,7 +426,7 @@ impl Dish {
self.set_cell(px, py, rule_cell); self.set_cell(px, py, rule_cell);
} }
RuleCellTo::GroupRandom(group_id) => { RuleCellTo::GroupRandom(group_id) => {
let group = &self.cell_groups[group_id]; let group = &self.groups[group_id];
if !group.cells.is_empty() { if !group.cells.is_empty() {
let i = random::<usize>() % group.cells.len(); let i = random::<usize>() % group.cells.len();
let cell = group.cells[i]; let cell = group.cells[i];
@ -452,7 +464,7 @@ impl Dish {
} }
} }
RuleCellFrom::Group(group_id) => { RuleCellFrom::Group(group_id) => {
let group = &self.cell_groups[group_id]; let group = &self.groups[group_id];
if let Some(cell) = cell { if let Some(cell) = cell {
if !group.cells.contains(&cell) { if !group.cells.contains(&cell) {
return false; return false;
@ -491,3 +503,12 @@ impl Cell {
self.0 as usize self.0 as usize
} }
} }
impl CellData {
pub fn new(name: &str, r: u8, g: u8, b: u8) -> Self {
Self {
name: name.to_owned(),
color: [r, g, b],
}
}
}

View file

@ -13,10 +13,8 @@ use eframe::{
use egui::{collapsing_header::CollapsingState, DragValue, PointerButton}; use egui::{collapsing_header::CollapsingState, DragValue, PointerButton};
use native_dialog::FileDialog; use native_dialog::FileDialog;
use rand::prelude::*; use rand::prelude::*;
use serde::{Deserialize, Serialize};
use petri::{Cell, CellGroup, Chunk, Dish, Rule, RuleCellFrom, RuleCellTo, CHUNK_SIZE}; use petri::{Cell, CellData, CellGroup, Chunk, Dish, Rule, RuleCellFrom, RuleCellTo, CHUNK_SIZE};
use serde_json::{json, Value};
fn main() { fn main() {
eframe::run_native( eframe::run_native(
@ -33,13 +31,6 @@ struct UScope {
brush: Cell, brush: Cell,
speed: usize, speed: usize,
show_grid: bool, show_grid: bool,
cell_types: Vec<CellData>,
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct CellData {
name: String,
color: Color32,
} }
impl UScope { impl UScope {
@ -49,10 +40,6 @@ impl UScope {
speed: 500, speed: 500,
show_grid: false, show_grid: false,
brush: Cell(1), brush: Cell(1),
cell_types: vec![
CellData::new("air", 0, 0, 0),
CellData::new("pink_sand", 255, 147, 219),
],
} }
} }
@ -62,36 +49,24 @@ impl UScope {
.add_filter("JSON", &["json"]) .add_filter("JSON", &["json"])
.show_save_single_file() .show_save_single_file()
{ {
let out = json!({
"cell_types": self.cell_types,
"rules": self.dish.rules,
"groups": self.dish.cell_groups,
});
let out = serde_json::to_string(&out).ok()?;
let mut file = File::create(path).ok()?; let mut file = File::create(path).ok()?;
let out = serde_json::to_string(&self.dish).ok()?;
file.write_all(out.as_bytes()).ok()?; file.write_all(out.as_bytes()).ok()?;
} }
Some(()) Some(())
} }
fn open_universe(&mut self) -> Option<()> { fn open_universe(&mut self) {
if let Ok(Some(path)) = FileDialog::new() if let Ok(Some(path)) = FileDialog::new()
.set_filename("universe_1.json") .set_filename("universe_1.json")
.add_filter("JSON", &["json"]) .add_filter("JSON", &["json"])
.show_open_single_file() .show_open_single_file()
{ {
let s = fs::read_to_string(path).ok()?;
let data: Value = serde_json::from_str(&s).ok()?;
// TODO: show errors to user // TODO: show errors to user
let cell_types = serde_json::from_value(data["cell_types"].clone()).unwrap(); let s = fs::read_to_string(path).unwrap();
let groups = serde_json::from_value(data["groups"].clone()).unwrap(); self.dish = serde_json::from_str(&s).unwrap();
let rules = serde_json::from_value(data["rules"].clone()).unwrap();
self.cell_types = cell_types;
self.dish.rules = rules;
self.dish.cell_groups = groups;
self.dish.update_rules(); self.dish.update_rules();
} }
Some(())
} }
} }
@ -120,12 +95,12 @@ impl eframe::App for UScope {
ScrollArea::vertical().show(ui, |ui| { ScrollArea::vertical().show(ui, |ui| {
ui.heading("Cells"); ui.heading("Cells");
for (i, cell) in self.cell_types.iter_mut().enumerate() { for (i, cell) in self.dish.types.iter_mut().enumerate() {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.set_width(120.); ui.set_width(120.);
ui.radio_value(&mut self.brush.0, i as u16, ""); ui.radio_value(&mut self.brush.0, i as u16, "");
ui.text_edit_singleline(&mut cell.name); ui.text_edit_singleline(&mut cell.name);
ui.color_edit_button_srgba(&mut cell.color); ui.color_edit_button_srgb(&mut cell.color);
}); });
} }
@ -133,9 +108,9 @@ impl eframe::App for UScope {
let h = random::<f32>(); let h = random::<f32>();
let s = random::<f32>() * 0.5 + 0.5; let s = random::<f32>() * 0.5 + 0.5;
let v = random::<f32>() * 0.5 + 0.5; let v = random::<f32>() * 0.5 + 0.5;
let color = Hsva::new(h, s, v, 1.).into(); let color = Hsva::new(h, s, v, 1.).to_srgb();
let name = format!("cell #{}", self.cell_types.len()); let name = format!("cell #{}", self.dish.types.len());
self.cell_types.push(CellData { name, color }) self.dish.types.push(CellData { name, color })
} }
if ui.button("fill").clicked() { if ui.button("fill").clicked() {
self.dish.chunk.contents.fill([self.brush; CHUNK_SIZE]); self.dish.chunk.contents.fill([self.brush; CHUNK_SIZE]);
@ -143,14 +118,14 @@ impl eframe::App for UScope {
ui.separator(); ui.separator();
ui.heading("Groups"); ui.heading("Groups");
for group in &mut self.dish.cell_groups { for group in &mut self.dish.groups {
let (rect, _response) = let (rect, _response) =
ui.allocate_exact_size(Vec2::splat(CSIZE), Sense::click()); ui.allocate_exact_size(Vec2::splat(CSIZE), Sense::click());
draw_group(ui, rect, group, &self.cell_types); draw_group(ui, rect, group, &self.dish.types);
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.menu_button("edit", |ui| { ui.menu_button("edit", |ui| {
ui.checkbox(&mut group.void, "void"); ui.checkbox(&mut group.void, "void");
for (i, celldata) in self.cell_types.iter().enumerate() { for (i, celldata) in self.dish.types.iter().enumerate() {
let mut included = group.cells.contains(&Cell(i as u16)); let mut included = group.cells.contains(&Cell(i as u16));
if ui.checkbox(&mut included, &celldata.name).changed() { if ui.checkbox(&mut included, &celldata.name).changed() {
if included { if included {
@ -165,7 +140,7 @@ impl eframe::App for UScope {
}); });
} }
if ui.button("add group").clicked() { if ui.button("add group").clicked() {
self.dish.cell_groups.push(CellGroup::default()); self.dish.groups.push(CellGroup::default());
} }
ui.heading("Rules"); ui.heading("Rules");
@ -177,8 +152,8 @@ impl eframe::App for UScope {
ui, ui,
rule, rule,
i, i,
&self.cell_types, &self.dish.types,
&self.dish.cell_groups, &self.dish.groups,
&mut to_remove, &mut to_remove,
&mut to_clone, &mut to_clone,
); );
@ -200,7 +175,7 @@ impl eframe::App for UScope {
CentralPanel::default().show(ctx, |ui| { CentralPanel::default().show(ctx, |ui| {
let bounds = ui.available_rect_before_wrap(); let bounds = ui.available_rect_before_wrap();
let painter = ui.painter_at(bounds); let painter = ui.painter_at(bounds);
paint_chunk(painter, &self.dish.chunk, &self.cell_types, self.show_grid); paint_chunk(painter, &self.dish.chunk, &self.dish.types, self.show_grid);
let rect = ui.allocate_rect(bounds, Sense::click_and_drag()); let rect = ui.allocate_rect(bounds, Sense::click_and_drag());
if let Some(pos) = rect.interact_pointer_pos() { if let Some(pos) = rect.interact_pointer_pos() {
@ -232,6 +207,7 @@ fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData], grid: bool)
continue; continue;
} }
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
let color = Color32::from_rgb(color[0], color[1], color[2]);
if grid { if grid {
painter.rect(rect, 0., color, (1., Color32::GRAY)); painter.rect(rect, 0., color, (1., Color32::GRAY));
} else { } else {
@ -431,6 +407,7 @@ fn rule_cell_edit_from(
RuleCellFrom::Any => (), RuleCellFrom::Any => (),
RuleCellFrom::One(cell) => { RuleCellFrom::One(cell) => {
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
let color = Color32::from_rgb(color[0], color[1], color[2]);
ui.painter() ui.painter()
.rect(rect.shrink(OUTLINE.0 / 2.), 0., color, OUTLINE); .rect(rect.shrink(OUTLINE.0 / 2.), 0., color, OUTLINE);
} }
@ -497,6 +474,7 @@ fn rule_cell_edit_to(
RuleCellTo::None => (), RuleCellTo::None => (),
RuleCellTo::One(cell) => { RuleCellTo::One(cell) => {
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
let color = Color32::from_rgb(color[0], color[1], color[2]);
ui.painter() ui.painter()
.rect(rect.shrink(OUTLINE.0 / 2.), 0., color, OUTLINE); .rect(rect.shrink(OUTLINE.0 / 2.), 0., color, OUTLINE);
} }
@ -565,6 +543,7 @@ fn draw_group(ui: &mut Ui, rect: Rect, group: &CellGroup, cells: &[CellData]) {
let radius_per_color = (CSIZE * 0.7) / (group_size as f32); let radius_per_color = (CSIZE * 0.7) / (group_size as f32);
for (i, cell) in group.cells.iter().enumerate() { for (i, cell) in group.cells.iter().enumerate() {
let color = cells[cell.id()].color; let color = cells[cell.id()].color;
let color = Color32::from_rgb(color[0], color[1], color[2]);
let radius = radius_per_color * ((group_size - i) as f32); let radius = radius_per_color * ((group_size - i) as f32);
ui.painter_at(rect) ui.painter_at(rect)
.circle_filled(rect.center(), radius, color); .circle_filled(rect.center(), radius, color);
@ -576,12 +555,3 @@ fn draw_group(ui: &mut Ui, rect: Rect, group: &CellGroup, cells: &[CellData]) {
ui.allocate_rect(rect, Sense::hover()) ui.allocate_rect(rect, Sense::hover())
.on_hover_text(&group.name); .on_hover_text(&group.name);
} }
impl CellData {
fn new(name: &str, r: u8, g: u8, b: u8) -> Self {
Self {
name: name.to_owned(),
color: Color32::from_rgb(r, g, b),
}
}
}