rule shrinking
This commit is contained in:
parent
6d86f6fdef
commit
e066025836
2 changed files with 60 additions and 42 deletions
|
@ -26,7 +26,17 @@ pub struct Rule {
|
|||
// rotate:
|
||||
}
|
||||
|
||||
type ResizeParam = (isize, isize, isize, isize);
|
||||
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 {
|
||||
Self {
|
||||
enabled: false,
|
||||
|
@ -34,6 +44,12 @@ impl Rule {
|
|||
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 {
|
||||
|
@ -226,42 +242,25 @@ impl RulePattern {
|
|||
self.width
|
||||
}
|
||||
|
||||
pub fn extend_left(&mut self) {
|
||||
let new_width = self.width + 1;
|
||||
let mut new_vec = vec![None; new_width * self.height];
|
||||
for y in 0..self.height {
|
||||
for x in 0..self.width {
|
||||
new_vec[x + new_width * y + 1] = self.contents[x + self.width * y];
|
||||
fn resize(&mut self, dw: isize, dh: isize, dx: isize, dy: isize) {
|
||||
let new_width = self.width.saturating_add_signed(dw);
|
||||
let new_height = self.height.saturating_add_signed(dh);
|
||||
if new_width < 1 || new_height < 1 {
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
let new_width = self.width + 1;
|
||||
let mut new_vec = vec![None; new_width * self.height];
|
||||
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;
|
||||
self.contents = new_contents;
|
||||
self.height = new_height;
|
||||
self.width = new_width;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
let delete_mode = ui.input(|i| i.modifiers.shift);
|
||||
|
||||
let mut resize_box = |x, y, 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());
|
||||
|
@ -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 result = a.union(b);
|
||||
let color = if result.hovered() {
|
||||
if delete_mode {
|
||||
Color32::RED
|
||||
} else {
|
||||
Color32::GRAY
|
||||
}
|
||||
} else {
|
||||
Color32::DARK_GRAY
|
||||
};
|
||||
|
@ -169,8 +176,11 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
|
|||
result.clicked()
|
||||
};
|
||||
if resize_box(bounds.min.x, bounds.min.y + margin, margin, patt_height) {
|
||||
rule.from.extend_left();
|
||||
rule.to.extend_left();
|
||||
if delete_mode {
|
||||
rule.resize(Rule::SHRINK_LEFT);
|
||||
} else {
|
||||
rule.resize(Rule::EXTEND_LEFT);
|
||||
}
|
||||
}
|
||||
if resize_box(
|
||||
from_cells_rect.max.x,
|
||||
|
@ -178,12 +188,18 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
|
|||
margin,
|
||||
patt_height,
|
||||
) {
|
||||
rule.from.extend_right();
|
||||
rule.to.extend_right();
|
||||
if delete_mode {
|
||||
rule.resize(Rule::SHRINK_RIGHT);
|
||||
} else {
|
||||
rule.resize(Rule::EXTEND_RIGHT);
|
||||
}
|
||||
}
|
||||
if resize_box(bounds.min.x + margin, bounds.min.y, patt_width, margin) {
|
||||
rule.from.extend_up();
|
||||
rule.to.extend_up();
|
||||
if delete_mode {
|
||||
rule.resize(Rule::SHRINK_UP);
|
||||
} else {
|
||||
rule.resize(Rule::EXTEND_UP);
|
||||
}
|
||||
}
|
||||
if resize_box(
|
||||
bounds.min.x + margin,
|
||||
|
@ -191,8 +207,11 @@ fn rule_editor(ui: &mut Ui, rule: &mut Rule, cells: &[CellData]) {
|
|||
patt_width,
|
||||
margin,
|
||||
) {
|
||||
rule.from.extend_down();
|
||||
rule.to.extend_down();
|
||||
if delete_mode {
|
||||
rule.resize(Rule::SHRINK_DOWN);
|
||||
} else {
|
||||
rule.resize(Rule::EXTEND_DOWN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue