Skip to content

Commit

Permalink
WIP: Reorganize demos #19
Browse files Browse the repository at this point in the history
Demo "CEF in 2D" is now nammed "Hello CEF" (like initial).
Add real demo "CEF in 2D" that will show a full 2D browser.
This demo will be used for showing all features (ideally).
  • Loading branch information
Lecrapouille committed Nov 23, 2022
1 parent 5cabdb4 commit 4d70d18
Show file tree
Hide file tree
Showing 15 changed files with 544 additions and 5 deletions.
155 changes: 155 additions & 0 deletions addons/gdcef/demos/2D/CEF.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# ==============================================================================
# Demo based on the initial asset https://godotengine.org/asset-library/asset/127
# Basic application showing how to use CEF inside Godot with a 3D scene and mouse
# and keyboard events.
# ==============================================================================

extends Control

# Name of the browser
const browser1 = "browser1"

# Memorize if the mouse was pressed
var mouse_pressed : bool = false

# ==============================================================================
# Home button pressed: get the browser node and load a new page.
# ==============================================================================
func _on_Home_pressed():
var browser = $CEF.get_node(browser1)
if browser == null:
$Panel/Label.set_text("Failed getting Godot node " + browser1)
return
browser.load_url("https://bitbucket.org/chromiumembedded/cef/wiki/Home")
pass

# ==============================================================================
# Go to previously visited page
# ==============================================================================
func _on_Prev_pressed():
var browser = $CEF.get_node(browser1)
if browser == null:
$Panel/Label.set_text("Failed getting Godot node " + browser1)
return
browser.previous_page()
pass

# ==============================================================================
# Go to next page
# ==============================================================================
func _on_Next_pressed():
var browser = $CEF.get_node(browser1)
if browser == null:
$Panel/Label.set_text("Failed getting Godot node " + browser1)
return
browser.next_page()
pass

# ==============================================================================
# Callback when a page has ended to load: we print a message
# ==============================================================================
func _on_page_loaded(node):
$Panel/Label.set_text(node.name + ": page " + node.get_url() + " loaded")

# ==============================================================================
# On new URL entered
# ==============================================================================
func _on_TextEdit_text_changed(new_text):
var browser = $CEF.get_node(browser1)
if browser == null:
$Panel/Label.set_text("Failed getting Godot node " + browser1)
return
browser.load_url(new_text)

# ==============================================================================
# Get mouse events and broadcast them to CEF
# ==============================================================================
func _on_TextureRect_gui_input(event):
var browser = $CEF.get_node(browser1)
if browser == null:
$Panel/Label.set_text("Failed getting Godot node " + browser1)
return
if event is InputEventMouseButton:
if event.button_index == BUTTON_WHEEL_UP:
browser.on_mouse_wheel_vertical(2)
elif event.button_index == BUTTON_WHEEL_DOWN:
browser.on_mouse_wheel_vertical(-2)
elif event.button_index == BUTTON_LEFT:
mouse_pressed = event.pressed
if event.pressed == true:
browser.on_mouse_left_down()
else:
browser.on_mouse_left_up()
elif event.button_index == BUTTON_RIGHT:
mouse_pressed = event.pressed
if event.pressed == true:
browser.on_mouse_right_down()
else:
browser.on_mouse_right_up()
else:
mouse_pressed = event.pressed
if event.pressed == true:
browser.on_mouse_middle_down()
else:
browser.on_mouse_middle_up()
elif event is InputEventMouseMotion:
if mouse_pressed == true :
browser.on_mouse_left_down()
browser.on_mouse_moved(event.position.x, event.position.y)
pass

# ==============================================================================
# Make the CEF browser reacts from keyboard events.
# ==============================================================================
func _input(event):
var browser = $CEF.get_node(browser1)
if browser == null:
$Panel/Label.set_text("Failed getting Godot node " + browser1)
return
if event is InputEventKey:
if event.unicode != 0:
browser.on_key_pressed(event.unicode, event.pressed, event.shift, event.alt, event.control)
else:
browser.on_key_pressed(event.scancode, event.pressed, event.shift, event.alt, event.control)

pass

# ==============================================================================
# Create a single briwser named "browser1" that is attached as child node to $CEF.
# ==============================================================================
func _ready():

# Configuration are:
# resource_path := {"artifacts", CEF_ARTIFACTS_FOLDER}
# resource_path := {"exported_artifacts", application_real_path()}
# {"incognito":false}
# {"cache_path", resource_path / "cache"}
# {"root_cache_path", resource_path / "cache"}
# {"browser_subprocess_path", resource_path / SUBPROCESS_NAME }
# {"log_file", resource_path / "debug.log"}
# {log_severity", "warning"}
# {"remote_debugging_port", 7777}
# {"exception_stack_size", 5}
#
# Configurate CEF. In incognito mode cache directories not used and in-memory
# caches are used instead and no data is persisted to disk.
#
# artifacts: allows path such as "build" or "res://build/". Note that "res://"
# will use ProjectSettings.globalize_path but exported projects don't support globalize_path:
# https://docs.godotengine.org/en/3.5/classes/class_projectsettings.html#class-projectsettings-method-globalize-path
var resource_path = "res://build/"
if !$CEF.initialize({"artifacts":resource_path, "incognito":true, "locale":"en-US"}):
push_error("Failed initializing CEF")
get_tree().quit()
pass

