-Work on addon editor plugin (disabled by default)
-New HTTPRequest node, to make HTTP requests simpler.
@ -31,10 +31,6 @@
|
||||
|
||||
VARIANT_ENUM_CAST(HTTPClient::Status);
|
||||
|
||||
Error HTTPClient::connect_url(const String& p_url) {
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){
|
||||
|
||||
|
@ -164,7 +164,7 @@ private:
|
||||
public:
|
||||
|
||||
|
||||
Error connect_url(const String& p_url); //connects to a full url and perform request
|
||||
//Error connect_and_get(const String& p_url,bool p_verify_host=true); //connects to a full url and perform request
|
||||
Error connect(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true);
|
||||
|
||||
void set_connection(const Ref<StreamPeer>& p_connection);
|
||||
|
124
scene/gui/link_button.cpp
Normal file
@ -0,0 +1,124 @@
|
||||
#include "link_button.h"
|
||||
|
||||
|
||||
void LinkButton::set_text(const String& p_text) {
|
||||
|
||||
text=p_text;
|
||||
update();
|
||||
minimum_size_changed();
|
||||
}
|
||||
|
||||
String LinkButton::get_text() const {
|
||||
return text;
|
||||
}
|
||||
|
||||
void LinkButton::set_underline_mode(UnderlineMode p_underline_mode) {
|
||||
|
||||
underline_mode=p_underline_mode;
|
||||
update();
|
||||
}
|
||||
|
||||
LinkButton::UnderlineMode LinkButton::get_underline_mode() const {
|
||||
|
||||
return underline_mode;
|
||||
}
|
||||
|
||||
|
||||
Size2 LinkButton::get_minimum_size() const {
|
||||
|
||||
return get_font("font")->get_string_size( text );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LinkButton::_notification(int p_what) {
|
||||
|
||||
switch( p_what ) {
|
||||
|
||||
case NOTIFICATION_DRAW: {
|
||||
|
||||
|
||||
RID ci = get_canvas_item();
|
||||
Size2 size=get_size();
|
||||
Color color;
|
||||
bool do_underline=false;
|
||||
|
||||
//print_line(get_text()+": "+itos(is_flat())+" hover "+itos(get_draw_mode()));
|
||||
|
||||
switch( get_draw_mode() ) {
|
||||
|
||||
case DRAW_NORMAL: {
|
||||
|
||||
color=get_color("font_color");
|
||||
do_underline=underline_mode==UNDERLINE_MODE_ALWAYS;
|
||||
} break;
|
||||
case DRAW_PRESSED: {
|
||||
|
||||
if (has_color("font_color_pressed"))
|
||||
color=get_color("font_color_pressed");
|
||||
else
|
||||
color=get_color("font_color");
|
||||
|
||||
do_underline=true;
|
||||
|
||||
} break;
|
||||
case DRAW_HOVER: {
|
||||
|
||||
color=get_color("font_color_hover");
|
||||
do_underline=true;
|
||||
|
||||
} break;
|
||||
case DRAW_DISABLED: {
|
||||
|
||||
color=get_color("font_color_disabled");
|
||||
do_underline=underline_mode==UNDERLINE_MODE_ALWAYS;
|
||||
|
||||
} break;
|
||||
}
|
||||
|
||||
if (has_focus()) {
|
||||
|
||||
Ref<StyleBox> style = get_stylebox("focus");
|
||||
style->draw(ci,Rect2(Point2(),size));
|
||||
}
|
||||
|
||||
Ref<Font> font=get_font("font");
|
||||
|
||||
draw_string(font,Vector2(0,font->get_ascent()),text,color);
|
||||
|
||||
|
||||
|
||||
if (do_underline) {
|
||||
int underline_spacing = get_constant("underline_spacing");
|
||||
int width = font->get_string_size(text).width;
|
||||
int y = font->get_ascent()+underline_spacing;
|
||||
|
||||
draw_line(Vector2(0,y),Vector2(width,y),color);
|
||||
}
|
||||
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void LinkButton::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_text","text"),&LinkButton::set_text);
|
||||
ObjectTypeDB::bind_method(_MD("get_text"),&LinkButton::get_text);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_underline_mode","underline_mode"),&LinkButton::set_underline_mode);
|
||||
ObjectTypeDB::bind_method(_MD("get_underline_mode"),&LinkButton::get_underline_mode);
|
||||
|
||||
|
||||
BIND_CONSTANT( UNDERLINE_MODE_ALWAYS );
|
||||
BIND_CONSTANT( UNDERLINE_MODE_ON_HOVER );
|
||||
|
||||
ADD_PROPERTYNZ(PropertyInfo(Variant::STRING,"text"), _SCS("set_text"), _SCS("get_text"));
|
||||
ADD_PROPERTYNZ(PropertyInfo(Variant::INT,"underline",PROPERTY_HINT_ENUM,"Always,On Hover"), _SCS("set_underline_mode"), _SCS("get_underline_mode"));
|
||||
|
||||
}
|
||||
|
||||
LinkButton::LinkButton() {
|
||||
underline_mode=UNDERLINE_MODE_ALWAYS;
|
||||
set_focus_mode(FOCUS_NONE);
|
||||
set_default_cursor_shape(CURSOR_POINTING_HAND);
|
||||
}
|
40
scene/gui/link_button.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef LINKBUTTON_H
|
||||
#define LINKBUTTON_H
|
||||
|
||||
|
||||
#include "scene/gui/base_button.h"
|
||||
#include "scene/resources/bit_mask.h"
|
||||
|
||||
class LinkButton : public BaseButton {
|
||||
|
||||
OBJ_TYPE( LinkButton, BaseButton );
|
||||
public:
|
||||
|
||||
enum UnderlineMode {
|
||||
UNDERLINE_MODE_ALWAYS,
|
||||
UNDERLINE_MODE_ON_HOVER
|
||||
};
|
||||
private:
|
||||
String text;
|
||||
UnderlineMode underline_mode;
|
||||
|
||||
protected:
|
||||
|
||||
virtual Size2 get_minimum_size() const;
|
||||
void _notification(int p_what);
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
|
||||
void set_text(const String& p_text);
|
||||
String get_text() const;
|
||||
|
||||
void set_underline_mode(UnderlineMode p_underline_mode);
|
||||
UnderlineMode get_underline_mode() const;
|
||||
|
||||
LinkButton();
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST( LinkButton::UnderlineMode );
|
||||
|
||||
#endif // LINKBUTTON_H
|
426
scene/main/http_request.cpp
Normal file
@ -0,0 +1,426 @@
|
||||
#include "http_request.h"
|
||||
|
||||
void HTTPRequest::_redirect_request(const String& p_new_url) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Error HTTPRequest::_request() {
|
||||
|
||||
return client->connect(url,port,use_ssl,validate_ssl);
|
||||
}
|
||||
|
||||
Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_headers, bool p_ssl_validate_domain) {
|
||||
|
||||
ERR_FAIL_COND_V(!is_inside_tree(),ERR_UNCONFIGURED);
|
||||
if ( requesting ) {
|
||||
ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
|
||||
ERR_FAIL_V(ERR_BUSY);
|
||||
}
|
||||
|
||||
url=p_url;
|
||||
use_ssl=false;
|
||||
|
||||
request_string="";
|
||||
port=80;
|
||||
headers=p_custom_headers;
|
||||
request_sent=false;
|
||||
got_response=false;
|
||||
validate_ssl=p_ssl_validate_domain;
|
||||
body_len=-1;
|
||||
body.resize(0);
|
||||
redirections=0;
|
||||
|
||||
print_line("1 url: "+url);
|
||||
if (url.begins_with("http://")) {
|
||||
|
||||
url=url.substr(7,url.length()-7);
|
||||
print_line("no SSL");
|
||||
|
||||
} else if (url.begins_with("https://")) {
|
||||
url=url.substr(8,url.length()-8);
|
||||
use_ssl=true;
|
||||
port=443;
|
||||
print_line("yes SSL");
|
||||
} else {
|
||||
ERR_EXPLAIN("Malformed URL");
|
||||
ERR_FAIL_V(ERR_INVALID_PARAMETER);
|
||||
}
|
||||
|
||||
print_line("2 url: "+url);
|
||||
|
||||
int slash_pos = url.find("/");
|
||||
|
||||
if (slash_pos!=-1) {
|
||||
request_string=url.substr(slash_pos,url.length());
|
||||
url=url.substr(0,slash_pos);
|
||||
print_line("request string: "+request_string);
|
||||
} else {
|
||||
request_string="/";
|
||||
print_line("no request");
|
||||
}
|
||||
|
||||
print_line("3 url: "+url);
|
||||
|
||||
int colon_pos = url.find(":");
|
||||
if (colon_pos!=-1) {
|
||||
port=url.substr(colon_pos+1,url.length()).to_int();
|
||||
url=url.substr(0,colon_pos);
|
||||
ERR_FAIL_COND_V(port<1 || port > 65535,ERR_INVALID_PARAMETER);
|
||||
}
|
||||
|
||||
print_line("4 url: "+url);
|
||||
|
||||
bool has_user_agent=false;
|
||||
bool has_accept=false;
|
||||
|
||||
for(int i=0;i<headers.size();i++) {
|
||||
|
||||
if (headers[i].findn("user-agent:")==0)
|
||||
has_user_agent=true;
|
||||
if (headers[i].findn("Accept:")==0)
|
||||
has_accept=true;
|
||||
}
|
||||
|
||||
if (!has_user_agent) {
|
||||
headers.push_back("User-Agent: GodotEngine/"+String(VERSION_MKSTRING)+" ("+OS::get_singleton()->get_name()+")");
|
||||
}
|
||||
|
||||
if (!has_accept) {
|
||||
headers.push_back("Accept: */*");
|
||||
}
|
||||
|
||||
|
||||
Error err = _request();
|
||||
|
||||
if (err==OK) {
|
||||
set_process(true);
|
||||
requesting=true;
|
||||
}
|
||||
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
void HTTPRequest::cancel_request() {
|
||||
|
||||
if (!requesting)
|
||||
return;
|
||||
|
||||
if (!use_threads) {
|
||||
set_process(false);
|
||||
}
|
||||
|
||||
client->close();
|
||||
body.resize(0);
|
||||
got_response=false;
|
||||
response_code=-1;
|
||||
body_len=-1;
|
||||
request_sent=false;
|
||||
requesting=false;
|
||||
}
|
||||
|
||||
|
||||
bool HTTPRequest::_update_connection() {
|
||||
|
||||
switch( client->get_status() ) {
|
||||
case HTTPClient::STATUS_DISCONNECTED: {
|
||||
return true; //end it, since it's doing something
|
||||
} break;
|
||||
case HTTPClient::STATUS_RESOLVING: {
|
||||
client->poll();
|
||||
//must wait
|
||||
return false;
|
||||
} break;
|
||||
case HTTPClient::STATUS_CANT_RESOLVE: {
|
||||
call_deferred("emit_signal","request_completed",RESULT_CANT_RESOLVE,0,StringArray(),ByteArray());
|
||||
return true;
|
||||
|
||||
} break;
|
||||
case HTTPClient::STATUS_CONNECTING: {
|
||||
client->poll();
|
||||
//must wait
|
||||
return false;
|
||||
} break; //connecting to ip
|
||||
case HTTPClient::STATUS_CANT_CONNECT: {
|
||||
|
||||
call_deferred("emit_signal","request_completed",RESULT_CANT_CONNECT,0,StringArray(),ByteArray());
|
||||
return true;
|
||||
|
||||
} break;
|
||||
case HTTPClient::STATUS_CONNECTED: {
|
||||
|
||||
if (request_sent) {
|
||||
|
||||
if (!got_response) {
|
||||
|
||||
//no body
|
||||
|
||||
got_response=true;
|
||||
response_code=client->get_response_code();
|
||||
List<String> rheaders;
|
||||
client->get_response_headers(&rheaders);
|
||||
response_headers.resize(0);
|
||||
for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
|
||||
print_line("HEADER: "+E->get());
|
||||
response_headers.push_back(E->get());
|
||||
}
|
||||
|
||||
if (response_code==301) {
|
||||
//redirect
|
||||
if (max_redirects>=0 && redirections>=max_redirects) {
|
||||
|
||||
call_deferred("emit_signal","request_completed",RESULT_REDIRECT_LIMIT_REACHED,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
}
|
||||
|
||||
String new_request;
|
||||
|
||||
for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
|
||||
if (E->get().findn("Location: ")!=-1) {
|
||||
new_request=E->get().substr(9,E->get().length()).strip_edges();
|
||||
}
|
||||
}
|
||||
|
||||
print_line("NEW LOCATION: "+new_request);
|
||||
|
||||
if (new_request!="") {
|
||||
//process redirect
|
||||
client->close();
|
||||
request_string=new_request;
|
||||
int new_redirs=redirections+1; //because _request() will clear it
|
||||
Error err = _request();
|
||||
print_line("new connection: "+itos(err));
|
||||
if (err==OK) {
|
||||
request_sent=false;
|
||||
got_response=false;
|
||||
body_len=-1;
|
||||
body.resize(0);
|
||||
redirections=new_redirs;
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
}
|
||||
if (got_response && body_len<0) {
|
||||
//chunked transfer is done
|
||||
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,body);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
call_deferred("emit_signal","request_completed",RESULT_CHUNKED_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
//request migh have been done
|
||||
} else {
|
||||
//did not request yet, do request
|
||||
|
||||
Error err = client->request(HTTPClient::METHOD_GET,request_string,headers);
|
||||
if (err!=OK) {
|
||||
call_deferred("emit_signal","request_completed",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray());
|
||||
return true;
|
||||
}
|
||||
|
||||
request_sent=true;
|
||||
return false;
|
||||
}
|
||||
} break; //connected: { } break requests only accepted here
|
||||
case HTTPClient::STATUS_REQUESTING: {
|
||||
//must wait, it's requesting
|
||||
client->poll();
|
||||
return false;
|
||||
|
||||
} break; // request in progress
|
||||
case HTTPClient::STATUS_BODY: {
|
||||
|
||||
if (!got_response) {
|
||||
if (!client->has_response()) {
|
||||
call_deferred("emit_signal","request_completed",RESULT_NO_RESPONSE,0,StringArray(),ByteArray());
|
||||
return true;
|
||||
}
|
||||
|
||||
got_response=true;
|
||||
response_code=client->get_response_code();
|
||||
List<String> rheaders;
|
||||
client->get_response_headers(&rheaders);
|
||||
response_headers.resize(0);
|
||||
for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
|
||||
print_line("HEADER: "+E->get());
|
||||
response_headers.push_back(E->get());
|
||||
}
|
||||
|
||||
if (!client->is_response_chunked() && client->get_response_body_length()==0) {
|
||||
|
||||
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (client->is_response_chunked()) {
|
||||
body_len=-1;
|
||||
} else {
|
||||
body_len=client->get_response_body_length();
|
||||
|
||||
if (body_size_limit>=0 && body_len>body_size_limit) {
|
||||
call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//print_line("BODY: "+itos(body.size()));
|
||||
client->poll();
|
||||
|
||||
body.append_array(client->read_response_body_chunk());
|
||||
|
||||
if (body_size_limit>=0 && body.size()>body_size_limit) {
|
||||
call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (body_len>=0) {
|
||||
|
||||
if (body.size()==body_len) {
|
||||
call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,body);
|
||||
return true;
|
||||
}
|
||||
/*if (body.size()>=body_len) {
|
||||
call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray());
|
||||
return true;
|
||||
}*/
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
} break; // request resulted in body: { } break which must be read
|
||||
case HTTPClient::STATUS_CONNECTION_ERROR: {
|
||||
call_deferred("emit_signal","request_completed",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray());
|
||||
return true;
|
||||
} break;
|
||||
case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
|
||||
call_deferred("emit_signal","request_completed",RESULT_SSL_HANDSHAKE_ERROR,0,StringArray(),ByteArray());
|
||||
return true;
|
||||
} break;
|
||||
|
||||
}
|
||||
|
||||
ERR_FAIL_V(false);
|
||||
}
|
||||
|
||||
void HTTPRequest::_notification(int p_what) {
|
||||
|
||||
if (p_what==NOTIFICATION_PROCESS) {
|
||||
|
||||
bool done = _update_connection();
|
||||
if (done) {
|
||||
|
||||
set_process(false);
|
||||
cancel_request();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPRequest::set_use_threads(bool p_use) {
|
||||
|
||||
ERR_FAIL_COND( status!=HTTPClient::STATUS_DISCONNECTED );
|
||||
use_threads=p_use;
|
||||
}
|
||||
|
||||
bool HTTPRequest::is_using_threads() const {
|
||||
|
||||
return use_threads;
|
||||
}
|
||||
|
||||
void HTTPRequest::set_body_size_limit(int p_bytes) {
|
||||
|
||||
ERR_FAIL_COND( status!=HTTPClient::STATUS_DISCONNECTED );
|
||||
|
||||
body_size_limit=p_bytes;
|
||||
}
|
||||
|
||||
int HTTPRequest::get_body_size_limit() const {
|
||||
|
||||
return body_size_limit;
|
||||
}
|
||||
|
||||
HTTPClient::Status HTTPRequest::get_http_client_status() const {
|
||||
return client->get_status();
|
||||
}
|
||||
|
||||
void HTTPRequest::set_max_redirects(int p_max) {
|
||||
|
||||
max_redirects=p_max;
|
||||
}
|
||||
|
||||
int HTTPRequest::get_max_redirects() const{
|
||||
|
||||
return max_redirects;
|
||||
}
|
||||
|
||||
|
||||
void HTTPRequest::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("request","url","custom_headers","ssl_validate_domain"),&HTTPRequest::request,DEFVAL(StringArray()),DEFVAL(true));
|
||||
ObjectTypeDB::bind_method(_MD("cancel_request"),&HTTPRequest::cancel_request);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("get_http_client_status"),&HTTPRequest::get_http_client_status);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_use_threads","enable"),&HTTPRequest::set_use_threads);
|
||||
ObjectTypeDB::bind_method(_MD("is_using_threads"),&HTTPRequest::is_using_threads);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_body_size_limit","bytes"),&HTTPRequest::set_body_size_limit);
|
||||
ObjectTypeDB::bind_method(_MD("get_body_size_limit"),&HTTPRequest::get_body_size_limit);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_max_redirects","amount"),&HTTPRequest::set_max_redirects);
|
||||
ObjectTypeDB::bind_method(_MD("get_max_redirects"),&HTTPRequest::get_max_redirects);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("_redirect_request"),&HTTPRequest::_redirect_request);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::BOOL,"use_threads"),_SCS("set_use_threads"),_SCS("is_using_threads"));
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT,"body_size_limit",PROPERTY_HINT_RANGE,"-1,2000000000"),_SCS("set_body_size_limit"),_SCS("get_body_size_limit"));
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT,"max_redirects",PROPERTY_HINT_RANGE,"-1,1024"),_SCS("set_max_redirects"),_SCS("get_max_redirects"));
|
||||
|
||||
ADD_SIGNAL(MethodInfo("request_completed",PropertyInfo(Variant::INT,"result"),PropertyInfo(Variant::INT,"response_code"),PropertyInfo(Variant::STRING_ARRAY,"headers"),PropertyInfo(Variant::RAW_ARRAY,"body")));
|
||||
|
||||
BIND_CONSTANT( RESULT_SUCCESS );
|
||||
//BIND_CONSTANT( RESULT_NO_BODY );
|
||||
BIND_CONSTANT( RESULT_CHUNKED_BODY_SIZE_MISMATCH );
|
||||
BIND_CONSTANT( RESULT_CANT_CONNECT );
|
||||
BIND_CONSTANT( RESULT_CANT_RESOLVE );
|
||||
BIND_CONSTANT( RESULT_CONNECTION_ERROR );
|
||||
BIND_CONSTANT( RESULT_SSL_HANDSHAKE_ERROR );
|
||||
BIND_CONSTANT( RESULT_NO_RESPONSE );
|
||||
BIND_CONSTANT( RESULT_BODY_SIZE_LIMIT_EXCEEDED );
|
||||
BIND_CONSTANT( RESULT_REQUEST_FAILED );
|
||||
BIND_CONSTANT( RESULT_REDIRECT_LIMIT_REACHED );
|
||||
|
||||
}
|
||||
|
||||
HTTPRequest::HTTPRequest()
|
||||
{
|
||||
|
||||
|
||||
port=80;
|
||||
redirections=0;
|
||||
max_redirects=8;
|
||||
body_len=-1;
|
||||
got_response=false;
|
||||
validate_ssl=false;
|
||||
use_ssl=false;
|
||||
response_code=0;
|
||||
request_sent=false;
|
||||
client.instance();
|
||||
use_threads=false;
|
||||
body_size_limit=-1;
|
||||
status=HTTPClient::STATUS_DISCONNECTED;
|
||||
|
||||
}
|
||||
|
85
scene/main/http_request.h
Normal file
@ -0,0 +1,85 @@
|
||||
#ifndef HTTPREQUEST_H
|
||||
#define HTTPREQUEST_H
|
||||
|
||||
#include "node.h"
|
||||
#include "io/http_client.h"
|
||||
|
||||
class HTTPRequest : public Node {
|
||||
|
||||
OBJ_TYPE(HTTPRequest,Node);
|
||||
public:
|
||||
|
||||
enum Result {
|
||||
RESULT_SUCCESS,
|
||||
//RESULT_NO_BODY,
|
||||
RESULT_CHUNKED_BODY_SIZE_MISMATCH,
|
||||
RESULT_CANT_CONNECT,
|
||||
RESULT_CANT_RESOLVE,
|
||||
RESULT_CONNECTION_ERROR,
|
||||
RESULT_SSL_HANDSHAKE_ERROR,
|
||||
RESULT_NO_RESPONSE,
|
||||
RESULT_BODY_SIZE_LIMIT_EXCEEDED,
|
||||
RESULT_REQUEST_FAILED,
|
||||
RESULT_REDIRECT_LIMIT_REACHED
|
||||
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
bool requesting;
|
||||
|
||||
String request_string;
|
||||
String url;
|
||||
int port;
|
||||
Vector<String> headers;
|
||||
bool validate_ssl;
|
||||
bool use_ssl;
|
||||
|
||||
bool request_sent;
|
||||
Ref<HTTPClient> client;
|
||||
ByteArray body;
|
||||
bool use_threads;
|
||||
|
||||
bool got_response;
|
||||
int response_code;
|
||||
DVector<String> response_headers;
|
||||
|
||||
int body_len;
|
||||
|
||||
int body_size_limit;
|
||||
|
||||
int redirections;
|
||||
|
||||
HTTPClient::Status status;
|
||||
|
||||
bool _update_connection();
|
||||
|
||||
int max_redirects;
|
||||
|
||||
void _redirect_request(const String& p_new_url);
|
||||
|
||||
Error _request();
|
||||
|
||||
protected:
|
||||
|
||||
void _notification(int p_what);
|
||||
static void _bind_methods();
|
||||
public:
|
||||
|
||||
Error request(const String& p_url,const Vector<String>& p_custom_headers=Vector<String>(),bool p_ssl_validate_domain=true); //connects to a full url and perform request
|
||||
void cancel_request();
|
||||
HTTPClient::Status get_http_client_status() const;
|
||||
|
||||
void set_use_threads(bool p_use);
|
||||
bool is_using_threads() const;
|
||||
|
||||
void set_body_size_limit(int p_bytes);
|
||||
int get_body_size_limit() const;
|
||||
|
||||
void set_max_redirects(int p_max);
|
||||
int get_max_redirects() const;
|
||||
|
||||
HTTPRequest();
|
||||
};
|
||||
|
||||
#endif // HTTPREQUEST_H
|
@ -38,9 +38,11 @@
|
||||
#include "scene/main/canvas_layer.h"
|
||||
#include "scene/main/instance_placeholder.h"
|
||||
#include "scene/main/viewport.h"
|
||||
#include "scene/main/http_request.h"
|
||||
#include "scene/gui/control.h"
|
||||
#include "scene/gui/texture_progress.h"
|
||||
#include "scene/gui/button.h"
|
||||
#include "scene/gui/link_button.h"
|
||||
#include "scene/gui/button_array.h"
|
||||
#include "scene/gui/button_group.h"
|
||||
#include "scene/gui/label.h"
|
||||
@ -273,6 +275,7 @@ void register_scene_types() {
|
||||
|
||||
ObjectTypeDB::register_type<Viewport>();
|
||||
ObjectTypeDB::register_virtual_type<RenderTargetTexture>();
|
||||
ObjectTypeDB::register_type<HTTPRequest>();
|
||||
ObjectTypeDB::register_type<Timer>();
|
||||
ObjectTypeDB::register_type<CanvasLayer>();
|
||||
ObjectTypeDB::register_type<CanvasModulate>();
|
||||
@ -300,6 +303,7 @@ void register_scene_types() {
|
||||
ObjectTypeDB::register_type<CheckBox>();
|
||||
ObjectTypeDB::register_type<CheckButton>();
|
||||
ObjectTypeDB::register_type<ToolButton>();
|
||||
ObjectTypeDB::register_type<LinkButton>();
|
||||
ObjectTypeDB::register_type<Panel>();
|
||||
ObjectTypeDB::register_type<Range>();
|
||||
|
||||
|
@ -241,7 +241,15 @@ void make_default_theme() {
|
||||
|
||||
t->set_constant("hseparation","Button", 2);
|
||||
|
||||
// LinkButton
|
||||
|
||||
t->set_font("font","LinkButton", default_font );
|
||||
|
||||
t->set_color("font_color","LinkButton", control_font_color );
|
||||
t->set_color("font_color_pressed","LinkButton", control_font_color_pressed );
|
||||
t->set_color("font_color_hover","LinkButton", control_font_color_hover );
|
||||
|
||||
t->set_constant("underline_spacing","LinkButton", 2 );
|
||||
|
||||
// ColorPickerButton
|
||||
|
||||
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 866 B |
@ -1,5 +1,707 @@
|
||||
#include "addon_editor_plugin.h"
|
||||
#include "editor_node.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void EditorAssetLibraryItem::configure(const String& p_title,int p_asset_id,const String& p_category,int p_category_id,const String& p_author,int p_author_id,int p_rating,const String& p_cost) {
|
||||
|
||||
title->set_text(p_title);
|
||||
asset_id=p_asset_id;
|
||||
category->set_text(p_category);
|
||||
category_id=p_category_id;
|
||||
author->set_text(p_author);
|
||||
author_id=p_author_id;
|
||||
price->set_text(p_cost);
|
||||
|
||||
for(int i=0;i<5;i++) {
|
||||
if (i>2)
|
||||
stars[i]->set_texture(get_icon("RatingNoStar","EditorIcons"));
|
||||
else
|
||||
stars[i]->set_texture(get_icon("RatingStar","EditorIcons"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void EditorAssetLibraryItem::set_image(int p_type,int p_index,const Ref<Texture>& p_image) {
|
||||
|
||||
ERR_FAIL_COND(p_type!=EditorAddonLibrary::IMAGE_QUEUE_ICON);
|
||||
ERR_FAIL_COND(p_index!=0);
|
||||
|
||||
icon->set_normal_texture(p_image);
|
||||
}
|
||||
|
||||
void EditorAssetLibraryItem::_notification(int p_what) {
|
||||
|
||||
if (p_what==NOTIFICATION_ENTER_TREE) {
|
||||
|
||||
icon->set_normal_texture(get_icon("GodotAssetDefault","EditorIcons"));
|
||||
category->add_color_override("font_color", Color(0.5,0.5,0.5) );
|
||||
author->add_color_override("font_color", Color(0.5,0.5,0.5) );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void EditorAssetLibraryItem::_asset_clicked() {
|
||||
|
||||
emit_signal("asset_selected",asset_id);
|
||||
}
|
||||
|
||||
void EditorAssetLibraryItem::_category_clicked(){
|
||||
|
||||
emit_signal("category_selected",category_id);
|
||||
}
|
||||
void EditorAssetLibraryItem::_author_clicked(){
|
||||
|
||||
emit_signal("author_selected",author_id);
|
||||
|
||||
}
|
||||
|
||||
void EditorAssetLibraryItem::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method("set_image",&EditorAssetLibraryItem::set_image);
|
||||
ObjectTypeDB::bind_method("_asset_clicked",&EditorAssetLibraryItem::_asset_clicked);
|
||||
ObjectTypeDB::bind_method("_category_clicked",&EditorAssetLibraryItem::_category_clicked);
|
||||
ObjectTypeDB::bind_method("_author_clicked",&EditorAssetLibraryItem::_author_clicked);
|
||||
ADD_SIGNAL( MethodInfo("asset_selected"));
|
||||
ADD_SIGNAL( MethodInfo("category_selected"));
|
||||
ADD_SIGNAL( MethodInfo("author_selected"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
EditorAssetLibraryItem::EditorAssetLibraryItem() {
|
||||
|
||||
Ref<StyleBoxEmpty> border;
|
||||
border.instance();
|
||||
/*border->set_default_margin(MARGIN_LEFT,5);
|
||||
border->set_default_margin(MARGIN_RIGHT,5);
|
||||
border->set_default_margin(MARGIN_BOTTOM,5);
|
||||
border->set_default_margin(MARGIN_TOP,5);*/
|
||||
add_style_override("panel",border);
|
||||
|
||||
HBoxContainer *hb = memnew( HBoxContainer );
|
||||
add_child(hb);
|
||||
|
||||
icon = memnew( TextureButton );
|
||||
icon->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
||||
icon->connect("pressed",this,"_asset_clicked");
|
||||
|
||||
hb->add_child(icon);
|
||||
|
||||
VBoxContainer *vb = memnew( VBoxContainer );
|
||||
|
||||
hb->add_child(vb);
|
||||
vb->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
title = memnew( LinkButton );
|
||||
title->set_text("My Awesome Addon");
|
||||
title->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
title->connect("pressed",this,"_asset_clicked");
|
||||
vb->add_child(title);
|
||||
|
||||
|
||||
category = memnew( LinkButton );
|
||||
category->set_text("Editor Tools");
|
||||
category->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
title->connect("pressed",this,"_category_clicked");
|
||||
vb->add_child(category);
|
||||
|
||||
author = memnew( LinkButton );
|
||||
author->set_text("Johny Tolengo");
|
||||
author->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
title->connect("pressed",this,"_author_clicked");
|
||||
vb->add_child(author);
|
||||
|
||||
HBoxContainer *rating_hb = memnew( HBoxContainer );
|
||||
vb->add_child(rating_hb);
|
||||
|
||||
for(int i=0;i<5;i++) {
|
||||
stars[i]=memnew(TextureFrame);
|
||||
rating_hb->add_child(stars[i]);
|
||||
}
|
||||
price = memnew( Label );
|
||||
price->set_text("Free");
|
||||
vb->add_child(price);
|
||||
|
||||
set_custom_minimum_size(Size2(250,100));
|
||||
set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
set_stop_mouse(false);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
void EditorAddonLibraryItemDescription::set_image(int p_type,int p_index,const Ref<Texture>& p_image) {
|
||||
|
||||
switch(p_type) {
|
||||
|
||||
case EditorAddonLibrary::IMAGE_QUEUE_ICON: {
|
||||
|
||||
item->call("set_image",p_type,p_index,p_image);
|
||||
} break;
|
||||
case EditorAddonLibrary::IMAGE_QUEUE_THUMBNAIL: {
|
||||
|
||||
for(int i=0;i<preview_images.size();i++) {
|
||||
if (preview_images[i].id==p_index) {
|
||||
preview_images[i].button->set_icon(p_image);
|
||||
}
|
||||
}
|
||||
//item->call("set_image",p_type,p_index,p_image);
|
||||
} break;
|
||||
case EditorAddonLibrary::IMAGE_QUEUE_SCREENSHOT: {
|
||||
|
||||
for(int i=0;i<preview_images.size();i++) {
|
||||
if (preview_images[i].id==p_index && preview_images[i].button->is_pressed()) {
|
||||
preview->set_texture(p_image);
|
||||
}
|
||||
}
|
||||
//item->call("set_image",p_type,p_index,p_image);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorAddonLibraryItemDescription::_bind_methods() {
|
||||
ObjectTypeDB::bind_method(_MD("set_image"),&EditorAddonLibraryItemDescription::set_image);
|
||||
}
|
||||
|
||||
void EditorAddonLibraryItemDescription::configure(const String& p_title,int p_asset_id,const String& p_category,int p_category_id,const String& p_author,int p_author_id,int p_rating,const String& p_cost,const String& p_description) {
|
||||
|
||||
item->configure(p_title,p_asset_id,p_category,p_category_id,p_author,p_author_id,p_rating,p_cost);
|
||||
description->parse_bbcode(p_description);
|
||||
set_title(p_title);
|
||||
}
|
||||
|
||||
void EditorAddonLibraryItemDescription::add_preview(int p_id, bool p_video,const String& p_url){
|
||||
|
||||
Preview preview;
|
||||
preview.id=p_id;
|
||||
preview.video_link=p_url;
|
||||
preview.button = memnew( Button );
|
||||
preview.button->set_flat(true);
|
||||
preview.button->set_icon(get_icon("ThumbnailWait","EditorIcons"));
|
||||
preview.button->set_toggle_mode(true);
|
||||
preview_hb->add_child(preview.button);
|
||||
if (preview_images.size()==0)
|
||||
preview.button->set_pressed(true);
|
||||
preview_images.push_back(preview);
|
||||
}
|
||||
|
||||
EditorAddonLibraryItemDescription::EditorAddonLibraryItemDescription() {
|
||||
|
||||
VBoxContainer *vbox = memnew( VBoxContainer );
|
||||
add_child(vbox);
|
||||
set_child_rect(vbox);
|
||||
|
||||
|
||||
HBoxContainer *hbox = memnew( HBoxContainer);
|
||||
vbox->add_child(hbox);
|
||||
vbox->add_constant_override("separation",15);
|
||||
VBoxContainer *desc_vbox = memnew( VBoxContainer );
|
||||
hbox->add_child(desc_vbox);
|
||||
hbox->add_constant_override("separation",15);
|
||||
|
||||
item = memnew( EditorAssetLibraryItem );
|
||||
|
||||
desc_vbox->add_child(item);
|
||||
desc_vbox->set_custom_minimum_size(Size2(300,0));
|
||||
|
||||
|
||||
PanelContainer * desc_bg = memnew( PanelContainer );
|
||||
desc_vbox->add_child(desc_bg);
|
||||
desc_bg->set_v_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
description = memnew( RichTextLabel );
|
||||
//desc_vbox->add_child(description);
|
||||
desc_bg->add_child(description);
|
||||
desc_bg->add_style_override("panel",get_stylebox("normal","TextEdit"));
|
||||
|
||||
preview = memnew( TextureFrame );
|
||||
preview->set_custom_minimum_size(Size2(640,345));
|
||||
hbox->add_child(preview);
|
||||
|
||||
PanelContainer * previews_bg = memnew( PanelContainer );
|
||||
vbox->add_child(previews_bg);
|
||||
previews_bg->set_custom_minimum_size(Size2(0,85));
|
||||
previews_bg->add_style_override("panel",get_stylebox("normal","TextEdit"));
|
||||
|
||||
previews = memnew( ScrollContainer );
|
||||
previews_bg->add_child(previews);
|
||||
previews->set_enable_v_scroll(false);
|
||||
previews->set_enable_h_scroll(true);
|
||||
preview_hb = memnew( HBoxContainer );
|
||||
preview_hb->set_v_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
previews->add_child(preview_hb);
|
||||
get_ok()->set_text("Install");
|
||||
get_cancel()->set_text("Close");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void EditorAddonLibrary::_notification(int p_what) {
|
||||
|
||||
if (p_what==NOTIFICATION_READY) {
|
||||
_api_request("api/configure");
|
||||
}
|
||||
|
||||
if (p_what==NOTIFICATION_PROCESS) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
const char* EditorAddonLibrary::sort_key[SORT_MAX]={
|
||||
"rating",
|
||||
"downloads",
|
||||
"name",
|
||||
"cost",
|
||||
"updated"
|
||||
};
|
||||
|
||||
const char* EditorAddonLibrary::sort_text[SORT_MAX]={
|
||||
"Rating",
|
||||
"Downloads",
|
||||
"Name",
|
||||
"Cost",
|
||||
"Updated"
|
||||
};
|
||||
|
||||
|
||||
void EditorAddonLibrary::_select_author(int p_id) {
|
||||
|
||||
//opemn author window
|
||||
}
|
||||
|
||||
void EditorAddonLibrary::_select_category(int p_id){
|
||||
|
||||
for(int i=0;i<categories->get_item_count();i++) {
|
||||
|
||||
if (i==0)
|
||||
continue;
|
||||
int id = categories->get_item_metadata(i);
|
||||
if (id==p_id) {
|
||||
categories->select(i);
|
||||
_search();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
void EditorAddonLibrary::_select_asset(int p_id){
|
||||
|
||||
_api_request("api/asset","?id="+itos(p_id));
|
||||
|
||||
/*
|
||||
if (description) {
|
||||
memdelete(description);
|
||||
}
|
||||
|
||||
|
||||
description = memnew( EditorAddonLibraryItemDescription );
|
||||
add_child(description);
|
||||
description->popup_centered_minsize();*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
void EditorAddonLibrary::_image_request_completed(int p_status, int p_code, const StringArray& headers, const ByteArray& p_data,int p_queue_id) {
|
||||
|
||||
ERR_FAIL_COND( !image_queue.has(p_queue_id) );
|
||||
|
||||
if (p_status==HTTPRequest::RESULT_SUCCESS) {
|
||||
|
||||
|
||||
print_line("GOT IMAGE YAY!");
|
||||
Object *obj = ObjectDB::get_instance(image_queue[p_queue_id].target);
|
||||
|
||||
if (obj) {
|
||||
int len=p_data.size();
|
||||
ByteArray::Read r=p_data.read();
|
||||
|
||||
Image image(r.ptr(),len);
|
||||
if (!image.empty()) {
|
||||
Ref<ImageTexture> tex;
|
||||
tex.instance();
|
||||
tex->create_from_image(image);
|
||||
|
||||
obj->call("set_image",image_queue[p_queue_id].image_type,image_queue[p_queue_id].image_index,tex);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
WARN_PRINTS("Error getting PNG file for asset id "+itos(image_queue[p_queue_id].asset_id));
|
||||
}
|
||||
|
||||
image_queue[p_queue_id].request->queue_delete();;
|
||||
image_queue.erase(p_queue_id);
|
||||
|
||||
_update_image_queue();
|
||||
|
||||
}
|
||||
|
||||
void EditorAddonLibrary::_update_image_queue() {
|
||||
|
||||
int max_images=2;
|
||||
int current_images=0;
|
||||
|
||||
List<int> to_delete;
|
||||
for (Map<int,ImageQueue>::Element *E=image_queue.front();E;E=E->next()) {
|
||||
if (!E->get().active && current_images<max_images) {
|
||||
|
||||
String api;
|
||||
switch(E->get().image_type) {
|
||||
case IMAGE_QUEUE_ICON: api="api/icon/icon.png"; break;
|
||||
case IMAGE_QUEUE_SCREENSHOT: api="api/screenshot/screenshot.png"; break;
|
||||
case IMAGE_QUEUE_THUMBNAIL: api="api/thumbnail/thumbnail.png"; break;
|
||||
}
|
||||
|
||||
print_line("REQUEST ICON FOR: "+itos(E->get().asset_id));
|
||||
Error err = E->get().request->request(host+"/"+api+"?asset_id="+itos(E->get().asset_id)+"&index="+itos(E->get().image_index));
|
||||
if (err!=OK) {
|
||||
to_delete.push_back(E->key());
|
||||
} else {
|
||||
E->get().active=true;
|
||||
}
|
||||
current_images++;
|
||||
} else if (E->get().active) {
|
||||
current_images++;
|
||||
}
|
||||
}
|
||||
|
||||
while(to_delete.size()) {
|
||||
image_queue[to_delete.front()->get()].request->queue_delete();
|
||||
image_queue.erase(to_delete.front()->get());
|
||||
to_delete.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void EditorAddonLibrary::_request_image(ObjectID p_for,int p_asset_id,ImageType p_type,int p_image_index) {
|
||||
|
||||
|
||||
ImageQueue iq;
|
||||
iq.asset_id=p_asset_id;
|
||||
iq.image_index=p_image_index;
|
||||
iq.image_type=p_type;
|
||||
iq.request = memnew( HTTPRequest );
|
||||
|
||||
iq.target=p_for;
|
||||
iq.queue_id=++last_queue_id;
|
||||
iq.active=false;
|
||||
|
||||
iq.request->connect("request_completed",this,"_image_request_completed",varray(iq.queue_id));
|
||||
|
||||
image_queue[iq.queue_id]=iq;
|
||||
|
||||
add_child(iq.request);
|
||||
|
||||
_update_image_queue();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void EditorAddonLibrary::_search(int p_page) {
|
||||
|
||||
String args;
|
||||
|
||||
args=String()+"?sort="+sort_key[sort->get_selected()];
|
||||
|
||||
if (categories->get_selected()>0) {
|
||||
|
||||
args+="&category="+itos(categories->get_item_metadata(categories->get_selected()));
|
||||
}
|
||||
|
||||
if (filter->get_text()!=String()) {
|
||||
args+="&filter="+filter->get_text().http_escape();
|
||||
}
|
||||
|
||||
if (p_page>0) {
|
||||
args+="&page="+itos(p_page);
|
||||
}
|
||||
|
||||
_api_request("api/search",args);
|
||||
}
|
||||
|
||||
HBoxContainer* EditorAddonLibrary::_make_pages(int p_page,int p_max_page,int p_page_len,int p_total_items,int p_current_items) {
|
||||
|
||||
HBoxContainer * hbc = memnew( HBoxContainer );
|
||||
|
||||
//do the mario
|
||||
int from = p_page-5;
|
||||
if (from<0)
|
||||
from=0;
|
||||
int to = from+10;
|
||||
if (to>p_max_page)
|
||||
to=p_max_page;
|
||||
|
||||
Color gray = Color(0.65,0.65,0.65);
|
||||
|
||||
hbc->add_spacer();
|
||||
hbc->add_constant_override("separation",10);
|
||||
|
||||
LinkButton *first = memnew( LinkButton );
|
||||
first->set_text("first");
|
||||
first->add_color_override("font_color", gray );
|
||||
first->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
first->connect("pressed",this,"_search",varray(0));
|
||||
hbc->add_child(first);
|
||||
|
||||
if (p_page>0) {
|
||||
LinkButton *prev = memnew( LinkButton );
|
||||
prev->set_text("prev");
|
||||
prev->add_color_override("font_color", gray );
|
||||
prev->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
prev->connect("pressed",this,"_search",varray(p_page-1));
|
||||
hbc->add_child(prev);
|
||||
}
|
||||
|
||||
for(int i=from;i<=to;i++) {
|
||||
|
||||
if (i==p_page) {
|
||||
|
||||
Label *current = memnew(Label);
|
||||
current->set_text(itos(i));
|
||||
hbc->add_child(current);
|
||||
} else {
|
||||
|
||||
LinkButton *current = memnew( LinkButton );
|
||||
current->add_color_override("font_color", gray );
|
||||
current->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
current->set_text(itos(i));
|
||||
current->connect("pressed",this,"_search",varray(i));
|
||||
|
||||
hbc->add_child(current);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (p_page<p_max_page) {
|
||||
LinkButton *next = memnew( LinkButton );
|
||||
next->set_text("next");
|
||||
next->add_color_override("font_color", gray );
|
||||
next->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
next->connect("pressed",this,"_search",varray(p_page+1));
|
||||
|
||||
hbc->add_child(next);
|
||||
}
|
||||
LinkButton *last = memnew( LinkButton );
|
||||
last->set_text("last");
|
||||
last->add_color_override("font_color", gray );
|
||||
last->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
|
||||
hbc->add_child(last);
|
||||
last->connect("pressed",this,"_search",varray(p_max_page));
|
||||
|
||||
Label *totals = memnew( Label );
|
||||
totals->set_text("( "+itos(from*p_page_len)+" - "+itos(from*p_page_len+p_current_items-1)+" / "+itos(p_total_items)+" )");
|
||||
hbc->add_child(totals);
|
||||
|
||||
hbc->add_spacer();
|
||||
|
||||
return hbc;
|
||||
}
|
||||
|
||||
|
||||
void EditorAddonLibrary::_api_request(const String& p_request,const String& p_arguments) {
|
||||
|
||||
if (requesting!=REQUESTING_NONE) {
|
||||
request->cancel_request();
|
||||
}
|
||||
|
||||
current_request=p_request;
|
||||
request->request(host+"/"+p_request+p_arguments);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void EditorAddonLibrary::_http_request_completed(int p_status, int p_code, const StringArray& headers, const ByteArray& p_data) {
|
||||
|
||||
|
||||
String str;
|
||||
|
||||
{
|
||||
int datalen=p_data.size();
|
||||
ByteArray::Read r = p_data.read();
|
||||
str.parse_utf8((const char*)r.ptr(),datalen);
|
||||
}
|
||||
|
||||
print_line("response: "+itos(p_status)+" code: "+itos(p_code));
|
||||
Dictionary d;
|
||||
d.parse_json(str);
|
||||
|
||||
print_line(Variant(d).get_construct_string());
|
||||
|
||||
if (current_request=="api/configure") {
|
||||
|
||||
categories->clear();
|
||||
categories->add_item("All");
|
||||
categories->set_item_metadata(0,0);
|
||||
if (d.has("categories")) {
|
||||
Array clist = d["categories"];
|
||||
for(int i=0;i<clist.size();i++) {
|
||||
Dictionary cat = clist[i];
|
||||
if (!cat.has("name") || !cat.has("id"))
|
||||
continue;
|
||||
String name=cat["name"];
|
||||
int id=cat["id"];
|
||||
categories->add_item(name);
|
||||
categories->set_item_metadata( categories->get_item_count() -1, id);
|
||||
}
|
||||
}
|
||||
|
||||
_search();
|
||||
} else if (current_request=="api/search") {
|
||||
|
||||
if (asset_items) {
|
||||
memdelete(asset_items);
|
||||
}
|
||||
|
||||
if (asset_top_page) {
|
||||
memdelete(asset_top_page);
|
||||
}
|
||||
|
||||
if (asset_bottom_page) {
|
||||
memdelete(asset_bottom_page);
|
||||
}
|
||||
|
||||
int page=0;
|
||||
int pages=1;
|
||||
int page_len=10;
|
||||
int total_items=1;
|
||||
Array result;
|
||||
|
||||
|
||||
if (d.has("page")) {
|
||||
page=d["page"];
|
||||
}
|
||||
if (d.has("pages")) {
|
||||
pages=d["pages"];
|
||||
}
|
||||
if (d.has("page_length")) {
|
||||
page_len=d["page_length"];
|
||||
}
|
||||
if (d.has("total")) {
|
||||
total_items=d["total"];
|
||||
}
|
||||
if (d.has("result")) {
|
||||
result=d["result"];
|
||||
}
|
||||
|
||||
asset_top_page = _make_pages(page,pages,page_len,total_items,result.size());
|
||||
library_vb->add_child(asset_top_page);
|
||||
|
||||
asset_items = memnew( GridContainer );
|
||||
asset_items->set_columns(2);
|
||||
asset_items->add_constant_override("hseparation",10);
|
||||
asset_items->add_constant_override("vseparation",10);
|
||||
|
||||
library_vb->add_child(asset_items);
|
||||
|
||||
asset_bottom_page = _make_pages(page,pages,page_len,total_items,result.size());
|
||||
library_vb->add_child(asset_bottom_page);
|
||||
|
||||
for(int i=0;i<result.size();i++) {
|
||||
|
||||
Dictionary r = result[i];
|
||||
|
||||
ERR_CONTINUE(!r.has("title"));
|
||||
ERR_CONTINUE(!r.has("asset_id"));
|
||||
ERR_CONTINUE(!r.has("author"));
|
||||
ERR_CONTINUE(!r.has("author_id"));
|
||||
ERR_CONTINUE(!r.has("category"));
|
||||
ERR_CONTINUE(!r.has("category_id"));
|
||||
ERR_CONTINUE(!r.has("rating"));
|
||||
ERR_CONTINUE(!r.has("cost"));
|
||||
|
||||
|
||||
EditorAssetLibraryItem *item = memnew( EditorAssetLibraryItem );
|
||||
asset_items->add_child(item);
|
||||
item->configure(r["title"],r["asset_id"],r["category"],r["category_id"],r["author"],r["author_id"],r["rating"],r["cost"]);
|
||||
item->connect("asset_selected",this,"_select_asset");
|
||||
item->connect("author_selected",this,"_select_author");
|
||||
item->connect("category_selected",this,"_category_selected");
|
||||
|
||||
_request_image(item->get_instance_ID(),r["asset_id"],IMAGE_QUEUE_ICON,0);
|
||||
}
|
||||
} else if (current_request=="api/asset") {
|
||||
|
||||
ERR_FAIL_COND(!d.has("info"));
|
||||
|
||||
Dictionary r = d["info"];
|
||||
|
||||
ERR_FAIL_COND(!r.has("title"));
|
||||
ERR_FAIL_COND(!r.has("asset_id"));
|
||||
ERR_FAIL_COND(!r.has("author"));
|
||||
ERR_FAIL_COND(!r.has("author_id"));
|
||||
ERR_FAIL_COND(!r.has("category"));
|
||||
ERR_FAIL_COND(!r.has("category_id"));
|
||||
ERR_FAIL_COND(!r.has("rating"));
|
||||
ERR_FAIL_COND(!r.has("cost"));
|
||||
ERR_FAIL_COND(!r.has("description"));
|
||||
|
||||
if (description) {
|
||||
memdelete(description);
|
||||
}
|
||||
|
||||
description = memnew( EditorAddonLibraryItemDescription );
|
||||
add_child(description);
|
||||
description->popup_centered_minsize();
|
||||
|
||||
description->configure(r["title"],r["asset_id"],r["category"],r["category_id"],r["author"],r["author_id"],r["rating"],r["cost"],r["description"]);
|
||||
/*item->connect("asset_selected",this,"_select_asset");
|
||||
item->connect("author_selected",this,"_select_author");
|
||||
item->connect("category_selected",this,"_category_selected");*/
|
||||
|
||||
_request_image(description->get_instance_ID(),r["asset_id"],IMAGE_QUEUE_ICON,0);
|
||||
|
||||
if (d.has("previews")) {
|
||||
Array previews = d["previews"];
|
||||
|
||||
for(int i=0;i<previews.size();i++) {
|
||||
|
||||
|
||||
Dictionary p=previews[i];
|
||||
|
||||
ERR_CONTINUE(!p.has("id"));
|
||||
|
||||
bool is_video=p.has("type") && String(p["type"])=="video";
|
||||
String video_url;
|
||||
if (is_video && p.has("link")) {
|
||||
video_url="link";
|
||||
}
|
||||
|
||||
int id=p["id"];
|
||||
|
||||
description->add_preview(id,is_video,video_url);
|
||||
|
||||
_request_image(description->get_instance_ID(),r["asset_id"],IMAGE_QUEUE_THUMBNAIL,id);
|
||||
if (i==0) {
|
||||
_request_image(description->get_instance_ID(),r["asset_id"],IMAGE_QUEUE_SCREENSHOT,id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void EditorAddonLibrary::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method("_http_request_completed",&EditorAddonLibrary::_http_request_completed);
|
||||
ObjectTypeDB::bind_method("_select_asset",&EditorAddonLibrary::_select_asset);
|
||||
ObjectTypeDB::bind_method("_select_author",&EditorAddonLibrary::_select_author);
|
||||
ObjectTypeDB::bind_method("_select_category",&EditorAddonLibrary::_select_category);
|
||||
ObjectTypeDB::bind_method("_image_request_completed",&EditorAddonLibrary::_image_request_completed);
|
||||
ObjectTypeDB::bind_method("_search",&EditorAddonLibrary::_search,DEFVAL(0));
|
||||
|
||||
}
|
||||
|
||||
EditorAddonLibrary::EditorAddonLibrary() {
|
||||
|
||||
tabs = memnew( TabContainer );
|
||||
@ -10,29 +712,136 @@ EditorAddonLibrary::EditorAddonLibrary() {
|
||||
installed->set_name("Installed");
|
||||
tabs->add_child(installed);
|
||||
|
||||
library = memnew( VBoxContainer );
|
||||
library->set_name("Online");
|
||||
tabs->add_child(library);
|
||||
Ref<StyleBoxEmpty> border;
|
||||
border.instance();
|
||||
border->set_default_margin(MARGIN_LEFT,15);
|
||||
border->set_default_margin(MARGIN_RIGHT,15);
|
||||
border->set_default_margin(MARGIN_BOTTOM,15);
|
||||
border->set_default_margin(MARGIN_TOP,15);
|
||||
|
||||
PanelContainer *margin_panel = memnew( PanelContainer );
|
||||
|
||||
margin_panel->set_name("Online");
|
||||
margin_panel->add_style_override("panel",border);
|
||||
tabs->add_child(margin_panel);
|
||||
|
||||
VBoxContainer *library_main = memnew( VBoxContainer );
|
||||
|
||||
margin_panel->add_child(library_main);
|
||||
|
||||
|
||||
HBoxContainer *search_hb = memnew( HBoxContainer );
|
||||
|
||||
library->add_child(search_hb);
|
||||
library_main->add_child(search_hb);
|
||||
library_main->add_constant_override("separation",20);
|
||||
|
||||
search_hb->add_child( memnew( Label("Search: ")));
|
||||
filter =memnew( LineEdit );
|
||||
search_hb->add_child(filter);
|
||||
filter->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
categories = memnew( OptionButton );
|
||||
categories->add_item("All Categories");
|
||||
search_hb->add_child(categories);
|
||||
|
||||
filter->connect("text_entered",this,"_search");
|
||||
search = memnew( Button("Search"));
|
||||
search->connect("pressed",this,"_search");
|
||||
search_hb->add_child(search);
|
||||
library_vb->add_child(search_hb);
|
||||
|
||||
HBoxContainer *search_hb2 = memnew( HBoxContainer );
|
||||
library_main->add_child(search_hb2);
|
||||
|
||||
search_hb2->add_child( memnew( Label("Sort: ")));
|
||||
sort = memnew( OptionButton );
|
||||
for(int i=0;i<SORT_MAX;i++) {
|
||||
sort->add_item(sort_text[i]);
|
||||
}
|
||||
|
||||
search_hb2->add_child(sort);
|
||||
|
||||
sort->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
reverse = memnew( CheckBox);
|
||||
reverse->set_text("Reverse");
|
||||
search_hb2->add_child(reverse);
|
||||
|
||||
search_hb2->add_child(memnew(VSeparator));
|
||||
|
||||
//search_hb2->add_spacer();
|
||||
|
||||
search_hb2->add_child( memnew( Label("Category: ")));
|
||||
categories = memnew( OptionButton );
|
||||
categories->add_item("All");
|
||||
search_hb2->add_child(categories);
|
||||
categories->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
//search_hb2->add_spacer();
|
||||
|
||||
search_hb2->add_child(memnew(VSeparator));
|
||||
|
||||
search_hb2->add_child( memnew( Label("Site: ")));
|
||||
repository = memnew( OptionButton );
|
||||
|
||||
repository->add_item("Godot");
|
||||
search_hb2->add_child(repository);
|
||||
repository->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
/////////
|
||||
|
||||
PanelContainer * library_scroll_bg = memnew( PanelContainer );
|
||||
library_main->add_child(library_scroll_bg);
|
||||
library_scroll_bg->add_style_override("panel",get_stylebox("normal","TextEdit"));
|
||||
library_scroll_bg->set_v_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
library_scroll = memnew( ScrollContainer );
|
||||
library_scroll->set_enable_v_scroll(true);
|
||||
library_scroll->set_enable_h_scroll(false);
|
||||
|
||||
library_scroll_bg->add_child(library_scroll);
|
||||
|
||||
|
||||
Ref<StyleBoxEmpty> border2;
|
||||
border2.instance();
|
||||
border2->set_default_margin(MARGIN_LEFT,15);
|
||||
border2->set_default_margin(MARGIN_RIGHT,35);
|
||||
border2->set_default_margin(MARGIN_BOTTOM,15);
|
||||
border2->set_default_margin(MARGIN_TOP,15);
|
||||
|
||||
|
||||
PanelContainer * library_vb_border = memnew( PanelContainer );
|
||||
library_scroll->add_child(library_vb_border);
|
||||
library_vb_border->add_style_override("panel",border2);
|
||||
library_vb_border->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
library_vb_border->set_stop_mouse(false);
|
||||
|
||||
|
||||
|
||||
library_vb = memnew( VBoxContainer );
|
||||
library_vb->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
library_vb_border->add_child(library_vb);
|
||||
// margin_panel->set_stop_mouse(false);
|
||||
|
||||
asset_top_page = memnew( HBoxContainer );
|
||||
library_vb->add_child(asset_top_page);
|
||||
|
||||
asset_items = memnew( GridContainer );
|
||||
asset_items->set_columns(2);
|
||||
asset_items->add_constant_override("hseparation",10);
|
||||
asset_items->add_constant_override("vseparation",10);
|
||||
|
||||
library_vb->add_child(asset_items);
|
||||
|
||||
asset_bottom_page = memnew( HBoxContainer );
|
||||
library_vb->add_child(asset_bottom_page);
|
||||
|
||||
request = memnew( HTTPRequest );
|
||||
add_child(request);
|
||||
request->connect("request_completed",this,"_http_request_completed");
|
||||
|
||||
last_queue_id=0;
|
||||
|
||||
library_vb->add_constant_override("separation",20);
|
||||
|
||||
description = NULL;
|
||||
|
||||
host="http://localhost:8000";
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,19 +7,183 @@
|
||||
#include "scene/gui/line_edit.h"
|
||||
#include "scene/gui/option_button.h"
|
||||
#include "scene/gui/tab_container.h"
|
||||
#include "scene/gui/panel_container.h"
|
||||
#include "scene/gui/link_button.h"
|
||||
#include "scene/gui/check_box.h"
|
||||
#include "scene/gui/separator.h"
|
||||
|
||||
#include "scene/gui/grid_container.h"
|
||||
#include "scene/gui/scroll_container.h"
|
||||
#include "scene/gui/texture_button.h"
|
||||
#include "scene/gui/rich_text_label.h"
|
||||
#include "editor_plugin_settings.h"
|
||||
|
||||
#include "scene/main/http_request.h"
|
||||
|
||||
class EditorAssetLibraryItem : public PanelContainer {
|
||||
|
||||
OBJ_TYPE( EditorAssetLibraryItem, PanelContainer );
|
||||
|
||||
TextureButton *icon;
|
||||
LinkButton* title;
|
||||
LinkButton* category;
|
||||
LinkButton* author;
|
||||
TextureFrame *stars[5];
|
||||
Label* price;
|
||||
|
||||
int asset_id;
|
||||
int category_id;
|
||||
int author_id;
|
||||
|
||||
|
||||
void _asset_clicked();
|
||||
void _category_clicked();
|
||||
void _author_clicked();
|
||||
|
||||
|
||||
void set_image(int p_type,int p_index,const Ref<Texture>& p_image);
|
||||
|
||||
protected:
|
||||
|
||||
void _notification(int p_what);
|
||||
static void _bind_methods();
|
||||
public:
|
||||
|
||||
void configure(const String& p_title,int p_asset_id,const String& p_category,int p_category_id,const String& p_author,int p_author_id,int p_rating,const String& p_cost);
|
||||
|
||||
|
||||
EditorAssetLibraryItem();
|
||||
};
|
||||
|
||||
|
||||
class EditorAddonLibraryItemDescription : public ConfirmationDialog {
|
||||
|
||||
OBJ_TYPE(EditorAddonLibraryItemDescription, ConfirmationDialog);
|
||||
|
||||
EditorAssetLibraryItem *item;
|
||||
RichTextLabel *description;
|
||||
ScrollContainer *previews;
|
||||
HBoxContainer *preview_hb;
|
||||
|
||||
struct Preview {
|
||||
int id;
|
||||
String video_link;
|
||||
Button *button;
|
||||
};
|
||||
|
||||
Vector<Preview> preview_images;
|
||||
TextureFrame *preview;
|
||||
|
||||
void set_image(int p_type,int p_index,const Ref<Texture>& p_image);
|
||||
|
||||
protected:
|
||||
|
||||
static void _bind_methods();
|
||||
public:
|
||||
|
||||
void configure(const String& p_title,int p_asset_id,const String& p_category,int p_category_id,const String& p_author,int p_author_id,int p_rating,const String& p_cost,const String& p_description);
|
||||
void add_preview(int p_id, bool p_video,const String& p_url);
|
||||
|
||||
EditorAddonLibraryItemDescription();
|
||||
|
||||
};
|
||||
|
||||
class EditorAddonLibrary : public VBoxContainer {
|
||||
OBJ_TYPE(EditorAddonLibrary,VBoxContainer);
|
||||
|
||||
String host;
|
||||
|
||||
TabContainer *tabs;
|
||||
EditorPluginSettings *installed;
|
||||
VBoxContainer *library;
|
||||
ScrollContainer *library_scroll;
|
||||
VBoxContainer *library_vb;
|
||||
LineEdit *filter;
|
||||
OptionButton *categories;
|
||||
OptionButton *repository;
|
||||
OptionButton *sort;
|
||||
CheckBox *reverse;
|
||||
Button *search;
|
||||
|
||||
HBoxContainer *contents;
|
||||
|
||||
HBoxContainer *asset_top_page;
|
||||
GridContainer *asset_items;
|
||||
HBoxContainer *asset_bottom_page;
|
||||
|
||||
HTTPRequest *request;
|
||||
|
||||
enum SortOrder {
|
||||
SORT_RATING,
|
||||
SORT_DOWNLOADS,
|
||||
SORT_NAME,
|
||||
SORT_COST,
|
||||
SORT_UPDATED,
|
||||
SORT_MAX
|
||||
};
|
||||
|
||||
|
||||
static const char* sort_key[SORT_MAX];
|
||||
static const char* sort_text[SORT_MAX];
|
||||
|
||||
|
||||
///MainListing
|
||||
|
||||
enum ImageType {
|
||||
IMAGE_QUEUE_ICON,
|
||||
IMAGE_QUEUE_THUMBNAIL,
|
||||
IMAGE_QUEUE_SCREENSHOT,
|
||||
|
||||
};
|
||||
|
||||
struct ImageQueue {
|
||||
|
||||
bool active;
|
||||
int queue_id;
|
||||
int asset_id;
|
||||
ImageType image_type;
|
||||
int image_index;
|
||||
HTTPRequest *request;
|
||||
ObjectID target;
|
||||
};
|
||||
|
||||
int last_queue_id;
|
||||
Map<int,ImageQueue> image_queue;
|
||||
|
||||
|
||||
void _image_request_completed(int p_status, int p_code, const StringArray& headers, const ByteArray& p_data, int p_queue_id);
|
||||
void _request_image(ObjectID p_for,int p_asset_id,ImageType p_type,int p_image_index);
|
||||
void _update_image_queue();
|
||||
|
||||
HBoxContainer* _make_pages(int p_page, int p_max_page, int p_page_len, int p_total_items, int p_current_items);
|
||||
|
||||
//
|
||||
EditorAddonLibraryItemDescription *description;
|
||||
|
||||
String current_request;
|
||||
//
|
||||
|
||||
enum RequestType {
|
||||
REQUESTING_NONE,
|
||||
REQUESTING_CONFIG,
|
||||
};
|
||||
|
||||
|
||||
RequestType requesting;
|
||||
|
||||
void _select_author(int p_id);
|
||||
void _select_category(int p_id);
|
||||
void _select_asset(int p_id);
|
||||
|
||||
void _search(int p_page=0);
|
||||
void _api_request(const String& p_request, const String &p_arguments="");
|
||||
void _http_request_completed(int p_status, int p_code, const StringArray& headers, const ByteArray& p_data);
|
||||
|
||||
friend class EditorAddonLibraryItemDescription;
|
||||
friend class EditorAssetLibraryItem;
|
||||
protected:
|
||||
|
||||
static void _bind_methods();
|
||||
void _notification(int p_what);
|
||||
public:
|
||||
EditorAddonLibrary();
|
||||
};
|
||||
|
BIN
tools/editor/icons/icon_godot_asset_default.png
Normal file
After Width: | Height: | Size: 4.1 KiB |
BIN
tools/editor/icons/icon_h_t_t_p_request.png
Normal file
After Width: | Height: | Size: 573 B |
BIN
tools/editor/icons/icon_link_button.png
Normal file
After Width: | Height: | Size: 376 B |
BIN
tools/editor/icons/icon_rating_no_star.png
Normal file
After Width: | Height: | Size: 476 B |
BIN
tools/editor/icons/icon_rating_star.png
Normal file
After Width: | Height: | Size: 896 B |
BIN
tools/editor/icons/icon_thumbnail_wait.png
Normal file
After Width: | Height: | Size: 2.6 KiB |