rule shrinking

This commit is contained in:
Crispy 2024-05-03 11:48:22 +02:00
parent 6d86f6fdef
commit e066025836
2 changed files with 60 additions and 42 deletions

View file

@ -26,7 +26,17 @@ pub struct Rule {
// rotate: // rotate:
} }
type ResizeParam = (isize, isize, isize, isize);
impl Rule { impl Rule {
pub const EXTEND_LEFT: ResizeParam = (1, 0, -1, 0);
pub const EXTEND_RIGHT: ResizeParam = (1, 0, 0, 0);
pub const EXTEND_UP: ResizeParam = (0, 1, 0, -1);
pub const EXTEND_DOWN: ResizeParam = (0, 1, 0, 0);
pub const SHRINK_LEFT: ResizeParam = (-1, 0, 1, 0);
pub const SHRINK_RIGHT: ResizeParam = (-1, 0, 0, 0);
pub const SHRINK_UP: ResizeParam = (0, -1, 0, 1);
pub const SHRINK_DOWN: ResizeParam = (0, -1, 0, 0);
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
enabled: false, enabled: false,
@ -34,6 +44,12 @@ impl Rule {
to: RulePattern::new(), to: RulePattern::new(),
} }
} }
pub fn resize(&mut self, params: ResizeParam) {
let (dw, dh, dx, dy) = params;
self.from.resize(dw, dh, dx, dy);
self.to.resize(dw, dh, dx, dy);
}
} }
impl Chunk { impl Chunk {
@ -226,42 +242,25 @@ impl RulePattern {
self.width self.width
} }
pub fn extend_left(&mut self) { fn resize(&mut self, dw: isize, dh: isize, dx: isize, dy: isize) {
let new_width = self.width + 1; let new_width = self.width.saturating_add_signed(dw);
let mut new_vec = vec![None; new_width * self.height]; let new_height = self.height.saturating_add_signed(dh);
for y in 0..self.height { if new_width < 1 || new_height < 1 {
for x in 0..self.width { return;
new_vec[x + new_width * y + 1] = self.contents[x + self.width * y];
} }
let mut new_contents = vec![None; new_width * new_height];
for nx in 0..new_width {
let oldx = nx.wrapping_add_signed(dx);
for ny in 0..new_height {
let oldy = ny.wrapping_add_signed(dy);
new_contents[nx + new_width * ny] = self.get(oldx, oldy);
} }
self.width += 1;
self.contents = new_vec;
} }
pub fn extend_right(&mut self) { self.contents = new_contents;
let new_width = self.width + 1; self.height = new_height;
let mut new_vec = vec![None; new_width * self.height]; self.width = new_width;
for y in 0..self.height {
for x in 0..self.width {
new_vec[x + new_width * y] = self.contents[x + self.width * y];
}
}
self.width += 1;
self.contents = new_vec;
}
pub fn extend_up(&mut self) {
for _ in 0..self.width {
self.contents.insert(0, None);
}
self.height += 1;
}
pub fn extend_down(&mut self) {
for _ in 0..self.width {
self.contents.push(None);
}
self.height += 1;
} }
} }

View file

@ -152,6 +152,9 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
rule_cell_edit(ui, to_cells_rect.min, &mut rule.to, x, y, cells); rule_cell_edit(ui, to_cells_rect.min, &mut rule.to, x, y, cells);
} }
} }
let delete_mode = ui.input(|i| i.modifiers.shift);
let mut resize_box = |x, y, w, h| { let mut resize_box = |x, y, w, h| {
let rect_a = Rect::from_min_size(Pos2::new(x, y), Vec2::new(w, h)); let rect_a = Rect::from_min_size(Pos2::new(x, y), Vec2::new(w, h));
let a = ui.allocate_rect(rect_a, Sense::click()); let a = ui.allocate_rect(rect_a, Sense::click());
@ -159,7 +162,11 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
let b = ui.allocate_rect(rect_b, Sense::click()); let b = ui.allocate_rect(rect_b, Sense::click());
let result = a.union(b); let result = a.union(b);
let color = if result.hovered() { let color = if result.hovered() {
if delete_mode {
Color32::RED
} else {
Color32::GRAY Color32::GRAY
}
} else { } else {
Color32::DARK_GRAY Color32::DARK_GRAY
}; };
@ -169,8 +176,11 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
result.clicked() result.clicked()
}; };
if resize_box(bounds.min.x, bounds.min.y + margin, margin, patt_height) { if resize_box(bounds.min.x, bounds.min.y + margin, margin, patt_height) {
rule.from.extend_left(); if delete_mode {
rule.to.extend_left(); rule.resize(Rule::SHRINK_LEFT);
} else {
rule.resize(Rule::EXTEND_LEFT);
}
} }
if resize_box( if resize_box(
from_cells_rect.max.x, from_cells_rect.max.x,
@ -178,12 +188,18 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
margin, margin,
patt_height, patt_height,
) { ) {
rule.from.extend_right(); if delete_mode {
rule.to.extend_right(); rule.resize(Rule::SHRINK_RIGHT);
} else {
rule.resize(Rule::EXTEND_RIGHT);
}
} }
if resize_box(bounds.min.x + margin, bounds.min.y, patt_width, margin) { if resize_box(bounds.min.x + margin, bounds.min.y, patt_width, margin) {
rule.from.extend_up(); if delete_mode {
rule.to.extend_up(); rule.resize(Rule::SHRINK_UP);
} else {
rule.resize(Rule::EXTEND_UP);
}
} }
if resize_box( if resize_box(
bounds.min.x + margin, bounds.min.x + margin,
@ -191,8 +207,11 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
patt_width, patt_width,
margin, margin,
) { ) {
rule.from.extend_down(); if delete_mode {
rule.to.extend_down(); rule.resize(Rule::SHRINK_DOWN);
} else {
rule.resize(Rule::EXTEND_DOWN);
}
} }
} }