Procházet zdrojové kódy

Add `uri` property for LinkButton

Co-authored-by: Rémi Verschelde <[email protected]>
(cherry picked from commit d73a9b56b08864b5e5ccf0df910190b064ff7463)
Zak před 6 roky
rodič
revize
f974bcf074

+ 10 - 1
doc/classes/LinkButton.xml

@@ -18,7 +18,16 @@
 			The button's text that will be displayed inside the button's area.
 		</member>
 		<member name="underline" type="int" setter="set_underline_mode" getter="get_underline_mode" enum="LinkButton.UnderlineMode" default="0">
-			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.
+		</member>
+		<member name="uri" type="String" setter="set_uri" getter="get_uri" default="&quot;&quot;">
+			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]
 		</member>
 	</members>
 	<constants>

+ 1 - 0
editor/editor_property_name_processor.cpp

@@ -230,6 +230,7 @@ EditorPropertyNameProcessor::EditorPropertyNameProcessor() {
 	capitalize_string_remaps["tcp"] = "TCP";
 	capitalize_string_remaps["tls"] = "TLS";
 	capitalize_string_remaps["ui"] = "UI";
+	capitalize_string_remaps["uri"] = "URI";
 	capitalize_string_remaps["url"] = "URL";
 	capitalize_string_remaps["urls"] = "URLs";
 	capitalize_string_remaps["us"] = String::utf8("(µs)"); // Unit.

+ 20 - 1
scene/gui/link_button.cpp

@@ -30,6 +30,7 @@
 
 #include "link_button.h"
 
+#include "core/os/os.h"
 #include "core/translation.h"
 
 void LinkButton::set_text(const String &p_text) {
@@ -47,6 +48,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();
@@ -56,6 +65,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_font("font")->get_string_size(xl_text);
 }
@@ -129,7 +146,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);
 
@@ -139,6 +157,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() {

+ 4 - 0
scene/gui/link_button.h

@@ -48,8 +48,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();
@@ -57,6 +59,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;