diff --git a/addons/gdcef/demos/2D/CEF.gd b/addons/gdcef/demos/2D/CEF.gd index 5e37745..ce02ee0 100644 --- a/addons/gdcef/demos/2D/CEF.gd +++ b/addons/gdcef/demos/2D/CEF.gd @@ -6,29 +6,54 @@ extends Control -# Default pages +# URL const DEFAULT_PAGE = "user://default_page.html" -const RADIO_PAGE = "http://streaming.radio.co/s9378c22ee/listen" -# "https://www.programmes-radio.com/fr/stream-e8BxeoRhsz9jY9mXXRiFTE/ecouter-KPJK" +const SAVED_PAGE = "user://saved_page.html" const HOME_PAGE = "https://github.com/Lecrapouille/gdcef" +const RADIO_PAGE = "http://streaming.radio.co/s9378c22ee/listen" +#const RADIO_PAGE = "https://www.programmes-radio.com/fr/stream-e8BxeoRhsz9jY9mXXRiFTE/ecouter-KPJK" # The current browser as Godot node @onready var current_browser = null - # Memorize if the mouse was pressed @onready var mouse_pressed : bool = false -@onready var playback = null +# ============================================================================== +# Create the home page. +# ============================================================================== +func create_default_page(): + var file = FileAccess.open(DEFAULT_PAGE, FileAccess.WRITE) + file.store_string("

Welcome to gdCEF !

This a generated page.

") + file.close() + pass + +# ============================================================================== +# Save page as html. +# ============================================================================== +func _on_saving_page(html, brower): + var path = ProjectSettings.globalize_path(SAVED_PAGE) + var file = FileAccess.open(SAVED_PAGE, FileAccess.WRITE) + if (file != null): + file.store_string(html) + file.close() + $AcceptDialog.title = brower.get_url() + $AcceptDialog.dialog_text = "Page saved at:\n" + path + else: + $AcceptDialog.title = "Alert!" + $AcceptDialog.dialog_text = "Failed creating the file " + path + $AcceptDialog.popup_centered(Vector2(0,0)) + $AcceptDialog.show() + pass # ============================================================================== # Callback when a page has ended to load with success (200): we print a message # ============================================================================== -func _on_page_loaded(node): +func _on_page_loaded(brower): var L = $Panel/VBox/HBox/BrowserList - var url = node.get_url() + var url = brower.get_url() L.set_item_text(L.get_selected_id(), url) - $Panel/VBox/HBox2/Info.set_text(url + " loaded as ID " + node.name) - print("Browser named '" + node.name + "' inserted on list at index " + str(L.get_selected_id()) + ": " + url) + $Panel/VBox/HBox2/Info.set_text(url + " loaded as ID " + brower.name) + print("Browser named '" + brower.name + "' inserted on list at index " + str(L.get_selected_id()) + ": " + url) pass # ============================================================================== @@ -69,6 +94,7 @@ func create_browser(url): return null # Loading callbacks + browser.connect("on_html_content_requested", _on_saving_page) browser.connect("on_page_loaded", _on_page_loaded) browser.connect("on_page_failed_loading", _on_page_failed_loading) @@ -242,9 +268,15 @@ func _input(event): if current_browser == null: return if event is InputEventKey: - current_browser.set_key_pressed( - event.unicode if event.unicode != 0 else event.keycode, # Godot3: event.scancode, - event.pressed, event.shift_pressed, event.alt_pressed, event.is_command_or_control_pressed()) + if event.is_command_or_control_pressed() && event.pressed && not event.echo: + if event.keycode == KEY_S: + # Will call the callback 'on_html_content_requested' + current_browser.request_html_content() + else: + current_browser.set_key_pressed( + event.unicode if event.unicode != 0 else event.keycode, + event.pressed, event.shift_pressed, event.alt_pressed, + event.is_command_or_control_pressed()) pass # ============================================================================== @@ -264,10 +296,7 @@ func _on_texture_rect_resized(): # Create a single briwser named "current_browser" that is attached as child node to $CEF. # ============================================================================== func _ready(): - # Create the home page - var file = FileAccess.open(DEFAULT_PAGE, FileAccess.WRITE) - file.store_string("

Welcome to gdCEF !

This a generated page.

