⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glutwindow.cpp

📁 光滑质点无网格法SPH并行计算程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void GLUTWindow::_onSpecialKeyboardEvent(int glutKey, int x, int y) {	msInstance->_doRegisterMouseMoveEvent(x, y);	// Keyrepeat cannot be disabled (software filtering)	if (mKeyRepeatOn) {		// Only raise event when the elapsed time is big enough		double now = Timer::getSystemTime();		if (glutKey != mLastSpecialKey || now - mLastKeyEventTime				>= KEY_EVENT_DELAY_THRESHOLD) {			_handleSpecialKeyboardEvent(glutKey, InputListener::KEY_DOWN);		}		mLastKeyEventTime = now;		mLastSpecialKey = glutKey;	}	else		_handleSpecialKeyboardEvent(glutKey, InputListener::KEY_DOWN);}void GLUTWindow::_onSpecialKeyboardUpEvent(int glutKey, int x, int y) {	msInstance->_doRegisterMouseMoveEvent(x, y);	// Keyrepeat cannot be disabled (software filtering)	if (mKeyRepeatOn) {		// Only raise event when the elapsed time is big enough		double now = Timer::getSystemTime();		if (glutKey != mLastSpecialKey || now - mLastKeyEventTime				>= KEY_EVENT_DELAY_THRESHOLD) {			_handleSpecialKeyboardEvent(glutKey, InputListener::KEY_UP);		}		mLastKeyEventTime = now;		mLastSpecialKey = glutKey;	}	else		_handleSpecialKeyboardEvent(glutKey, InputListener::KEY_UP);}void GLUTWindow::_onKeyboardTimer(int key) {	KeyEventMapIterator i = msInstance->mCachedKeyEvents.find(key);	// The key down event is in the cache => generating simple key down event 	if (i != msInstance->mCachedKeyEvents.end() && System::getTime()			- i->second.downTime >= MAX_PRESS_TIME) {		msInstance->_doRegisterKeyboardEvent(key, InputListener::KEY_DOWN);		msInstance->_doRegisterKeyboardModifierEvent(i->second.modifiers);		msInstance->_doKeyEvent(key, i->second.modifiers,				InputListener::KEY_DOWN);		msInstance->mCachedKeyEvents.erase(i);	}}void GLUTWindow::_onMouseEvent(int button, int state, int x, int y) {	msInstance->_doRegisterMouseMoveEvent(x, y);	// Convert button	InputListener::MouseButton _button = InputListener::LEFT_BUTTON;	switch (button) {	// Buttons	case GLUT_MIDDLE_BUTTON:		_button = InputListener::MIDDLE_BUTTON;		break;	case GLUT_RIGHT_BUTTON:		_button = InputListener::RIGHT_BUTTON;		break;#ifdef GLUT_WHEEL_UP		case GLUT_RIGHT_BUTTON+1: _button = InputListener::FOURTH_BUTTON; break;		case GLUT_RIGHT_BUTTON+2: _button = InputListener::FIFTH_BUTTON; break;#endif		// Win32 and MacOSX GLUT wheel event#ifdef GLUT_WHEEL_UP		case GLUT_WHEEL_UP:		if(state == GLUT_UP) {			msInstance->_doRegisterMouseWheelEvent(-WHEEL_SCROLLING);			msInstance->_doMouseWheelEvent(x, y, -WHEEL_SCROLLING);		}		return;		break;		case GLUT_WHEEL_DOWN:		if(state == GLUT_UP) {			msInstance->_doRegisterMouseWheelEvent(WHEEL_SCROLLING);			msInstance->_doMouseWheelEvent(x, y, WHEEL_SCROLLING);		}		return;		break;#else		// Linux GLUT wheel event (fourth and fifth button)		// NOTE: FOURTH_BUTTON, FIFTH_BUTTON cannot be handled	case GLUT_RIGHT_BUTTON+1:		if (state == GLUT_UP) {			msInstance->_doRegisterMouseWheelEvent(-WHEEL_SCROLLING);			msInstance->_doMouseWheelEvent(x, y, -WHEEL_SCROLLING);		}		return;		break;	case GLUT_RIGHT_BUTTON+2:		if (state == GLUT_UP) {			msInstance->_doRegisterMouseWheelEvent(WHEEL_SCROLLING);			msInstance->_doMouseWheelEvent(x, y, WHEEL_SCROLLING);		}		return;		break;#endif	}	// The button has been changed	/*if(msInstance->mLastMouseActionTime != 0.0 && 	 msInstance->mCachedMouseEvent.button != _button) _resetClickInfo();*/	// Convert state	double currentTime = System::getTime();	switch (state) {	case GLUT_DOWN:		switch (msInstance->mCachedMouseEvent.state) {		case BUTTON_STATE_REST:			_startClickTimeOut(x, y, _button, BUTTON_STATE_DOWN1);			break;		case BUTTON_STATE_DOWN1:			// NOTE: invalid			break;		case BUTTON_STATE_UP1:			if (currentTime - msInstance->mLastMouseActionTime					<= MAX_CLICK_TIME)				_startClickTimeOut(x, y, _button, BUTTON_STATE_DOWN2);			else				_resetClickInfo();			break;		case BUTTON_STATE_DOWN2:			// NOTE: invalid			break;		case BUTTON_STATE_UP2:			_resetClickInfo();			_startClickTimeOut(x, y, _button, BUTTON_STATE_DOWN1);			break;		}		break;	case GLUT_UP:		switch (msInstance->mCachedMouseEvent.state) {		case BUTTON_STATE_REST:			msInstance->_doRegisterMouseButtonEvent(_button,			//msInstance->mCachedMouseEvent.button,					InputListener::BUTTON_UP);			msInstance->_doMouseButtonEvent(x, y, _button,					InputListener::BUTTON_UP);			_resetClickInfo();			break;		case BUTTON_STATE_DOWN1:			_startClickTimeOut(x, y, _button, BUTTON_STATE_UP1);			break;		case BUTTON_STATE_UP1:			// NOTE: invalid			break;		case BUTTON_STATE_DOWN2:			if (currentTime - msInstance->mLastMouseActionTime					<= MAX_CLICK_TIME)				_startClickTimeOut(x, y, _button, BUTTON_STATE_UP2);			else				_resetClickInfo();			break;		case BUTTON_STATE_UP2:			// NOTE: invalid			break;		}		break;	}}void GLUTWindow::_onMotionEvent(int x, int y) {	msInstance->_doRegisterMouseMoveEvent(x, y);	_resetClickInfo();	msInstance->_doMouseMoveEvent(x, y, InputListener::HOVER);}void GLUTWindow::_onPassiveMotionEvent(int x, int y) {	msInstance->_doRegisterMouseMoveEvent(x, y);	_resetClickInfo();	msInstance->_doMouseMoveEvent(x, y, InputListener::HOVER);}void GLUTWindow::_onEntryEvent(int state) {	_resetClickInfo();	switch (state) {	case GLUT_LEFT:		msInstance->_doMouseMoveEvent(InputListener::LEAVE);		break;	case GLUT_ENTERED:		msInstance->_doMouseMoveEvent(InputListener::ENTER);		break;	}}void GLUTWindow::_onMouseTimer(int value) {	// Timer is not actual	if (msInstance->mCachedMouseEvent.state != value)		return;	_resetClickInfo();}void GLUTWindow::_handleNormalKeyboardEvent(unsigned char glutCode,		InputListener::KeyEventState state) {	_handleKeyboardEvent(_convertKeyNormal(glutCode), _getModifiers(), state);}void GLUTWindow::_handleSpecialKeyboardEvent(int glutKey,		InputListener::KeyEventState state) {	_handleKeyboardEvent(_convertKeySpecial(glutKey), _getModifiers(), state);}void GLUTWindow::_handleKeyboardEvent(int key, unsigned modifiers,		InputListener::KeyEventState state) {	switch (state) {	case InputListener::KEY_DOWN: {		_startPressTimeOut(key, modifiers);	}		break;	case InputListener::KEY_UP: {		KeyEventMapIterator i = msInstance->mCachedKeyEvents.find(key);		// The key down event is in the key press interval 		if (i != msInstance->mCachedKeyEvents.end()) {			msInstance->_doRegisterKeyboardEvent(key, InputListener::KEY_PRESS);			msInstance->_doRegisterKeyboardModifierEvent(i->second.modifiers);			msInstance->_doKeyEvent(key, i->second.modifiers,					InputListener::KEY_PRESS);			msInstance->mCachedKeyEvents.erase(i);		}		// The key belongs to a key down event		else {			msInstance->_doKeyEvent(key, i->second.modifiers,					InputListener::KEY_UP);		}	}		break;	case InputListener::KEY_PRESS:		// NOTE: not valid, this method is never called with this argument		break;	}	msInstance->checkRedisplay();}int GLUTWindow::_convertKeyNormal(unsigned char glutCode) {	return glutCode;}int GLUTWindow::_convertKeySpecial(int glutKey) {	switch (glutKey) {	case GLUT_KEY_F1:		return InputListener::KEY_F1;	case GLUT_KEY_F2:		return InputListener::KEY_F2;	case GLUT_KEY_F3:		return InputListener::KEY_F3;	case GLUT_KEY_F4:		return InputListener::KEY_F4;	case GLUT_KEY_F5:		return InputListener::KEY_F5;	case GLUT_KEY_F6:		return InputListener::KEY_F6;	case GLUT_KEY_F7:		return InputListener::KEY_F7;	case GLUT_KEY_F8:		return InputListener::KEY_F8;	case GLUT_KEY_F9:		return InputListener::KEY_F9;	case GLUT_KEY_F10:		return InputListener::KEY_F10;	case GLUT_KEY_F11:		return InputListener::KEY_F11;	case GLUT_KEY_F12:		return InputListener::KEY_F12;	case GLUT_KEY_LEFT:		return InputListener::KEY_LEFT_ARROW;	case GLUT_KEY_UP:		return InputListener::KEY_UP_ARROW;	case GLUT_KEY_RIGHT:		return InputListener::KEY_RIGHT_ARROW;	case GLUT_KEY_DOWN:		return InputListener::KEY_DOWN_ARROW;	case GLUT_KEY_PAGE_UP:		return InputListener::KEY_PAGE_UP;	case GLUT_KEY_PAGE_DOWN:		return InputListener::KEY_PAGE_DOWN;	case GLUT_KEY_HOME:		return InputListener::KEY_HOME;	case GLUT_KEY_END:		return InputListener::KEY_END;	case GLUT_KEY_INSERT:		return InputListener::KEY_INSERT;	default:		return 0;	}	return 0;}void GLUTWindow::_startPressTimeOut(int key, unsigned modifiers) {	KeyEvent ke = { key, modifiers, System::getTime() };	msInstance->mCachedKeyEvents[key] = ke;	glutTimerFunc((unsigned) (1000.0 * MAX_PRESS_TIME), _onKeyboardTimer, key);}void GLUTWindow::_startClickTimeOut(int x, int y,		InputListener::MouseButton button, ClickState state) {	msInstance->mLastMouseActionTime = System::getTime();	msInstance->mCachedMouseEvent.x = x;	msInstance->mCachedMouseEvent.y = y;	msInstance->mCachedMouseEvent.button = button;	msInstance->mCachedMouseEvent.state = state;	glutTimerFunc((unsigned) (1000.0 * MAX_CLICK_TIME), _onMouseTimer, state);}unsigned GLUTWindow::_getModifiers() {	int glutModifiers = glutGetModifiers();	unsigned modifiers = 0;	// Unable to distiguish left and right keys (only alt and alt gr)	if (glutModifiers & GLUT_ACTIVE_SHIFT)		modifiers |= InputListener::KEY_SHIFT;	if (glutModifiers & GLUT_ACTIVE_ALT)		modifiers |= InputListener::KEY_ALT;	if (glutModifiers & GLUT_ACTIVE_CTRL)		modifiers |= InputListener::KEY_CTRL;	return modifiers;}void GLUTWindow::_fireMouseEvent() {	switch (msInstance->mCachedMouseEvent.state) {	case BUTTON_STATE_REST:		// NOTE: do nothing		break;	case BUTTON_STATE_DOWN1:		msInstance->_doRegisterMouseButtonEvent(				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_DOWN);		msInstance->_doMouseButtonEvent(msInstance->mCachedMouseEvent.x,				msInstance->mCachedMouseEvent.y,				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_DOWN);		break;	case BUTTON_STATE_UP1:		msInstance->_doMouseButtonEvent(msInstance->mCachedMouseEvent.x,				msInstance->mCachedMouseEvent.y,				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_CLICK);		break;	case BUTTON_STATE_DOWN2:		msInstance->_doRegisterMouseButtonEvent(				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_DOWN);		msInstance->_doMouseButtonEvent(msInstance->mCachedMouseEvent.x,				msInstance->mCachedMouseEvent.y,				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_CLICK);		msInstance->_doMouseButtonEvent(msInstance->mCachedMouseEvent.x,				msInstance->mCachedMouseEvent.y,				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_DOWN);		break;	case BUTTON_STATE_UP2:		msInstance->_doMouseButtonEvent(msInstance->mCachedMouseEvent.x,				msInstance->mCachedMouseEvent.y,				msInstance->mCachedMouseEvent.button,				InputListener::BUTTON_DBLCLICK);		break;	}	msInstance->checkRedisplay();}} // namespace MinGLE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -