Ability to set autoloads as singleton global variables

This commit is contained in:
reduz 2015-12-28 15:59:20 -03:00
parent 5d47e42eb6
commit e0d21d2158
7 changed files with 137 additions and 12 deletions

View file

@ -165,6 +165,7 @@ public:
virtual String make_function(const String& p_class,const String& p_name,const StringArray& p_args) const=0;
virtual Error complete_code(const String& p_code, const String& p_base_path, Object*p_owner,List<String>* r_options,String& r_call_hint) { return ERR_UNAVAILABLE; }
virtual void auto_indent_code(String& p_code,int p_from_line,int p_to_line) const=0;
virtual void add_global_constant(const StringName& p_variable,const Variant& p_value)=0;
/* DEBUGGER FUNCTIONS */

View file

@ -1330,6 +1330,12 @@ bool Main::start() {
continue;
String name = s.get_slicec('/',1);
String path = Globals::get_singleton()->get(s);
bool global_var=false;
if (path.begins_with("*")) {
global_var=true;
path=path.substr(1,path.length()-1);
}
RES res = ResourceLoader::load(path);
ERR_EXPLAIN("Can't autoload: "+path);
ERR_CONTINUE(res.is_null());
@ -1355,7 +1361,16 @@ bool Main::start() {
ERR_EXPLAIN("Path in autoload not a node or script: "+path);
ERR_CONTINUE(!n);
n->set_name(name);
sml->get_root()->add_child(n);
if (global_var) {
for(int i=0;i<ScriptServer::get_language_count();i++) {
ScriptServer::get_language(i)->add_global_constant(name,n);
}
}
}
}

View file

