diff --git a/src/androidhelper.cpp b/src/androidhelper.cpp new file mode 100644 index 0000000..cb9e790 --- /dev/null +++ b/src/androidhelper.cpp @@ -0,0 +1,22 @@ +#include "androidhelper.h" + +#include +#include + +AndroidHelper::AndroidHelper() +{ + +} + +void AndroidHelper::setScreenOrientation(int orientation) +{ +QAndroidJniObject activity = QtAndroid::androidActivity(); +if ( activity.isValid() ) + { + activity.callMethod + ("setRequestedOrientation" // method name + , "(I)V" // signature + , orientation); + } +} + diff --git a/src/androidhelper.h b/src/androidhelper.h new file mode 100644 index 0000000..6567154 --- /dev/null +++ b/src/androidhelper.h @@ -0,0 +1,12 @@ +#ifndef ANDROIDHELPER_H +#define ANDROIDHELPER_H + + +class AndroidHelper +{ +public: + AndroidHelper(); + void setScreenOrientation(int orientation); +}; + +#endif // ANDROIDHELPER_H diff --git a/src/control.js b/src/control.js index 0ef963e..17681f2 100644 --- a/src/control.js +++ b/src/control.js @@ -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 @@ -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]); diff --git a/src/graphicsview.cpp b/src/graphicsview.cpp index 0e9e9d2..af06e5c 100644 --- a/src/graphicsview.cpp +++ b/src/graphicsview.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include "graphicsview.h" #include "globalvariables.h" #include "vehicle.h" @@ -42,6 +44,7 @@ GraphicsView::GraphicsView(QWidget * parent) + // disable scroll bars this->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); this->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff ); @@ -62,6 +65,15 @@ GraphicsView::GraphicsView(QWidget * parent) |QGraphicsView::DontAdjustForAntialiasing); //*/ + // pan and swipe gestures only work with 2 fingers + QList 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 }"); @@ -89,6 +101,53 @@ GraphicsView::~GraphicsView() delete graphicsEngine; } +/*virtual*/ bool GraphicsView::viewportEvent(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + { + QGestureEvent* e = static_cast(event); + //QGesture* gesture = e->gesture(Qt::TapGesture); + QList 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 diff --git a/src/graphicsview.h b/src/graphicsview.h index 2eb7bc9..8a10b3a 100644 --- a/src/graphicsview.h +++ b/src/graphicsview.h @@ -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); diff --git a/src/main.cpp b/src/main.cpp index 296c152..82a1dda 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,14 @@ #include #include #include +#include "androidhelper.h" #include "graphicsview.h" int main(int argc, char ** argv) { + + + QApplication app( argc, argv ); app.setApplicationName("tasteroids"); app.setOrganizationName("Taiko"); @@ -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(); diff --git a/src/scriptproxy.h b/src/scriptproxy.h index 6d8e8a0..232add9 100644 --- a/src/scriptproxy.h +++ b/src/scriptproxy.h @@ -29,6 +29,8 @@ Q_OBJECT signals: void signalKeyPress(int); void signalKeyRelease(int); + void signalGestureStarted(int); + void signalGestureFinished(int); void signalTimerEvent(); private: diff --git a/tasteroids.pro b/tasteroids.pro index 6606d2b..f6f9e95 100644 --- a/tasteroids.pro +++ b/tasteroids.pro @@ -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