From b5c01b87030911ad19bbdf7058405b08f0b6d7f8 Mon Sep 17 00:00:00 2001 From: Zak Date: Thu, 18 Jul 2019 14:34:33 +0300 Subject: [PATCH] Add `uri` property for LinkButton MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: RĂ©mi Verschelde (cherry picked from commit d73a9b56b08864b5e5ccf0df910190b064ff7463) --- doc/classes/LinkButton.xml | 11 ++++++++++- editor/editor_property_name_processor.cpp | 1 + scene/gui/link_button.cpp | 21 ++++++++++++++++++++- scene/gui/link_button.h | 4 ++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/doc/classes/LinkButton.xml b/doc/classes/LinkButton.xml index 649c70ee0..6b029c5b7 100644 --- a/doc/classes/LinkButton.xml +++ b/doc/classes/LinkButton.xml @@ -18,7 +18,16 @@ The button's text that will be displayed inside the button's area. - Determines when to show the underline. See [enum UnderlineMode] for options. + The underline mode to use for the text. See [enum LinkButton.UnderlineMode] for the available modes. + + + The [url=https://en.wikipedia.org/wiki/Uniform_Resource_Identifier]URI[/url] for this [LinkButton]. If set to a valid URI, pressing the button opens the URI using the operating system's default program for the protocol (via [method OS.shell_open]). HTTP and HTTPS URLs open the default web browser. + [b]Examples:[/b] + [codeblock] + uri = "https://godotengine.org" # Opens the URL in the default web browser. + uri = "C:\SomeFolder" # Opens the file explorer at the given path. + uri = "C:\SomeImage.png" # Opens the given image in the default viewing app. + [/codeblock] diff --git a/editor/editor_property_name_processor.cpp b/editor/editor_property_name_processor.cpp index f62918b6a..b8b2b064e 100644 --- a/editor/editor_property_name_processor.cpp +++ b/editor/editor_property_name_processor.cpp @@ -136,6 +136,7 @@ EditorPropertyNameProcessor::EditorPropertyNameProcessor() { capitalize_string_remaps["rcedit"] = "rcedit"; capitalize_string_remaps["rcodesign"] = "rcodesign"; capitalize_string_remaps["signtool"] = "signtool"; + capitalize_string_remaps["uri"] = "URI"; capitalize_string_remaps["wine"] = "wine"; } diff --git a/scene/gui/link_button.cpp b/scene/gui/link_button.cpp index 5bb65ce40..e9e5866cf 100644 --- a/scene/gui/link_button.cpp +++ b/scene/gui/link_button.cpp @@ -31,6 +31,7 @@ #include "link_button.h" #include "core/string/translation.h" +#include "core/os/os.h" #include "core/input/shortcut.h" void LinkButton::set_text(const String &p_text) { @@ -48,6 +49,14 @@ String LinkButton::get_text() const { return text; } +void LinkButton::set_uri(const String &p_uri) { + uri = p_uri; +} + +String LinkButton::get_uri() const { + return uri; +} + void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) { underline_mode = p_underline_mode; update(); @@ -57,6 +66,14 @@ LinkButton::UnderlineMode LinkButton::get_underline_mode() const { return underline_mode; } +void LinkButton::pressed() { + if (uri.empty()) { + return; + } + + OS::get_singleton()->shell_open(uri); +} + Size2 LinkButton::get_minimum_size() const { return get_theme_font("font")->get_string_size(xl_text); } @@ -130,7 +147,8 @@ void LinkButton::_notification(int p_what) { void LinkButton::_bind_methods() { ClassDB::bind_method(D_METHOD("set_text", "text"), &LinkButton::set_text); ClassDB::bind_method(D_METHOD("get_text"), &LinkButton::get_text); - + ClassDB::bind_method(D_METHOD("set_uri", "uri"), &LinkButton::set_uri); + ClassDB::bind_method(D_METHOD("get_uri"), &LinkButton::get_uri); ClassDB::bind_method(D_METHOD("set_underline_mode", "underline_mode"), &LinkButton::set_underline_mode); ClassDB::bind_method(D_METHOD("get_underline_mode"), &LinkButton::get_underline_mode); @@ -140,6 +158,7 @@ void LinkButton::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text"); ADD_PROPERTY(PropertyInfo(Variant::INT, "underline", PROPERTY_HINT_ENUM, "Always,On Hover,Never"), "set_underline_mode", "get_underline_mode"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "uri"), "set_uri", "get_uri"); } LinkButton::LinkButton() { diff --git a/scene/gui/link_button.h b/scene/gui/link_button.h index 54f8dadcc..0daf04b35 100644 --- a/scene/gui/link_button.h +++ b/scene/gui/link_button.h @@ -46,8 +46,10 @@ private: String text; String xl_text; UnderlineMode underline_mode; + String uri; protected: + virtual void pressed(); virtual Size2 get_minimum_size() const; void _notification(int p_what); static void _bind_methods(); @@ -55,6 +57,8 @@ protected: public: void set_text(const String &p_text); String get_text() const; + void set_uri(const String &p_uri); + String get_uri() const; void set_underline_mode(UnderlineMode p_underline_mode); UnderlineMode get_underline_mode() const;