#include /* This file is part of moth. moth is a program for creating and editing textured 3D models. Copyright (C) 2004 Peter Uray. moth is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. moth is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with moth; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include MGLView::MGLView(QWidget *parent, const char *name) : QGLWidget(parent, name) { this->setCursor(QCursor(CrossCursor)); _oldMouseX = 0; _oldMouseY = 0; _newMouseX = 0; _newMouseY = 0; _mouseDX = 0; _mouseDY = 0; _mouseDXF = 0.0; _mouseDYF = 0.0; _mouseDS = 0.0; _initFlag = false; setMouseTracking(false); } void MGLView::setBackgroundColor(QColor color) { _backgroundColor = color; } void MGLView::showEvent(QShowEvent *e) { } void MGLView::renderView(void) { } void MGLView::mousePressEvent(QMouseEvent *event) { _oldMouseX = event->x(); _oldMouseY = event->y(); _newMouseX = _oldMouseX; _newMouseY = _oldMouseY; _mouseDX = 0; _mouseDY = 0; _mouseDXF = 0.0; _mouseDYF = 0.0; _mouseDS = 0.0; _state = (Qt::ButtonState)((int)event->state() | (int)event->button()); onMousePressEvent(); } void MGLView::mouseMoveEvent(QMouseEvent *event) { _newMouseX = event->x(); _newMouseY = event->y(); _mouseDX = _newMouseX - _oldMouseX; _mouseDY = _newMouseY - _oldMouseY; _mouseDXF = (float)_mouseDX; _mouseDYF = (float)_mouseDY; _mouseDS = sqrt(_mouseDXF*_mouseDXF + _mouseDYF*_mouseDYF); _oldMouseX = _newMouseX; _oldMouseY = _newMouseY; _state = (Qt::ButtonState)((int)event->state() | (int)event->button()); MMainWindow::get()->setPositionDisplay(mousePosition3D()); onMouseMoveEvent(); } void MGLView::mouseReleaseEvent(QMouseEvent *event) { _oldMouseX = event->x(); _oldMouseY = event->y(); _newMouseX = _oldMouseX; _newMouseY = _oldMouseY; _mouseDX = 0; _mouseDY = 0; _mouseDXF = 0.0; _mouseDYF = 0.0; _mouseDS = 0.0; _state = (Qt::ButtonState)((int)event->state() | (int)event->button()); onMouseReleaseEvent(); } void MGLView::mouseDoubleClickEvent(QMouseEvent *event) { _oldMouseX = event->x(); _oldMouseY = event->y(); _newMouseX = _oldMouseX; _newMouseY = _oldMouseY; _mouseDX = 0; _mouseDY = 0; _mouseDXF = 0.0; _mouseDYF = 0.0; _mouseDS = 0.0; _state = (Qt::ButtonState)((int)event->state() | (int)event->button()); onMouseDoubleClickEvent(); onMousePressEvent(); } void MGLView::onMousePressEvent(void) { // if((_state & ControlButton)) return; if((_state & ShiftButton)) return; _initFlag = true; switch(MMainWindow::get()->getActiveFunction()) { case MMainWindow::Select: onSelectModel(); break; case MMainWindow::Zoom: if(_state & RightButton) onZoomOut(); if(_state & LeftButton) onZoomIn(); break; case MMainWindow::Move: if(MDocument::get()->getSelectedModel()) onMoveModelInit(); if(MDocument::get()->getSelectedShape()) onMoveShapeInit(); if(MDocument::get()->getSelectedLightSource()) onMoveLightInit(); break; case MMainWindow::Rotate: if(MDocument::get()->getSelectedModel()) onRotateModelInit(); if(MDocument::get()->getSelectedShape()) onRotateShapeInit(); if(MDocument::get()->getSelectedLightSource()) onRotateLightInit(); break; case MMainWindow::Scale: if(MDocument::get()->getSelectedModel()) onScaleModelInit(); if(MDocument::get()->getSelectedShape()) onScaleShapeInit(); break; case MMainWindow::SelectVertex: if(MDocument::get()->getSelectedShape()) onSelectVertexInit(); break; case MMainWindow::CreateVertex: if(MDocument::get()->getSelectedShape()) onCreateVertex(); break; case MMainWindow::CreateTriangle: if(MDocument::get()->getSelectedShape()) onCreateTriangle(); break; case MMainWindow::MoveVertex: if(MDocument::get()->getSelectedShape()) onMoveVertexInit(); break; case MMainWindow::RotateVertex: if(MDocument::get()->getSelectedShape()) onRotateVertexInit(); break; case MMainWindow::ScaleVertex: if(MDocument::get()->getSelectedShape()) onScaleVertexInit(); break; case MMainWindow::Magnet: if(MDocument::get()->getSelectedShape()) onMagnetInit(); break; case MMainWindow::FlipEdge: if(MDocument::get()->getSelectedShape()) onFlipEdge(); break; case MMainWindow::SnapToMaster: if(MDocument::get()->getSelectedShape()) onSnapToMaster(); break; default: break; } } void MGLView::onMouseMoveEvent(void) { // if((_state & ControlButton)) return; if((_state & ShiftButton)) { if(_state & LeftButton) { onRotateView(); return; } else if(_state & MidButton) { onZoomView(); return; } else if(_state & RightButton) { onMoveView(); return; } } switch(MMainWindow::get()->getActiveFunction()) { case MMainWindow::Select: break; case MMainWindow::Move: if(MDocument::get()->getSelectedModel() && _initFlag) onMoveModelPerform(); if(MDocument::get()->getSelectedShape() && _initFlag) onMoveShapePerform(); if(MDocument::get()->getSelectedLightSource() && _initFlag) onMoveLightPerform(); break; case MMainWindow::Rotate: if(MDocument::get()->getSelectedModel() && _initFlag) onRotateModelPerform(); if(MDocument::get()->getSelectedShape() && _initFlag) onRotateShapePerform(); if(MDocument::get()->getSelectedLightSource() && _initFlag) onRotateLightPerform(); break; case MMainWindow::Scale: if(MDocument::get()->getSelectedModel() && _initFlag) onScaleModelPerform(); if(MDocument::get()->getSelectedShape() && _initFlag) onScaleShapePerform(); break; case MMainWindow::SelectVertex: if(MDocument::get()->getSelectedShape() && _initFlag) onSelectVertexPerform(); break; case MMainWindow::CreateVertex: break; case MMainWindow::CreateTriangle: break; case MMainWindow::MoveVertex: if(MDocument::get()->getSelectedShape() && _initFlag) onMoveVertexPerform(); break; case MMainWindow::RotateVertex: if(MDocument::get()->getSelectedShape() && _initFlag) onRotateVertexPerform(); break; case MMainWindow::ScaleVertex: if(MDocument::get()->getSelectedShape() && _initFlag) onScaleVertexPerform(); break; case MMainWindow::Magnet: if(MDocument::get()->getSelectedShape() && _initFlag) onMagnetPerform(); break; case MMainWindow::FlipEdge: break; default: break; } } void MGLView::onMouseReleaseEvent(void) { if((_state & ShiftButton)) return; // if((_state & ControlButton)) return; _initFlag = false; switch(MMainWindow::get()->getActiveFunction()) { case MMainWindow::Select: break; case MMainWindow::Move: if(MDocument::get()->getSelectedModel()) onMoveModelFinish(); if(MDocument::get()->getSelectedShape()) onMoveShapeFinish(); if(MDocument::get()->getSelectedLightSource()) onMoveLightFinish(); break; case MMainWindow::Rotate: if(MDocument::get()->getSelectedModel()) onRotateModelFinish(); if(MDocument::get()->getSelectedShape()) onRotateShapeFinish(); if(MDocument::get()->getSelectedLightSource()) onRotateLightFinish(); break; case MMainWindow::Scale: if(MDocument::get()->getSelectedModel()) onScaleModelFinish(); if(MDocument::get()->getSelectedShape()) onScaleShapeFinish(); break; case MMainWindow::SelectVertex: if(MDocument::get()->getSelectedShape()) onSelectVertexFinish(); break; case MMainWindow::CreateVertex: break; case MMainWindow::CreateTriangle: break; case MMainWindow::MoveVertex: if(MDocument::get()->getSelectedShape()) onMoveVertexFinish(); break; case MMainWindow::RotateVertex: if(MDocument::get()->getSelectedShape()) onRotateVertexFinish(); break; case MMainWindow::ScaleVertex: if(MDocument::get()->getSelectedShape()) onScaleVertexFinish(); break; case MMainWindow::Magnet: if(MDocument::get()->getSelectedShape()) onMagnetFinish(); break; case MMainWindow::FlipEdge: break; default: break; } } void MGLView::onMouseDoubleClickEvent(void) { if((_state & ShiftButton)) { onResetView(); return; } } void MGLView::initializeGL(void) { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glViewport(0, 0, (GLint)width(), (GLint)height()); } void MGLView::resizeGL(int w, int h) { glMatrixMode(GL_MODELVIEW); glViewport(0, 0, (GLint)width(), (GLint)height()); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void MGLView::paintGL(void) { renderView(); } // ----------------------------------------------------------------------------