refactor
This commit is contained in:
parent
bff268515d
commit
aa4cec6dbf
2 changed files with 50 additions and 37 deletions
|
@ -16,11 +16,24 @@ fn main() {
|
|||
#[derive(Debug)]
|
||||
struct UScope {
|
||||
dish: Dish,
|
||||
celltypes: Vec<CellData>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct CellData {
|
||||
name: String,
|
||||
color: Color32,
|
||||
}
|
||||
|
||||
impl UScope {
|
||||
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
|
||||
Self { dish: Dish::new() }
|
||||
Self {
|
||||
dish: Dish::new(),
|
||||
celltypes: vec![
|
||||
CellData::new("air", 0, 0, 0),
|
||||
CellData::new("pink_sand", 255, 147, 219),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -37,18 +50,18 @@ impl eframe::App for UScope {
|
|||
dbg!(&self.dish.rules);
|
||||
}
|
||||
for rule in &mut self.dish.rules {
|
||||
rule_editor(ui, rule);
|
||||
rule_editor(ui, rule, &self.celltypes);
|
||||
}
|
||||
});
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
let bounds = ui.available_rect_before_wrap();
|
||||
let painter = ui.painter_at(bounds);
|
||||
paint_chunk(painter, &self.dish.chunk);
|
||||
paint_chunk(painter, &self.dish.chunk, &self.celltypes);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn paint_chunk(painter: Painter, chunk: &Chunk) {
|
||||
fn paint_chunk(painter: Painter, chunk: &Chunk, cells: &[CellData]) {
|
||||
let bounds = painter.clip_rect();
|
||||
let size = 16.;
|
||||
for x in 0..CHUNK_SIZE {
|
||||
|
@ -56,17 +69,18 @@ fn paint_chunk(painter: Painter, chunk: &Chunk) {
|
|||
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);
|
||||
let color = cells[cell.id()].color;
|
||||
painter.rect(rect, 0., color, (1., Color32::GRAY));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rule_editor(ui: &mut Ui, rule: &mut Rule) {
|
||||
fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
|
||||
let patt_height = rule.from.height();
|
||||
let patt_width = rule.from.height();
|
||||
|
||||
const CSIZE: f32 = 24.;
|
||||
const OUTLINE: (f32, Color32) = (1., Color32::GRAY);
|
||||
|
||||
let (_, bounds) = ui.allocate_space(Vec2::new(
|
||||
CSIZE * (patt_width * 2 + 1) as f32,
|
||||
|
@ -79,24 +93,24 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule) {
|
|||
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),
|
||||
);
|
||||
let color = cells[cell.id()].color;
|
||||
ui.painter().rect(rect, 2., color, OUTLINE);
|
||||
}
|
||||
|
||||
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),
|
||||
);
|
||||
let color = cells[cell.id()].color;
|
||||
ui.painter().rect(rect, 2., color, OUTLINE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue