Skip to content

Commit

Permalink
Merge pull request #1 from raunakab/feature/2-dimensional
Browse files Browse the repository at this point in the history
Implement 2-dimensional layouting
  • Loading branch information
Raunak Bhagat authored Sep 22, 2023
2 parents 0031e65 + 44e9827 commit e9305be
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 244 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = [
description = "A 2-dimensional layout solver."

[dependencies]
cherrytree = { path = "../cherrytree" }
cherrytree = { git = "https://github.com/raunakab/cherrytree" }
indexmap = "2.0.0"
petgraph = "0.6.4"
slotmap = "1.0.6"
143 changes: 137 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ impl Solver {
constraint: Constraint,
capacity: usize,
) -> Option<ConstraintKey> {
matches!(constraint.fill_x, Fill::Scale(..)).then(|| {
let both_fills_are_absolute_scales = matches! { constraint.fill, Fill::Absolute { x: FillType::Scale(..), y: FillType::Scale(..) }};
let both_fills_are_relative_scales = matches! { constraint.fill, Fill::Relative { main: FillType::Scale(..), cross: FillType::Scale(..) }};

let both_fills_are_scales = both_fills_are_absolute_scales | both_fills_are_relative_scales;

both_fills_are_scales.then(|| {
let root_key = self
.constraint_tree
.insert_root_with_capacity(constraint, capacity);
Expand Down Expand Up @@ -189,7 +194,7 @@ impl Solver {

// Solve method:

pub fn solve(&mut self, length_x: f64) {
pub fn solve(&mut self, length_x: f64, length_y: f64) {
let is_dirty = self.is_dirty;
let is_empty = self.constraint_tree.is_empty();

Expand All @@ -198,12 +203,14 @@ impl Solver {

(true, false) => {
let length_x = length_x.max(0.);
let length_y = length_y.max(0.);

solve(
&self.constraint_tree,
&mut self.frame_tree,
&mut self.key_map,
length_x,
length_y,
);

self.is_dirty = false;
Expand All @@ -216,31 +223,117 @@ impl Solver {

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct Constraint {
pub fill_x: Fill,
pub padding: Padding,
pub align_x: Align,
pub fill: Fill,
pub content: Content,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Fill {
Absolute { x: FillType, y: FillType },
Relative { main: FillType, cross: FillType },
}

impl Fill {
fn to_relative_fill(self, direction: Direction) -> RelativeFill {
match self {
Self::Absolute { x, y } => match direction {
Direction::Horizontal => RelativeFill { main: x, cross: y },
Direction::Vertical => RelativeFill { main: y, cross: x },
},
Self::Relative { main, cross } => RelativeFill { main, cross },
}
}
}

impl Default for Fill {
fn default() -> Self {
Self::Relative {
main: FillType::default(),
cross: FillType::default(),
}
}
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct RelativeFill {
main: FillType,
cross: FillType,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FillType {
Exact(f64),
Scale(usize),
Minimize,
}

impl Default for Fill {
impl Default for FillType {
fn default() -> Self {
Self::Scale(1)
}
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct Content {
pub direction: Direction,
pub padding: Padding,
pub align_main: Align,
pub align_cross: Align,
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct Padding {
pub left: f64,
pub right: f64,

pub top: f64,
pub bottom: f64,
}

impl Padding {
fn to_relative_padding(self, direction: Direction) -> RelativePadding {
let Self {
left,
right,
top,
bottom,
} = self;

match direction {
Direction::Horizontal => RelativePadding {
main_start: left,
main_end: right,
cross_start: top,
cross_end: bottom,
},
Direction::Vertical => RelativePadding {
main_start: top,
main_end: bottom,
cross_start: left,
cross_end: right,
},
}
}
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct RelativePadding {
pub main_start: f64,
pub main_end: f64,

pub cross_start: f64,
pub cross_end: f64,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Horizontal,

#[default]
Vertical,
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Align {
#[default]
Start,
Expand All @@ -252,4 +345,42 @@ pub enum Align {
pub struct Frame {
pub offset_x: f64,
pub length_x: f64,

pub offset_y: f64,
pub length_y: f64,
}

#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct RelativeFrame {
pub offset_main: f64,
pub length_main: f64,

pub offset_cross: f64,
pub length_cross: f64,
}

impl RelativeFrame {
fn to_frame(self, direction: Direction) -> Frame {
let Self {
offset_main,
length_main,
offset_cross,
length_cross,
} = self;

match direction {
Direction::Horizontal => Frame {
offset_x: offset_main,
length_x: length_main,
offset_y: offset_cross,
length_y: length_cross,
},
Direction::Vertical => Frame {
offset_x: offset_cross,
length_x: length_cross,
offset_y: offset_main,
length_y: length_main,
},
}
}
}
Loading

0 comments on commit e9305be

Please sign in to comment.