Exemple #1
0
void RayCast2D::_notification(int p_what) {

	switch (p_what) {

		case NOTIFICATION_ENTER_TREE: {

			if (enabled && !get_tree()->is_editor_hint())
				set_fixed_process(true);
			else
				set_fixed_process(false);

			if (get_parent()->cast_to<PhysicsBody2D>()) {
				if (exclude_parent_body)
					exclude.insert(get_parent()->cast_to<PhysicsBody2D>()->get_rid());
				else
					exclude.erase(get_parent()->cast_to<PhysicsBody2D>()->get_rid());
			}
		} break;
		case NOTIFICATION_EXIT_TREE: {

			if (enabled)
				set_fixed_process(false);

		} break;

		case NOTIFICATION_DRAW: {

			if (!get_tree()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint())
				break;
			Transform2D xf;
			xf.rotate(cast_to.angle());
			xf.translate(Vector2(cast_to.length(), 0));

			//Vector2 tip = Vector2(0,s->get_length());
			Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
			draw_line(Vector2(), cast_to, dcol, 3);
			Vector<Vector2> pts;
			float tsize = 4;
			pts.push_back(xf.xform(Vector2(tsize, 0)));
			pts.push_back(xf.xform(Vector2(0, 0.707 * tsize)));
			pts.push_back(xf.xform(Vector2(0, -0.707 * tsize)));
			Vector<Color> cols;
			for (int i = 0; i < 3; i++)
				cols.push_back(dcol);

			draw_primitive(pts, cols, Vector<Vector2>()); //small arrow

		} break;

		case NOTIFICATION_FIXED_PROCESS: {

			if (!enabled)
				break;

			_update_raycast_state();

		} break;
	}
}
Exemple #2
0
void RayCast::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_ENTER_TREE: {

			if (enabled && !get_tree()->is_editor_hint()) {
				set_fixed_process(true);
			} else
				set_fixed_process(false);



		} break;
		case NOTIFICATION_EXIT_TREE: {

			if (enabled) {
				set_fixed_process(false);
			}


		} break;
		case NOTIFICATION_FIXED_PROCESS: {

			if (!enabled)
				break;



			Ref<World> w3d = get_world();
			ERR_BREAK( w3d.is_null() );

			PhysicsDirectSpaceState *dss = PhysicsServer::get_singleton()->space_get_direct_state(w3d->get_space());
			ERR_BREAK( !dss );

			Transform gt = get_global_transform();

			Vector3 to = cast_to;
			if (to==Vector3())
				to=Vector3(0,0.01,0);

			PhysicsDirectSpaceState::RayResult rr;

			if (dss->intersect_ray(gt.get_origin(),gt.xform(to),rr,exclude)) {

				collided=true;
				against=rr.collider_id;
				collision_point=rr.position;
				collision_normal=rr.normal;
				against_shape=rr.shape;
			} else {
				collided=false;
			}



		} break;
	}
}
Exemple #3
0
void Camera2D::set_follow_smoothing(float p_speed) {

	smoothing=p_speed;
	if (smoothing>0 && !(is_inside_tree() && get_tree()->is_editor_hint()))
		set_fixed_process(true);
	else
		set_fixed_process(false);
}
Exemple #4
0
void Camera2D::set_follow_smoothing(float p_speed) {

	smoothing=p_speed;
	if (smoothing>0)
		set_fixed_process(true);
	else
		set_fixed_process(false);
}
Exemple #5
0
void Timer::_set_process(bool p_process, bool p_force) 
{
	switch (timer_process_mode) {
		case TIMER_PROCESS_FIXED: set_fixed_process(p_process); break;
		case TIMER_PROCESS_IDLE: set_process(p_process); break;
	}
}
void NetGameServer::_update_signal_mode()
{
	set_process(false);
	set_fixed_process(false);
	switch(signal_mode)
	{
		case PROCESS:
			set_process(true);
			break;
		case FIXED:
			set_fixed_process(true);
			break;
		case THREADED:
			// noop
			break;
	}
}
Exemple #7
0
void RayCast2D::set_enabled(bool p_enabled) {

	enabled = p_enabled;
	if (is_inside_tree() && !get_tree()->is_editor_hint())
		set_fixed_process(p_enabled);
	if (!p_enabled)
		collided = false;
}
Exemple #8
0
void ScrollContainer::_cancel_drag() {
	set_fixed_process(false);
	drag_touching_deaccel=false;
	drag_touching=false;
	drag_speed=Vector2();
	drag_accum=Vector2();
	last_drag_accum=Vector2();
	drag_from=Vector2();
}
Exemple #9
0
void Timer::set_timer_process_mode(TimerProcessMode p_mode) {

	if (timer_process_mode == p_mode)
		return;

	switch (timer_process_mode) {
		case TIMER_PROCESS_FIXED:
			if (is_fixed_processing()) {
				set_fixed_process(false);
				set_process(true);
			}
		break;
		case TIMER_PROCESS_IDLE:
			if (is_processing()) {
				set_process(false);
				set_fixed_process(true);
			}
		break;
	}
	timer_process_mode = p_mode;
}
void SampleEditor::edit(Ref<Sample> p_sample) {

    sample=p_sample;

    if (!sample.is_null())
        _update_sample();
    else {

        hide();
        set_fixed_process(false);
    }

}
Exemple #11
0
void Tween::_set_process(bool p_process,bool p_force) {

	if (processing==p_process && !p_force)
		return;

	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;
	}

	processing=p_process;
}
void AnimationPlayer::_set_process(bool p_process,bool p_force) {

	if (processing==p_process && !p_force)
		return;

	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;
	}

	processing=p_process;
}
void InterpolatedCamera::_notification(int p_what) {

	switch(p_what) {
		case NOTIFICATION_ENTER_SCENE: {

			if (get_scene()->is_editor_hint() && enabled)
				set_fixed_process(false);

		} break;
		case NOTIFICATION_PROCESS: {

			if (!enabled)
				break;
			if (has_node(target)) {

				Spatial *node = get_node(target)->cast_to<Spatial>();
				if (!node)
					break;

				float delta = speed*get_process_delta_time();
				Transform target_xform = node->get_global_transform();
				Transform local_transform = get_transform();
				local_transform = local_transform.interpolate_with(target_xform,delta);
				set_global_transform(local_transform);

				if (node->cast_to<Camera>()) {
					Camera *cam = node->cast_to<Camera>();
					if (cam->get_projection()==get_projection())  {

						float new_near = Math::lerp(get_znear(),cam->get_znear(),delta);
						float new_far = Math::lerp(get_zfar(),cam->get_zfar(),delta);

						if (cam->get_projection()==PROJECTION_ORTHOGONAL) {

							float size = Math::lerp(get_size(),cam->get_size(),delta);
							set_orthogonal(size,new_near,new_far);
						} else {

							float fov = Math::lerp(get_fov(),cam->get_fov(),delta);
							set_perspective(fov,new_near,new_far);
						}
					}
				}


			}

		} break;
	}
}
void ResourcePreloaderEditor::edit(ResourcePreloader* p_preloader) {

	preloader=p_preloader;


	if (p_preloader) {
		_update_library();
	} else {

		hide();
		set_fixed_process(false);
	}

}
void AnimationPlayer::_notification(int p_what) {

    switch(p_what) {

    case NOTIFICATION_ENTER_SCENE: {

        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_process(false);
        clear_caches();
    }
    break;
    case NOTIFICATION_READY: {

        if (!get_scene()->is_editor_hint() && animation_set.has(autoplay)) {
            play(autoplay);
        }
    }
    break;
    case NOTIFICATION_PROCESS: {
        if (animation_process_mode==ANIMATION_PROCESS_FIXED)
            break;

        if (processing)
            _animation_process( get_process_delta_time() );
    }
    break;
    case NOTIFICATION_FIXED_PROCESS: {

        if (animation_process_mode==ANIMATION_PROCESS_IDLE)
            break;

        if (processing)
            _animation_process( get_fixed_process_delta_time() );
    }
    break;
    case NOTIFICATION_EXIT_SCENE: {

        stop_all();
        clear_caches();
    }
    break;
    }
}
Exemple #16
0
void AnimationTreePlayer::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_ENTER_TREE: {

			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);
			}
		} break;
		case NOTIFICATION_READY: {
			dirty_caches=true;
			if (master!=NodePath()) {
				_update_sources();
			}
		} break;
		case NOTIFICATION_PROCESS: {
			if (animation_process_mode==ANIMATION_PROCESS_FIXED)
				break;

			if (processing)
				_process_animation( get_process_delta_time() );
		} break;
		case NOTIFICATION_FIXED_PROCESS: {

			if (animation_process_mode==ANIMATION_PROCESS_IDLE)
				break;

			if (processing)
				_process_animation(get_fixed_process_delta_time());
		} break;
	}

}
Exemple #17
0
void Tween::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_ENTER_TREE: {

			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);
			}
		} break;
		case NOTIFICATION_READY: {

		} break;
		case NOTIFICATION_PROCESS: {
			if (tween_process_mode==TWEEN_PROCESS_FIXED)
				break;

			if (processing)
				_tween_process( get_process_delta_time() );
		} break;
		case NOTIFICATION_FIXED_PROCESS: {

			if (tween_process_mode==TWEEN_PROCESS_IDLE)
				break;

			if (processing)
				_tween_process( get_fixed_process_delta_time() );
		} break;
		case NOTIFICATION_EXIT_TREE: {

			stop_all();
		} break;
	}
}
Exemple #18
0
void ScrollBar::_drag_slave_input(const InputEvent& p_input) {

    switch(p_input.type) {

    case InputEvent::MOUSE_BUTTON: {

        const InputEventMouseButton &mb=p_input.mouse_button;

        if (mb.button_index!=1)
            break;

        if (mb.pressed) {

            if (drag_slave_touching) {
                set_fixed_process(false);
                drag_slave_touching_deaccel=false;
                drag_slave_touching=false;
                drag_slave_speed=Vector2();
                drag_slave_accum=Vector2();
                last_drag_slave_accum=Vector2();
                drag_slave_from=Vector2();
            }

            if (true) {
                drag_slave_speed=Vector2();
                drag_slave_accum=Vector2();
                last_drag_slave_accum=Vector2();
                //drag_slave_from=Vector2(h_scroll->get_val(),v_scroll->get_val());
                drag_slave_from= Vector2(orientation==HORIZONTAL?get_val():0,orientation==VERTICAL?get_val():0);

                drag_slave_touching=OS::get_singleton()->has_touchscreen_ui_hint();
                drag_slave_touching_deaccel=false;
                time_since_motion=0;
                if (drag_slave_touching) {
                    set_fixed_process(true);
                    time_since_motion=0;

                }
            }

        } else {

            if (drag_slave_touching) {

                if (drag_slave_speed==Vector2()) {
                    drag_slave_touching_deaccel=false;
                    drag_slave_touching=false;
                    set_fixed_process(false);
                } else {

                    drag_slave_touching_deaccel=true;
                }
            }
        }
    }
    break;
    case InputEvent::MOUSE_MOTION: {

        const InputEventMouseMotion &mm=p_input.mouse_motion;

        if (drag_slave_touching && ! drag_slave_touching_deaccel) {

            Vector2 motion = Vector2(mm.relative_x,mm.relative_y);

            drag_slave_accum-=motion;
            Vector2 diff = drag_slave_from+drag_slave_accum;

            if (orientation==HORIZONTAL)
                set_val(diff.x);
            //else
            //	drag_slave_accum.x=0;
            if (orientation==VERTICAL)
                set_val(diff.y);
            //else
            //	drag_slave_accum.y=0;
            time_since_motion=0;
        }

    }
    break;
    }
}
Exemple #19
0
void RayCast2D::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_ENTER_TREE: {

			if (enabled && !get_tree()->is_editor_hint())
				set_fixed_process(true);
			else
				set_fixed_process(false);

		} break;
		case NOTIFICATION_EXIT_TREE: {

			if (enabled)
				set_fixed_process(false);

		} break;