") - file = null + create_default_page() # See API.md for more details. CEF Configuration is: # resource_path := {"artifacts", CEF_ARTIFACTS_FOLDER} diff --git a/addons/gdcef/demos/2D/CEF.tscn b/addons/gdcef/demos/2D/CEF.tscn index ae90709..c7fbd00 100644 --- a/addons/gdcef/demos/2D/CEF.tscn +++ b/addons/gdcef/demos/2D/CEF.tscn @@ -123,6 +123,7 @@ size_flags_horizontal = 3 mouse_filter = 0 [node name="ColorPopup" type="Popup" parent="."] +size = Vector2i(298, 471) [node name="ColorPicker" type="ColorPicker" parent="ColorPopup"] anchors_preset = 15 @@ -133,6 +134,8 @@ presets_visible = false [node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="."] +[node name="AcceptDialog" type="AcceptDialog" parent="."] + [connection signal="pressed" from="Panel/VBox/HBox/New" to="." method="_on_Add_pressed"] [connection signal="pressed" from="Panel/VBox/HBox/Home" to="." method="_on_Home_pressed"] [connection signal="pressed" from="Panel/VBox/HBox/Go" to="." method="_on_go_pressed"] diff --git a/addons/gdcef/gdcef/src/gdbrowser.cpp b/addons/gdcef/gdcef/src/gdbrowser.cpp index 6a24a03..b40f9db 100644 --- a/addons/gdcef/gdcef/src/gdbrowser.cpp +++ b/addons/gdcef/gdcef/src/gdbrowser.cpp @@ -36,6 +36,28 @@ # include #endif +//------------------------------------------------------------------------------ +// Visit the html content of the current page. +class Visitor : public CefStringVisitor +{ +public: + Visitor(GDBrowserView& node) + : m_node(node) + {} + + virtual void Visit(const CefString& string) override + { + godot::String html = string.ToString().c_str(); + + m_node.emit_signal("on_html_content_requested", html, &m_node); + } + +private: + + GDBrowserView& m_node; + IMPLEMENT_REFCOUNTING(Visitor); +}; + //------------------------------------------------------------------------------ // in a GDNative module, "_bind_methods" is replaced by the "_register_methods" // method CefRefPtr m_browser; this is used to expose various methods @@ -60,6 +82,7 @@ void GDBrowserView::_bind_methods() ClassDB::bind_method(D_METHOD("is_loaded"), &GDBrowserView::loaded); ClassDB::bind_method(D_METHOD("reload"), &GDBrowserView::reload); ClassDB::bind_method(D_METHOD("stop_loading"), &GDBrowserView::stopLoading); + ClassDB::bind_method(D_METHOD("request_html_content"), &GDBrowserView::requestHtmlContent); ClassDB::bind_method(D_METHOD("execute_javascript"), &GDBrowserView::executeJavaScript); ClassDB::bind_method(D_METHOD("has_previous_page"), &GDBrowserView::canNavigateBackward); ClassDB::bind_method(D_METHOD("has_next_page"), &GDBrowserView::canNavigateForward); @@ -91,6 +114,8 @@ void GDBrowserView::_bind_methods() ADD_SIGNAL(MethodInfo("on_page_failed_loading", PropertyInfo(Variant::BOOL, "aborted"), PropertyInfo(Variant::STRING, "err_msg"), PropertyInfo(Variant::OBJECT, "node"))); ADD_SIGNAL(MethodInfo("on_browser_paint", PropertyInfo(Variant::OBJECT, "node"))); + ADD_SIGNAL(MethodInfo("on_html_content_requested", PropertyInfo(Variant::STRING, "html"), + PropertyInfo(Variant::OBJECT, "node"))); } //------------------------------------------------------------------------------ @@ -358,6 +383,18 @@ void GDBrowserView::stopLoading() m_browser->StopLoad(); } +//------------------------------------------------------------------------------ +void GDBrowserView::requestHtmlContent() +{ + CefRefPtr visitor = new Visitor(*this); + if (m_browser && m_browser->GetMainFrame()) + { + m_browser->GetMainFrame()->GetSource(visitor); + } + + BROWSER_ERROR("Not possible to retrieving text"); +} + //------------------------------------------------------------------------------ void GDBrowserView::executeJavaScript(godot::String javascript) { diff --git a/addons/gdcef/gdcef/src/gdbrowser.hpp b/addons/gdcef/gdcef/src/gdbrowser.hpp index c46c4a9..137d15d 100644 --- a/addons/gdcef/gdcef/src/gdbrowser.hpp +++ b/addons/gdcef/gdcef/src/gdbrowser.hpp @@ -333,6 +333,12 @@ class GDBrowserView : public godot::Node // ------------------------------------------------------------------------- bool reload() const; + // ------------------------------------------------------------------------- + //! \brief Request the HTML content of the page. The result is given by the + //! Godot callback + // ------------------------------------------------------------------------- + void requestHtmlContent(); + // ------------------------------------------------------------------------- //! \brief Exported method to Godot script. Execute Execute a string of // JavaScript code in this browser.