mirror of
https://github.com/lihop/godot-xterm.git
synced 2025-05-04 12:14:24 +02:00
Add HTML5 support
This commit is contained in:
parent
fbb23661d3
commit
bb8d40df58
18 changed files with 284 additions and 31 deletions
|
@ -18,7 +18,7 @@ const TITLE_WIDTH = 42
|
|||
|
||||
var menu_items := [
|
||||
{"name": "Asciicast", "scene": preload("../asciicast/asciicast.tscn")},
|
||||
{"name": "Terminal", "scene": preload("../terminal/terminal.tscn")},
|
||||
{"name": "Terminal", "scene": preload("../web_console/web_console.tscn") if OS.has_feature("JavaScript") else preload("../terminal/terminal.tscn")},
|
||||
{"name": "Exit"}
|
||||
]
|
||||
|
||||
|
@ -140,15 +140,19 @@ func _on_Terminal_key_pressed(data: String, event: InputEventKey) -> void:
|
|||
"Terminal not Supported on Windows"
|
||||
)
|
||||
var scene = item.scene.instance()
|
||||
var pty = scene.get_node("Pseudoterminal")
|
||||
var pty = scene if OS.has_feature("JavaScript") else scene.get_node("Pseudoterminal")
|
||||
get_tree().get_root().add_child(scene)
|
||||
visible = false
|
||||
scene.grab_focus()
|
||||
print("await the exit!")
|
||||
yield(pty, "exited")
|
||||
print("a got here")
|
||||
visible = true
|
||||
$Terminal.grab_focus()
|
||||
scene.queue_free()
|
||||
"Exit":
|
||||
if OS.has_feature("JavaScript"):
|
||||
JavaScript.eval("window.location.reload()")
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ cols = 102
|
|||
|
||||
[node name="Pseudoterminal" type="Node" parent="."]
|
||||
script = ExtResource( 2 )
|
||||
|
||||
[connection signal="data_sent" from="." to="Pseudoterminal" method="write"]
|
||||
[connection signal="size_changed" from="." to="Pseudoterminal" method="resize"]
|
||||
[connection signal="data_sent" from="Pseudoterminal" to="." method="write"]
|
||||
|
|
76
examples/web_console/web_console.gd
Normal file
76
examples/web_console/web_console.gd
Normal file
|
@ -0,0 +1,76 @@
|
|||
extends Node
|
||||
|
||||
signal exited(status)
|
||||
|
||||
var line := ""
|
||||
var _tput: TPut
|
||||
|
||||
onready var terminal = $Terminal
|
||||
onready var _has_js: bool = OS.has_feature("JavaScript")
|
||||
|
||||
|
||||
func prompt(prompt: String):
|
||||
_tput.setaf(TPut.ANSIColor.bright_green)
|
||||
terminal.write(prompt)
|
||||
_tput.sgr0()
|
||||
|
||||
|
||||
func _ready():
|
||||
_tput = TPut.new(terminal)
|
||||
_tput.setaf(TPut.ANSIColor.bright_cyan)
|
||||
terminal.write("*** Web Console ***\r\n\r\n")
|
||||
_tput.setaf(TPut.ANSIColor.yellow)
|
||||
terminal.write("Input will be passed to Godot's JavaScript.eval() function.\r\n")
|
||||
terminal.write("Some things you could try:\r\n\r\n")
|
||||
terminal.write(">> 1 + 1\r\n")
|
||||
terminal.write(">> window.location\r\n")
|
||||
terminal.write(">> alert('Hello')\r\n\r\n")
|
||||
_tput.setaf(TPut.ANSIColor.bright_cyan)
|
||||
terminal.write("Enter 'q', 'quit', or 'exit' to return to the previous menu.\r\n\r\n")
|
||||
_tput.sgr0()
|
||||
prompt(">> ")
|
||||
terminal.grab_focus()
|
||||
|
||||
|
||||
func grab_focus():
|
||||
terminal.grab_focus()
|
||||
|
||||
|
||||
func _on_Terminal_key_pressed(_data, event: InputEventKey):
|
||||
if not event:
|
||||
return
|
||||
|
||||
# For some reason, data String is malformed on HTML5, so only use event.unicode.
|
||||
var data = char(event.unicode)
|
||||
|
||||
match event.scancode:
|
||||
KEY_ENTER:
|
||||
terminal.write("\r\n")
|
||||
|
||||
if line == "q" or line == "quit" or line == "exit":
|
||||
return emit_signal("exited", 0)
|
||||
|
||||
if not _has_js:
|
||||
var msg := "WebConsole only available in HTML5 build."
|
||||
push_error(msg)
|
||||
_tput.setaf(TPut.ANSIColor.red)
|
||||
terminal.write(msg)
|
||||
_tput.sgr0()
|
||||
prompt("\r\n>> ")
|
||||
else:
|
||||
var json = JavaScript.eval("JSON.stringify(%s)" % line, true)
|
||||
_tput.setaf(TPut.ANSIColor.magenta)
|
||||
terminal.write(str(json))
|
||||
_tput.sgr0()
|
||||
|
||||
line = ""
|
||||
#_tput.srg0()
|
||||
prompt("\r\n>> ")
|
||||
|
||||
KEY_BACKSPACE:
|
||||
if line.length() > 0:
|
||||
terminal.write("\b \b")
|
||||
line = line.substr(0, line.length() - 1)
|
||||
_:
|
||||
line += data
|
||||
terminal.write(data)
|
27
examples/web_console/web_console.tscn
Normal file
27
examples/web_console/web_console.tscn
Normal file
|
@ -0,0 +1,27 @@
|
|||
[gd_scene load_steps=4 format=2]
|
||||
|
||||
[ext_resource path="res://addons/godot_xterm/themes/default_dark.theme" type="Theme" id=1]
|
||||
[ext_resource path="res://addons/godot_xterm/nodes/terminal/terminal.gdns" type="Script" id=2]
|
||||
[ext_resource path="res://examples/web_console/web_console.gd" type="Script" id=3]
|
||||
|
||||
[node name="WebConsole" type="Control"]
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
script = ExtResource( 3 )
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
|
||||
[node name="Terminal" type="Control" parent="."]
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
focus_mode = 2
|
||||
theme = ExtResource( 1 )
|
||||
script = ExtResource( 2 )
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
rows = 31
|
||||
cols = 102
|
||||
|
||||
[connection signal="key_pressed" from="Terminal" to="." method="_on_Terminal_key_pressed"]
|
Loading…
Add table
Add a link
Reference in a new issue