#ifdef TOOLS_ENABLED
		case NOTIFICATION_DRAW: {

			if (!get_tree()->is_editor_hint())
				break;
			Matrix32 xf;
			xf.rotate(cast_to.atan2());
			xf.translate(Vector2(0,cast_to.length()));

			//Vector2 tip = Vector2(0,s->get_length());
			Color dcol(0.9,0.2,0.2,0.4);
			draw_line(Vector2(),cast_to,dcol,3);
			Vector<Vector2> pts;
			float tsize=4;
			pts.push_back(xf.xform(Vector2(0,tsize)));
			pts.push_back(xf.xform(Vector2(0.707*tsize,0)));
			pts.push_back(xf.xform(Vector2(-0.707*tsize,0)));
			Vector<Color> cols;
			for(int i=0;i<3;i++)
				cols.push_back(dcol);

			draw_primitive(pts,cols,Vector<Vector2>()); //small arrow

		} break;
#endif

		case NOTIFICATION_FIXED_PROCESS: {

			if (!enabled)
				break;



			Ref<World2D> w2d = get_world_2d();
			ERR_BREAK( w2d.is_null() );

			Physics2DDirectSpaceState *dss = Physics2DServer::get_singleton()->space_get_direct_state(w2d->get_space());
			ERR_BREAK( !dss );

			Matrix32 gt = get_global_transform();

			Vector2 to = cast_to;
			if (to==Vector2())
				to=Vector2(0,0.01);

			Physics2DDirectSpaceState::RayResult rr;

			if (dss->intersect_ray(gt.get_origin(),gt.xform(to),rr,exclude,layer_mask)) {

				collided=true;
				against=rr.collider_id;
				collision_point=rr.position;
				collision_normal=rr.normal;
				against_shape=rr.shape;
			} else {
				collided=false;
			}



		} break;
	}
}
Exemple #20
0
void Camera2D::_notification(int p_what) {

	switch (p_what) {

		case NOTIFICATION_FIXED_PROCESS: {

			_update_scroll();

		} break;
		case NOTIFICATION_TRANSFORM_CHANGED: {

			if (!is_fixed_processing())
				_update_scroll();

		} break;
		case NOTIFICATION_ENTER_TREE: {

			if (custom_viewport && ObjectDB::get_instance(custom_viewport_id)) {
				viewport = custom_viewport;
			} else {
				viewport = get_viewport();
			}

			canvas = get_canvas();

			RID vp = viewport->get_viewport_rid();

			group_name = "__cameras_" + itos(vp.get_id());
			canvas_group_name = "__cameras_c" + itos(canvas.get_id());
			add_to_group(group_name);
			add_to_group(canvas_group_name);

			if (Engine::get_singleton()->is_editor_hint()) {
				set_fixed_process(false);
			}

			_update_scroll();
			first = true;

		} break;
		case NOTIFICATION_EXIT_TREE: {

			if (is_current()) {
				if (viewport && !(custom_viewport && !ObjectDB::get_instance(custom_viewport_id))) {
					viewport->set_canvas_transform(Transform2D());
				}
			}
			remove_from_group(group_name);
			remove_from_group(canvas_group_name);
			viewport = NULL;

		} break;
		case NOTIFICATION_DRAW: {

			if (!is_inside_tree() || !Engine::get_singleton()->is_editor_hint())
				break;

			if (screen_drawing_enabled) {
				Color area_axis_color(0.5, 0.42, 0.87, 0.63);
				float area_axis_width = 1;
				if (is_current()) {
					area_axis_width = 3;
					area_axis_color.a = 0.83;
				}

				Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
				Size2 screen_size = get_viewport_rect().size;

				Vector2 screen_endpoints[4] = {
					inv_camera_transform.xform(Vector2(0, 0)),
					inv_camera_transform.xform(Vector2(screen_size.width, 0)),
					inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
					inv_camera_transform.xform(Vector2(0, screen_size.height))
				};

				Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space

				for (int i = 0; i < 4; i++) {
					draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i + 1) % 4]), area_axis_color, area_axis_width);
				}
			}

			if (limit_drawing_enabled) {
				Color limit_drawing_color(1, 1, 0, 0.63);
				float limit_drawing_width = 1;
				if (is_current()) {
					limit_drawing_color.a = 0.83;
					limit_drawing_width = 3;
				}

				Vector2 camera_origin = get_global_transform().get_origin();
				Vector2 camera_scale = get_global_transform().get_scale().abs();
				Vector2 limit_points[4] = {
					(Vector2(limit[MARGIN_LEFT], limit[MARGIN_TOP]) - camera_origin) / camera_scale,
					(Vector2(limit[MARGIN_RIGHT], limit[MARGIN_TOP]) - camera_origin) / camera_scale,
					(Vector2(limit[MARGIN_RIGHT], limit[MARGIN_BOTTOM]) - camera_origin) / camera_scale,
					(Vector2(limit[MARGIN_LEFT], limit[MARGIN_BOTTOM]) - camera_origin) / camera_scale
				};

				for (int i = 0; i < 4; i++) {
					draw_line(limit_points[i], limit_points[(i + 1) % 4], limit_drawing_color, limit_drawing_width);
				}
			}

			if (margin_drawing_enabled) {
				Color margin_drawing_color(0, 1, 1, 0.63);
				float margin_drawing_width = 1;
				if (is_current()) {
					margin_drawing_width = 3;
					margin_drawing_color.a = 0.83;
				}

				Transform2D inv_camera_transform = get_camera_transform().affine_inverse();
				Size2 screen_size = get_viewport_rect().size;

				Vector2 margin_endpoints[4] = {
					inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[MARGIN_LEFT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[MARGIN_TOP]))),
					inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[MARGIN_RIGHT]), (screen_size.height / 2) - ((screen_size.height / 2) * drag_margin[MARGIN_TOP]))),
					inv_camera_transform.xform(Vector2((screen_size.width / 2) + ((screen_size.width / 2) * drag_margin[MARGIN_RIGHT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[MARGIN_BOTTOM]))),
					inv_camera_transform.xform(Vector2((screen_size.width / 2) - ((screen_size.width / 2) * drag_margin[MARGIN_LEFT]), (screen_size.height / 2) + ((screen_size.height / 2) * drag_margin[MARGIN_BOTTOM])))
				};

				Transform2D inv_transform = get_global_transform().affine_inverse(); // undo global space

				for (int i = 0; i < 4; i++) {
					draw_line(inv_transform.xform(margin_endpoints[i]), inv_transform.xform(margin_endpoints[(i + 1) % 4]), margin_drawing_color, margin_drawing_width);
				}
			}

		} break;
	}
}
Exemple #21
0
void ScrollContainer::_gui_input(const Ref<InputEvent> &p_gui_input) {

	Ref<InputEventMouseButton> mb = p_gui_input;

	if (mb.is_valid()) {

		if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
			// only horizontal is enabled, scroll horizontally
			if (h_scroll->is_visible() && !v_scroll->is_visible()) {
				h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8 * mb->get_factor());
			} else if (v_scroll->is_visible_in_tree()) {
				v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8 * mb->get_factor());
			}
		}

		if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
			// only horizontal is enabled, scroll horizontally
			if (h_scroll->is_visible() && !v_scroll->is_visible()) {
				h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb->get_factor());
			} else if (v_scroll->is_visible()) {
				v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb->get_factor());
			}
		}

		if (mb->get_button_index() == BUTTON_WHEEL_LEFT && mb->is_pressed()) {
			if (h_scroll->is_visible_in_tree()) {
				h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb->get_factor() / 8);
			}
		}

		if (mb->get_button_index() == BUTTON_WHEEL_RIGHT && mb->is_pressed()) {
			if (h_scroll->is_visible_in_tree()) {
				h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb->get_factor() / 8);
			}
		}

		if (!OS::get_singleton()->has_touchscreen_ui_hint())
			return;

		if (mb->get_button_index() != BUTTON_LEFT)
			return;

		if (mb->is_pressed()) {

			if (drag_touching) {
				set_fixed_process(false);
				drag_touching_deaccel = false;
				drag_touching = false;
				drag_speed = Vector2();
				drag_accum = Vector2();
				last_drag_accum = Vector2();
				drag_from = Vector2();
			}

			if (true) {
				drag_speed = Vector2();
				drag_accum = Vector2();
				last_drag_accum = Vector2();
				drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value());
				drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
				drag_touching_deaccel = false;
				time_since_motion = 0;
				if (drag_touching) {
					set_fixed_process(true);
					time_since_motion = 0;
				}
			}

		} else {
			if (drag_touching) {

				if (drag_speed == Vector2()) {
					drag_touching_deaccel = false;
					drag_touching = false;
					set_fixed_process(false);
				} else {

					drag_touching_deaccel = true;
				}
			}
		}
	}

	Ref<InputEventMouseMotion> mm = p_gui_input;

	if (mm.is_valid()) {

		if (drag_touching && !drag_touching_deaccel) {

			Vector2 motion = Vector2(mm->get_relative().x, mm->get_relative().y);
			drag_accum -= motion;
			Vector2 diff = drag_from + drag_accum;

			if (scroll_h)
				h_scroll->set_value(diff.x);
			else
				drag_accum.x = 0;
			if (scroll_v)
				v_scroll->set_value(diff.y);
			else
				drag_accum.y = 0;
			time_since_motion = 0;
		}
	}
}
Exemple #22
0
void ScrollBar::_drag_slave_input(const Ref<InputEvent> &p_input) {

	Ref<InputEventMouseButton> mb = p_input;

	if (mb.is_valid()) {

		if (mb->get_button_index() != 1)
			return;

		if (mb->is_pressed()) {

			if (drag_slave_touching) {
				set_fixed_process(false);
				drag_slave_touching_deaccel = false;
				drag_slave_touching = false;
				drag_slave_speed = Vector2();
				drag_slave_accum = Vector2();
				last_drag_slave_accum = Vector2();
				drag_slave_from = Vector2();
			}

			if (true) {
				drag_slave_speed = Vector2();
				drag_slave_accum = Vector2();
				last_drag_slave_accum = Vector2();
				//drag_slave_from=Vector2(h_scroll->get_val(),v_scroll->get_val());
				drag_slave_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);

				drag_slave_touching = OS::get_singleton()->has_touchscreen_ui_hint();
				drag_slave_touching_deaccel = false;
				time_since_motion = 0;
				if (drag_slave_touching) {
					set_fixed_process(true);
					time_since_motion = 0;
				}
			}

		} else {

			if (drag_slave_touching) {

				if (drag_slave_speed == Vector2()) {
					drag_slave_touching_deaccel = false;
					drag_slave_touching = false;
					set_fixed_process(false);
				} else {

					drag_slave_touching_deaccel = true;
				}
			}
		}
	}

	Ref<InputEventMouseMotion> mm = p_input;

	if (mm.is_valid()) {

		if (drag_slave_touching && !drag_slave_touching_deaccel) {

			Vector2 motion = Vector2(mm->get_relative().x, mm->get_relative().y);

			drag_slave_accum -= motion;
			Vector2 diff = drag_slave_from + drag_slave_accum;

			if (orientation == HORIZONTAL)
				set_value(diff.x);
			/*
			else
				drag_slave_accum.x=0;
			*/
			if (orientation == VERTICAL)
				set_value(diff.y);
			/*
			else
				drag_slave_accum.y=0;
			*/
			time_since_motion = 0;
		}
	}
}
Exemple #23
0
void ScrollBar::_gui_input(Ref<InputEvent> p_event) {

	Ref<InputEventMouseButton> b = p_event;

	if (b.is_valid()) {
		accept_event();

		if (b->get_button_index() == 5 && b->is_pressed()) {

			/*
			if (orientation==VERTICAL)
				set_val( get_val() + get_page() / 4.0 );
			else
			*/
			set_value(get_value() + get_page() / 4.0);
			accept_event();
		}

		if (b->get_button_index() == 4 && b->is_pressed()) {

			/*
			if (orientation==HORIZONTAL)
				set_val( get_val() - get_page() / 4.0 );
			else
			*/
			set_value(get_value() - get_page() / 4.0);
			accept_event();
		}

		if (b->get_button_index() != 1)
			return;

		if (b->is_pressed()) {

			double ofs = orientation == VERTICAL ? b->get_position().y : b->get_position().x;
			Ref<Texture> decr = get_icon("decrement");
			Ref<Texture> incr = get_icon("increment");

			double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
			double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
			double grabber_ofs = get_grabber_offset();
			double grabber_size = get_grabber_size();
			double total = orientation == VERTICAL ? get_size().height : get_size().width;

			if (ofs < decr_size) {

				set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
				return;
			}

			if (ofs > total - incr_size) {

				set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
				return;
			}

			ofs -= decr_size;

			if (ofs < grabber_ofs) {

				if (scrolling) {
					target_scroll = target_scroll - get_page();
				} else {
					target_scroll = get_value() - get_page();
				}

				if (smooth_scroll_enabled) {
					scrolling = true;
					set_fixed_process(true);
				} else {
					set_value(target_scroll);
				}
				return;
			}

			ofs -= grabber_ofs;

			if (ofs < grabber_size) {

				drag.active = true;
				drag.pos_at_click = grabber_ofs + ofs;
				drag.value_at_click = get_as_ratio();
				update();
			} else {
				if (scrolling) {
					target_scroll = target_scroll + get_page();
				} else {
					target_scroll = get_value() + get_page();
				}

				if (smooth_scroll_enabled) {
					scrolling = true;
					set_fixed_process(true);
				} else {
					set_value(target_scroll);
				}
			}

		} else {

			drag.active = false;
			update();
		}
	}

	Ref<InputEventMouseMotion> m = p_event;

	if (m.is_valid()) {

		accept_event();

		if (drag.active) {

			double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
			Ref<Texture> decr = get_icon("decrement");

			double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
			ofs -= decr_size;

			double diff = (ofs - drag.pos_at_click) / get_area_size();

			set_as_ratio(drag.value_at_click + diff);
		} else {

			double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
			Ref<Texture> decr = get_icon("decrement");
			Ref<Texture> incr = get_icon("increment");

			double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
			double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
			double total = orientation == VERTICAL ? get_size().height : get_size().width;

			HighlightStatus new_hilite;

			if (ofs < decr_size) {

				new_hilite = HIGHLIGHT_DECR;

			} else if (ofs > total - incr_size) {

				new_hilite = HIGHLIGHT_INCR;

			} else {

				new_hilite = HIGHLIGHT_RANGE;
			}

			if (new_hilite != highlight) {

				highlight = new_hilite;
				update();
			}
		}
	}

	Ref<InputEventKey> k = p_event;

	if (k.is_valid()) {

		if (!k->is_pressed())
			return;

		switch (k->get_scancode()) {

			case KEY_LEFT: {

				if (orientation != HORIZONTAL)
					return;
				set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));

			} break;
			case KEY_RIGHT: {

				if (orientation != HORIZONTAL)
					return;
				set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));

			} break;
			case KEY_UP: {

				if (orientation != VERTICAL)
					return;

				set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));

			} break;
			case KEY_DOWN: {

				if (orientation != VERTICAL)
					return;
				set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));

			} break;
			case KEY_HOME: {

				set_value(get_min());

			} break;
			case KEY_END: {

				set_value(get_max());

			} break;
		}
	}
}
Exemple #24
0
void ScrollBar::_notification(int p_what) {

	if (p_what == NOTIFICATION_DRAW) {

		RID ci = get_canvas_item();

		Ref<Texture> decr = highlight == HIGHLIGHT_DECR ? get_icon("decrement_highlight") : get_icon("decrement");
		Ref<Texture> incr = highlight == HIGHLIGHT_INCR ? get_icon("increment_highlight") : get_icon("increment");
		Ref<StyleBox> bg = has_focus() ? get_stylebox("scroll_focus") : get_stylebox("scroll");

		Ref<StyleBox> grabber;
		if (drag.active)
			grabber = get_stylebox("grabber_pressed");
		else if (highlight == HIGHLIGHT_RANGE)
			grabber = get_stylebox("grabber_highlight");
		else
			grabber = get_stylebox("grabber");

		Point2 ofs;

		VisualServer *vs = VisualServer::get_singleton();

		vs->canvas_item_add_texture_rect(ci, Rect2(Point2(), decr->get_size()), decr->get_rid());

		if (orientation == HORIZONTAL)
			ofs.x += decr->get_width();
		else
			ofs.y += decr->get_height();

		Size2 area = get_size();

		if (orientation == HORIZONTAL)
			area.width -= incr->get_width() + decr->get_width();
		else
			area.height -= incr->get_height() + decr->get_height();

		bg->draw(ci, Rect2(ofs, area));

		if (orientation == HORIZONTAL)
			ofs.width += area.width;
		else
			ofs.height += area.height;

		vs->canvas_item_add_texture_rect(ci, Rect2(ofs, decr->get_size()), incr->get_rid());
		Rect2 grabber_rect;

		if (orientation == HORIZONTAL) {

			grabber_rect.size.width = get_grabber_size();
			grabber_rect.size.height = get_size().height;
			grabber_rect.position.y = 0;
			grabber_rect.position.x = get_grabber_offset() + decr->get_width() + bg->get_margin(MARGIN_LEFT);
		} else {

			grabber_rect.size.width = get_size().width;
			grabber_rect.size.height = get_grabber_size();
			grabber_rect.position.y = get_grabber_offset() + decr->get_height() + bg->get_margin(MARGIN_TOP);
			grabber_rect.position.x = 0;
		}

		grabber->draw(ci, grabber_rect);
	}

	if (p_what == NOTIFICATION_ENTER_TREE) {

		if (has_node(drag_slave_path)) {
			Node *n = get_node(drag_slave_path);
			drag_slave = n->cast_to<Control>();
		}

		if (drag_slave) {
			drag_slave->connect("gui_input", this, "_drag_slave_input");
			drag_slave->connect("tree_exited", this, "_drag_slave_exit", varray(), CONNECT_ONESHOT);
		}
	}
	if (p_what == NOTIFICATION_EXIT_TREE) {

		if (drag_slave) {
			drag_slave->disconnect("gui_input", this, "_drag_slave_input");
			drag_slave->disconnect("tree_exited", this, "_drag_slave_exit");
		}

		drag_slave = NULL;
	}

	if (p_what == NOTIFICATION_FIXED_PROCESS) {

		if (scrolling) {
			if (get_value() != target_scroll) {
				double target = target_scroll - get_value();
				double dist = sqrt(target * target);
				double vel = ((target / dist) * 500) * get_fixed_process_delta_time();

				if (vel >= dist) {
					set_value(target_scroll);
				} else {
					set_value(get_value() + vel);
				}
			} else {
				scrolling = false;
				set_fixed_process(false);
			}
		} else if (drag_slave_touching) {

			if (drag_slave_touching_deaccel) {

				Vector2 pos = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
				pos += drag_slave_speed * get_fixed_process_delta_time();

				bool turnoff = false;

				if (orientation == HORIZONTAL) {

					if (pos.x < 0) {
						pos.x = 0;
						turnoff = true;
					}

					if (pos.x > (get_max() - get_page())) {
						pos.x = get_max() - get_page();
						turnoff = true;
					}

					set_value(pos.x);

					float sgn_x = drag_slave_speed.x < 0 ? -1 : 1;
					float val_x = Math::abs(drag_slave_speed.x);
					val_x -= 1000 * get_fixed_process_delta_time();

					if (val_x < 0) {
						turnoff = true;
					}

					drag_slave_speed.x = sgn_x * val_x;

				} else {

					if (pos.y < 0) {
						pos.y = 0;
						turnoff = true;
					}

					if (pos.y > (get_max() - get_page())) {
						pos.y = get_max() - get_page();
						turnoff = true;
					}

					set_value(pos.y);

					float sgn_y = drag_slave_speed.y < 0 ? -1 : 1;
					float val_y = Math::abs(drag_slave_speed.y);
					val_y -= 1000 * get_fixed_process_delta_time();

					if (val_y < 0) {
						turnoff = true;
					}
					drag_slave_speed.y = sgn_y * val_y;
				}

				if (turnoff) {
					set_fixed_process(false);
					drag_slave_touching = false;
					drag_slave_touching_deaccel = false;
				}

			} else {

				if (time_since_motion == 0 || time_since_motion > 0.1) {

					Vector2 diff = drag_slave_accum - last_drag_slave_accum;
					last_drag_slave_accum = drag_slave_accum;
					drag_slave_speed = diff / get_fixed_process_delta_time();
				}

				time_since_motion += get_fixed_process_delta_time();
			}
		}
	}

	if (p_what == NOTIFICATION_MOUSE_EXIT) {

		highlight = HIGHLIGHT_NONE;
		update();
	}
}
Exemple #25
0
void ScrollContainer::_notification(int p_what) {

	if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {

		call_deferred("_update_scrollbar_pos");
	};


	if (p_what==NOTIFICATION_SORT_CHILDREN) {

		child_max_size = Size2(0, 0);
		Size2 size = get_size();
		if (h_scroll->is_visible())
			size.y-=h_scroll->get_minimum_size().y;

		if (v_scroll->is_visible())
			size.x-=h_scroll->get_minimum_size().x;

		for(int i=0;i<get_child_count();i++) {

			Control *c = get_child(i)->cast_to<Control>();
			if (!c)
				continue;
			if (c->is_set_as_toplevel())
				continue;
			if (c == h_scroll || c == v_scroll)
				continue;
			Size2 minsize = c->get_combined_minimum_size();
			child_max_size.x = MAX(child_max_size.x, minsize.x);
			child_max_size.y = MAX(child_max_size.y, minsize.y);

			Rect2 r = Rect2(-scroll,minsize);
			if (!(scroll_h || h_scroll->is_visible())) {
				r.pos.x=0;
				if (c->get_h_size_flags()&SIZE_EXPAND)
					r.size.width=MAX(size.width,minsize.width);
				else
					r.size.width=minsize.width;
			}
			if (!(scroll_v || v_scroll->is_visible())) {
				r.pos.y=0;
				r.size.height=size.height;
				if (c->get_v_size_flags()&SIZE_EXPAND)
					r.size.height=MAX(size.height,minsize.height);
				else
					r.size.height=minsize.height;

			}
			fit_child_in_rect(c,r);
		}
		update();
	};

	if (p_what == NOTIFICATION_DRAW) {

		update_scrollbars();

		VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
	}

	if (p_what==NOTIFICATION_FIXED_PROCESS) {

		if (drag_touching)  {

			if (drag_touching_deaccel) {

				Vector2 pos = Vector2(h_scroll->get_val(),v_scroll->get_val());
				pos+=drag_speed*get_fixed_process_delta_time();

				bool turnoff_h=false;
				bool turnoff_v=false;

				if (pos.x<0) {
					pos.x=0;
					turnoff_h=true;
				}
				if (pos.x > (h_scroll->get_max()-h_scroll->get_page())) {
					pos.x=h_scroll->get_max()-h_scroll->get_page();
					turnoff_h=true;
				}

				if (pos.y<0) {
					pos.y=0;
					turnoff_v=true;
				}
				if (pos.y > (v_scroll->get_max()-v_scroll->get_page())) {
					pos.y=v_scroll->get_max()-v_scroll->get_page();
					turnoff_v=true;
				}

				if (scroll_h)
					h_scroll->set_val(pos.x);
				if (scroll_v)
					v_scroll->set_val(pos.y);

				float sgn_x = drag_speed.x<0? -1 : 1;
				float val_x = Math::abs(drag_speed.x);
				val_x-=1000*get_fixed_process_delta_time();

				if (val_x<0) {
					turnoff_h=true;
				}


				float sgn_y = drag_speed.y<0? -1 : 1;
				float val_y = Math::abs(drag_speed.y);
				val_y-=1000*get_fixed_process_delta_time();

				if (val_y<0) {
					turnoff_v=true;
				}


				drag_speed=Vector2(sgn_x*val_x,sgn_y*val_y);

				if (turnoff_h && turnoff_v) {
					set_fixed_process(false);
					drag_touching=false;
					drag_touching_deaccel=false;
				}


			} else {


				if (time_since_motion==0 || time_since_motion>0.1) {

					Vector2 diff = drag_accum - last_drag_accum;
					last_drag_accum=drag_accum;
					drag_speed=diff/get_fixed_process_delta_time();
				}

				time_since_motion+=get_fixed_process_delta_time();
			}
		}
	}

};
Exemple #26
0
void ScrollContainer::_input_event(const InputEvent& p_input_event) {


	switch(p_input_event.type) {

		case InputEvent::MOUSE_BUTTON: {

			const InputEventMouseButton &mb=p_input_event.mouse_button;

			if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed && v_scroll->is_visible()) {

				v_scroll->set_val( v_scroll->get_val()-v_scroll->get_page()/8 );
			}

			if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed && v_scroll->is_visible()) {

				v_scroll->set_val( v_scroll->get_val()+v_scroll->get_page()/8 );
			}

			if(!OS::get_singleton()->has_touchscreen_ui_hint())
				return;

			if (mb.button_index!=BUTTON_LEFT)
				break;

			if (mb.pressed) {

				if (drag_touching) {
					set_fixed_process(false);
					drag_touching_deaccel=false;
					drag_touching=false;
					drag_speed=Vector2();
					drag_accum=Vector2();
					last_drag_accum=Vector2();
					drag_from=Vector2();
				}

				if (true) {
					drag_speed=Vector2();
					drag_accum=Vector2();
					last_drag_accum=Vector2();
					drag_from=Vector2(h_scroll->get_val(),v_scroll->get_val());
					drag_touching=OS::get_singleton()->has_touchscreen_ui_hint();
					drag_touching_deaccel=false;
					time_since_motion=0;
					if (drag_touching) {
						set_fixed_process(true);
						time_since_motion=0;

					}
				}


			} else {
				if (drag_touching) {

					if (drag_speed==Vector2()) {
						drag_touching_deaccel=false;
						drag_touching=false;
						set_fixed_process(false);
					} else {

						drag_touching_deaccel=true;
					}
				}
			}


		} break;
		case InputEvent::MOUSE_MOTION: {

			const InputEventMouseMotion &mm=p_input_event.mouse_motion;

			if (drag_touching && ! drag_touching_deaccel) {

				Vector2 motion = Vector2(mm.relative_x,mm.relative_y);
				drag_accum-=motion;
				Vector2 diff = drag_from+drag_accum;

				if (scroll_h)
					h_scroll->set_val(diff.x);
				else
					drag_accum.x=0;
				if (scroll_v)
					v_scroll->set_val(diff.y);
				else
					drag_accum.y=0;
				time_since_motion=0;
			}

		} break;
	}

}
Exemple #27
0
void Camera2D::_notification(int p_what) {

	switch(p_what) {

		case NOTIFICATION_FIXED_PROCESS: {

			_update_scroll();

		} break;
		case NOTIFICATION_TRANSFORM_CHANGED: {


			if (!is_fixed_processing())
				_update_scroll();

		} break;
		case NOTIFICATION_ENTER_TREE: {


			viewport = NULL;
			Node *n=this;
			while(n){

				viewport = n->cast_to<Viewport>();
				if (viewport)
					break;
				n=n->get_parent();
			}

			canvas = get_canvas();

			RID vp = viewport->get_viewport();

			group_name = "__cameras_"+itos(vp.get_id());
			canvas_group_name ="__cameras_c"+itos(canvas.get_id());
			add_to_group(group_name);
			add_to_group(canvas_group_name);

			if(get_tree()->is_editor_hint()) {
				set_fixed_process(false);
			}

			_update_scroll();
			first=true;


		} break;
		case NOTIFICATION_EXIT_TREE: {

			if (is_current()) {
				if (viewport) {
					viewport->set_canvas_transform( Matrix32() );
				}
			}
			remove_from_group(group_name);
			remove_from_group(canvas_group_name);
			viewport=NULL;

		} break;
		case NOTIFICATION_DRAW: {

			if (!is_inside_tree() || !get_tree()->is_editor_hint())
				break;

			Color area_axis_color(0.5, 0.42, 0.87, 0.63);
			float area_axis_width = 1;
			if(current)
				area_axis_width = 3;

			Matrix32 inv_camera_transform = get_camera_transform().affine_inverse();
			Size2 screen_size = get_viewport_rect().size;

			Vector2 screen_endpoints[4]= {
				inv_camera_transform.xform(Vector2(0, 0)),
				inv_camera_transform.xform(Vector2(screen_size.width,0)),
				inv_camera_transform.xform(Vector2(screen_size.width, screen_size.height)),
				inv_camera_transform.xform(Vector2(0, screen_size.height))
			};

			Matrix32 inv_transform = get_global_transform().affine_inverse(); // undo global space

			for(int i=0;i<4;i++) {
				draw_line(inv_transform.xform(screen_endpoints[i]), inv_transform.xform(screen_endpoints[(i+1)%4]), area_axis_color, area_axis_width);
			}

		} break;
	}
}