Merge pull request #3871 from ZappoMan/HMDMode

Various HMD Tools improvements
This commit is contained in:
Philip Rosedale 2014-11-26 20:33:38 -08:00
commit 56b27a2595
3 changed files with 114 additions and 56 deletions

View file

@ -438,6 +438,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) :
void Application::aboutToQuit() { void Application::aboutToQuit() {
_aboutToQuit = true; _aboutToQuit = true;
setFullscreen(false); // if you exit while in full screen, you'll get bad behavior when you restart.
} }
Application::~Application() { Application::~Application() {

View file

@ -30,27 +30,27 @@ HMDToolsDialog::HMDToolsDialog(QWidget* parent) :
QDialog(parent, Qt::Window | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint) , QDialog(parent, Qt::Window | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint) ,
_previousScreen(NULL), _previousScreen(NULL),
_hmdScreen(NULL), _hmdScreen(NULL),
_previousDialogScreen(NULL) _hmdScreenNumber(-1),
_switchModeButton(NULL),
_debugDetails(NULL),
_previousDialogScreen(NULL),
_inHDMMode(false)
{ {
this->setWindowTitle("HMD Tools"); this->setWindowTitle("HMD Tools");
// Create layouter // Create layouter
QFormLayout* form = new QFormLayout(); QFormLayout* form = new QFormLayout();
const int WIDTH = 350;
// Add a button to enter // Add a button to enter
QPushButton* enterModeButton = new QPushButton("Enter HMD Mode"); _switchModeButton = new QPushButton("Enter HMD Mode");
form->addRow("", enterModeButton); _switchModeButton->setFixedWidth(WIDTH);
connect(enterModeButton,SIGNAL(clicked(bool)),this,SLOT(enterModeClicked(bool))); form->addRow("", _switchModeButton);
connect(_switchModeButton,SIGNAL(clicked(bool)),this,SLOT(switchModeClicked(bool)));
// Add a button to leave
QPushButton* leaveModeButton = new QPushButton("Leave HMD Mode");
form->addRow("", leaveModeButton);
connect(leaveModeButton,SIGNAL(clicked(bool)),this,SLOT(leaveModeClicked(bool)));
// Create a label with debug details... // Create a label with debug details...
_debugDetails = new QLabel(); _debugDetails = new QLabel();
_debugDetails->setText(getDebugDetails()); _debugDetails->setText(getDebugDetails());
const int WIDTH = 350;
const int HEIGHT = 100; const int HEIGHT = 100;
_debugDetails->setFixedSize(WIDTH, HEIGHT); _debugDetails->setFixedSize(WIDTH, HEIGHT);
form->addRow("", _debugDetails); form->addRow("", _debugDetails);
@ -65,14 +65,21 @@ HMDToolsDialog::HMDToolsDialog(QWidget* parent) :
QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle();
connect(mainWindow, &QWindow::screenChanged, this, &HMDToolsDialog::applicationWindowScreenChanged); connect(mainWindow, &QWindow::screenChanged, this, &HMDToolsDialog::applicationWindowScreenChanged);
// watch for our dialog window moving screens. If it does we want to enforce our rules about what screens we're // watch for our dialog window moving screens. If it does we want to enforce our rules about
// allowed on // what screens we're allowed on
QWindow* dialogWindow = windowHandle(); QWindow* dialogWindow = windowHandle();
connect(dialogWindow, &QWindow::screenChanged, this, &HMDToolsDialog::dialogWindowScreenChanged); connect(dialogWindow, &QWindow::screenChanged, this, &HMDToolsDialog::dialogWindowScreenChanged);
connect(dialogWindow, &QWindow::xChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::xChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged);
connect(dialogWindow, &QWindow::yChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::yChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged);
connect(dialogWindow, &QWindow::widthChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::widthChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged);
connect(dialogWindow, &QWindow::heightChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::heightChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged);
// when the application is about to quit, leave HDM mode
connect(Application::getInstance(), SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));
// keep track of changes to the number of screens
connect(QApplication::desktop(), &QDesktopWidget::screenCountChanged, this, &HMDToolsDialog::screenCountChanged);
} }
HMDToolsDialog::~HMDToolsDialog() { HMDToolsDialog::~HMDToolsDialog() {
@ -96,11 +103,11 @@ void HMDToolsDialog::dialogWindowScreenChanged(QScreen* screen) {
// if we have more than one screen, and a known hmdScreen then try to // if we have more than one screen, and a known hmdScreen then try to
// keep our dialog off of the hmdScreen // keep our dialog off of the hmdScreen
if (QApplication::desktop()->screenCount() > 1) { if (QApplication::desktop()->screenCount() > 1) {
// we want to use a local variable here because we are not necesarily in HMD mode
int hmdScreenNumber = OculusManager::getHMDScreen(); int hmdScreenNumber = OculusManager::getHMDScreen();
if (_hmdScreenNumber >= 0) {
if (hmdScreenNumber >= 0) {
QScreen* hmdScreen = QGuiApplication::screens()[hmdScreenNumber]; QScreen* hmdScreen = QGuiApplication::screens()[hmdScreenNumber];
if (screen == hmdScreen) { if (screen == hmdScreen) {
qDebug() << "HMD Tools: Whoa! What are you doing? You don't want to move me to the HMD Screen!"; qDebug() << "HMD Tools: Whoa! What are you doing? You don't want to move me to the HMD Screen!";
QWindow* dialogWindow = windowHandle(); QWindow* dialogWindow = windowHandle();
@ -154,42 +161,55 @@ QString HMDToolsDialog::getDebugDetails() const {
results += "Application Primary Screen: " + QGuiApplication::primaryScreen()->name() + "\n"; results += "Application Primary Screen: " + QGuiApplication::primaryScreen()->name() + "\n";
QScreen* mainWindowScreen = Application::getInstance()->getWindow()->windowHandle()->screen(); QScreen* mainWindowScreen = Application::getInstance()->getWindow()->windowHandle()->screen();
results += "Application Main Window Screen: " + mainWindowScreen->name() + "\n"; results += "Application Main Window Screen: " + mainWindowScreen->name() + "\n";
results += "Total Screens: " + QString::number(QApplication::desktop()->screenCount()) + "\n";
return results; return results;
} }
void HMDToolsDialog::enterModeClicked(bool checked) { void HMDToolsDialog::switchModeClicked(bool checked) {
_debugDetails->setText(getDebugDetails()); if (!_inHDMMode) {
enterHDMMode();
int hmdScreen = OculusManager::getHMDScreen(); } else {
qDebug() << "enterModeClicked().... hmdScreen:" << hmdScreen; leaveHDMMode();
if (hmdScreen >= 0) {
QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle();
_hmdScreen = QGuiApplication::screens()[hmdScreen];
_previousRect = Application::getInstance()->getWindow()->rect();
_previousRect = QRect(mainWindow->mapToGlobal(_previousRect.topLeft()),
mainWindow->mapToGlobal(_previousRect.bottomRight()));
_previousScreen = mainWindow->screen();
QRect rect = QApplication::desktop()->screenGeometry(hmdScreen);
mainWindow->setScreen(_hmdScreen);
mainWindow->setGeometry(rect);
_wasMoved = true;
} }
}
void HMDToolsDialog::enterHDMMode() {
if (!_inHDMMode) {
_switchModeButton->setText("Leave HMD Mode");
_debugDetails->setText(getDebugDetails());
_hmdScreenNumber = OculusManager::getHMDScreen();
if (_hmdScreenNumber >= 0) {
QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle();
_hmdScreen = QGuiApplication::screens()[_hmdScreenNumber];
_previousRect = Application::getInstance()->getWindow()->rect();
_previousRect = QRect(mainWindow->mapToGlobal(_previousRect.topLeft()),
mainWindow->mapToGlobal(_previousRect.bottomRight()));
_previousScreen = mainWindow->screen();
QRect rect = QApplication::desktop()->screenGeometry(_hmdScreenNumber);
mainWindow->setScreen(_hmdScreen);
mainWindow->setGeometry(rect);
_wasMoved = true;
}
// if we're on a single screen setup, then hide our tools window when entering HMD mode // if we're on a single screen setup, then hide our tools window when entering HMD mode
if (QApplication::desktop()->screenCount() == 1) { if (QApplication::desktop()->screenCount() == 1) {
close(); close();
}
Application::getInstance()->setFullscreen(true);
Application::getInstance()->setEnableVRMode(true);
const int SLIGHT_DELAY = 500;
QTimer::singleShot(SLIGHT_DELAY, this, SLOT(activateWindowAfterEnterMode()));
_inHDMMode = true;
} }
Application::getInstance()->setFullscreen(true);
Application::getInstance()->setEnableVRMode(true);
const int SLIGHT_DELAY = 500;
QTimer::singleShot(SLIGHT_DELAY, this, SLOT(activateWindowAfterEnterMode()));
} }
void HMDToolsDialog::activateWindowAfterEnterMode() { void HMDToolsDialog::activateWindowAfterEnterMode() {
@ -199,23 +219,26 @@ void HMDToolsDialog::activateWindowAfterEnterMode() {
centerCursorOnWidget(Application::getInstance()->getWindow()); centerCursorOnWidget(Application::getInstance()->getWindow());
} }
void HMDToolsDialog::leaveHDMMode() {
if (_inHDMMode) {
_switchModeButton->setText("Enter HMD Mode");
_debugDetails->setText(getDebugDetails());
void HMDToolsDialog::leaveModeClicked(bool checked) { Application::getInstance()->setFullscreen(false);
_debugDetails->setText(getDebugDetails()); Application::getInstance()->setEnableVRMode(false);
Application::getInstance()->getWindow()->activateWindow();
Application::getInstance()->setFullscreen(false); if (_wasMoved) {
Application::getInstance()->setEnableVRMode(false); QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle();
Application::getInstance()->getWindow()->activateWindow(); mainWindow->setScreen(_previousScreen);
mainWindow->setGeometry(_previousRect);
if (_wasMoved) { const int SLIGHT_DELAY = 1500;
QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); QTimer::singleShot(SLIGHT_DELAY, this, SLOT(moveWindowAfterLeaveMode()));
mainWindow->setScreen(_previousScreen); }
mainWindow->setGeometry(_previousRect); _wasMoved = false;
_inHDMMode = false;
const int SLIGHT_DELAY = 1500;
QTimer::singleShot(SLIGHT_DELAY, this, SLOT(moveWindowAfterLeaveMode()));
} }
_wasMoved = false;
} }
void HMDToolsDialog::moveWindowAfterLeaveMode() { void HMDToolsDialog::moveWindowAfterLeaveMode() {
@ -256,3 +279,31 @@ void HMDToolsDialog::hideEvent(QHideEvent* event) {
} }
void HMDToolsDialog::aboutToQuit() {
if (_inHDMMode) {
leaveHDMMode();
}
}
void HMDToolsDialog::screenCountChanged(int newCount) {
if (!OculusManager::isConnected()) {
OculusManager::connect();
}
int hmdScreenNumber = OculusManager::getHMDScreen();
if (_inHDMMode && _hmdScreenNumber != hmdScreenNumber) {
qDebug() << "HMD Display changed WHILE IN HMD MODE";
leaveHDMMode();
// if there is a new best HDM screen then go back into HDM mode after done leaving
if (hmdScreenNumber >= 0) {
qDebug() << "Trying to go back into HDM Mode";
const int SLIGHT_DELAY = 2000;
QTimer::singleShot(SLIGHT_DELAY, this, SLOT(enterHDMMode()));
}
}
_debugDetails->setText(getDebugDetails());
}

View file

@ -28,13 +28,14 @@ signals:
public slots: public slots:
void reject(); void reject();
void enterModeClicked(bool checked); void switchModeClicked(bool checked);
void leaveModeClicked(bool checked);
void activateWindowAfterEnterMode(); void activateWindowAfterEnterMode();
void moveWindowAfterLeaveMode(); void moveWindowAfterLeaveMode();
void applicationWindowScreenChanged(QScreen* screen); void applicationWindowScreenChanged(QScreen* screen);
void dialogWindowScreenChanged(QScreen* screen); void dialogWindowScreenChanged(QScreen* screen);
void dialogWindowGeometryChanged(int arg); void dialogWindowGeometryChanged(int arg);
void aboutToQuit();
void screenCountChanged(int newCount);
protected: protected:
virtual void closeEvent(QCloseEvent*); // Emits a 'closed' signal when this dialog is closed. virtual void closeEvent(QCloseEvent*); // Emits a 'closed' signal when this dialog is closed.
@ -43,15 +44,20 @@ protected:
private: private:
void centerCursorOnWidget(QWidget* widget); void centerCursorOnWidget(QWidget* widget);
void enterHDMMode();
void leaveHDMMode();
bool _wasMoved; bool _wasMoved;
QRect _previousRect; QRect _previousRect;
QScreen* _previousScreen; QScreen* _previousScreen;
QScreen* _hmdScreen; QScreen* _hmdScreen;
int _hmdScreenNumber;
QPushButton* _switchModeButton;
QLabel* _debugDetails; QLabel* _debugDetails;
QRect _previousDialogRect; QRect _previousDialogRect;
QScreen* _previousDialogScreen; QScreen* _previousDialogScreen;
bool _inHDMMode;
}; };
#endif // hifi_HMDToolsDialog_h #endif // hifi_HMDToolsDialog_h