Exemple #1
0
void ButtonArray::_input_event(const InputEvent &p_event) {

	if (
			((orientation == HORIZONTAL && p_event.is_action("ui_left")) ||
					(orientation == VERTICAL && p_event.is_action("ui_up"))) &&
			p_event.is_pressed() && selected > 0) {
		set_selected(selected - 1);
		accept_event();
		emit_signal("button_selected", selected);
		return;
	}

	if (
			((orientation == HORIZONTAL && p_event.is_action("ui_right")) ||
					(orientation == VERTICAL && p_event.is_action("ui_down"))) &&
			p_event.is_pressed() && selected < (buttons.size() - 1)) {
		set_selected(selected + 1);
		accept_event();
		emit_signal("button_selected", selected);
		return;
	}

	if (p_event.type == InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index == BUTTON_LEFT) {

		int ofs = orientation == HORIZONTAL ? p_event.mouse_button.x : p_event.mouse_button.y;

		for (int i = 0; i < buttons.size(); i++) {

			if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) {

				set_selected(i);
				emit_signal("button_selected", i);
				return;
			}
		}
	}

	if (p_event.type == InputEvent::MOUSE_MOTION) {

		int ofs = orientation == HORIZONTAL ? p_event.mouse_motion.x : p_event.mouse_motion.y;
		int new_hover = -1;
		for (int i = 0; i < buttons.size(); i++) {

			if (ofs >= buttons[i]._pos_cache && ofs < buttons[i]._pos_cache + buttons[i]._size_cache) {

				new_hover = i;
				break;
			}
		}

		if (new_hover != hover) {
			hover = new_hover;
			emit_signal("mouse_sub_enter");
			update();
		}
	}
}
Exemple #2
0
void MenuButton::_unhandled_key_input(InputEvent p_event) {


	if (p_event.is_pressed() && !p_event.is_echo() && (p_event.type==InputEvent::KEY || p_event.type==InputEvent::ACTION || p_event.type==InputEvent::JOYSTICK_BUTTON)) {

		if (!get_parent() || !is_visible() || is_disabled())
			return;


		if (popup->activate_item_by_event(p_event))
			accept_event();
	}
}
Exemple #3
0
void BaseButton::_unhandled_input(InputEvent p_event) {

	if (!is_disabled() && is_visible() && p_event.is_pressed() && !p_event.is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {

		if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this))
			return; //ignore because of modal window

		if (is_toggle_mode()) {
			set_pressed(!is_pressed());
			emit_signal("toggled",is_pressed());
		}

		emit_signal("pressed");
	}
}
Exemple #4
0
void BaseButton::_input_event(InputEvent p_event) {


	if (status.disabled) // no interaction with disabled button
		return;

	switch(p_event.type) {

		case InputEvent::MOUSE_BUTTON: {

			const InputEventMouseButton &b=p_event.mouse_button;

			if ( status.disabled || b.button_index!=1 )
				return;

			if (status.pressing_button)
				break;

			if (status.click_on_press) {

				if (b.pressed) {

					emit_signal("button_down");

					if (!toggle_mode) { //mouse press attempt

						status.press_attempt=true;
						status.pressing_inside=true;

						pressed();
						if (get_script_instance()) {
							Variant::CallError ce;
							get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce);
						}

						emit_signal("pressed");

					} else {

						status.pressed=!status.pressed;
						pressed();
						if (get_script_instance()) {
							Variant::CallError ce;
							get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce);
						}
						emit_signal("pressed");

						toggled(status.pressed);
						emit_signal("toggled",status.pressed);

					}


				} else {

					emit_signal("button_up");

					if (status.press_attempt && status.pressing_inside) {
//						released();
						emit_signal("released");
					}
					status.press_attempt=false;
				}
				update();
				break;
			}

			if (b.pressed) {

				status.press_attempt=true;
				status.pressing_inside=true;
				emit_signal("button_down");

			} else {

				emit_signal("button_up");

				if (status.press_attempt &&status.pressing_inside) {

					if (!toggle_mode) { //mouse press attempt

						pressed();
						if (get_script_instance()) {
							Variant::CallError ce;
							get_script_instance()->call(SceneStringNames::get_singleton()->_pressed,NULL,0,ce);
						}

						emit_signal("pressed");

					} else {

						status.pressed=!status.pressed;

						pressed();
						emit_signal("pressed");

						toggled(status.pressed);
						emit_signal("toggled",status.pressed);
						if (get_script_instance()) {
							get_script_instance()->call(SceneStringNames::get_singleton()->_toggled,status.pressed);
						}


					}

				}

				status.press_attempt=false;

			}

			update();
		} break;
		case InputEvent::MOUSE_MOTION: {

			if (status.press_attempt && status.pressing_button==0) {
				bool last_press_inside=status.pressing_inside;
				status.pressing_inside=has_point(Point2(p_event.mouse_motion.x,p_event.mouse_motion.y));
				if (last_press_inside!=status.pressing_inside)
					update();
			}
		} break;
		case InputEvent::ACTION:
		case InputEvent::JOYSTICK_BUTTON:
		case InputEvent::KEY: {


			if (p_event.is_echo()) {
				break;
			}

			if (status.disabled) {
				break;
			}

			if (status.press_attempt && status.pressing_button==0) {
				break;
			}

			if (p_event.is_action("ui_accept")) {

				if (p_event.is_pressed()) {

					status.pressing_button++;
					status.press_attempt=true;
					status.pressing_inside=true;
					emit_signal("button_down");

				} else if (status.press_attempt) {

					if (status.pressing_button)
						status.pressing_button--;

					if (status.pressing_button)
						break;

					status.press_attempt=false;
					status.pressing_inside=false;

					emit_signal("button_up");

					if (!toggle_mode) { //mouse press attempt

						pressed();
						emit_signal("pressed");
					} else {

						status.pressed=!status.pressed;

						pressed();
						emit_signal("pressed");

						toggled(status.pressed);
						if (get_script_instance()) {
							get_script_instance()->call(SceneStringNames::get_singleton()->_toggled,status.pressed);
						}
						emit_signal("toggled",status.pressed);
					}
				}

				accept_event();
				update();

			}
		}

	}
}
Exemple #5
0
void ItemList::_input_event(const InputEvent& p_event) {

	if (defer_select_single>=0 && p_event.type==InputEvent::MOUSE_MOTION) {
		defer_select_single=-1;
		return;
	}
	if (defer_select_single>=0 && p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT && !p_event.mouse_button.pressed) {

		select(defer_select_single,true);

		emit_signal("multi_selected",defer_select_single,true);
		defer_select_single=-1;
		return;
	}

	if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT && p_event.mouse_button.pressed) {

		const InputEventMouseButton &mb = p_event.mouse_button;

		search_string=""; //any mousepress cancels
		Vector2 pos(mb.x,mb.y);
		Ref<StyleBox> bg = get_stylebox("bg");
		pos-=bg->get_offset();
		pos.y+=scroll_bar->get_val();

		int closest = -1;
		int closest_dist=0x7FFFFFFF;

		for(int i=0;i<items.size();i++) {

			Rect2 rc = items[i].rect_cache;
			if (i%current_columns==current_columns-1) {
				rc.size.width=get_size().width; //not right but works
			}

			if (rc.has_point(pos)) {
				closest=i;
				break;
			}

			float dist = rc.distance_to(pos);
			if (dist<closest_dist) {
				closest=i;
				closest_dist=dist;
			}
		}

		if (closest!=-1) {

			int i = closest;

			if (select_mode==SELECT_MULTI && items[i].selected && mb.mod.command) {
				unselect(i);
				emit_signal("multi_selected",i,false);
			} else if (select_mode==SELECT_MULTI && mb.mod.shift && current>=0 && current<items.size() && current!=i) {

				int from = current;
				int to = i;
				if (i<current) {
					SWAP(from,to);
				}
				for(int j=from;j<=to;j++) {
					bool selected = !items[j].selected;
					select(j,false);
					if (selected)
						emit_signal("multi_selected",i,true);
				}
			} else {

				if (!mb.doubleclick && !mb.mod.command && select_mode==SELECT_MULTI && items[i].selectable && items[i].selected) {
					defer_select_single=i;
					return;
				}
				bool selected = !items[i].selected;

				select(i,select_mode==SELECT_SINGLE || !mb.mod.command);
				if (selected) {
					if (select_mode==SELECT_SINGLE) {
						emit_signal("item_selected",i);
					} else
						emit_signal("multi_selected",i,true);
				}

				if (/*select_mode==SELECT_SINGLE &&*/ mb.doubleclick) {

					emit_signal("item_activated",i);

				}


			}


			return;
		}
	}
	if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_WHEEL_UP && p_event.mouse_button.pressed) {

		scroll_bar->set_val( scroll_bar->get_val()-scroll_bar->get_page()/8 );

	}
	if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_WHEEL_DOWN && p_event.mouse_button.pressed) {

		scroll_bar->set_val( scroll_bar->get_val()+scroll_bar->get_page()/8 );

	}

	if (p_event.is_pressed() && items.size()>0) {
		if (p_event.is_action("ui_up")) {

			if (search_string!="") {

				uint64_t now = OS::get_singleton()->get_ticks_msec();
				uint64_t diff = now-search_time_msec;

				if (diff<int(Globals::get_singleton()->get("gui/incr_search_max_interval_msec"))*2) {

					for(int i=current-1;i>=0;i--) {

						if (items[i].text.begins_with(search_string)) {

							set_current(i);
							ensure_current_is_visible();
							if (select_mode==SELECT_SINGLE) {
								emit_signal("item_selected",current);
							}


							break;
						}
					}
					accept_event();
					return;
				}
			}

			if (current>=current_columns) {
				set_current(current-current_columns);
				ensure_current_is_visible();
				if (select_mode==SELECT_SINGLE) {
					emit_signal("item_selected",current);
				}
				accept_event();
			}
		} else if (p_event.is_action("ui_down")) {

			if (search_string!="") {

				uint64_t now = OS::get_singleton()->get_ticks_msec();
				uint64_t diff = now-search_time_msec;

				if (diff<int(Globals::get_singleton()->get("gui/incr_search_max_interval_msec"))*2) {

					for(int i=current+1;i<items.size();i++) {

						if (items[i].text.begins_with(search_string)) {

							set_current(i);
							ensure_current_is_visible();
							if (select_mode==SELECT_SINGLE) {
								emit_signal("item_selected",current);
							}
							break;
						}
					}
					accept_event();
					return;
				}
			}

			if (current<items.size()-current_columns) {
				set_current(current+current_columns);
				ensure_current_is_visible();
				if (select_mode==SELECT_SINGLE) {
					emit_signal("item_selected",current);
				}
				accept_event();

			}
		} else if (p_event.is_action("ui_page_up")) {

			search_string=""; //any mousepress cancels

			for(int i=4;i>0;i--) {
				if (current-current_columns*i >=0 ) {
					set_current( current- current_columns*i);
					ensure_current_is_visible();
					if (select_mode==SELECT_SINGLE) {
						emit_signal("item_selected",current);
					}
					accept_event();
					break;
				}
			}
		} else if (p_event.is_action("ui_page_down")) {

			search_string=""; //any mousepress cancels

			for(int i=4;i>0;i--) {
				if (current+current_columns*i < items.size() ) {
					set_current( current+ current_columns*i);
					ensure_current_is_visible();
					if (select_mode==SELECT_SINGLE) {
						emit_signal("item_selected",current);
					}
					accept_event();

					break;
				}
			}
		} else if (p_event.is_action("ui_left")) {

			search_string=""; //any mousepress cancels

			if (current%current_columns!=0) {
				set_current(current-1);
				ensure_current_is_visible();
				if (select_mode==SELECT_SINGLE) {
					emit_signal("item_selected",current);
				}
				accept_event();

			}
		} else if (p_event.is_action("ui_right")) {

			search_string=""; //any mousepress cancels

			if (current%current_columns!=(current_columns-1)) {
				set_current(current+1);
				ensure_current_is_visible();
				if (select_mode==SELECT_SINGLE) {
					emit_signal("item_selected",current);
				}
				accept_event();

			}
		} else if (p_event.is_action("ui_cancel")) {
			search_string="";
		} else if (p_event.is_action("ui_select")) {


			if (select_mode==SELECT_MULTI && current>=0 && current<items.size()) {
				if (items[current].selectable && !items[current].selected) {
					select(current,false);
					emit_signal("multi_selected",current,true);
				} else if (items[current].selected) {
					unselect(current);
					emit_signal("multi_selected",current,false);
				}
			}
		} else if (p_event.is_action("ui_accept")) {
			search_string=""; //any mousepress cance

			if (current>=0 && current<items.size()) {
				emit_signal("item_activated",current);
			}
		} else if (p_event.type==InputEvent::KEY) {

			if (p_event.key.unicode) {

				uint64_t now = OS::get_singleton()->get_ticks_msec();
				uint64_t diff = now-search_time_msec;
				uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000));
				search_time_msec = now;

				if (diff>max_interval) {
					search_string="";
				}

				search_string+=String::chr(p_event.key.unicode);
				for(int i=0;i<items.size();i++) {
					if (items[i].text.begins_with(search_string)) {
						set_current(i);
						ensure_current_is_visible();
						if (select_mode==SELECT_SINGLE) {
							emit_signal("item_selected",current);
						}
						break;
					}
				}

			}

		}
	}




}
Exemple #6
0
void Slider::_input_event(InputEvent p_event) {



	if (p_event.type==InputEvent::MOUSE_BUTTON) {

		InputEventMouseButton &mb = p_event.mouse_button;
		if (mb.button_index==BUTTON_LEFT) {

			if (mb.pressed)	{
				grab.pos=orientation==VERTICAL?mb.y:mb.x;
				double max = orientation==VERTICAL ? get_size().height : get_size().width ;
				if (orientation==VERTICAL)
					set_unit_value( 1 - ((double)grab.pos / max) );
				else
					set_unit_value((double)grab.pos / max);
				grab.active=true;
				grab.uvalue=get_unit_value();
			} else {
				grab.active=false;
			}
		} else if (mb.pressed && mb.button_index==BUTTON_WHEEL_UP) {

			set_val( get_val() + get_step());
		} else if (mb.pressed && mb.button_index==BUTTON_WHEEL_DOWN) {
			set_val( get_val() - get_step());
		}

	} else if (p_event.type==InputEvent::MOUSE_MOTION) {

		if (grab.active) {

			Size2i size = get_size();
			Ref<Texture> grabber = get_icon("grabber");
			float motion =  (orientation==VERTICAL?p_event.mouse_motion.y:p_event.mouse_motion.x) - grab.pos;
			if (orientation==VERTICAL)
				motion=-motion;
			float areasize = orientation==VERTICAL?size.height - grabber->get_size().height:size.width - grabber->get_size().width;
			if (areasize<=0)
				return;
			float umotion = motion / float(areasize);
			set_unit_value( grab.uvalue + umotion );
		}
	} else {

		if (p_event.is_action("ui_left") && p_event.is_pressed()) {

			if (orientation!=HORIZONTAL)
				return;
			set_val( get_val() - (custom_step>=0?custom_step:get_step()) );
			accept_event();
		} else if (p_event.is_action("ui_right") && p_event.is_pressed()) {

			if (orientation!=HORIZONTAL)
				return;
			set_val( get_val() + (custom_step>=0?custom_step:get_step()) );
			accept_event();
		} else if (p_event.is_action("ui_up") && p_event.is_pressed()) {

			if (orientation!=VERTICAL)
				return;

			set_val( get_val() + (custom_step>=0?custom_step:get_step()) );
			accept_event();
		} else if (p_event.is_action("ui_down") && p_event.is_pressed()) {

			if (orientation!=VERTICAL)
				return;
			set_val( get_val() - (custom_step>=0?custom_step:get_step()) );
			accept_event();

		} else if (p_event.type==InputEvent::KEY) {

			const InputEventKey &k=p_event.key;

			if (!k.pressed)
				return;

			switch (k.scancode) {

				case KEY_HOME: {

					set_val( get_min() );
					accept_event();
				} break;
				case KEY_END: {

					set_val( get_max() );
					accept_event();

				} break;

			} ;
		}
	}

}