]> git.mxchange.org Git - flightgear.git/blobdiff - src/Viewer/fg_os_osgviewer.cxx
toggle fullscreen: also adapt GUI plane when resizing
[flightgear.git] / src / Viewer / fg_os_osgviewer.cxx
index 3600a65ee5a722c9ef7f28d0a422b9c754855e22..d08b7923784326e918d6fc2acf6e89912ee8ecab 100644 (file)
@@ -309,9 +309,84 @@ void fgOSInit(int* argc, char** argv)
     WindowSystemAdapter::setWSA(new WindowSystemAdapter);
 }
 
-// Noop
 void fgOSFullScreen()
 {
+    std::vector<osgViewer::GraphicsWindow*> windows;
+    viewer->getWindows(windows);
+
+    if (windows.size() == 0)
+        return; // Huh?!?
+
+    /* Toggling window fullscreen is only supported for the main GUI window.
+     * The other windows should use fixed setup from the camera.xml file anyway. */
+    osgViewer::GraphicsWindow* window = windows[0];
+
+    {
+        osg::GraphicsContext::WindowingSystemInterface    *wsi = osg::GraphicsContext::getWindowingSystemInterface();
+
+        if (wsi == NULL)
+        {
+            SG_LOG(SG_VIEW, SG_ALERT, "ERROR: No WindowSystemInterface available. Cannot toggle window fullscreen.");
+            return;
+        }
+
+        static int previous_x = 0;
+        static int previous_y = 0;
+        static int previous_width = 800;
+        static int previous_height = 600;
+
+        unsigned int screenWidth;
+        unsigned int screenHeight;
+        wsi->getScreenResolution(*(window->getTraits()), screenWidth, screenHeight);
+
+        int x;
+        int y;
+        int width;
+        int height;
+        window->getWindowRectangle(x, y, width, height);
+
+        bool isFullScreen = x == 0 && y == 0 && width == (int)screenWidth && height == (int)screenHeight;
+
+        SG_LOG(SG_VIEW, SG_DEBUG, "Toggling fullscreen. Previous window rectangle ("
+               << x << ", " << y << ") x (" << width << ", " << height << "), fullscreen: " << isFullScreen);
+        if (isFullScreen)
+        {
+            // limit x,y coordinates and window size to screen area
+            if (previous_x + previous_width > (int)screenWidth)
+                previous_x = 0;
+            if (previous_y + previous_height > (int)screenHeight)
+                previous_y = 0;
+
+            // disable fullscreen mode, restore previous window size/coordinates
+            x = previous_x;
+            y = previous_y;
+            width = previous_width;
+            height = previous_height;
+        }
+        else
+        {
+            // remember previous setting
+            previous_x = x;
+            previous_y = y;
+            previous_width = width;
+            previous_height = height;
+
+            // enable fullscreen mode, set new width/height
+            x = 0;
+            y = 0;
+            width = screenWidth;
+            height = screenHeight;
+        }
+
+        // set xsize/ysize properties to adapt GUI planes
+        fgSetInt("/sim/startup/xsize", width);
+        fgSetInt("/sim/startup/ysize", height);
+
+        // reconfigure window
+        window->setWindowDecoration(isFullScreen);
+        window->setWindowRectangle(x, y, width, height);
+        window->grabFocusIfPointerInWindow();
+    }
 }
 
 static void setMouseCursor(osgViewer::GraphicsWindow* gw, int cursor)