var S = $Panel/TextureRect.get_size()
var browser = $CEF.create_browser("https://github.com/Lecrapouille/gdcef", browser1, S.x, S.y, {"javascript":true})
browser.connect("page_loaded", self, "_on_page_loaded")
$Panel/TextureRect.texture = browser.get_texture()

# ==============================================================================
# $CEF is periodically updated
# ==============================================================================
func _process(_delta):
pass
79 changes: 79 additions & 0 deletions addons/gdcef/demos/2D/CEF.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[gd_scene load_steps=3 format=2]

[ext_resource path="res://CEF.gd" type="Script" id=1]
[ext_resource path="res://libs/gdcef.gdns" type="Script" id=2]

[node name="GUI" type="Control"]
margin_right = 280.0
margin_bottom = 180.0
mouse_filter = 1
script = ExtResource( 1 )

[node name="CEF" type="Node" parent="."]
script = ExtResource( 2 )

[node name="Panel" type="Panel" parent="."]
anchor_right = 2.504
anchor_bottom = 1.856
margin_right = 323.88
margin_bottom = 262.92

[node name="TextureRect" type="TextureRect" parent="Panel"]
anchor_top = 0.015
anchor_bottom = 0.015
margin_left = 5.0
margin_top = 28.045
margin_right = 1019.0
margin_bottom = 559.045
mouse_filter = 0
expand = true

[node name="Label" type="Label" parent="Panel"]
margin_left = 5.0
margin_top = 578.0
margin_right = 82.0
margin_bottom = 592.0
mouse_filter = 0
text = "Hello world!"

[node name="Label2" type="Label" parent="Panel"]
margin_left = 7.0
margin_top = 7.0
margin_right = 41.0
margin_bottom = 21.0
mouse_filter = 0
text = "URL:"

[node name="Home" type="Button" parent="Panel"]
margin_left = 208.0
margin_top = 3.0
margin_right = 259.0
margin_bottom = 25.0
text = "Home"

[node name="Prev" type="Button" parent="Panel"]
margin_left = 186.0
margin_top = 3.0
margin_right = 206.0
margin_bottom = 25.0
text = "<"

[node name="Next" type="Button" parent="Panel"]
margin_left = 259.0
margin_top = 3.0
margin_right = 279.0
margin_bottom = 25.0
text = ">"

[node name="TextEdit" type="LineEdit" parent="Panel"]
margin_left = 43.0
margin_top = 2.0
margin_right = 185.0
margin_bottom = 26.0

[connection signal="gui_input" from="Panel/TextureRect" to="." method="_on_TextureRect_gui_input"]
[connection signal="pressed" from="Panel/Home" to="." method="_on_Home_pressed"]
[connection signal="pressed" from="Panel/Prev" to="." method="_on_Prev_pressed"]
[connection signal="pressed" from="Panel/Next" to="." method="_on_Prev_pressed"]
[connection signal="pressed" from="Panel/Next" to="." method="_on_Next_pressed"]
[connection signal="text_changed" from="Panel/TextEdit" to="." method="_on_TextEdit_text_changed"]
Binary file modified addons/gdcef/demos/2D/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion addons/gdcef/demos/2D/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _global_script_class_icons={
[application]

config/name="CEF in 2D"
run/main_scene="res://Control.tscn"
run/main_scene="res://CEF.tscn"
config/icon="res://icon.png"

[physics]
Expand Down
2 changes: 1 addition & 1 deletion addons/gdcef/demos/3D/CEF.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ margin_bottom = 26.0
[connection signal="gui_input" from="Panel/TextureRect" to="." method="_on_TextureRect_gui_input"]
[connection signal="pressed" from="Panel/Home" to="." method="_on_Home_pressed"]
[connection signal="pressed" from="Panel/Prev" to="." method="_on_Prev_pressed"]
[connection signal="pressed" from="Panel/Next" to="." method="_on_Next_pressed"]
[connection signal="pressed" from="Panel/Next" to="." method="_on_Prev_pressed"]
[connection signal="pressed" from="Panel/Next" to="." method="_on_Next_pressed"]
[connection signal="text_changed" from="Panel/TextEdit" to="." method="_on_TextEdit_text_changed"]
Loading

0 comments on commit 4d70d18

Please sign in to comment.