//! called if an event happened.
	bool CGUIScrollBar::OnEvent(const SEvent& event)
	{
		if (isEnabled())
		{

			switch (event.EventType)
			{
			case EET_KEY_INPUT_EVENT:
				if (event.KeyInput.PressedDown)
				{
					const SINT32 oldPos = Pos;
					bool absorb = true;
					switch (event.KeyInput.Key)
					{
					case KEY_LEFT:
					case KEY_UP:
						setPos(Pos - SmallStep);
						break;
					case KEY_RIGHT:
					case KEY_DOWN:
						setPos(Pos + SmallStep);
						break;
					case KEY_HOME:
						setPos(Min);
						break;
					case KEY_PRIOR:
						setPos(Pos - LargeStep);
						break;
					case KEY_END:
						setPos(Max);
						break;
					case KEY_NEXT:
						setPos(Pos + LargeStep);
						break;
					default:
						absorb = false;
					}

					if (Pos != oldPos)
					{
						SEvent newEvent;
						newEvent.EventType = EET_GUI_EVENT;
						newEvent.GUIEvent.Caller = this;
						newEvent.GUIEvent.Element = 0;
						newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
						Parent->OnEvent(newEvent);
					}
					if (absorb)
						return true;
				}
				break;
			case EET_GUI_EVENT:
				if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
				{
					if (event.GUIEvent.Caller == UpButton)
						setPos(Pos - SmallStep);
					else
						if (event.GUIEvent.Caller == DownButton)
							setPos(Pos + SmallStep);

					SEvent newEvent;
					newEvent.EventType = EET_GUI_EVENT;
					newEvent.GUIEvent.Caller = this;
					newEvent.GUIEvent.Element = 0;
					newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
					Parent->OnEvent(newEvent);

					return true;
				}
				else
					if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
					{
						if (event.GUIEvent.Caller == this)
							Dragging = false;
					}
				break;
			case EET_MOUSE_INPUT_EVENT:
			{
				const Position2d p(event.MouseInput.X, event.MouseInput.Y);
				bool isInside = isPointInside(p);
				switch (event.MouseInput.Event)
				{
				case EMIE_MOUSE_WHEEL:
					if (Environment->hasFocus(this))
					{
						// thanks to a bug report by REAPER
						// thanks to tommi by tommi for another bugfix
						// everybody needs a little thanking. hallo niko!;-)
						setPos(getPos() +
							((event.MouseInput.Wheel < 0 ? -1 : 1) * SmallStep * (Horizontal ? 1 : -1))
							);

						SEvent newEvent;
						newEvent.EventType = EET_GUI_EVENT;
						newEvent.GUIEvent.Caller = this;
						newEvent.GUIEvent.Element = 0;
						newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
						Parent->OnEvent(newEvent);
						return true;
					}
					break;
				case EMIE_LMOUSE_PRESSED_DOWN:
				{
					if (isInside)
					{
						Dragging = true;
						DraggedBySlider = SliderRect.isPointInside(p);
						TrayClick = !DraggedBySlider;
						DesiredPos = getPosFromMousePos(p);
						Environment->setFocus(this);
						return true;
					}
					break;
				}
				case EMIE_LMOUSE_LEFT_UP:
				case EMIE_MOUSE_MOVED:
				{
					if (!event.MouseInput.isLeftPressed())
						Dragging = false;

					if (!Dragging)
					{
						if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
							break;
						return isInside;
					}

					if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
						Dragging = false;

					const SINT32 newPos = getPosFromMousePos(p);
					const SINT32 oldPos = Pos;

					if (!DraggedBySlider)
					{
						if (isInside)
						{
							DraggedBySlider = SliderRect.isPointInside(p);
							TrayClick = !DraggedBySlider;
						}

						if (DraggedBySlider)
						{
							setPos(newPos);
						}
						else
						{
							TrayClick = false;
							if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
								return isInside;
						}
					}

					if (DraggedBySlider)
					{
						setPos(newPos);
					}
					else
					{
						DesiredPos = newPos;
					}

					if (Pos != oldPos && Parent)
					{
						SEvent newEvent;
						newEvent.EventType = EET_GUI_EVENT;
						newEvent.GUIEvent.Caller = this;
						newEvent.GUIEvent.Element = 0;
						newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
						Parent->OnEvent(newEvent);
					}
					return isInside;
				} break;

				default:
					break;
				}
			} break;
			default:
				break;
			}
		}

		return IGUIElement::OnEvent(event);
	}
