diff --git a/document.tscn b/document.tscn index 741a672..69a19a0 100644 --- a/document.tscn +++ b/document.tscn @@ -23,8 +23,11 @@ [node name="Root" type="Node2D"] script = ExtResource("1_i3q73") -[node name="Document" type="Node2D" parent="."] +[node name="Document" type="Node2D" parent="." node_paths=PackedStringArray("http_request", "address_box")] +position = Vector2(10, 10) script = ExtResource("1_t74iv") +http_request = NodePath("../HTTPRequest") +address_box = NodePath("../Control/PanelContainer/VBoxContainer/TextEdit") [node name="Floor" type="StaticBody2D" parent="."] position = Vector2(0, 1080) @@ -71,9 +74,9 @@ text = "Address" [node name="TextEdit" type="TextEdit" parent="Control/PanelContainer/VBoxContainer"] -custom_minimum_size = Vector2(0, 36) +custom_minimum_size = Vector2(0, 43) layout_mode = 2 -text = "http://google.com" +text = "http://info.cern.ch/hypertext/WWW/TheProject.html" [node name="HSeparator2" type="HSeparator" parent="Control/PanelContainer/VBoxContainer"] custom_minimum_size = Vector2(0, 15.185) @@ -97,4 +100,6 @@ layout_mode = 2 text = "Refresh" +[node name="HTTPRequest" type="HTTPRequest" parent="."] + [connection signal="pressed" from="Control/PanelContainer/VBoxContainer/Button" to="Document" method="on_refresh"] diff --git a/scripts/document.gd b/scripts/document.gd index 391db3b..e89f97a 100644 --- a/scripts/document.gd +++ b/scripts/document.gd @@ -1,5 +1,8 @@ extends Node2D +@export var http_request: HTTPRequest +@export var address_box: TextEdit + func _create_block_node(block: Layout.BlockNode) -> Node: var box = Control.new() box.name = block.dom_node.tag_name + "-" + str(randi_range(0, 1000)) @@ -43,12 +46,8 @@ # _create_text_fragment(box, text_fragment) _create_text_fragment_with_rigid_body(box, text_fragment) -func on_refresh() -> void: - for child in get_children(): - remove_child(child) - - var file = FileAccess.open("res://TheProject.html", FileAccess.READ); - var source = file.get_as_text() +func _on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray): + var source = body.get_string_from_utf8() var parser = Parser.new(source) var dom_tree = DOM.build_dom_tree(parser) @@ -56,5 +55,11 @@ layout_tree.layout(1000) _create_block(self, layout_tree) +func on_refresh() -> void: + for child in get_children(): + remove_child(child) + http_request.request(address_box.text) + func _ready(): + http_request.request_completed.connect(_on_request_completed) on_refresh() diff --git a/scripts/layout.gd b/scripts/layout.gd index 2ea8981..4e58d32 100644 --- a/scripts/layout.gd +++ b/scripts/layout.gd @@ -11,6 +11,10 @@ 'dd', ] +const NON_LAYOUT_NODES = [ + 'title', +] + static func _text_size(text: String, style: Style) -> Vector2: var font = Label.new().get_theme_default_font() return font.get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, style.font_size) @@ -138,6 +142,9 @@ block.block_children.append(_build_layout_tree_block(child)) continue + if child.tag_name in NON_LAYOUT_NODES: + continue + if child.tag_name == 'TEXT' and len(child.text.strip_edges()) == 0: continue # Ignore whitespace text. block.inline_children.append(InlineNode.new(child)) @@ -149,8 +156,7 @@ static func build_layout_tree(dom: DOM.DomNode) -> BlockNode: var body = dom.find('body') - assert(body != null) + if body == null: + body = dom - var html = BlockNode.new(dom) - html.block_children.append(_build_layout_tree_block(body)) - return html + return _build_layout_tree_block(body)