Browse Source

- _ready() callback only happens once now, if you want to receive it again, use request_ready()
- C++ Nodes mostly do an internal process callback, so it does not conflict with users willing to use their own process callbacks
- callbacks such as _input, _process, _fixed_process _unhandled_input, _unhandled_key_input do not requiere calling a function to enable them. They are enabled automatically if found on the script.

Juan Linietsky 8 years ago
parent
commit
f3f4a11cfb

+ 2 - 2
scene/2d/animated_sprite.cpp

@@ -332,7 +332,7 @@ void AnimatedSprite::_validate_property(PropertyInfo& property) const {
 void AnimatedSprite::_notification(int p_what) {
 
 	switch(p_what) {
-		case NOTIFICATION_PROCESS: {
+		case NOTIFICATION_INTERNAL_PROCESS: {
 
 			if (frames.is_null())
 				return;
@@ -582,7 +582,7 @@ void AnimatedSprite::_set_playing(bool p_playing) {
 		return;
 	playing=p_playing;
 	_reset_timeout();
-	set_process(playing);
+	set_process_internal(playing);
 }
 
 bool AnimatedSprite::_is_playing() const {

+ 2 - 2
scene/3d/sprite_3d.cpp

@@ -1036,7 +1036,7 @@ void AnimatedSprite3D::_validate_property(PropertyInfo& property) const {
 void AnimatedSprite3D::_notification(int p_what) {
 
 	switch(p_what) {
-		case NOTIFICATION_PROCESS: {
+		case NOTIFICATION_INTERNAL_PROCESS: {
 
 			if (frames.is_null())
 				return;
@@ -1239,7 +1239,7 @@ void AnimatedSprite3D::_set_playing(bool p_playing) {
 		return;
 	playing=p_playing;
 	_reset_timeout();
-	set_process(playing);
+	set_process_internal(playing);
 }
 
 bool AnimatedSprite3D::_is_playing() const {

+ 4 - 4
scene/animation/animation_player.cpp

@@ -214,14 +214,14 @@ void AnimationPlayer::_notification(int p_what) {
 				set_autoplay(""); //this line is the fix for autoplay issues with animatio
 			}
 		} break;
-		case NOTIFICATION_PROCESS: {
+		case NOTIFICATION_INTERNAL_PROCESS: {
 			if (animation_process_mode==ANIMATION_PROCESS_FIXED)
 				break;
 
 			if (processing)
 				_animation_process( get_process_delta_time() );
 		} break;
-		case NOTIFICATION_FIXED_PROCESS: {
+		case NOTIFICATION_INTERNAL_FIXED_PROCESS: {
 
 			if (animation_process_mode==ANIMATION_PROCESS_IDLE)
 				break;
@@ -1231,8 +1231,8 @@ void AnimationPlayer::_set_process(bool p_process,bool p_force) {
 
 	switch(animation_process_mode) {
 
-		case ANIMATION_PROCESS_FIXED: set_fixed_process(p_process && active); break;
-		case ANIMATION_PROCESS_IDLE: set_process(p_process && active); break;
+		case ANIMATION_PROCESS_FIXED: set_fixed_process_internal(p_process && active); break;
+		case ANIMATION_PROCESS_IDLE: set_process_internal(p_process && active); break;
 	}
 
 	processing=p_process;

+ 6 - 6
scene/animation/animation_tree_player.cpp

@@ -58,8 +58,8 @@ void AnimationTreePlayer::_set_process(bool p_process, bool p_force)
 
 	switch (animation_process_mode) {
 
-	case ANIMATION_PROCESS_FIXED: set_fixed_process(p_process && active); break;
-	case ANIMATION_PROCESS_IDLE: set_process(p_process && active); break;
+	case ANIMATION_PROCESS_FIXED: set_fixed_process_internal(p_process && active); break;
+	case ANIMATION_PROCESS_IDLE: set_process_internal(p_process && active); break;
 	}
 
 	processing = p_process;
@@ -416,8 +416,8 @@ void AnimationTreePlayer::_notification(int p_what) {
 			if (!processing) {
 				//make sure that a previous process state was not saved
 				//only process if "processing" is set
-				set_fixed_process(false);
-				set_process(false);
+				set_fixed_process_internal(false);
+				set_process_internal(false);
 			}
 		} break;
 		case NOTIFICATION_READY: {
@@ -426,14 +426,14 @@ void AnimationTreePlayer::_notification(int p_what) {
 				_update_sources();
 			}
 		} break;
-		case NOTIFICATION_PROCESS: {
+		case NOTIFICATION_INTERNAL_PROCESS: {
 			if (animation_process_mode==ANIMATION_PROCESS_FIXED)
 				break;
 
 			if (processing)
 				_process_animation( get_process_delta_time() );
 		} break;
-		case NOTIFICATION_FIXED_PROCESS: {
+		case NOTIFICATION_INTERNAL_FIXED_PROCESS: {
 
 			if (animation_process_mode==ANIMATION_PROCESS_IDLE)
 				break;

+ 6 - 6
scene/animation/tween.cpp

@@ -155,21 +155,21 @@ void Tween::_notification(int p_what) {
 			if (!processing) {
 				//make sure that a previous process state was not saved
 				//only process if "processing" is set
-				set_fixed_process(false);
-				set_process(false);
+				set_fixed_process_internal(false);
+				set_process_internal(false);
 			}
 		} break;
 		case NOTIFICATION_READY: {
 
 		} break;
-		case NOTIFICATION_PROCESS: {
+		case NOTIFICATION_INTERNAL_PROCESS: {
 			if (tween_process_mode==TWEEN_PROCESS_FIXED)
 				break;
 
 			if (processing)
 				_tween_process( get_process_delta_time() );
 		} break;
-		case NOTIFICATION_FIXED_PROCESS: {
+		case NOTIFICATION_INTERNAL_FIXED_PROCESS: {
 
 			if (tween_process_mode==TWEEN_PROCESS_IDLE)
 				break;
@@ -666,8 +666,8 @@ void Tween::_set_process(bool p_process,bool p_force) {
 
 	switch(tween_process_mode) {
 
-		case TWEEN_PROCESS_FIXED: set_fixed_process(p_process && active); break;
-		case TWEEN_PROCESS_IDLE: set_process(p_process && active); break;
+		case TWEEN_PROCESS_FIXED: set_fixed_process_internal(p_process && active); break;
+		case TWEEN_PROCESS_IDLE: set_process_internal(p_process && active); break;
 	}
 
 	processing=p_process;

+ 4 - 4
scene/gui/video_player.cpp

@@ -121,7 +121,7 @@ void VideoPlayer::_notification(int p_notification) {
 			}
 		} break;
 
-		case NOTIFICATION_PROCESS: {
+		case NOTIFICATION_INTERNAL_PROCESS: {
 
 			if (stream.is_null())
 				return;
@@ -233,7 +233,7 @@ void VideoPlayer::play() {
 		return;
 	playback->stop();
 	playback->play();
-	set_process(true);
+	set_process_internal(true);
 	AudioServer::get_singleton()->stream_set_active(stream_rid,true);
 	AudioServer::get_singleton()->stream_set_volume_scale(stream_rid,volume);
 	last_audio_time=0;
@@ -249,7 +249,7 @@ void VideoPlayer::stop() {
 	playback->stop();
 	AudioServer::get_singleton()->stream_set_active(stream_rid,false);
 	resampler.flush();
-	set_process(false);
+	set_process_internal(false);
 	last_audio_time=0;
 };
 
@@ -266,7 +266,7 @@ void VideoPlayer::set_paused(bool p_paused) {
 	paused=p_paused;
 	if (playback.is_valid()) {
 		playback->set_paused(p_paused);
-		set_process(!p_paused);
+		set_process_internal(!p_paused);
 	};
 	last_audio_time = 0;
 };

+ 4 - 4
scene/main/http_request.cpp

@@ -154,7 +154,7 @@ Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_h
 			return ERR_CANT_CONNECT;
 		}
 
-		set_process(true);
+		set_process_internal(true);
 
 	}
 
@@ -190,7 +190,7 @@ void HTTPRequest::cancel_request() {
 		return;
 
 	if (!use_threads) {
-		set_process(false);
+		set_process_internal(false);
 	} else {
 		thread_request_quit=true;
 		Thread::wait_to_finish(thread);
@@ -459,14 +459,14 @@ void HTTPRequest::_request_done(int p_status, int p_code, const StringArray& hea
 
 void HTTPRequest::_notification(int p_what) {
 
-	if (p_what==NOTIFICATION_PROCESS) {
+	if (p_what==NOTIFICATION_INTERNAL_PROCESS) {
 
 		if (use_threads)
 			return;
 		bool done = _update_connection();
 		if (done) {
 
-			set_process(false);
+			set_process_internal(false);
 			//cancel_request(); called from _request done now
 		}
 	}

+ 98 - 11
scene/main/node.cpp

@@ -89,6 +89,7 @@ void Node::_notification(int p_notification) {
 				data.network_owner=this;
 			}
 
+
 			if (data.input)
 				add_to_group("_vp_input"+itos(get_viewport()->get_instance_ID()));
 			if (data.unhandled_input)
@@ -128,6 +129,26 @@ void Node::_notification(int p_notification) {
 
 			if (get_script_instance()) {
 
+				if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_input)) {
+					set_process_input(true);
+				}
+
+				if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_unhandled_input)) {
+					set_process_unhandled_input(true);
+				}
+
+				if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_unhandled_key_input)) {
+					set_process_unhandled_key_input(true);
+				}
+
+				if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_process)) {
+					set_process(true);
+				}
+
+				if (get_script_instance()->has_method(SceneStringNames::get_singleton()->_fixed_process)) {
+					set_fixed_process(true);
+				}
+
 				Variant::CallError err;
 				get_script_instance()->call_multilevel_reversed(SceneStringNames::get_singleton()->_ready,NULL,0);
 			}
@@ -173,7 +194,10 @@ void Node::_propagate_ready() {
 		data.children[i]->_propagate_ready();
 	}
 	data.blocked--;
-	notification(NOTIFICATION_READY);
+	if (data.ready_first) {
+		notification(NOTIFICATION_READY);
+		data.ready_first=false;
+	}
 
 }
 
@@ -294,6 +318,7 @@ void Node::_propagate_exit_tree() {
 		data.tree->tree_changed();
 
 	data.inside_tree=false;
+	data.ready_notified=false;
 	data.tree=NULL;
 	data.depth=-1;
 
@@ -401,6 +426,33 @@ void Node::set_fixed_process(bool p_process) {
 	_change_notify("fixed_process");
 }
 
+bool Node::is_fixed_processing() const {
+
+	return data.fixed_process;
+}
+
+void Node::set_fixed_process_internal(bool p_process_internal) {
+
+	if (data.fixed_process_internal==p_process_internal)
+		return;
+
+	data.fixed_process_internal=p_process_internal;
+
+	if (data.fixed_process_internal)
+		add_to_group("fixed_process_internal",false);
+	else
+		remove_from_group("fixed_process_internal");
+
+	data.fixed_process_internal=p_process_internal;
+	_change_notify("fixed_process_internal");
+}
+
+bool Node::is_fixed_processing_internal() const {
+
+	return data.fixed_process_internal;
+}
+
+
 void Node::set_pause_mode(PauseMode p_mode) {
 
 	if (data.pause_mode==p_mode)
@@ -1115,6 +1167,14 @@ float Node::get_fixed_process_delta_time() const {
 		return 0;
 }
 
+float Node::get_process_delta_time() const {
+
+	if (data.tree)
+		return data.tree->get_idle_process_time();
+	else
+		return 0;
+}
+
 void Node::set_process(bool p_idle_process) {
 
 	if (data.idle_process==p_idle_process)
@@ -1131,22 +1191,31 @@ void Node::set_process(bool p_idle_process) {
 	_change_notify("idle_process");
 }
 
-float Node::get_process_delta_time() const {
 
-	if (data.tree)
-		return data.tree->get_idle_process_time();
-	else
-		return 0;
+bool Node::is_processing() const {
+
+	return data.idle_process;
 }
 
-bool Node::is_fixed_processing() const {
+void Node::set_process_internal(bool p_idle_process_internal) {
 
-	return data.fixed_process;
+	if (data.idle_process_internal==p_idle_process_internal)
+		return;
+
+	data.idle_process_internal=p_idle_process_internal;
+
+	if (data.idle_process_internal)
+		add_to_group("idle_process_internal",false);
+	else
+		remove_from_group("idle_process_internal");
+
+	data.idle_process_internal=p_idle_process_internal;
+	_change_notify("idle_process_internal");
 }
 
-bool Node::is_processing() const {
+bool Node::is_processing_internal() const {
 
-	return data.idle_process;
+	return data.idle_process_internal;
 }
 
 
@@ -2813,6 +2882,10 @@ bool Node::is_displayed_folded() const {
 	return data.display_folded;
 }
 
+void Node::request_ready() {
+	data.ready_first=true;
+}
+
 void Node::_bind_methods() {
 
 	_GLOBAL_DEF("editor/node_name_num_separator",0);
@@ -2858,8 +2931,8 @@ void Node::_bind_methods() {
 	ClassDB::bind_method(_MD("set_fixed_process","enable"),&Node::set_fixed_process);
 	ClassDB::bind_method(_MD("get_fixed_process_delta_time"),&Node::get_fixed_process_delta_time);
 	ClassDB::bind_method(_MD("is_fixed_processing"),&Node::is_fixed_processing);
-	ClassDB::bind_method(_MD("set_process","enable"),&Node::set_process);
 	ClassDB::bind_method(_MD("get_process_delta_time"),&Node::get_process_delta_time);
+	ClassDB::bind_method(_MD("set_process","enable"),&Node::set_process);
 	ClassDB::bind_method(_MD("is_processing"),&Node::is_processing);
 	ClassDB::bind_method(_MD("set_process_input","enable"),&Node::set_process_input);
 	ClassDB::bind_method(_MD("is_processing_input"),&Node::is_processing_input);
@@ -2875,6 +2948,12 @@ void Node::_bind_methods() {
 	ClassDB::bind_method(_MD("set_display_folded","fold"),&Node::set_display_folded);
 	ClassDB::bind_method(_MD("is_displayed_folded"),&Node::is_displayed_folded);
 
+	ClassDB::bind_method(_MD("set_process_internal","enable"),&Node::set_process_internal);
+	ClassDB::bind_method(_MD("is_processing_internal"),&Node::is_processing_internal);
+
+	ClassDB::bind_method(_MD("set_fixed_process_internal","enable"),&Node::set_fixed_process_internal);
+	ClassDB::bind_method(_MD("is_fixed_processing_internal"),&Node::is_fixed_processing_internal);
+
 	ClassDB::bind_method(_MD("get_tree:SceneTree"),&Node::get_tree);
 
 	ClassDB::bind_method(_MD("duplicate:Node","use_instancing"),&Node::duplicate,DEFVAL(false));
@@ -2888,6 +2967,8 @@ void Node::_bind_methods() {
 
 	ClassDB::bind_method(_MD("queue_free"),&Node::queue_delete);
 
+	ClassDB::bind_method(_MD("request_ready"),&Node::request_ready);
+
 	ClassDB::bind_method(_MD("set_network_mode","mode"),&Node::set_network_mode);
 	ClassDB::bind_method(_MD("get_network_mode"),&Node::get_network_mode);
 
@@ -2946,6 +3027,9 @@ void Node::_bind_methods() {
 	BIND_CONSTANT( NOTIFICATION_DRAG_BEGIN );
 	BIND_CONSTANT( NOTIFICATION_DRAG_END );
 	BIND_CONSTANT( NOTIFICATION_PATH_CHANGED);
+	BIND_CONSTANT( NOTIFICATION_TRANSLATION_CHANGED );
+	BIND_CONSTANT( NOTIFICATION_INTERNAL_PROCESS );
+	BIND_CONSTANT( NOTIFICATION_INTERNAL_FIXED_PROCESS );
 
 
 	BIND_CONSTANT( NETWORK_MODE_INHERIT );
@@ -3008,6 +3092,8 @@ Node::Node() {
 	data.tree=NULL;
 	data.fixed_process=false;
 	data.idle_process=false;
+	data.fixed_process_internal=false;
+	data.idle_process_internal=false;
 	data.inside_tree=false;
 	data.ready_notified=false;
 
@@ -3026,6 +3112,7 @@ Node::Node() {
 	data.viewport=NULL;
 	data.use_placeholder=false;
 	data.display_folded=false;
+	data.ready_first=true;
 
 }
 

+ 15 - 1
scene/main/node.h

@@ -103,7 +103,8 @@ private:
 		StringName name;
 		SceneTree *tree;
 		bool inside_tree;
-		bool ready_notified;
+		bool ready_notified; //this is a small hack, so if a node is added during _ready() to the tree, it corretly gets the _ready() notification
+		bool ready_first;
 #ifdef TOOLS_ENABLED
 		NodePath import_path; //path used when imported, used by scene editors to keep tracking
 #endif
@@ -129,6 +130,9 @@ private:
 		bool fixed_process;
 		bool idle_process;
 
+		bool fixed_process_internal;
+		bool idle_process_internal;
+
 		bool input;
 		bool unhandled_input;
 		bool unhandled_key_input;
@@ -223,6 +227,9 @@ public:
 		NOTIFICATION_DRAG_END=22,
 		NOTIFICATION_PATH_CHANGED=23,
 		NOTIFICATION_TRANSLATION_CHANGED=24,
+		NOTIFICATION_INTERNAL_PROCESS = 25,
+		NOTIFICATION_INTERNAL_FIXED_PROCESS = 26,
+
 	};
 
 	/* NODE/TREE */
@@ -302,6 +309,11 @@ public:
 	float get_process_delta_time() const;
 	bool is_processing() const;
 
+	void set_fixed_process_internal(bool p_process);
+	bool is_fixed_processing_internal() const;
+
+	void set_process_internal(bool p_process);
+	bool is_processing_internal() const;
 
 	void set_process_input(bool p_enable);
 	bool is_processing_input() const;
@@ -337,6 +349,8 @@ public:
 	PauseMode get_pause_mode() const;
 	bool can_process() const;
 
+	void request_ready();
+
 	static void print_stray_nodes();
 
 #ifdef TOOLS_ENABLED

+ 2 - 0
scene/main/scene_main_loop.cpp

@@ -523,6 +523,7 @@ bool SceneTree::iteration(float p_time) {
 
 	emit_signal("fixed_frame");
 
+	_notify_group_pause("fixed_process_internal",Node::NOTIFICATION_INTERNAL_FIXED_PROCESS);
 	_notify_group_pause("fixed_process",Node::NOTIFICATION_FIXED_PROCESS);
 	_flush_ugc();
 	_flush_transform_notifications();
@@ -554,6 +555,7 @@ bool SceneTree::idle(float p_time){
 
 	_flush_transform_notifications();
 
+	_notify_group_pause("idle_process_internal",Node::NOTIFICATION_INTERNAL_PROCESS);
 	_notify_group_pause("idle_process",Node::NOTIFICATION_PROCESS);
 
 	Size2 win_size=Size2( OS::get_singleton()->get_video_mode().width, OS::get_singleton()->get_video_mode().height );

+ 12 - 12
scene/main/timer.cpp

@@ -45,8 +45,8 @@ void Timer::_notification(int p_what) {
 				autostart=false;
 			}
 		} break;
-		case NOTIFICATION_PROCESS: {
-			if (timer_process_mode == TIMER_PROCESS_FIXED || !is_processing())
+		case NOTIFICATION_INTERNAL_PROCESS: {
+			if (timer_process_mode == TIMER_PROCESS_FIXED || !is_processing_internal())
 				return;
 			time_left -= get_process_delta_time();
 
@@ -61,8 +61,8 @@ void Timer::_notification(int p_what) {
 			}
 
 		} break;
-		case NOTIFICATION_FIXED_PROCESS: {
-			if (timer_process_mode == TIMER_PROCESS_IDLE || !is_fixed_processing())
+		case NOTIFICATION_INTERNAL_FIXED_PROCESS: {
+			if (timer_process_mode == TIMER_PROCESS_IDLE || !is_fixed_processing_internal())
 				return;
 			time_left -= get_fixed_process_delta_time();
 
@@ -147,15 +147,15 @@ void Timer::set_timer_process_mode(TimerProcessMode p_mode) {
 
 	switch (timer_process_mode) {
 		case TIMER_PROCESS_FIXED:
-			if (is_fixed_processing()) {
-				set_fixed_process(false);
-				set_process(true);
+			if (is_fixed_processing_internal()) {
+				set_fixed_process_internal(false);
+				set_process_internal(true);
 			}
 		break;
 		case TIMER_PROCESS_IDLE:
-			if (is_processing()) {
-				set_process(false);
-				set_fixed_process(true);
+			if (is_processing_internal()) {
+				set_process_internal(false);
+				set_fixed_process_internal(true);
 			}
 		break;
 	}
@@ -171,8 +171,8 @@ Timer::TimerProcessMode Timer::get_timer_process_mode() const{
 void Timer::_set_process(bool p_process, bool p_force)
 {
 	switch (timer_process_mode) {
-		case TIMER_PROCESS_FIXED: set_fixed_process(p_process && active); break;
-		case TIMER_PROCESS_IDLE: set_process(p_process && active); break;
+		case TIMER_PROCESS_FIXED: set_fixed_process_internal(p_process && active); break;
+		case TIMER_PROCESS_IDLE: set_process_internal(p_process && active); break;
 	}
 	processing = p_process;
 }

+ 6 - 5
scene/main/viewport.cpp

@@ -1315,11 +1315,7 @@ Image Viewport::get_screen_capture() const {
 
 Ref<ViewportTexture> Viewport::get_texture() const {
 
-	Ref<ViewportTexture> vt;
-	vt.instance();
-	vt->vp=const_cast<Viewport*>(this);
-
-	return vt;
+	return default_texture;
 }
 
 void Viewport::set_vflip(bool p_enable) {
@@ -2852,6 +2848,11 @@ Viewport::Viewport() {
 	viewport = VisualServer::get_singleton()->viewport_create();
 	texture_rid=VisualServer::get_singleton()->viewport_get_texture(viewport);
 	texture_flags=0;
+
+	default_texture.instance();
+	default_texture->vp=const_cast<Viewport*>(this);
+	viewport_textures.insert(default_texture.ptr());
+
 	internal_listener = SpatialSoundServer::get_singleton()->listener_create();
 	audio_listener=false;
 	internal_listener_2d = SpatialSound2DServer::get_singleton()->listener_create();

+ 1 - 0
scene/main/viewport.h

@@ -207,6 +207,7 @@ friend class ViewportTexture;
 	MSAA msaa;
 	bool hdr;
 
+	Ref<ViewportTexture> default_texture;
 	Set<ViewportTexture*> viewport_textures;
 
 

+ 3 - 0
scene/scene_string_names.cpp

@@ -120,6 +120,9 @@ SceneStringNames::SceneStringNames() {
 	gui_input=StaticCString::create("gui_input");
 	_gui_input=StaticCString::create("_gui_input");
 
+	_unhandled_input=StaticCString::create("_unhandled_input");
+	_unhandled_key_input=StaticCString::create("_unhandled_key_input");
+
 	changed=StaticCString::create("changed");
 	_shader_changed=StaticCString::create("_shader_changed");
 

+ 2 - 0
scene/scene_string_names.h

@@ -108,6 +108,8 @@ public:
 	StringName _draw;
 	StringName _input;
 	StringName _ready;
+	StringName _unhandled_input;
+	StringName _unhandled_key_input;
 
 	StringName _pressed;
 	StringName _toggled;