Skip to content

Commit

Permalink
Added touch screen pinch zoom and rotate
Browse files Browse the repository at this point in the history
Added pinch zoom on the glwidget and rotate around center on object axis on pinch roatate. May want to add a roate lock on the pinch gesture to stop roation of object by mistake.
  • Loading branch information
alanspencer committed Oct 18, 2018
1 parent bef3362 commit f7011a4
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 11 deletions.
118 changes: 118 additions & 0 deletions src/gl3widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QMouseEvent>
#include <QWheelEvent>
#include <QVector4D>
#include <QGesture>

#include "rowmans.h"
#include "globals.h"

Expand Down Expand Up @@ -38,6 +40,14 @@ GlWidget::GlWidget(QWidget *parent)
StereoSeparation = static_cast<float>(.04);
setFocusPolicy(Qt::NoFocus);
DefaultClipAngle = MainWin->ui->ClipAngle->value() / 10;

// Capture the following touch screen gestures
QList<Qt::GestureType> gestures;
gestures << Qt::PanGesture;
gestures << Qt::PinchGesture;
gestures << Qt::SwipeGesture;
grabGestures(gestures);

//setAutoBufferSwap(true);

//qDebug() << "Done init widget";
Expand Down Expand Up @@ -191,11 +201,15 @@ void GlWidget::resizeGL(int width, int height)
{
height = 1;
}

// Scale all x and y by the application screen ratio
xdim = static_cast<int>(static_cast<double>(width) * applicationScaleX);
ydim = static_cast<int>(static_cast<double>(height) * applicationScaleY);

DoPMatrix(xdim, ydim);

glfunctions->glViewport(0, 0, xdim, ydim);

update();
}

Expand All @@ -208,6 +222,7 @@ void GlWidget::DoPMatrix(int width, int height)
{
float asp = static_cast<float>(width) / static_cast<float>(height);
float fudge = static_cast<float>(1300) / static_cast<float>(width);

pMatrix.setToIdentity();
if (MainWin->ui->actionOrthographic_View->isChecked())
pMatrix.ortho((0 - ClipAngle / (10 * fudge)), ClipAngle / (10 * fudge), (0 - ClipAngle / (10 * fudge)) / asp, ClipAngle / (10 * fudge) / asp, ClipStart, ClipDepth);
Expand Down Expand Up @@ -1107,3 +1122,106 @@ void GlWidget::NewDefault()
Ztrans = 0;
}
}

/**
* @brief GlWidget::event
* @param event
* @return
*/
bool GlWidget::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent *>(event));
return QWidget::event(event);
}

/**
* @brief GlWidget::grabGestures
* @param gestures
*/
void GlWidget::grabGestures(const QList<Qt::GestureType> &gestures)
{
foreach (Qt::GestureType gesture, gestures)
grabGesture(gesture);
}

/**
* @brief GlWidget::gestureEvent
* @param event
* @return
*/
bool GlWidget::gestureEvent(QGestureEvent *event)
{
//qDebug() << "gestureEvent():" << event;

/*
if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
{
swipeTriggered(static_cast<QSwipeGesture *>(swipe));
}
else if (QGesture *pan = event->gesture(Qt::PanGesture))
{
panTriggered(static_cast<QPanGesture *>(pan));
}
*/

if (QGesture *pinch = event->gesture(Qt::PinchGesture))
{
//qDebug() << "Pinch detected...";
pinchTriggered(static_cast<QPinchGesture *>(pinch));
}
return true;
}

