diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-01-21 12:30:36 +0100 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-01-30 19:53:07 +0300 |
commit | 8292b80eac900ec5dd75d184063b7b35934e800a (patch) | |
tree | 3ce68afff38d75d0cb1cdb89260f2b49f232e86a | |
parent | 502a0a0163e702af7334979a2eabfc4826a94091 (diff) | |
download | mv-8292b80eac900ec5dd75d184063b7b35934e800a.tar.gz mv-8292b80eac900ec5dd75d184063b7b35934e800a.tar.bz2 mv-8292b80eac900ec5dd75d184063b7b35934e800a.tar.xz mv-8292b80eac900ec5dd75d184063b7b35934e800a.zip |
Allow resizing of the game in windowed mode
Window positions are semi-smartly corrected as a result of the resize.
Not supported when using OpenGL on Windows for now.
Reviewed-by: Yohann Ferreira
Conflicts:
src/client.cpp
src/client.h
src/game.cpp
src/gui/gui.cpp
src/gui/widgets/window.cpp
-rw-r--r-- | src/client.cpp | 54 | ||||
-rw-r--r-- | src/client.h | 4 | ||||
-rw-r--r-- | src/game.cpp | 16 | ||||
-rw-r--r-- | src/game.h | 2 | ||||
-rw-r--r-- | src/graphics.cpp | 27 | ||||
-rw-r--r-- | src/graphics.h | 5 | ||||
-rw-r--r-- | src/gui/gui.cpp | 14 | ||||
-rw-r--r-- | src/gui/gui.h | 5 | ||||
-rw-r--r-- | src/gui/widgets/window.cpp | 78 | ||||
-rw-r--r-- | src/gui/widgets/window.h | 15 | ||||
-rw-r--r-- | src/gui/widgets/windowcontainer.cpp | 10 | ||||
-rw-r--r-- | src/gui/widgets/windowcontainer.h | 6 | ||||
-rw-r--r-- | src/opengl1graphics.cpp | 11 | ||||
-rw-r--r-- | src/openglgraphics.cpp | 11 |
14 files changed, 190 insertions, 68 deletions
diff --git a/src/client.cpp b/src/client.cpp index 017bcadb4..de874c8a8 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -251,7 +251,7 @@ Client::Client(const Options &options): mServerConfigDir(""), mUsersDir(""), mNpcsDir(""), - mRootDir(""), + mGame(0), mCurrentDialog(nullptr), mQuitDialog(nullptr), mDesktop(nullptr), @@ -816,15 +816,14 @@ int Client::gameExec() if (!mumbleManager) mumbleManager = new MumbleManager(); - Game *game = nullptr; SDL_Event event; while (mState != STATE_EXIT) { - if (game) + if (mGame) { // Let the game handle the events while it is active - game->handleInput(); + mGame->handleInput(); } else { @@ -840,6 +839,10 @@ int Client::gameExec() case SDL_KEYDOWN: default: break; + + case SDL_VIDEORESIZE: + resizeVideo(event.resize.w, event.resize.h); + break; } guiInput->pushInput(event); @@ -858,8 +861,8 @@ int Client::gameExec() { if (gui) gui->logic(); - if (game) - game->logic(); + if (mGame) + mGame->logic(); sound.logic(); @@ -967,10 +970,8 @@ int Client::gameExec() top->add(mThemesButton); #endif - int screenWidth = config.getIntValue("screenwidth"); - int screenHeight = config.getIntValue("screenheight"); - - mDesktop->setSize(screenWidth, screenHeight); + mDesktop->setSize(mainGraphics->getWidth(), + mainGraphics->getHeight()); } if (mState == STATE_SWITCH_LOGIN && mOldState == STATE_GAME) @@ -985,8 +986,8 @@ int Client::gameExec() if (mOldState == STATE_GAME) { - delete game; - game = nullptr; + delete mGame; + mGame = nullptr; Game::clearInstance(); ResourceManager *resman = ResourceManager::getInstance(); if (resman) @@ -1291,7 +1292,7 @@ int Client::gameExec() logger->log1("State: GAME"); if (Net::getGeneralHandler()) Net::getGeneralHandler()->reloadPartially(); - game = new Game; + mGame = new Game; break; case STATE_LOGIN_ERROR: @@ -2354,3 +2355,30 @@ bool Client::isTmw() } return false; } + +void Client::resizeVideo(int width, int height) +{ + // Keep a minimum size. This isn't adhered to by the actual window, but + // it keeps some window positions from getting messed up. + width = std::max(640, width); + height = std::max(480, height); + + if (mainGraphics->mWidth == width && mainGraphics->mHeight == height) + return; + + if (mainGraphics->resize(width, height)) + { + gui->videoResized(); + + if (mDesktop) + mDesktop->setSize(width, height); + + if (mSetupButton) + mSetupButton->setPosition(width - mSetupButton->getWidth() - 3, 3); + + if (mGame) + mGame->videoResized(width, height); + + gui->draw(); + } +} diff --git a/src/client.h b/src/client.h index 27e6249bd..29059e83a 100644 --- a/src/client.h +++ b/src/client.h @@ -38,6 +38,7 @@ class Button; class Desktop; +class Game; class LoginData; class Window; class QuitDialog; @@ -281,6 +282,8 @@ public: void writePacketLimits(std::string packetLimitsName); + void resizeVideo(int width, int height); + static bool limitPackets(int type); static bool checkPackets(int type); @@ -334,6 +337,7 @@ private: ServerInfo mCurrentServer; + Game *mGame; Window *mCurrentDialog; QuitDialog *mQuitDialog; Desktop *mDesktop; diff --git a/src/game.cpp b/src/game.cpp index e48b37ebc..70468ebfd 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -376,8 +376,7 @@ Game::Game(): // Create the viewport viewport = new Viewport; - viewport->setDimension(gcn::Rectangle(0, 0, mainGraphics->mWidth, - mainGraphics->mHeight)); + viewport->setSize(mainGraphics->mWidth, mainGraphics->mHeight); gcn::Container *top = static_cast<gcn::Container*>(gui->getTop()); top->add(viewport); @@ -1636,8 +1635,13 @@ void Game::handleInput() updateHistory(event); checkKeys(); + if (event.type == SDL_VIDEORESIZE) + { + // Let the client deal with this one (it'll pass down from there) + Client::instance()->resizeVideo(event.resize.w, event.resize.h); + } // Keyboard events (for discontinuous keys) - if (event.type == SDL_KEYDOWN) + else if (event.type == SDL_KEYDOWN) { wasDown = true; @@ -1940,3 +1944,9 @@ void Game::closeDialogs() deathNotice = nullptr; } } + +void Game::videoResized(int width, int height) +{ + viewport->setSize(width, height); + windowMenu->setPosition(width - 3 - windowMenu->getWidth(), 3); +} diff --git a/src/game.h b/src/game.h index c6d942fe1..6064a300b 100644 --- a/src/game.h +++ b/src/game.h @@ -108,6 +108,8 @@ class Game static void closeDialogs(); + void videoResized(int width, int height); + private: void updateHistory(SDL_Event &event); diff --git a/src/graphics.cpp b/src/graphics.cpp index bbd398aa5..46433639e 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -76,6 +76,8 @@ bool Graphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel) if (fs) displayFlags |= SDL_FULLSCREEN; + else + displayFlags |= SDL_RESIZABLE; if (hwaccel) displayFlags |= SDL_HWSURFACE | SDL_DOUBLEBUF; @@ -141,6 +143,31 @@ bool Graphics::setFullscreen(bool fs) return setVideoMode(mWidth, mHeight, mBpp, fs, mHWAccel); } +bool Graphics::resize(int width, int height) +{ + if (mWidth == width && mHeight == height) + return true; + + const int prevWidth = mWidth; + const int prevHeight = mHeight; + + _endDraw(); + + bool success = setVideoMode(width, height, mBpp, mFullscreen, mHWAccel); + + // If it didn't work, try to restore the previous size. If that didn't + // work either, bail out (but then we're in deep trouble). + if (!success) + { + if (!setVideoMode(prevWidth, prevHeight, mBpp, mFullscreen, mHWAccel)) + return false; + } + + _beginDraw(); + + return success; +} + int Graphics::getWidth() const { return mWidth; diff --git a/src/graphics.h b/src/graphics.h index 4c47e690d..7fab02cbb 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -105,6 +105,11 @@ class Graphics : public gcn::SDLGraphics bool setFullscreen(bool fs); /** + * Resize the window to the specified size. + */ + bool resize(int width, int height); + + /** * Blits an image onto the screen. * * @return <code>true</code> if the image was blitted properly diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 3d79f0cdf..e441111a9 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -99,8 +99,7 @@ Gui::Gui(Graphics *graphics): // Initialize top GUI widget WindowContainer *guiTop = new WindowContainer; guiTop->setFocusable(true); - guiTop->setDimension(gcn::Rectangle(0, 0, - graphics->mWidth, graphics->mHeight)); + guiTop->setSize(graphics->mWidth, graphics->mHeight); guiTop->setOpaque(false); Window::setWindowContainer(guiTop); setTop(guiTop); @@ -287,6 +286,17 @@ void Gui::draw() mGraphics->popClipArea(); } +void Gui::videoResized() +{ + WindowContainer *top = static_cast<WindowContainer*>(getTop()); + + int oldWidth = top->getWidth(); + int oldHeight = top->getHeight(); + + top->setSize(mainGraphics->mWidth, mainGraphics->mHeight); + top->adjustAfterResize(oldWidth, oldHeight); +} + void Gui::setUseCustomCursor(bool customCursor) { if (customCursor != mCustomCursor) diff --git a/src/gui/gui.h b/src/gui/gui.h index cadcc89ac..5ace42323 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -70,6 +70,11 @@ class Gui : public gcn::Gui */ void draw(); + /** + * Called when the application window has been resized. + */ + void videoResized(); + gcn::FocusHandler *getFocusHandler() const { return mFocusHandler; } diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index 3858b0d81..6e6918694 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -433,7 +433,7 @@ void Window::setVisible(bool visible, bool forceSticky) // Check if the window is off screen... if (visible) - checkIfIsOffScreen(); + ensureOnScreen(); if (isStickyButtonLock()) gcn::Window::setVisible(visible); @@ -713,7 +713,7 @@ void Window::loadWindowState() } // Check if the window is off screen... - checkIfIsOffScreen(); + ensureOnScreen(); if (viewport) { @@ -848,6 +848,22 @@ void Window::resetToDefaultSize() saveWindowState(); } +void Window::adjustPositionAfterResize(int oldScreenWidth, int oldScreenHeight) +{ + gcn::Rectangle dimension = getDimension(); + + // If window was aligned to the right or bottom, keep it there + const int rightMargin = oldScreenWidth - (getX() + getWidth()); + const int bottomMargin = oldScreenHeight - (getY() + getHeight()); + if (getX() > 0 && getX() > rightMargin) + dimension.x = mainGraphics->mWidth - rightMargin - getWidth(); + if (getY() > 0 && getY() > bottomMargin) + dimension.y = mainGraphics->mHeight - bottomMargin - getHeight(); + + setDimension(dimension); + ensureOnScreen(); +} + int Window::getResizeHandles(gcn::MouseEvent &event) { if ((mStickyButtonLock && mSticky) || event.getX() < 0 || event.getY() < 0) @@ -972,57 +988,27 @@ void Window::centerHorisontally() setLocationHorisontallyRelativeTo(getParent()); } -void Window::checkIfIsOffScreen(bool partially, bool entirely) +void Window::ensureOnScreen() { - // Move the window onto screen if it has become off screen - // For instance, because of resolution change... - - // First of all, don't deal when a window hasn't got - // any size initialized yet... + // Skip when a window hasn't got any size initialized yet if (getWidth() == 0 && getHeight() == 0) return; - // Made partially the default behaviour - if (!partially && !entirely) - partially = true; - - // Keep guichan window inside screen (supports resizing any side) - - gcn::Rectangle winDimension = getDimension(); - - if (winDimension.x < 0) - { - winDimension.width += winDimension.x; - winDimension.x = 0; - } - if (winDimension.y < 0) - { - winDimension.height += winDimension.y; - winDimension.y = 0; - } - - // Look if the window is partially off-screen limits... - if (partially) - { - if (winDimension.x + winDimension.width > mainGraphics->mWidth) - winDimension.x = mainGraphics->mWidth - winDimension.width; - - if (winDimension.y + winDimension.height > mainGraphics->mHeight) - winDimension.y = mainGraphics->mHeight - winDimension.height; + gcn::Rectangle dimension = getDimension(); - setDimension(winDimension); - return; - } + // Check the left and bottom screen boundaries + if (dimension.x + dimension.width > mainGraphics->mWidth) + dimension.x = mainGraphics->mWidth - dimension.width; + if (dimension.y + dimension.height > mainGraphics->mHeight) + dimension.y = mainGraphics->mHeight - dimension.height; - if (entirely) - { - if (winDimension.x > mainGraphics->mWidth) - winDimension.x = mainGraphics->mWidth - winDimension.width; + // But never allow the windows to disappear in to the right and top + if (dimension.x < 0) + dimension.x = 0; + if (dimension.y < 0) + dimension.y = 0; - if (winDimension.y > mainGraphics->mHeight) - winDimension.y = mainGraphics->mHeight - winDimension.height; - } - setDimension(winDimension); + setDimension(dimension); } gcn::Rectangle Window::getWindowArea() diff --git a/src/gui/widgets/window.h b/src/gui/widgets/window.h index 65dbf196b..6fa47dedc 100644 --- a/src/gui/widgets/window.h +++ b/src/gui/widgets/window.h @@ -329,6 +329,13 @@ class Window : public gcn::Window, gcn::WidgetListener virtual void resetToDefaultSize(); /** + * Adjusts the window position after the application window has been + * resized. + */ + void adjustPositionAfterResize(int oldScreenWidth, + int oldScreenHeight); + + /** * Gets the layout handler for this window. */ Layout &getLayout(); @@ -406,11 +413,11 @@ class Window : public gcn::Window, gcn::WidgetListener }; /** - * Check if the window is off-screen and then move it to be visible - * again. This is internally used by loadWindowState - * and setVisible(true) members. + * Ensures the window is on the screen, moving it if necessary. This is + * used by loadWindowState and setVisible(true), and when the screen + * is resized. */ - void checkIfIsOffScreen(bool partially = true, bool entirely = true); + void ensureOnScreen(); /** * Determines if the mouse is in a resize area and returns appropriate diff --git a/src/gui/widgets/windowcontainer.cpp b/src/gui/widgets/windowcontainer.cpp index 43aaea8a4..ce218e042 100644 --- a/src/gui/widgets/windowcontainer.cpp +++ b/src/gui/widgets/windowcontainer.cpp @@ -22,6 +22,8 @@ #include "gui/widgets/windowcontainer.h" +#include "gui/widgets/window.h" + #include "utils/dtor.h" #include "debug.h" @@ -41,3 +43,11 @@ void WindowContainer::scheduleDelete(gcn::Widget *widget) if (widget) mDeathList.push_back(widget); } + +void WindowContainer::adjustAfterResize(int oldScreenWidth, + int oldScreenHeight) +{ + for (WidgetListIterator i = mWidgets.begin(); i != mWidgets.end(); ++i) + if (Window *window = dynamic_cast<Window*>(*i)) + window->adjustPositionAfterResize(oldScreenWidth, oldScreenHeight); +} diff --git a/src/gui/widgets/windowcontainer.h b/src/gui/widgets/windowcontainer.h index 00ef04c19..1cec11861 100644 --- a/src/gui/widgets/windowcontainer.h +++ b/src/gui/widgets/windowcontainer.h @@ -48,6 +48,12 @@ class WindowContainer : public Container */ void scheduleDelete(gcn::Widget *widget); + /** + * Ensures that all visible windows are on the screen after the screen + * has been resized. + */ + void adjustAfterResize(int oldScreenWidth, int oldScreenHeight); + private: /** * List of widgets that are scheduled to be deleted. diff --git a/src/opengl1graphics.cpp b/src/opengl1graphics.cpp index 6f712f698..7995b122b 100644 --- a/src/opengl1graphics.cpp +++ b/src/opengl1graphics.cpp @@ -76,7 +76,17 @@ bool OpenGL1Graphics::setVideoMode(int w, int h, int bpp, mHWAccel = hwaccel; if (fs) + { displayFlags |= SDL_FULLSCREEN; + } + else + { + // Resizing currently not supported on Windows, where it would require + // reuploading all textures. +#if !defined(_WIN32) + displayFlags |= SDL_RESIZABLE; +#endif + } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); @@ -472,6 +482,7 @@ void OpenGL1Graphics::_beginDraw() void OpenGL1Graphics::_endDraw() { + popClipArea(); } SDL_Surface* OpenGL1Graphics::getScreenshot() diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index b744a3caf..9fb91b201 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -85,7 +85,17 @@ bool OpenGLGraphics::setVideoMode(int w, int h, int bpp, bool fs, bool hwaccel) mHWAccel = hwaccel; if (fs) + { displayFlags |= SDL_FULLSCREEN; + } + else + { + // Resizing currently not supported on Windows, where it would require + // reuploading all textures. +#if !defined(_WIN32) + displayFlags |= SDL_RESIZABLE; +#endif + } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); @@ -1017,6 +1027,7 @@ void OpenGLGraphics::_beginDraw() void OpenGLGraphics::_endDraw() { + popClipArea(); } SDL_Surface* OpenGLGraphics::getScreenshot() |