From 690b88f9824a85bc0f9abed12c2ec6b7a72b5ced Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Fri, 2 Jan 2009 03:21:40 +0000 Subject: Renamed gui/newgui.cpp -> gui/GuiManager.cpp and gui/newgui.h -> gui/GuiManager.h svn-id: r35668 --- gui/EditTextWidget.cpp | 2 +- gui/GuiManager.cpp | 424 ++++++++++++++++++++++++++++++++++++++++++++++++ gui/GuiManager.h | 151 +++++++++++++++++ gui/KeysDialog.h | 2 +- gui/ListWidget.cpp | 2 +- gui/PopUpWidget.cpp | 2 +- gui/ScrollBarWidget.cpp | 2 +- gui/TabWidget.cpp | 2 +- gui/ThemeParser.cpp | 2 +- gui/about.cpp | 2 +- gui/browser.cpp | 2 +- gui/chooser.cpp | 2 +- gui/console.h | 2 +- gui/dialog.cpp | 2 +- gui/editable.cpp | 2 +- gui/editable.h | 2 +- gui/launcher.cpp | 2 +- gui/massadd.cpp | 2 +- gui/message.cpp | 2 +- gui/module.mk | 2 +- gui/newgui.cpp | 424 ------------------------------------------------ gui/newgui.h | 151 ----------------- gui/object.cpp | 2 +- gui/options.cpp | 2 +- gui/widget.cpp | 2 +- 25 files changed, 596 insertions(+), 596 deletions(-) create mode 100644 gui/GuiManager.cpp create mode 100644 gui/GuiManager.h delete mode 100644 gui/newgui.cpp delete mode 100644 gui/newgui.h (limited to 'gui') diff --git a/gui/EditTextWidget.cpp b/gui/EditTextWidget.cpp index 873c578638..c977ecc3d0 100644 --- a/gui/EditTextWidget.cpp +++ b/gui/EditTextWidget.cpp @@ -24,7 +24,7 @@ #include "gui/EditTextWidget.h" #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" diff --git a/gui/GuiManager.cpp b/gui/GuiManager.cpp new file mode 100644 index 0000000000..00babbea17 --- /dev/null +++ b/gui/GuiManager.cpp @@ -0,0 +1,424 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#include "common/events.h" +#include "common/system.h" +#include "common/util.h" +#include "common/config-manager.h" + +#include "gui/GuiManager.h" +#include "gui/dialog.h" +#include "gui/ThemeEngine.h" +#include "gui/ThemeEval.h" + +#include "graphics/cursorman.h" + +DECLARE_SINGLETON(GUI::GuiManager); + +namespace GUI { + +enum { + kDoubleClickDelay = 500, // milliseconds + kCursorAnimateDelay = 250 +}; + +// Constructor +GuiManager::GuiManager() : _redrawStatus(kRedrawDisabled), + _stateIsSaved(false), _cursorAnimateCounter(0), _cursorAnimateTimer(0) { + _theme = 0; + _useStdCursor = false; + + _system = g_system; + _lastScreenChangeID = _system->getScreenChangeID(); + + // Clear the cursor + memset(_cursor, 0xFF, sizeof(_cursor)); + + + ConfMan.registerDefault("gui_theme", "scummmodern.zip"); + Common::String themefile(ConfMan.get("gui_theme")); + if (themefile.compareToIgnoreCase("default") == 0) + themefile = "builtin"; + + ConfMan.registerDefault("gui_renderer", ThemeEngine::findModeConfigName(ThemeEngine::_defaultRendererMode)); + ThemeEngine::GraphicsMode gfxMode = (ThemeEngine::GraphicsMode)ThemeEngine::findMode(ConfMan.get("gui_renderer")); + + // Try to load the theme + if (!loadNewTheme(themefile, gfxMode)) { + // Loading the theme failed, try to load the built-in theme + if (!loadNewTheme("builtin", gfxMode)) { + // Loading the built-in theme failed as well. Bail out + error("Failed to load any GUI theme, aborting"); + } + } + _themeChange = false; +} + +GuiManager::~GuiManager() { + delete _theme; +} + +bool GuiManager::loadNewTheme(Common::String filename, ThemeEngine::GraphicsMode gfx) { + // If we are asked to reload the currently active theme, just do nothing + // FIXME: Actually, why? It might be desirable at times to force a theme reload... + if (_theme && filename == _theme->getThemeId() && gfx == _theme->getGraphicsMode()) + return true; + + ThemeEngine *newTheme = 0; + + if (gfx == ThemeEngine::kGfxDisabled) + gfx = ThemeEngine::_defaultRendererMode; + + // Try to load the new theme + newTheme = new ThemeEngine(filename, gfx); + assert(newTheme); + + if (!newTheme->init()) + return false; + + // + // Disable and delete the old theme + // + if (_theme) + _theme->disable(); + delete _theme; + + if (_useStdCursor) { + CursorMan.popCursorPalette(); + CursorMan.popCursor(); + } + + // + // Enable the new theme + // + _theme = newTheme; + _themeChange = true; + + // refresh all dialogs + for (int i = 0; i < _dialogStack.size(); ++i) { + _dialogStack[i]->reflowLayout(); + } + + // We need to redraw immediately. Otherwise + // some other event may cause a widget to be + // redrawn before redraw() has been called. + _redrawStatus = kRedrawFull; + redraw(); + _system->updateScreen(); + + return true; +} + +void GuiManager::redraw() { + int i; + + if (_redrawStatus == kRedrawDisabled) + return; + + if (_dialogStack.empty()) + return; + + switch (_redrawStatus) { + case kRedrawCloseDialog: + case kRedrawFull: + case kRedrawTopDialog: + _theme->clearAll(); + _theme->openDialog(true); + + for (i = 0; i < _dialogStack.size() - 1; i++) { + _dialogStack[i]->drawDialog(); + } + + _theme->finishBuffering(); + _theme->updateScreen(); + + case kRedrawOpenDialog: + _theme->openDialog(true, (ThemeEngine::ShadingStyle)xmlEval()->getVar("Dialog." + _dialogStack.top()->_name + ".Shading", 0)); + _dialogStack.top()->drawDialog(); + _theme->finishBuffering(); + break; + + default: + return; + } + + _theme->updateScreen(); + _redrawStatus = kRedrawDisabled; +} + +Dialog *GuiManager::getTopDialog() const { + if (_dialogStack.empty()) + return 0; + return _dialogStack.top(); +} + +void GuiManager::runLoop() { + Dialog *activeDialog = getTopDialog(); + bool didSaveState = false; + int button; + uint32 time; + + if (activeDialog == 0) + return; + + if (!_stateIsSaved) { + saveState(); + _theme->enable(); + didSaveState = true; + + _useStdCursor = !_theme->ownCursor(); + if (_useStdCursor) + setupCursor(); + +// _theme->refresh(); + + _themeChange = false; + _redrawStatus = kRedrawFull; + redraw(); + } + + Common::EventManager *eventMan = _system->getEventManager(); + uint32 lastRedraw = 0; + const uint32 waitTime = 1000 / 45; + + while (!_dialogStack.empty() && activeDialog == getTopDialog()) { + redraw(); + + // Don't "tickle" the dialog until the theme has had a chance + // to re-allocate buffers in case of a scaler change. + + activeDialog->handleTickle(); + + if (_useStdCursor) + animateCursor(); +// _theme->updateScreen(); +// _system->updateScreen(); + + if (lastRedraw + waitTime < _system->getMillis()) { + _theme->updateScreen(); + _system->updateScreen(); + lastRedraw = _system->getMillis(); + } + + Common::Event event; + while (eventMan->pollEvent(event)) { + + // The top dialog can change during the event loop. In that case, flush all the + // dialog-related events since they were probably generated while the old dialog + // was still visible, and therefore not intended for the new one. + // + // This hopefully fixes strange behaviour/crashes with pop-up widgets. (Most easily + // triggered in 3x mode or when running ScummVM under Valgrind.) + if (activeDialog != getTopDialog() && event.type != Common::EVENT_SCREEN_CHANGED) + continue; + + Common::Point mouse(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y); + + // HACK to change the cursor to the new themes one + if (_themeChange) { + _theme->enable(); + + _useStdCursor = !_theme->ownCursor(); + if (_useStdCursor) + setupCursor(); + +// _theme->refresh(); + + _themeChange = false; + _redrawStatus = kRedrawFull; + redraw(); + } + + if (lastRedraw + waitTime < _system->getMillis()) { + _theme->updateScreen(); + _system->updateScreen(); + lastRedraw = _system->getMillis(); + } + + switch (event.type) { + case Common::EVENT_KEYDOWN: + activeDialog->handleKeyDown(event.kbd); + break; + case Common::EVENT_KEYUP: + activeDialog->handleKeyUp(event.kbd); + break; + case Common::EVENT_MOUSEMOVE: + activeDialog->handleMouseMoved(mouse.x, mouse.y, 0); + break; + // We don't distinguish between mousebuttons (for now at least) + case Common::EVENT_LBUTTONDOWN: + case Common::EVENT_RBUTTONDOWN: + button = (event.type == Common::EVENT_LBUTTONDOWN ? 1 : 2); + time = _system->getMillis(); + if (_lastClick.count && (time < _lastClick.time + kDoubleClickDelay) + && ABS(_lastClick.x - event.mouse.x) < 3 + && ABS(_lastClick.y - event.mouse.y) < 3) { + _lastClick.count++; + } else { + _lastClick.x = event.mouse.x; + _lastClick.y = event.mouse.y; + _lastClick.count = 1; + } + _lastClick.time = time; + activeDialog->handleMouseDown(mouse.x, mouse.y, button, _lastClick.count); + break; + case Common::EVENT_LBUTTONUP: + case Common::EVENT_RBUTTONUP: + button = (event.type == Common::EVENT_LBUTTONUP ? 1 : 2); + activeDialog->handleMouseUp(mouse.x, mouse.y, button, _lastClick.count); + break; + case Common::EVENT_WHEELUP: + activeDialog->handleMouseWheel(mouse.x, mouse.y, -1); + break; + case Common::EVENT_WHEELDOWN: + activeDialog->handleMouseWheel(mouse.x, mouse.y, 1); + break; + case Common::EVENT_QUIT: + return; + case Common::EVENT_SCREEN_CHANGED: + screenChange(); + break; + default: + break; + } + } + + // Delay for a moment + _system->delayMillis(10); + } + + if (didSaveState) { + _theme->disable(); + restoreState(); + _useStdCursor = false; + } +} + +#pragma mark - + +void GuiManager::saveState() { + // Backup old cursor + _lastClick.x = _lastClick.y = 0; + _lastClick.time = 0; + _lastClick.count = 0; + + _stateIsSaved = true; +} + +void GuiManager::restoreState() { + if (_useStdCursor) { + CursorMan.popCursor(); + CursorMan.popCursorPalette(); + } + + _system->updateScreen(); + + _stateIsSaved = false; +} + +void GuiManager::openDialog(Dialog *dialog) { + _dialogStack.push(dialog); + if (_redrawStatus != kRedrawFull) + _redrawStatus = kRedrawOpenDialog; + + // We reflow the dialog just before opening it. If the screen changed + // since the last time we looked, also refresh the loaded theme, + // and reflow all other open dialogs, too. + if (!checkScreenChange()) + dialog->reflowLayout(); +} + +void GuiManager::closeTopDialog() { + // Don't do anything if no dialog is open + if (_dialogStack.empty()) + return; + + // Remove the dialog from the stack + _dialogStack.pop(); + if (_redrawStatus != kRedrawFull) + _redrawStatus = kRedrawCloseDialog; +} + +void GuiManager::setupCursor() { + const byte palette[] = { + 255, 255, 255, 0, + 255, 255, 255, 0, + 171, 171, 171, 0, + 87, 87, 87, 0 + }; + + CursorMan.pushCursorPalette(palette, 0, 4); + CursorMan.pushCursor(NULL, 0, 0, 0, 0); + CursorMan.showMouse(true); +} + +// Draw the mouse cursor (animated). This is pretty much the same as in old +// SCUMM games, but the code no longer resembles what we have in cursor.cpp +// very much. We could plug in a different cursor here if we like to. + +void GuiManager::animateCursor() { + int time = _system->getMillis(); + if (time > _cursorAnimateTimer + kCursorAnimateDelay) { + for (int i = 0; i < 15; i++) { + if ((i < 6) || (i > 8)) { + _cursor[16 * 7 + i] = _cursorAnimateCounter; + _cursor[16 * i + 7] = _cursorAnimateCounter; + } + } + + CursorMan.replaceCursor(_cursor, 16, 16, 7, 7); + + _cursorAnimateTimer = time; + _cursorAnimateCounter = (_cursorAnimateCounter + 1) % 4; + } +} + +bool GuiManager::checkScreenChange() { + int tmpScreenChangeID = _system->getScreenChangeID(); + if (_lastScreenChangeID != tmpScreenChangeID) { + GuiManager::screenChange(); + return true; + } + return false; +} + +void GuiManager::screenChange() { + _lastScreenChangeID = _system->getScreenChangeID(); + + // reinit the whole theme + _theme->refresh(); + + // refresh all dialogs + for (int i = 0; i < _dialogStack.size(); ++i) { + _dialogStack[i]->reflowLayout(); + } + // We need to redraw immediately. Otherwise + // some other event may cause a widget to be + // redrawn before redraw() has been called. + _redrawStatus = kRedrawFull; + redraw(); + _system->updateScreen(); +} + +} // End of namespace GUI diff --git a/gui/GuiManager.h b/gui/GuiManager.h new file mode 100644 index 0000000000..c2ec61dd34 --- /dev/null +++ b/gui/GuiManager.h @@ -0,0 +1,151 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#ifndef NEWGUI_H +#define NEWGUI_H + +#include "common/scummsys.h" +#include "common/singleton.h" +#include "common/stack.h" +#include "common/str.h" +#include "graphics/fontman.h" + +#include "gui/widget.h" + +#include "gui/ThemeEngine.h" + +class OSystem; + +namespace GUI { + +class Dialog; +class Eval; +class ThemeEval; + +#define g_gui (GUI::GuiManager::instance()) + + +// Height of a single text line +#define kLineHeight (g_gui.getFontHeight() + 2) + + + +// Simple dialog stack class +// Anybody nesting dialogs deeper than 4 is mad anyway +typedef Common::FixedStack DialogStack; + + +/** + * GUI manager singleton. + */ +class GuiManager : public Common::Singleton { + friend class Dialog; + friend class Common::Singleton; + GuiManager(); + ~GuiManager(); +public: + + // Main entry for the GUI: this will start an event loop that keeps running + // until no dialogs are active anymore. + void runLoop(); + + bool isActive() const { return ! _dialogStack.empty(); } + + bool loadNewTheme(Common::String file, ThemeEngine::GraphicsMode gfx = ThemeEngine::kGfxDisabled); + ThemeEngine *theme() { return _theme; } + + ThemeEval *xmlEval() { return _theme->getEvaluator(); } + + const Graphics::Font &getFont(ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return *(_theme->getFont(style)); } + int getFontHeight(ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return _theme->getFontHeight(style); } + int getStringWidth(const Common::String &str, ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return _theme->getStringWidth(str, style); } + int getCharWidth(byte c, ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return _theme->getCharWidth(c, style); } + + /** + * Tell the GuiManager to check whether the screen resolution has changed. + * If that is the case, the GuiManager will reload/refresh the active theme. + * + * @return true if the a screen change indeed occurred, false otherwise + */ + bool checkScreenChange(); + +protected: + enum RedrawStatus { + kRedrawDisabled = 0, + kRedrawOpenDialog, + kRedrawCloseDialog, + kRedrawTopDialog, + kRedrawFull + }; + + + + OSystem *_system; + + ThemeEngine *_theme; + +// bool _needRedraw; + RedrawStatus _redrawStatus; + int _lastScreenChangeID; + DialogStack _dialogStack; + + bool _stateIsSaved; + + bool _useStdCursor; + + // position and time of last mouse click (used to detect double clicks) + struct { + int16 x, y; // Position of mouse when the click occured + uint32 time; // Time + int count; // How often was it already pressed? + } _lastClick; + + // mouse cursor state + int _cursorAnimateCounter; + int _cursorAnimateTimer; + byte _cursor[2048]; + + bool _themeChange; + + void saveState(); + void restoreState(); + + void openDialog(Dialog *dialog); + void closeTopDialog(); + + void redraw(); + + void loop(); + + void setupCursor(); + void animateCursor(); + + Dialog *getTopDialog() const; + + void screenChange(); +}; + +} // End of namespace GUI + +#endif diff --git a/gui/KeysDialog.h b/gui/KeysDialog.h index 6d780e665a..d18bcb3617 100644 --- a/gui/KeysDialog.h +++ b/gui/KeysDialog.h @@ -26,7 +26,7 @@ #ifndef KEYSDIALOG_H #define KEYSDIALOG_H -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/dialog.h" #include "gui/ListWidget.h" #include "common/str.h" diff --git a/gui/ListWidget.cpp b/gui/ListWidget.cpp index cb0ef54888..86838d9832 100644 --- a/gui/ListWidget.cpp +++ b/gui/ListWidget.cpp @@ -27,7 +27,7 @@ #include "gui/ListWidget.h" #include "gui/ScrollBarWidget.h" #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" diff --git a/gui/PopUpWidget.cpp b/gui/PopUpWidget.cpp index 60f36247d5..4792833580 100644 --- a/gui/PopUpWidget.cpp +++ b/gui/PopUpWidget.cpp @@ -25,7 +25,7 @@ #include "common/system.h" #include "common/events.h" #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/PopUpWidget.h" #include "engines/engine.h" diff --git a/gui/ScrollBarWidget.cpp b/gui/ScrollBarWidget.cpp index 797bf1fdab..6e27535bfd 100644 --- a/gui/ScrollBarWidget.cpp +++ b/gui/ScrollBarWidget.cpp @@ -24,7 +24,7 @@ #include "ScrollBarWidget.h" #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" namespace GUI { diff --git a/gui/TabWidget.cpp b/gui/TabWidget.cpp index 402e9075c1..3fe000e1ec 100644 --- a/gui/TabWidget.cpp +++ b/gui/TabWidget.cpp @@ -25,7 +25,7 @@ #include "common/util.h" #include "gui/TabWidget.h" #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" diff --git a/gui/ThemeParser.cpp b/gui/ThemeParser.cpp index 1a82671c7b..72f841a7a9 100644 --- a/gui/ThemeParser.cpp +++ b/gui/ThemeParser.cpp @@ -33,7 +33,7 @@ #include "gui/ThemeEngine.h" #include "gui/ThemeEval.h" #include "gui/ThemeParser.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "graphics/VectorRenderer.h" namespace GUI { diff --git a/gui/about.cpp b/gui/about.cpp index 923ef36ed9..8e887076b6 100644 --- a/gui/about.cpp +++ b/gui/about.cpp @@ -29,7 +29,7 @@ #include "common/system.h" #include "common/util.h" #include "gui/about.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/widget.h" #include "gui/ThemeEval.h" diff --git a/gui/browser.cpp b/gui/browser.cpp index 02af47ee7c..961f2ced4c 100644 --- a/gui/browser.cpp +++ b/gui/browser.cpp @@ -23,7 +23,7 @@ */ #include "gui/browser.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ListWidget.h" #include "common/config-manager.h" diff --git a/gui/chooser.cpp b/gui/chooser.cpp index 5ec30f3612..04f68a60b0 100644 --- a/gui/chooser.cpp +++ b/gui/chooser.cpp @@ -24,7 +24,7 @@ #include "common/system.h" #include "gui/chooser.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ListWidget.h" namespace GUI { diff --git a/gui/console.h b/gui/console.h index 298662a40f..cf86e40bc8 100644 --- a/gui/console.h +++ b/gui/console.h @@ -26,7 +26,7 @@ #define CONSOLE_DIALOG_H #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" namespace GUI { diff --git a/gui/dialog.cpp b/gui/dialog.cpp index 7aea3cb722..2c42cb1d75 100644 --- a/gui/dialog.cpp +++ b/gui/dialog.cpp @@ -24,7 +24,7 @@ #include "common/events.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/dialog.h" #include "gui/widget.h" #include "gui/PopUpWidget.h" diff --git a/gui/editable.cpp b/gui/editable.cpp index f82f06f4ce..38dcb1ecf5 100644 --- a/gui/editable.cpp +++ b/gui/editable.cpp @@ -24,7 +24,7 @@ #include "common/events.h" #include "gui/editable.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" namespace GUI { diff --git a/gui/editable.h b/gui/editable.h index e09c4a3df4..f01a4a5da2 100644 --- a/gui/editable.h +++ b/gui/editable.h @@ -28,7 +28,7 @@ #include "common/str.h" #include "common/rect.h" #include "gui/widget.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" namespace GUI { diff --git a/gui/launcher.cpp b/gui/launcher.cpp index d28e322d15..f28671dff7 100644 --- a/gui/launcher.cpp +++ b/gui/launcher.cpp @@ -37,7 +37,7 @@ #include "gui/launcher.h" #include "gui/massadd.h" #include "gui/message.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/options.h" #include "gui/saveload.h" #include "gui/EditTextWidget.h" diff --git a/gui/massadd.cpp b/gui/massadd.cpp index ae37db46f9..642e4c713a 100644 --- a/gui/massadd.cpp +++ b/gui/massadd.cpp @@ -30,7 +30,7 @@ #include "gui/launcher.h" // For addGameToConf() #include "gui/massadd.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/widget.h" diff --git a/gui/message.cpp b/gui/message.cpp index 4d63a96c56..7dc850482b 100644 --- a/gui/message.cpp +++ b/gui/message.cpp @@ -26,7 +26,7 @@ #include "common/str.h" #include "common/system.h" #include "gui/message.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" #include "gui/widget.h" diff --git a/gui/module.mk b/gui/module.mk index 249d6e7fa5..5dfb524e6b 100644 --- a/gui/module.mk +++ b/gui/module.mk @@ -9,11 +9,11 @@ MODULE_OBJS := \ dialog.o \ editable.o \ EditTextWidget.o \ + GuiManager.o \ launcher.o \ ListWidget.o \ massadd.o \ message.o \ - newgui.o \ object.o \ options.o \ PopUpWidget.o \ diff --git a/gui/newgui.cpp b/gui/newgui.cpp deleted file mode 100644 index 49879011e6..0000000000 --- a/gui/newgui.cpp +++ /dev/null @@ -1,424 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - */ - -#include "common/events.h" -#include "common/system.h" -#include "common/util.h" -#include "common/config-manager.h" - -#include "gui/newgui.h" -#include "gui/dialog.h" -#include "gui/ThemeEngine.h" -#include "gui/ThemeEval.h" - -#include "graphics/cursorman.h" - -DECLARE_SINGLETON(GUI::GuiManager); - -namespace GUI { - -enum { - kDoubleClickDelay = 500, // milliseconds - kCursorAnimateDelay = 250 -}; - -// Constructor -GuiManager::GuiManager() : _redrawStatus(kRedrawDisabled), - _stateIsSaved(false), _cursorAnimateCounter(0), _cursorAnimateTimer(0) { - _theme = 0; - _useStdCursor = false; - - _system = g_system; - _lastScreenChangeID = _system->getScreenChangeID(); - - // Clear the cursor - memset(_cursor, 0xFF, sizeof(_cursor)); - - - ConfMan.registerDefault("gui_theme", "scummmodern.zip"); - Common::String themefile(ConfMan.get("gui_theme")); - if (themefile.compareToIgnoreCase("default") == 0) - themefile = "builtin"; - - ConfMan.registerDefault("gui_renderer", ThemeEngine::findModeConfigName(ThemeEngine::_defaultRendererMode)); - ThemeEngine::GraphicsMode gfxMode = (ThemeEngine::GraphicsMode)ThemeEngine::findMode(ConfMan.get("gui_renderer")); - - // Try to load the theme - if (!loadNewTheme(themefile, gfxMode)) { - // Loading the theme failed, try to load the built-in theme - if (!loadNewTheme("builtin", gfxMode)) { - // Loading the built-in theme failed as well. Bail out - error("Failed to load any GUI theme, aborting"); - } - } - _themeChange = false; -} - -GuiManager::~GuiManager() { - delete _theme; -} - -bool GuiManager::loadNewTheme(Common::String filename, ThemeEngine::GraphicsMode gfx) { - // If we are asked to reload the currently active theme, just do nothing - // FIXME: Actually, why? It might be desirable at times to force a theme reload... - if (_theme && filename == _theme->getThemeId() && gfx == _theme->getGraphicsMode()) - return true; - - ThemeEngine *newTheme = 0; - - if (gfx == ThemeEngine::kGfxDisabled) - gfx = ThemeEngine::_defaultRendererMode; - - // Try to load the new theme - newTheme = new ThemeEngine(filename, gfx); - assert(newTheme); - - if (!newTheme->init()) - return false; - - // - // Disable and delete the old theme - // - if (_theme) - _theme->disable(); - delete _theme; - - if (_useStdCursor) { - CursorMan.popCursorPalette(); - CursorMan.popCursor(); - } - - // - // Enable the new theme - // - _theme = newTheme; - _themeChange = true; - - // refresh all dialogs - for (int i = 0; i < _dialogStack.size(); ++i) { - _dialogStack[i]->reflowLayout(); - } - - // We need to redraw immediately. Otherwise - // some other event may cause a widget to be - // redrawn before redraw() has been called. - _redrawStatus = kRedrawFull; - redraw(); - _system->updateScreen(); - - return true; -} - -void GuiManager::redraw() { - int i; - - if (_redrawStatus == kRedrawDisabled) - return; - - if (_dialogStack.empty()) - return; - - switch (_redrawStatus) { - case kRedrawCloseDialog: - case kRedrawFull: - case kRedrawTopDialog: - _theme->clearAll(); - _theme->openDialog(true); - - for (i = 0; i < _dialogStack.size() - 1; i++) { - _dialogStack[i]->drawDialog(); - } - - _theme->finishBuffering(); - _theme->updateScreen(); - - case kRedrawOpenDialog: - _theme->openDialog(true, (ThemeEngine::ShadingStyle)xmlEval()->getVar("Dialog." + _dialogStack.top()->_name + ".Shading", 0)); - _dialogStack.top()->drawDialog(); - _theme->finishBuffering(); - break; - - default: - return; - } - - _theme->updateScreen(); - _redrawStatus = kRedrawDisabled; -} - -Dialog *GuiManager::getTopDialog() const { - if (_dialogStack.empty()) - return 0; - return _dialogStack.top(); -} - -void GuiManager::runLoop() { - Dialog *activeDialog = getTopDialog(); - bool didSaveState = false; - int button; - uint32 time; - - if (activeDialog == 0) - return; - - if (!_stateIsSaved) { - saveState(); - _theme->enable(); - didSaveState = true; - - _useStdCursor = !_theme->ownCursor(); - if (_useStdCursor) - setupCursor(); - -// _theme->refresh(); - - _themeChange = false; - _redrawStatus = kRedrawFull; - redraw(); - } - - Common::EventManager *eventMan = _system->getEventManager(); - uint32 lastRedraw = 0; - const uint32 waitTime = 1000 / 45; - - while (!_dialogStack.empty() && activeDialog == getTopDialog()) { - redraw(); - - // Don't "tickle" the dialog until the theme has had a chance - // to re-allocate buffers in case of a scaler change. - - activeDialog->handleTickle(); - - if (_useStdCursor) - animateCursor(); -// _theme->updateScreen(); -// _system->updateScreen(); - - if (lastRedraw + waitTime < _system->getMillis()) { - _theme->updateScreen(); - _system->updateScreen(); - lastRedraw = _system->getMillis(); - } - - Common::Event event; - while (eventMan->pollEvent(event)) { - - // The top dialog can change during the event loop. In that case, flush all the - // dialog-related events since they were probably generated while the old dialog - // was still visible, and therefore not intended for the new one. - // - // This hopefully fixes strange behaviour/crashes with pop-up widgets. (Most easily - // triggered in 3x mode or when running ScummVM under Valgrind.) - if (activeDialog != getTopDialog() && event.type != Common::EVENT_SCREEN_CHANGED) - continue; - - Common::Point mouse(event.mouse.x - activeDialog->_x, event.mouse.y - activeDialog->_y); - - // HACK to change the cursor to the new themes one - if (_themeChange) { - _theme->enable(); - - _useStdCursor = !_theme->ownCursor(); - if (_useStdCursor) - setupCursor(); - -// _theme->refresh(); - - _themeChange = false; - _redrawStatus = kRedrawFull; - redraw(); - } - - if (lastRedraw + waitTime < _system->getMillis()) { - _theme->updateScreen(); - _system->updateScreen(); - lastRedraw = _system->getMillis(); - } - - switch (event.type) { - case Common::EVENT_KEYDOWN: - activeDialog->handleKeyDown(event.kbd); - break; - case Common::EVENT_KEYUP: - activeDialog->handleKeyUp(event.kbd); - break; - case Common::EVENT_MOUSEMOVE: - activeDialog->handleMouseMoved(mouse.x, mouse.y, 0); - break; - // We don't distinguish between mousebuttons (for now at least) - case Common::EVENT_LBUTTONDOWN: - case Common::EVENT_RBUTTONDOWN: - button = (event.type == Common::EVENT_LBUTTONDOWN ? 1 : 2); - time = _system->getMillis(); - if (_lastClick.count && (time < _lastClick.time + kDoubleClickDelay) - && ABS(_lastClick.x - event.mouse.x) < 3 - && ABS(_lastClick.y - event.mouse.y) < 3) { - _lastClick.count++; - } else { - _lastClick.x = event.mouse.x; - _lastClick.y = event.mouse.y; - _lastClick.count = 1; - } - _lastClick.time = time; - activeDialog->handleMouseDown(mouse.x, mouse.y, button, _lastClick.count); - break; - case Common::EVENT_LBUTTONUP: - case Common::EVENT_RBUTTONUP: - button = (event.type == Common::EVENT_LBUTTONUP ? 1 : 2); - activeDialog->handleMouseUp(mouse.x, mouse.y, button, _lastClick.count); - break; - case Common::EVENT_WHEELUP: - activeDialog->handleMouseWheel(mouse.x, mouse.y, -1); - break; - case Common::EVENT_WHEELDOWN: - activeDialog->handleMouseWheel(mouse.x, mouse.y, 1); - break; - case Common::EVENT_QUIT: - return; - case Common::EVENT_SCREEN_CHANGED: - screenChange(); - break; - default: - break; - } - } - - // Delay for a moment - _system->delayMillis(10); - } - - if (didSaveState) { - _theme->disable(); - restoreState(); - _useStdCursor = false; - } -} - -#pragma mark - - -void GuiManager::saveState() { - // Backup old cursor - _lastClick.x = _lastClick.y = 0; - _lastClick.time = 0; - _lastClick.count = 0; - - _stateIsSaved = true; -} - -void GuiManager::restoreState() { - if (_useStdCursor) { - CursorMan.popCursor(); - CursorMan.popCursorPalette(); - } - - _system->updateScreen(); - - _stateIsSaved = false; -} - -void GuiManager::openDialog(Dialog *dialog) { - _dialogStack.push(dialog); - if (_redrawStatus != kRedrawFull) - _redrawStatus = kRedrawOpenDialog; - - // We reflow the dialog just before opening it. If the screen changed - // since the last time we looked, also refresh the loaded theme, - // and reflow all other open dialogs, too. - if (!checkScreenChange()) - dialog->reflowLayout(); -} - -void GuiManager::closeTopDialog() { - // Don't do anything if no dialog is open - if (_dialogStack.empty()) - return; - - // Remove the dialog from the stack - _dialogStack.pop(); - if (_redrawStatus != kRedrawFull) - _redrawStatus = kRedrawCloseDialog; -} - -void GuiManager::setupCursor() { - const byte palette[] = { - 255, 255, 255, 0, - 255, 255, 255, 0, - 171, 171, 171, 0, - 87, 87, 87, 0 - }; - - CursorMan.pushCursorPalette(palette, 0, 4); - CursorMan.pushCursor(NULL, 0, 0, 0, 0); - CursorMan.showMouse(true); -} - -// Draw the mouse cursor (animated). This is pretty much the same as in old -// SCUMM games, but the code no longer resembles what we have in cursor.cpp -// very much. We could plug in a different cursor here if we like to. - -void GuiManager::animateCursor() { - int time = _system->getMillis(); - if (time > _cursorAnimateTimer + kCursorAnimateDelay) { - for (int i = 0; i < 15; i++) { - if ((i < 6) || (i > 8)) { - _cursor[16 * 7 + i] = _cursorAnimateCounter; - _cursor[16 * i + 7] = _cursorAnimateCounter; - } - } - - CursorMan.replaceCursor(_cursor, 16, 16, 7, 7); - - _cursorAnimateTimer = time; - _cursorAnimateCounter = (_cursorAnimateCounter + 1) % 4; - } -} - -bool GuiManager::checkScreenChange() { - int tmpScreenChangeID = _system->getScreenChangeID(); - if (_lastScreenChangeID != tmpScreenChangeID) { - GuiManager::screenChange(); - return true; - } - return false; -} - -void GuiManager::screenChange() { - _lastScreenChangeID = _system->getScreenChangeID(); - - // reinit the whole theme - _theme->refresh(); - - // refresh all dialogs - for (int i = 0; i < _dialogStack.size(); ++i) { - _dialogStack[i]->reflowLayout(); - } - // We need to redraw immediately. Otherwise - // some other event may cause a widget to be - // redrawn before redraw() has been called. - _redrawStatus = kRedrawFull; - redraw(); - _system->updateScreen(); -} - -} // End of namespace GUI diff --git a/gui/newgui.h b/gui/newgui.h deleted file mode 100644 index c2ec61dd34..0000000000 --- a/gui/newgui.h +++ /dev/null @@ -1,151 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - */ - -#ifndef NEWGUI_H -#define NEWGUI_H - -#include "common/scummsys.h" -#include "common/singleton.h" -#include "common/stack.h" -#include "common/str.h" -#include "graphics/fontman.h" - -#include "gui/widget.h" - -#include "gui/ThemeEngine.h" - -class OSystem; - -namespace GUI { - -class Dialog; -class Eval; -class ThemeEval; - -#define g_gui (GUI::GuiManager::instance()) - - -// Height of a single text line -#define kLineHeight (g_gui.getFontHeight() + 2) - - - -// Simple dialog stack class -// Anybody nesting dialogs deeper than 4 is mad anyway -typedef Common::FixedStack DialogStack; - - -/** - * GUI manager singleton. - */ -class GuiManager : public Common::Singleton { - friend class Dialog; - friend class Common::Singleton; - GuiManager(); - ~GuiManager(); -public: - - // Main entry for the GUI: this will start an event loop that keeps running - // until no dialogs are active anymore. - void runLoop(); - - bool isActive() const { return ! _dialogStack.empty(); } - - bool loadNewTheme(Common::String file, ThemeEngine::GraphicsMode gfx = ThemeEngine::kGfxDisabled); - ThemeEngine *theme() { return _theme; } - - ThemeEval *xmlEval() { return _theme->getEvaluator(); } - - const Graphics::Font &getFont(ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return *(_theme->getFont(style)); } - int getFontHeight(ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return _theme->getFontHeight(style); } - int getStringWidth(const Common::String &str, ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return _theme->getStringWidth(str, style); } - int getCharWidth(byte c, ThemeEngine::FontStyle style = ThemeEngine::kFontStyleBold) const { return _theme->getCharWidth(c, style); } - - /** - * Tell the GuiManager to check whether the screen resolution has changed. - * If that is the case, the GuiManager will reload/refresh the active theme. - * - * @return true if the a screen change indeed occurred, false otherwise - */ - bool checkScreenChange(); - -protected: - enum RedrawStatus { - kRedrawDisabled = 0, - kRedrawOpenDialog, - kRedrawCloseDialog, - kRedrawTopDialog, - kRedrawFull - }; - - - - OSystem *_system; - - ThemeEngine *_theme; - -// bool _needRedraw; - RedrawStatus _redrawStatus; - int _lastScreenChangeID; - DialogStack _dialogStack; - - bool _stateIsSaved; - - bool _useStdCursor; - - // position and time of last mouse click (used to detect double clicks) - struct { - int16 x, y; // Position of mouse when the click occured - uint32 time; // Time - int count; // How often was it already pressed? - } _lastClick; - - // mouse cursor state - int _cursorAnimateCounter; - int _cursorAnimateTimer; - byte _cursor[2048]; - - bool _themeChange; - - void saveState(); - void restoreState(); - - void openDialog(Dialog *dialog); - void closeTopDialog(); - - void redraw(); - - void loop(); - - void setupCursor(); - void animateCursor(); - - Dialog *getTopDialog() const; - - void screenChange(); -}; - -} // End of namespace GUI - -#endif diff --git a/gui/object.cpp b/gui/object.cpp index c6f554ab83..233a5f07c6 100644 --- a/gui/object.cpp +++ b/gui/object.cpp @@ -25,7 +25,7 @@ #include "common/system.h" #include "gui/object.h" #include "gui/widget.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" namespace GUI { diff --git a/gui/options.cpp b/gui/options.cpp index d435db85a5..c5331bfc42 100644 --- a/gui/options.cpp +++ b/gui/options.cpp @@ -26,7 +26,7 @@ #include "gui/themebrowser.h" #include "gui/chooser.h" #include "gui/message.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" #include "gui/options.h" #include "gui/PopUpWidget.h" diff --git a/gui/widget.cpp b/gui/widget.cpp index 41576b5933..995de2fe06 100644 --- a/gui/widget.cpp +++ b/gui/widget.cpp @@ -26,7 +26,7 @@ #include "graphics/fontman.h" #include "gui/widget.h" #include "gui/dialog.h" -#include "gui/newgui.h" +#include "gui/GuiManager.h" #include "gui/ThemeEval.h" -- cgit v1.2.3