/**
* @brief GlWidget::pinchTriggered
* @param gesture
*/
void GlWidget::pinchTriggered(QPinchGesture *gesture)
{
QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags();
double currentStepScaleFactor = 0;

if (changeFlags & QPinchGesture::RotationAngleChanged)
{
double rotationDelta = gesture->lastRotationAngle() - gesture->rotationAngle();
Rotate(rotationDelta);
update();

//qDebug() << "pinchTriggered(): rotate by" << rotationDelta;
}

if (changeFlags & QPinchGesture::ScaleFactorChanged)
{
currentStepScaleFactor = gesture->totalScaleFactor();
int currentClipAngle = MainWin->ui->ClipAngle->value();
int maximumClipAngleAllowed = MainWin->ui->ClipAngle->maximum();
int minimumClipAngleAllowed = MainWin->ui->ClipAngle->minimum();
int newClipAngle = currentClipAngle;

if (currentStepScaleFactor > 0.0)
{
// Zoom in
newClipAngle = static_cast<int>(static_cast<double>(currentClipAngle) * (1 + ((currentStepScaleFactor - 1) / 20)));
if (newClipAngle > maximumClipAngleAllowed) newClipAngle = maximumClipAngleAllowed;
MainWin->ui->ClipAngle->setValue(newClipAngle);
}
else if (currentStepScaleFactor < 0.0)
{
// Zoom Out
newClipAngle = static_cast<int>(static_cast<double>(currentClipAngle) * (currentStepScaleFactor / 20));
if (newClipAngle < minimumClipAngleAllowed) newClipAngle = minimumClipAngleAllowed;
MainWin->ui->ClipAngle->setValue(newClipAngle);
}
SetClip(MainWin->ui->ClipStart->value(), MainWin->ui->ClipDepth->value(), MainWin->ui->ClipAngle->value());
update();

//qDebug() << "pinchTriggered(): zoom by" << gesture->scaleFactor() << "->" << currentStepScaleFactor << " Current Clip = " << currentClipAngle << " New Clip = " << newClipAngle;
}

if (gesture->state() == Qt::GestureFinished)
{
update();
}

}
29 changes: 18 additions & 11 deletions src/gl3widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QGestureEvent>

#include "globals.h"

Expand All @@ -19,19 +20,19 @@ class GlWidget : public QOpenGLWidget
public:
GlWidget(QWidget *parent);
~GlWidget();
void grabGestures(const QList<Qt::GestureType> &gestures);

//Application specific stuff

QOpenGLFunctions *glfunctions;
float ClipStart;
float ClipDepth;
float ClipAngle;
float DefaultClipAngle;
int LastMouseXpos;
int LastMouseYpos;
int xdim, ydim;
float campos;
float StereoSeparation;
int LastMouseXpos;
int LastMouseYpos;
int xdim;
int ydim;
QOpenGLFunctions *glfunctions;

bool CanISee(int index);
void SetClip(int Start, int Depth, int Angle);
Expand All @@ -40,12 +41,10 @@ class GlWidget : public QOpenGLWidget
void MoveAway(double dist);
void DrawObjects(bool rightview, bool halfsize);
void SetStereoSeparation(float s);
//void setQuadStereo(bool st);
void YRotate(float angle);
void ZRotate(float angle);
void XRotate(float angle);
void Translate(float x, float y, float z);

void Resize(float value);
void ResetSize();
void ResetToDefault();
Expand All @@ -54,18 +53,26 @@ class GlWidget : public QOpenGLWidget
void DrawLine(QMatrix4x4 vMatrix, QVector3D lPosition, float pos, bool major, bool horizontal);
void RenderCharacter(GLfloat x, GLfloat y, GLfloat z, int charactercode, QMatrix4x4 vMatrix, QColor Colour);
void RenderNumber(GLfloat x, GLfloat y, GLfloat z, float number, int decimalplaces, bool mm, bool major, QMatrix4x4 vMatrix);

protected:
// Overrides
bool event(QEvent *event);
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
void mouseMoveEvent(QMouseEvent *event);
QOpenGLBuffer VBOline, occBuffer;
int numOccVertices;

int numOccVertices;
GLfloat CharacterWidths[13]; //widths of these characters
GLfloat CharacterLineCounts[13]; //widths of these characters
QOpenGLBuffer VBOcharacters[13]; //0-9 are digits 0-9, 10 is -, 11 is ., 12 is m
GLfloat CharacterWidths[13], CharacterLineCounts[13]; //widths of these characters
QOpenGLBuffer VBOline;
QOpenGLBuffer occBuffer;

private:
bool gestureEvent(QGestureEvent *event);
void pinchTriggered(QPinchGesture *gesture);

QMatrix4x4 pMatrix;
QMatrix4x4 ScaleMatrix; //Manipulations to scale ball
QOpenGLShaderProgram lightingShaderProgram;
Expand Down

0 comments on commit f7011a4

Please sign in to comment.