From 0e693fd4814572712a4192cb4f176e71f7a344f7 Mon Sep 17 00:00:00 2001 From: j-james Date: Wed, 21 Dec 2022 17:36:49 -0800 Subject: Implement the basics of a recursive layout renderer --- data/example.html | 54 +++++++++++++++---------------- src/main/model/layout/BlockLayout.java | 8 +++-- src/main/model/layout/DocumentLayout.java | 3 +- src/main/model/layout/InlineLayout.java | 18 ++++++++++- src/main/model/layout/Layout.java | 3 +- src/main/ui/BrowserCanvas.java | 32 ++++++++++++++++-- src/main/ui/BrowserWindow.java | 1 + 7 files changed, 85 insertions(+), 34 deletions(-) diff --git a/data/example.html b/data/example.html index b62fc42..8eff336 100644 --- a/data/example.html +++ b/data/example.html @@ -6,33 +6,6 @@ - @@ -44,3 +17,30 @@ + diff --git a/src/main/model/layout/BlockLayout.java b/src/main/model/layout/BlockLayout.java index 7f59dc6..f011847 100644 --- a/src/main/model/layout/BlockLayout.java +++ b/src/main/model/layout/BlockLayout.java @@ -19,13 +19,17 @@ public class BlockLayout extends Layout { this.setLocation(this.getParent().getLocation()); this.getPreviousSibling().ifPresent( sibling -> this.setY(sibling.getY() + sibling.getHeight())); + this.getPreviousSibling().ifPresent( + sibling -> System.out.println("bluh" + sibling.getAssociatedNode().getData())); - - this.setDimension(this.getParent().getDimension()); +// this.setDimension(this.getParent().getDimension()); for (Layout child : this.getChildren()) { child.layout(); this.setHeight(this.getHeight() + child.getHeight()); } + System.out.println(this.getAssociatedNode().getData() + this.getLocation()); + System.out.println(System.identityHashCode(this.getLocation())); +// System.out.println(this.getAssociatedNode().getData() + this.getDimension()); } } diff --git a/src/main/model/layout/DocumentLayout.java b/src/main/model/layout/DocumentLayout.java index 299be25..92c0b00 100644 --- a/src/main/model/layout/DocumentLayout.java +++ b/src/main/model/layout/DocumentLayout.java @@ -1,6 +1,7 @@ package model.layout; import java.awt.*; +import java.util.ArrayList; // root document layout style public class DocumentLayout extends Layout { @@ -17,7 +18,7 @@ public class DocumentLayout extends Layout { // MODIFIES: this // EFFECTS: recursively constructs the layout tree public void layout() { - this.setLocation(new Point(0, 0)); + this.setLocation(new Point(10, 20)); this.setDimension(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); for (Layout child : this.getChildren()) { diff --git a/src/main/model/layout/InlineLayout.java b/src/main/model/layout/InlineLayout.java index 2de69d6..8e375d5 100644 --- a/src/main/model/layout/InlineLayout.java +++ b/src/main/model/layout/InlineLayout.java @@ -1,6 +1,8 @@ package model.layout; +import model.html.ElementNode; import model.html.Node; +import model.html.TextNode; import java.awt.*; @@ -25,12 +27,26 @@ public class InlineLayout extends Layout { this.setWidth(this.getParent().getWidth()); this.setCursor(this.getX(), this.getY()); + Node node = this.getAssociatedNode(); + if (node instanceof TextNode) { + if (node.getData().length() > 5) { + this.setHeight(20); +// this.setWidth(this.getWidth() + node.getData().length()); + } + } else if (node instanceof ElementNode) { + if (((ElementNode) node).getTag().equals("a")) { + this.setX(this.getX() + this.getParent().getWidth()); + } + } + for (Layout child : this.getChildren()) { child.layout(); + this.setHeight(this.getHeight() + child.getHeight()); // fixme } // todo: recurse to calculate cursor - this.setHeight(cursor.getY() - this.getY()); +// this.setHeight(cursor.getY() - this.getY()); +// System.out.println(this.getAssociatedNode().getData() + this.getLocation()); } public void setCursor(Point cursor) { diff --git a/src/main/model/layout/Layout.java b/src/main/model/layout/Layout.java index 4dbedb5..4d9f48f 100644 --- a/src/main/model/layout/Layout.java +++ b/src/main/model/layout/Layout.java @@ -86,7 +86,8 @@ public abstract class Layout { // man, fuck design patterns, this is so much goddamn code public void setLocation(Point point) { - this.location = point; + this.location.x = point.x; + this.location.y = point.y; } public void setX(double x) { diff --git a/src/main/ui/BrowserCanvas.java b/src/main/ui/BrowserCanvas.java index 1502c3d..a58f6e1 100644 --- a/src/main/ui/BrowserCanvas.java +++ b/src/main/ui/BrowserCanvas.java @@ -3,6 +3,8 @@ package ui; import model.html.ElementNode; import model.html.TextNode; import model.html.Node; +import model.layout.DocumentLayout; +import model.layout.Layout; import javax.swing.*; import java.awt.*; @@ -10,12 +12,22 @@ import java.util.*; public class BrowserCanvas extends JPanel { private ArrayList html; + private DocumentLayout currentLayout; // MODIFIES: this // EFFECTS: constructs a BrowserCanvas object public BrowserCanvas(ArrayList html) { super(); this.html = html; + this.currentLayout = Layout.constructTree(html); + printTree(this.currentLayout.getChildren()); + } + + private void printTree(ArrayList tree) { + for (Layout node : tree) { + System.out.println(System.identityHashCode(node.getLocation())); + printTree((node).getChildren()); + } } // EFFECTS: paints a component onto a canvas @@ -23,11 +35,11 @@ public class BrowserCanvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Point location = new Point(10, 20); // we need a mutable reference - renderHtml(html, g, location); + renderHtml(this.currentLayout.getChildren(), g, location); } // EFFECTS: naively renders our html file by printing text nodes - private void renderHtml(ArrayList html, Graphics g, Point location) { + /*private void renderHtml(ArrayList html, Graphics g, Point location) { for (Node node : html) { if (node instanceof TextNode) { if (node.getData().length() > 5) { @@ -38,6 +50,22 @@ public class BrowserCanvas extends JPanel { renderHtml(((ElementNode) node).getChildren(), g, location); } } + }*/ + + private void renderHtml(ArrayList tree, Graphics g, Point location) { + for (Layout layout : tree) { +// System.out.println(layout.getLocation()); + g.drawRect(layout.getLocation().x, layout.getLocation().y, layout.getDimension().width, layout.getDimension().height); + if (layout.getAssociatedNode() instanceof TextNode) { + if (layout.getAssociatedNode().getData().length() > 5) { +// System.out.println(location); + g.drawString(layout.getAssociatedNode().getData(), layout.getLocation().x, layout.getLocation().y); + g.drawString("X", 10, 20); + } + } else { + renderHtml(layout.getChildren(), g, location); + } + } } } diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java index 640fb1a..2375fc1 100644 --- a/src/main/ui/BrowserWindow.java +++ b/src/main/ui/BrowserWindow.java @@ -43,6 +43,7 @@ public class BrowserWindow extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); render("/home/apropos/Projects/website/j-james/index.html"); +// render("data/example.hctml"); // browserBar.addTab("/home/apropos/Projects/website/j-james/index.html"); setVisible(true); setClosingBehavior(); -- cgit v1.2.3-70-g09d2