Skip to content

Commit 34fb6ab

Browse files
committed
Add run code for python mode 🔥
1 parent 8638a71 commit 34fb6ab

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

‎modes/python/mode.gd‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var code_regions: Array[Array] = [
1616

1717
func _initialize_mode() -> Error:
1818
_initialize_highlighter()
19-
_setup_panel()
19+
panel = U.load_resource("user://modes/python/panel.tscn").instantiate()
2020
comment_delimiters.append({
2121
"start_key": "#",
2222
"end_key": "",
@@ -105,23 +105,3 @@ func _initialize_highlighter() -> void:
105105
for region in code_regions:
106106
syntax_highlighter.add_color_region(region[1], region[2], region[0], region[3])
107107

108-
109-
func _setup_panel() -> void:
110-
panel = TextForgePanel.new()
111-
var run = Button.new()
112-
var output = TextEdit.new()
113-
panel.add_child(VBoxContainer.new())
114-
panel.get_child(0).size = Vector2(100, 300)
115-
panel.get_child(0).add_child(run)
116-
panel.get_child(0).add_child(output)
117-
run.size_flags_horizontal = Control.SIZE_FILL
118-
output.size_flags_horizontal = Control.SIZE_FILL
119-
output.size_flags_vertical = Control.SIZE_EXPAND_FILL
120-
run.text = "Run Code"
121-
run.pressed.connect(_execute.bind(output))
122-
123-
124-
func _execute(output) -> void:
125-
var out: Array
126-
OS.execute("python", [Global.get_file_path()], out)
127-
output.text = "\n---\n".join(out)

‎modes/python/panel.gd‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extends TextForgePanel
2+
3+
4+
func _on_button_pressed() -> void:
5+
var output: Array
6+
OS.execute("python", [Global.get_file_path()], output)
7+
await get_tree().process_frame
8+
$MarginContainer/VBoxContainer/Output.text = "\n\n".join(output)

‎modes/python/panel.tscn‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://ck6ro32uyxst8"]
2+
3+
[ext_resource type="Script" uid="uid://ckwbxp7rqbsdi" path="user://modes/python/panel.gd" id="1_y26ic"]
4+
5+
[node name="Panel" type="Control"]
6+
custom_minimum_size = Vector2(300, 0)
7+
layout_mode = 3
8+
anchors_preset = 15
9+
anchor_right = 1.0
10+
anchor_bottom = 1.0
11+
grow_horizontal = 2
12+
grow_vertical = 2
13+
script = ExtResource("1_y26ic")
14+
15+
[node name="MarginContainer" type="MarginContainer" parent="."]
16+
layout_mode = 1
17+
anchors_preset = 15
18+
anchor_right = 1.0
19+
anchor_bottom = 1.0
20+
grow_horizontal = 2
21+
grow_vertical = 2
22+
23+
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
24+
layout_mode = 2
25+
text = "Output..."
26+
27+
[node name="Button" type="Button" parent="MarginContainer/VBoxContainer"]
28+
layout_mode = 2
29+
text = "Run code (by python)"
30+
31+
[node name="Output" type="RichTextLabel" parent="MarginContainer/VBoxContainer"]
32+
layout_mode = 2
33+
size_flags_vertical = 3
34+
text = "Run to see output..."
35+
36+
[connection signal="pressed" from="MarginContainer/VBoxContainer/Button" to="." method="_on_button_pressed"]

0 commit comments

Comments
 (0)