aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/layout/Layout.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/model/layout/Layout.java')
-rw-r--r--src/main/model/layout/Layout.java140
1 files changed, 27 insertions, 113 deletions
diff --git a/src/main/model/layout/Layout.java b/src/main/model/layout/Layout.java
index 5a19302..d9c3954 100644
--- a/src/main/model/layout/Layout.java
+++ b/src/main/model/layout/Layout.java
@@ -8,17 +8,20 @@ import java.util.*;
// Generic Layout class
public abstract class Layout {
- private Point location;
- private Dimension dimension;
+ // fuck encapsulation all my homies hate encapsulation
+ // but seriously, what a garbage idea: get a better language
+ // (please read the above comment in the voice of https://www.youtube.com/watch?v=EdWSg6YwUeo)
+ public Point location;
+ public Dimension dimension;
- private Node associatedNode;
- private Layout parent;
- private Optional<Layout> previousSibling;
- private Optional<Layout> nextSibling;
- private ArrayList<Layout> children;
+ public final Node associatedNode;
+ public final Layout parent;
+ public Optional<Layout> previousSibling;
+ public Optional<Layout> nextSibling;
+ public ArrayList<Layout> children;
- public static final int DEFAULT_WIDTH = 1920;
- public static final int DEFAULT_HEIGHT = 1080;
+ public static final int DEFAULT_WIDTH = 1000;
+ public static final int DEFAULT_HEIGHT = 800;
// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
public static final Set<String> BLOCK_ELEMENTS = new HashSet<>(
@@ -46,125 +49,36 @@ public abstract class Layout {
// eh, probably the best place to put this
// parent MAY BE nil: a handy call to Optional.ofNullable allows this
public static ArrayList<Layout> constructTree(ArrayList<Node> html, Layout parent) {
- ArrayDeque<Layout> result = new ArrayDeque<>();
+ var result = new ArrayDeque<Layout>();
for (Node node : html) {
Layout layout;
- if (node instanceof ElementNode) {
- if (BLOCK_ELEMENTS.contains(((ElementNode) node).getTag())) {
- layout = new BlockLayout(node, parent);
- } else {
+ switch (node) {
+ case ElementNode e -> {
+ if (BLOCK_ELEMENTS.contains(e.tag)) {
+ layout = new BlockLayout(node, parent);
+ } else {
+ layout = new InlineLayout(node, parent);
+ }
+ layout.children = constructTree(e.children, layout);
+ }
+ default -> {
layout = new InlineLayout(node, parent);
}
- layout.setChildren(constructTree(((ElementNode) node).getChildren(), layout));
- } else {
- layout = new InlineLayout(node, parent);
}
if (result.size() > 0) {
- layout.setPreviousSibling(result.getLast());
- result.getLast().setNextSibling(layout);
+ layout.previousSibling = Optional.of(result.getLast());
+ result.getLast().nextSibling = Optional.of(layout);
}
-
result.add(layout);
}
return new ArrayList<>(result); // haha
}
public static DocumentLayout constructTree(ArrayList<Node> html) {
- DocumentLayout result = new DocumentLayout();
- result.setChildren(constructTree(html, result));
+ var result = new DocumentLayout();
+ result.children = constructTree(html, result);
result.layout();
return result;
}
-
- // man, fuck design patterns, this is so much goddamn code
-
- public void setLocation(Point point) {
- this.location.x = point.x;
- this.location.y = point.y;
- }
-
- public void setX(double x) {
- this.location.setLocation(x, this.getLocation().getY());
- }
-
- public void setY(double y) {
- this.location.setLocation(this.getLocation().getX(), y);
- }
-
- public void setDimension(Dimension dimension) {
- this.dimension = dimension;
- }
-
- public void setDimension(double x, double y) {
- this.dimension.setSize(x, y);
- }
-
- public void setWidth(double width) {
- this.dimension.setSize(width, this.getDimension().getWidth());
- }
-
- public void setHeight(double height) {
- this.dimension.setSize(this.getDimension().getHeight(), height);
- }
-
- public void setPreviousSibling(Layout sibling) {
- this.previousSibling = Optional.ofNullable(sibling);
- }
-
- public void setNextSibling(Layout parent) {
- this.nextSibling = Optional.ofNullable(parent);
- }
-
- public void setChildren(ArrayList<Layout> children) {
- this.children = children;
- }
-
- public void addChild(Layout child) {
- this.children.add(child);
- }
-
- public Node getAssociatedNode() {
- return this.associatedNode;
- }
-
- public Point getLocation() {
- return this.location;
- }
-
- public double getX() {
- return this.location.getX();
- }
-
- public double getY() {
- return this.location.getY();
- }
-
- public Dimension getDimension() {
- return this.dimension;
- }
-
- public double getWidth() {
- return this.dimension.getWidth();
- }
-
- public double getHeight() {
- return this.dimension.getHeight();
- }
-
- public Layout getParent() {
- return this.parent;
- }
-
- public Optional<Layout> getPreviousSibling() {
- return this.previousSibling;
- }
-
- public Optional<Layout> getNextSibling() {
- return this.nextSibling;
- }
-
- public ArrayList<Layout> getChildren() {
- return this.children;
- }
}