Skip to content

Commit

Permalink
force landscape on android, added tap gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
nebomuk committed Oct 9, 2016
1 parent e28af11 commit c0d21f2
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/androidhelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "androidhelper.h"

#include <QAndroidJniObject>
#include <QtAndroid>

AndroidHelper::AndroidHelper()
{

}

void AndroidHelper::setScreenOrientation(int orientation)
{
QAndroidJniObject activity = QtAndroid::androidActivity();
if ( activity.isValid() )
{
activity.callMethod<void>
("setRequestedOrientation" // method name
, "(I)V" // signature
, orientation);
}
}

12 changes: 12 additions & 0 deletions src/androidhelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef ANDROIDHELPER_H
#define ANDROIDHELPER_H


class AndroidHelper
{
public:
AndroidHelper();
void setScreenOrientation(int orientation);
};

#endif // ANDROIDHELPER_H
21 changes: 20 additions & 1 deletion src/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
ScriptProxy.signalKeyPress.connect(onKeyPress);
ScriptProxy.signalKeyRelease.connect(onKeyRelease);

ScriptProxy.signalGestureStarted.connect(onGestureStarted);
ScriptProxy.signalGestureFinished.connect(onGestureFinished);

//coordinate system 0,0 of playerVehicles[1] is in topleft corner
// angles are degree by default

Expand Down Expand Up @@ -86,13 +89,29 @@ function startWormholeTravel(/*Vehicle* */ vehicle)
soundEngine.play('vanish.wav');
}

function onGestureStarted(gesture)
{
if(gesture === Qt.TapGesture)
{
startAcceleration(playerVehicles[0]);
}
}

function onGestureFinished(gesture)
{
if(gesture === Qt.TapGesture)
{
stopAcceleration(playerVehicles[0]);
}
}

function onKeyPress(key)
{

if(!isNullQObject(playerVehicles[0])
&& playerVehicles[0].control == true && playerVehicles[0].wormholeState == Vehicle.outside)
switch(key)
{
{
case Qt.Key_Up:
{
startAcceleration(playerVehicles[0]);
Expand Down
59 changes: 59 additions & 0 deletions src/graphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QDir>
#include <QScriptValue>
#include <QMessageBox>
#include <QEvent>
#include <QGestureEvent>
#include "graphicsview.h"
#include "globalvariables.h"
#include "vehicle.h"
Expand Down Expand Up @@ -42,6 +44,7 @@ GraphicsView::GraphicsView(QWidget * parent)




// disable scroll bars
this->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
this->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
Expand All @@ -62,6 +65,15 @@ GraphicsView::GraphicsView(QWidget * parent)
|QGraphicsView::DontAdjustForAntialiasing);
//*/

// pan and swipe gestures only work with 2 fingers
QList<Qt::GestureType> gestures;
gestures << Qt::TapGesture << Qt::TapAndHoldGesture;
foreach(Qt::GestureType gesture, gestures)
{
this->viewport()->grabGesture(gesture);
}
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);

this->setRenderHint(QPainter::Antialiasing,false);

setStyleSheet("QGraphicsView { border: none }");
Expand Down Expand Up @@ -89,6 +101,53 @@ GraphicsView::~GraphicsView()
delete graphicsEngine;
}

/*virtual*/ bool GraphicsView::viewportEvent(QEvent *event)
{
if (event->type() == QEvent::Gesture)
{
QGestureEvent* e = static_cast<QGestureEvent*>(event);
//QGesture* gesture = e->gesture(Qt::TapGesture);
QList<QGesture*> gestures = e->gestures();
foreach(QGesture* gesture, gestures)
{
Qt::GestureType type = gesture->gestureType();
if(type == Qt::TapGesture)
{
if(gesture->state() == Qt::GestureStarted)
{
qDebug() << "Tap Gesture started";
emit signalKeyPress(Qt::Key_Space);
}
else if(gesture->state() == Qt::GestureFinished)
{
qDebug() << "Tap Gesture stopped";
emit signalKeyRelease(Qt::Key_Space);
}
}
// else if(type == Qt::TapAndHoldGesture)
// {
// if(gesture->state() == Qt::GestureStarted)
// {
// qDebug() << "Tap and hold Gesture started";
// emit signalKeyPress(Qt::Key_Up);
// }
// else if(gesture->state() == Qt::GestureFinished)
// {
// qDebug() << "Tap and hold Gesture stopped";
// emit signalKeyRelease(Qt::Key_Up);
// }
// }
}

return true;
}
// else if (event->type() == QEvent::TouchBegin)
// {
// return false;
// }
else return QGraphicsView::viewportEvent(event);
}

void GraphicsView::restart()
{
// clears the screen
Expand Down
9 changes: 5 additions & 4 deletions src/graphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ private slots:
QGraphicsRectItem * addRect(QPointF pos, QSizeF size);

// events
virtual void timerEvent(QTimerEvent* event);
void resizeEvent(QResizeEvent *event);
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
virtual void timerEvent(QTimerEvent* event) Q_DECL_OVERRIDE;
virtual void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
virtual void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
virtual void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
virtual bool viewportEvent(QEvent *event) Q_DECL_OVERRIDE;

// save window geometry
void closeEvent(QCloseEvent *event);
Expand Down
15 changes: 14 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <QApplication>
#include <QTime>
#include <QInputDialog>
#include "androidhelper.h"
#include "graphicsview.h"

int main(int argc, char ** argv)
{



QApplication app( argc, argv );
app.setApplicationName("tasteroids");
app.setOrganizationName("Taiko");
Expand All @@ -14,7 +18,16 @@ int main(int argc, char ** argv)
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); // seed the random number generator
//int playerCount = QInputDialog::getInt(NULL,QObject::tr("Player Count"),QObject::tr("Enter number of players"),
// 1,1,2);
GraphicsView view;


// background image and tap gesture only work in landscape
#ifdef Q_OS_ANDROID
AndroidHelper helper;
const int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;
helper.setScreenOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
#endif

GraphicsView view;
//view.setPlayerCount(playerCount);
view.restart();
view.show();
Expand Down
2 changes: 2 additions & 0 deletions src/scriptproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Q_OBJECT
signals:
void signalKeyPress(int);
void signalKeyRelease(int);
void signalGestureStarted(int);
void signalGestureFinished(int);
void signalTimerEvent();

private:
Expand Down
5 changes: 5 additions & 0 deletions tasteroids.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ win32:{
INCLUDEPATH += SDL/include
QMAKE_LIBDIR += SDL
}
android {
QT += androidextras
SOURCES += src/androidhelper.cpp #my android specific cpp file
HEADERS += src/androidhelper.h #my android specific header file
}
#CONFIG += c++11
CONFIG += create_prl
CONFIG += warn_on
Expand Down

0 comments on commit c0d21f2

Please sign in to comment.