@ -2570,6 +2570,12 @@ void GDScriptLanguage::_add_global(const StringName& p_name,const Variant& p_val
_global_array=global_array.ptr();
}
void GDScriptLanguage::add_global_constant(const StringName& p_variable,const Variant& p_value) {
_add_global(p_variable,p_value);
}
void GDScriptLanguage::init() {

View file

@ -529,6 +529,7 @@ public:
virtual String make_function(const String& p_class,const String& p_name,const StringArray& p_args) const;
virtual Error complete_code(const String& p_code, const String& p_base_path, Object*p_owner,List<String>* r_options,String& r_call_hint);
virtual void auto_indent_code(String& p_code,int p_from_line,int p_to_line) const;
virtual void add_global_constant(const StringName& p_variable,const Variant& p_value);
/* DEBUGGER FUNCTIONS */

View file

@ -856,7 +856,10 @@ Node *Node::_get_child_by_name(const StringName& p_name) const {
Node *Node::_get_node(const NodePath& p_path) const {
ERR_FAIL_COND_V( !data.inside_tree && p_path.is_absolute(), NULL );
if (!data.inside_tree && p_path.is_absolute()) {
ERR_EXPLAIN("Can't use get_node() with absolute paths from outside the active scene tree.");
ERR_FAIL_V(NULL);
}
Node *current=NULL;
Node *root=NULL;

View file

@ -33,6 +33,7 @@
#include "editor_node.h"
#include "scene/gui/margin_container.h"
#include "translation.h"
#include "global_constants.h"
ProjectSettings *ProjectSettings::singleton=NULL;
@ -777,10 +778,11 @@ void ProjectSettings::_translation_file_open() {
void ProjectSettings::_autoload_file_callback(const String& p_path) {
autoload_add_path->set_text(p_path);
if (autoload_add_name->get_text().strip_edges()==String()) {
//if (autoload_add_name->get_text().strip_edges()==String()) {
autoload_add_name->set_text( p_path.get_file().basename() );
}
//}
//_translation_add(p_translation);
}
@ -789,6 +791,40 @@ void ProjectSettings::_autoload_file_open() {
autoload_file_open->popup_centered_ratio();
}
void ProjectSettings::_autoload_edited() {
if (updating_autoload)
return;
TreeItem *ti = autoload_list->get_edited();
if (!ti || autoload_list->get_edited_column()!=2)
return;
updating_autoload=true;
bool checked=ti->is_checked(2);
String base="autoload/"+ti->get_text(0);
String path = Globals::get_singleton()->get(base);
if (path.begins_with("*"))
path=path.substr(1,path.length());
if (checked)
path="*"+path;
undo_redo->create_action("Toggle Autoload GlobalVar");
undo_redo->add_do_property(Globals::get_singleton(),base,path);
undo_redo->add_undo_property(Globals::get_singleton(),base,Globals::get_singleton()->get(base));
undo_redo->add_do_method(this,"_update_autoload");
undo_redo->add_undo_method(this,"_update_autoload");
undo_redo->add_do_method(this,"_settings_changed");
undo_redo->add_undo_method(this,"_settings_changed");
undo_redo->commit_action();
updating_autoload=false;
}
void ProjectSettings::_autoload_add() {
String name = autoload_add_name->get_text();
@ -799,6 +835,35 @@ void ProjectSettings::_autoload_add() {
}
if (ObjectTypeDB::type_exists(name)) {
message->set_text("Invalid Name.Must not collide with an existing engine class name.");
message->popup_centered(Size2(300,100));
return;
}
for(int i=0;i<Variant::VARIANT_MAX;i++) {
if (Variant::get_type_name(Variant::Type(i))==name) {
message->set_text("Invalid Name.Must not collide with an existing buit-in type name.");
message->popup_centered(Size2(300,100));
return;
}
}
for(int i=0;i<GlobalConstants::get_global_constant_count();i++) {
if (GlobalConstants::get_global_constant_name(i)==name) {
message->set_text("Invalid Name.Must not collide with an existing global constant name.");
message->popup_centered(Size2(300,100));
return;
}
}
String path = autoload_add_path->get_text();
if (!FileAccess::exists(path)) {
message->set_text("Invalid Path.\nFile does not exist.");
@ -815,7 +880,7 @@ void ProjectSettings::_autoload_add() {
undo_redo->create_action("Add Autoload");
name = "autoload/"+name;
undo_redo->add_do_property(Globals::get_singleton(),name,path);
undo_redo->add_do_property(Globals::get_singleton(),name,"*"+path);
if (Globals::get_singleton()->has(name))
undo_redo->add_undo_property(Globals::get_singleton(),name,Globals::get_singleton()->get(name));
else
@ -1208,6 +1273,11 @@ void ProjectSettings::_update_translations() {
void ProjectSettings::_update_autoload() {
if (updating_autoload)
return;
updating_autoload=true;
autoload_list->clear();
TreeItem *root = autoload_list->create_item();
autoload_list->set_hide_root(true);
@ -1222,18 +1292,31 @@ void ProjectSettings::_update_autoload() {
continue;
String name = pi.name.get_slice("/",1);
String path = Globals::get_singleton()->get(pi.name);
if (name=="")
continue;
bool global=false;
if (path.begins_with("*")) {
path=path.substr(1,path.length());
global=true;
}
TreeItem *t = autoload_list->create_item(root);
t->set_text(0,name);
t->set_text(1,Globals::get_singleton()->get(pi.name));
t->add_button(1,get_icon("MoveUp","EditorIcons"),1);
t->add_button(1,get_icon("MoveDown","EditorIcons"),2);
t->add_button(1,get_icon("Del","EditorIcons"),0);
t->set_text(1,path);
t->set_cell_mode(2,TreeItem::CELL_MODE_CHECK);
t->set_editable(2,true);
t->set_text(2,"Enable");
t->set_checked(2,global);
t->add_button(3,get_icon("MoveUp","EditorIcons"),1);
t->add_button(3,get_icon("MoveDown","EditorIcons"),2);
t->add_button(3,get_icon("Del","EditorIcons"),0);
}
updating_autoload=false;
}
void ProjectSettings::_toggle_search_bar(bool p_pressed) {
@ -1302,6 +1385,7 @@ void ProjectSettings::_bind_methods() {
ObjectTypeDB::bind_method(_MD("_autoload_file_callback"),&ProjectSettings::_autoload_file_callback);
ObjectTypeDB::bind_method(_MD("_update_autoload"),&ProjectSettings::_update_autoload);
ObjectTypeDB::bind_method(_MD("_autoload_delete"),&ProjectSettings::_autoload_delete);
ObjectTypeDB::bind_method(_MD("_autoload_edited"),&ProjectSettings::_autoload_edited);
ObjectTypeDB::bind_method(_MD("_clear_search_box"),&ProjectSettings::_clear_search_box);
ObjectTypeDB::bind_method(_MD("_toggle_search_bar"),&ProjectSettings::_toggle_search_bar);
@ -1694,11 +1778,24 @@ ProjectSettings::ProjectSettings(EditorData *p_data) {
autoload_file_open->set_mode(EditorFileDialog::MODE_OPEN_FILE);
autoload_file_open->connect("file_selected",this,"_autoload_file_callback");
autoload_list->set_columns(2);
autoload_list->set_columns(4);
autoload_list->set_column_titles_visible(true);
autoload_list->set_column_title(0,"name");
autoload_list->set_column_title(1,"path");
autoload_list->set_column_title(0,"Name");
autoload_list->set_column_expand(0,true);
autoload_list->set_column_min_width(0,100);
autoload_list->set_column_title(1,"Path");
autoload_list->set_column_expand(1,true);
autoload_list->set_column_min_width(1,100);
autoload_list->set_column_title(2,"GlobalVar");
autoload_list->set_column_expand(2,false);
autoload_list->set_column_min_width(2,80);
autoload_list->set_column_expand(3,false);
autoload_list->set_column_min_width(3,80);
autoload_list->connect("button_pressed",this,"_autoload_delete");
autoload_list->connect("item_edited",this,"_autoload_edited");
updating_autoload=false;
}

View file

@ -95,8 +95,10 @@ class ProjectSettings : public AcceptDialog {
void _update_autoload();
void _autoload_file_callback(const String& p_path);
void _autoload_add();
void _autoload_edited();
void _autoload_file_open();
void _autoload_delete(Object *p_item,int p_column, int p_button);
bool updating_autoload;
void _item_selected();