示例#2
0
//! called if an event happened.
bool CGUIScrollBar::OnEvent(const SEvent& event)
{
	if (IsEnabled)
	{

		switch(event.EventType)
		{
		case EET_KEY_INPUT_EVENT:
			if (event.KeyInput.PressedDown)
			{
				const s32 oldPos = Pos;
				bool absorb = true;
				switch (event.KeyInput.Key)
				{
				case KEY_LEFT:
				case KEY_UP:
					setPos(Pos-SmallStep);
					break;
				case KEY_RIGHT:
				case KEY_DOWN:
					setPos(Pos+SmallStep);
					break;
				case KEY_HOME:
					setPos(0);
					break;
				case KEY_PRIOR:
					setPos(Pos-LargeStep);
					break;
				case KEY_END:
					setPos(Max);
					break;
				case KEY_NEXT:
					setPos(Pos+LargeStep);
					break;
				default:
					absorb = false;
				}

				if (Pos != oldPos)
				{
					SEvent newEvent;
					newEvent.EventType = EET_GUI_EVENT;
					newEvent.GUIEvent.Caller = this;
					newEvent.GUIEvent.Element = 0;
					newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
					Parent->OnEvent(newEvent);
				}
				if (absorb)
					return true;
			}
			break;
		case EET_GUI_EVENT:
			if (event.GUIEvent.EventType == EGET_BUTTON_CLICKED)
			{
				if (event.GUIEvent.Caller == UpButton)
					setPos(Pos-SmallStep);
				else
				if (event.GUIEvent.Caller == DownButton)
					setPos(Pos+SmallStep);

				SEvent newEvent;
				newEvent.EventType = EET_GUI_EVENT;
				newEvent.GUIEvent.Caller = this;
				newEvent.GUIEvent.Element = 0;
				newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
				Parent->OnEvent(newEvent);

				return true;
			}
			else
			if (event.GUIEvent.EventType == EGET_ELEMENT_FOCUS_LOST)
			{
				if (event.GUIEvent.Caller == this)
					Dragging = false;
			}
			break;
		case EET_MOUSE_INPUT_EVENT:
			switch(event.MouseInput.Event)
			{
			case EMIE_MOUSE_WHEEL:
				if (Environment->hasFocus(this))
				{ // thanks to a bug report by REAPER
					setPos(getPos() + (s32)event.MouseInput.Wheel* -SmallStep);
					SEvent newEvent;
					newEvent.EventType = EET_GUI_EVENT;
					newEvent.GUIEvent.Caller = this;
					newEvent.GUIEvent.Element = 0;
					newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
					Parent->OnEvent(newEvent);
					return true;
				}
				break;
			case EMIE_LMOUSE_PRESSED_DOWN:
			{
				if (AbsoluteClippingRect.isPointInside(core::position2di(event.MouseInput.X, event.MouseInput.Y)))
				{
					Dragging = true;
					DraggedBySlider = SliderRect.isPointInside(core::position2di(event.MouseInput.X, event.MouseInput.Y));
					TrayClick = !DraggedBySlider;
					DesiredPos = getPosFromMousePos(event.MouseInput.X, event.MouseInput.Y);
					return true;
				}
				break;
			}
			case EMIE_LMOUSE_LEFT_UP:
			case EMIE_MOUSE_MOVED:
				if (Dragging)
				{
					if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
						Dragging = false;

					const s32 newPos = getPosFromMousePos(event.MouseInput.X, event.MouseInput.Y);
					const s32 oldPos = Pos;

					if (!DraggedBySlider)
					{
						if (AbsoluteClippingRect.isPointInside(core::position2di(event.MouseInput.X, event.MouseInput.Y)))
						{
							DraggedBySlider = SliderRect.isPointInside(core::position2di(event.MouseInput.X, event.MouseInput.Y));
							TrayClick = !DraggedBySlider;
						}
						else
						{
							TrayClick = false;
							if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
								return true;
						}
					}

					if (DraggedBySlider)
					{
						setPos(newPos);
					}
					else
					{
						DesiredPos = newPos;
					}

					if (Pos != oldPos && Parent)
					{
						SEvent newEvent;
						newEvent.EventType = EET_GUI_EVENT;
						newEvent.GUIEvent.Caller = this;
						newEvent.GUIEvent.Element = 0;
						newEvent.GUIEvent.EventType = EGET_SCROLL_BAR_CHANGED;
						Parent->OnEvent(newEvent);
					}
					return true;
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}

	return IGUIElement::OnEvent(event);
}